From d532289a3e604bc1b43156f529f48537a1652415 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 15 May 2017 16:45:49 +0800 Subject: [PATCH] - --- OrmTest/UnitTest/Insert.cs | 4 ++- .../DMLBuilder/InsertBuilder.cs | 32 ++++++++++++++++--- .../InsertableProvider/InsertableProvider.cs | 1 + 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/OrmTest/UnitTest/Insert.cs b/OrmTest/UnitTest/Insert.cs index 5f11ca04b..ed995abf5 100644 --- a/OrmTest/UnitTest/Insert.cs +++ b/OrmTest/UnitTest/Insert.cs @@ -19,7 +19,7 @@ namespace OrmTest.UnitTest public void Init() { var db = GetInstance(); var insertObj = new Student() { Name="jack",CreateTime=DateTime.Now }; - var insertObjs = new List() { insertObj }.ToArray(); + var insertObjs = new List() { insertObj , insertObj }.ToArray(); db.IgnoreColumns.Add("TestId", "Student"); //db.MappingColumns.Add("id","dbid", "Student"); @@ -45,6 +45,8 @@ namespace OrmTest.UnitTest var s7= db.Insertable(insertObj).With(SqlWith.UpdLock) .InsertColumns(it => new { it.Name }).ToSql(); + db.IgnoreColumns = new IgnoreComumnList(); + db.IgnoreColumns.Add("TestId", "Student"); //Insert List var s8= db.Insertable(insertObjs).With(SqlWith.UpdLock).ToSql(); } diff --git a/SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/InsertBuilder.cs b/SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/InsertBuilder.cs index fd9cb0624..8ffaab0d4 100644 --- a/SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/InsertBuilder.cs +++ b/SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/InsertBuilder.cs @@ -54,7 +54,7 @@ namespace SqlSugar public virtual string SqlTemplateBatchSelect { get { - return "N'{0}' AS {1}"; + return "{0} AS {1}"; } } @@ -62,7 +62,7 @@ namespace SqlSugar { get { - return "\r\nUNION ALL "; + return "\t\r\nUNION ALL "; } } @@ -100,7 +100,7 @@ namespace SqlSugar { var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList(); var isSingle = groupList.Count() == 1; - string columnsString = string.Join(",", this.DbColumnInfoList.Select(it => Builder.GetTranslationColumnName(it.ColumnName))); + string columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.ColumnName))); if (isSingle) { string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it => Builder.SqlParameterKeyWord + it.ColumnName)); @@ -108,7 +108,7 @@ namespace SqlSugar } else { StringBuilder batchInsetrSql = new StringBuilder(); - batchInsetrSql.AppendFormat(GetTableNameString, columnsString); + batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString); int i = 0; foreach (var columns in groupList) { @@ -116,11 +116,33 @@ namespace SqlSugar if (!isFirst) { batchInsetrSql.Append(SqlTemplateBatchUnion); } - batchInsetrSql.Append("\r\n SELECT "+string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect,it.ColumnName,it.Value)))); + batchInsetrSql.Append("\r\n SELECT "+string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), it.ColumnName)))); ++i; } return batchInsetrSql.ToString(); } } + + public object FormatValue(object value) + { + if (value == null) + { + return "NULL"; + } + else { + var type = value.GetType(); + if (type == PubConst.DateType) + { + return "'" + value.ObjToDate().ToString("yyyy-MM-dd hh:mm:ss.ms") + "'"; + } + else if (type == PubConst.StringType|| type == PubConst.ObjType) + { + return "N'" + value.ToString().ToSqlFilter() + "'"; + } + else { + return "N'" + value.ToString() + "'"; + } + } + } } } diff --git a/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index f129cf7e3..77bb7214b 100644 --- a/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -154,6 +154,7 @@ namespace SqlSugar insertItem.Add(columnInfo); } this.InsertBuilder.DbColumnInfoList.AddRange(insertItem); + ++i; } } private string GetDbColumnName(string entityName)