123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using System;
- using System.Linq;
- using System.Text;
- namespace SqlSugar
- {
- public class SqliteInsertBuilder : InsertBuilder
- {
- public override string SqlTemplate
- {
- get
- {
- if (IsReturnIdentity)
- {
- return @"INSERT INTO {0}
- ({1})
- VALUES
- ({2}) ;SELECT LAST_INSERT_ROWID();";
- }
- else
- {
- return @"INSERT INTO {0}
- ({1})
- VALUES
- ({2}) ;";
- }
- }
- }
- public override string SqlTemplateBatch
- {
- get
- {
- return "INSERT INTO {0} ({1})";
- }
- }
- public override string ToSqlString()
- {
- if (IsNoInsertNull)
- {
- DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
- }
- var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
- var isSingle = groupList.Count() == 1;
- string columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName)));
- if (isSingle)
- {
- string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it =>base.GetDbColumn(it, Builder.SqlParameterKeyWord + it.DbColumnName)));
- ActionMinDate();
- return string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString);
- }
- else
- {
- StringBuilder batchInsetrSql = new StringBuilder();
- batchInsetrSql.Append("INSERT INTO " + GetTableNameString + " ");
- batchInsetrSql.Append("(");
- batchInsetrSql.Append(columnsString);
- batchInsetrSql.Append(") VALUES");
- string insertColumns = "";
- int i = 0;
- foreach (var item in groupList)
- {
- batchInsetrSql.Append("(");
- insertColumns = string.Join(",", item.Select(it =>base.GetDbColumn(it, FormatValue(i,it.DbColumnName,it.Value))));
- batchInsetrSql.Append(insertColumns);
- if (groupList.Last() == item)
- {
- batchInsetrSql.Append(") ");
- }
- else
- {
- batchInsetrSql.Append("), ");
- }
- i++;
- }
- batchInsetrSql.AppendLine(";SELECT LAST_INSERT_ROWID();");
- var result = batchInsetrSql.ToString();
- return result;
- }
- }
- public object FormatValue(int i,string name,object value)
- {
- if (value == null)
- {
- return "NULL";
- }
- else
- {
- var type = UtilMethods.GetUnderType(value.GetType());
- if (type == UtilConstants.DateType)
- {
- var date = value.ObjToDate();
- if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
- {
- date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
- }
- if (this.Context.CurrentConnectionConfig?.MoreSettings?.DisableMillisecond == true)
- {
- return "'" + date.ToString("yyyy-MM-dd HH:mm:ss") + "'";
- }
- else
- {
- return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
- }
- }
- else if (type.IsEnum())
- {
- if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
- {
- return value.ToSqlValue();
- }
- else
- {
- return Convert.ToInt64(value);
- }
- }
- else if (type == UtilConstants.DateTimeOffsetType)
- {
- return GetDateTimeOffsetString(value);
- }
- else if (type == UtilConstants.ByteArrayType)
- {
- var parameterName = this.Builder.SqlParameterKeyWord + name + i;
- this.Parameters.Add(new SugarParameter(parameterName, value));
- return parameterName;
- }
- else if (type == UtilConstants.BoolType)
- {
- return value.ObjToBool() ? "1" : "0";
- }
- else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
- {
- return "'" + value.ToString().ToSqlFilter() + "'";
- }
- else
- {
- return "'" + value.ToString() + "'";
- }
- }
- }
- private object GetDateTimeOffsetString(object value)
- {
- var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
- if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
- {
- date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
- }
- return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fffffff") + "'";
- }
- private object GetDateTimeString(object value)
- {
- var date = value.ObjToDate();
- if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
- {
- date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
- }
- return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
- }
- }
- }
|