SqliteInsertBuilder.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. namespace SqlSugar
  5. {
  6. public class SqliteInsertBuilder : InsertBuilder
  7. {
  8. public override string SqlTemplate
  9. {
  10. get
  11. {
  12. if (IsReturnIdentity)
  13. {
  14. return @"INSERT INTO {0}
  15. ({1})
  16. VALUES
  17. ({2}) ;SELECT LAST_INSERT_ROWID();";
  18. }
  19. else
  20. {
  21. return @"INSERT INTO {0}
  22. ({1})
  23. VALUES
  24. ({2}) ;";
  25. }
  26. }
  27. }
  28. public override string SqlTemplateBatch
  29. {
  30. get
  31. {
  32. return "INSERT INTO {0} ({1})";
  33. }
  34. }
  35. public override string ToSqlString()
  36. {
  37. if (IsNoInsertNull)
  38. {
  39. DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
  40. }
  41. var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
  42. var isSingle = groupList.Count() == 1;
  43. string columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName)));
  44. if (isSingle)
  45. {
  46. string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it =>base.GetDbColumn(it, Builder.SqlParameterKeyWord + it.DbColumnName)));
  47. ActionMinDate();
  48. return string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString);
  49. }
  50. else
  51. {
  52. StringBuilder batchInsetrSql = new StringBuilder();
  53. batchInsetrSql.Append("INSERT INTO " + GetTableNameString + " ");
  54. batchInsetrSql.Append("(");
  55. batchInsetrSql.Append(columnsString);
  56. batchInsetrSql.Append(") VALUES");
  57. string insertColumns = "";
  58. int i = 0;
  59. foreach (var item in groupList)
  60. {
  61. batchInsetrSql.Append("(");
  62. insertColumns = string.Join(",", item.Select(it =>base.GetDbColumn(it, FormatValue(i,it.DbColumnName,it.Value))));
  63. batchInsetrSql.Append(insertColumns);
  64. if (groupList.Last() == item)
  65. {
  66. batchInsetrSql.Append(") ");
  67. }
  68. else
  69. {
  70. batchInsetrSql.Append("), ");
  71. }
  72. i++;
  73. }
  74. batchInsetrSql.AppendLine(";SELECT LAST_INSERT_ROWID();");
  75. var result = batchInsetrSql.ToString();
  76. return result;
  77. }
  78. }
  79. public object FormatValue(int i,string name,object value)
  80. {
  81. if (value == null)
  82. {
  83. return "NULL";
  84. }
  85. else
  86. {
  87. var type = UtilMethods.GetUnderType(value.GetType());
  88. if (type == UtilConstants.DateType)
  89. {
  90. var date = value.ObjToDate();
  91. if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
  92. {
  93. date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
  94. }
  95. if (this.Context.CurrentConnectionConfig?.MoreSettings?.DisableMillisecond == true)
  96. {
  97. return "'" + date.ToString("yyyy-MM-dd HH:mm:ss") + "'";
  98. }
  99. else
  100. {
  101. return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
  102. }
  103. }
  104. else if (type.IsEnum())
  105. {
  106. if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
  107. {
  108. return value.ToSqlValue();
  109. }
  110. else
  111. {
  112. return Convert.ToInt64(value);
  113. }
  114. }
  115. else if (type == UtilConstants.DateTimeOffsetType)
  116. {
  117. return GetDateTimeOffsetString(value);
  118. }
  119. else if (type == UtilConstants.ByteArrayType)
  120. {
  121. var parameterName = this.Builder.SqlParameterKeyWord + name + i;
  122. this.Parameters.Add(new SugarParameter(parameterName, value));
  123. return parameterName;
  124. }
  125. else if (type == UtilConstants.BoolType)
  126. {
  127. return value.ObjToBool() ? "1" : "0";
  128. }
  129. else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
  130. {
  131. return "'" + value.ToString().ToSqlFilter() + "'";
  132. }
  133. else
  134. {
  135. return "'" + value.ToString() + "'";
  136. }
  137. }
  138. }
  139. private object GetDateTimeOffsetString(object value)
  140. {
  141. var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
  142. if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
  143. {
  144. date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
  145. }
  146. return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fffffff") + "'";
  147. }
  148. private object GetDateTimeString(object value)
  149. {
  150. var date = value.ObjToDate();
  151. if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
  152. {
  153. date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
  154. }
  155. return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
  156. }
  157. }
  158. }