Browse Source

-

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
d532289a3e
  1. 4
      OrmTest/UnitTest/Insert.cs
  2. 32
      SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/InsertBuilder.cs
  3. 1
      SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

4
OrmTest/UnitTest/Insert.cs

@ -19,7 +19,7 @@ namespace OrmTest.UnitTest
public void Init() { public void Init() {
var db = GetInstance(); var db = GetInstance();
var insertObj = new Student() { Name="jack",CreateTime=DateTime.Now }; var insertObj = new Student() { Name="jack",CreateTime=DateTime.Now };
var insertObjs = new List<Student>() { insertObj }.ToArray(); var insertObjs = new List<Student>() { insertObj , insertObj }.ToArray();
db.IgnoreColumns.Add("TestId", "Student"); db.IgnoreColumns.Add("TestId", "Student");
//db.MappingColumns.Add("id","dbid", "Student"); //db.MappingColumns.Add("id","dbid", "Student");
@ -45,6 +45,8 @@ namespace OrmTest.UnitTest
var s7= db.Insertable<Student>(insertObj).With(SqlWith.UpdLock) var s7= db.Insertable<Student>(insertObj).With(SqlWith.UpdLock)
.InsertColumns(it => new { it.Name }).ToSql(); .InsertColumns(it => new { it.Name }).ToSql();
db.IgnoreColumns = new IgnoreComumnList();
db.IgnoreColumns.Add("TestId", "Student");
//Insert List<T> //Insert List<T>
var s8= db.Insertable<Student>(insertObjs).With(SqlWith.UpdLock).ToSql(); var s8= db.Insertable<Student>(insertObjs).With(SqlWith.UpdLock).ToSql();
} }

32
SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/InsertBuilder.cs

@ -54,7 +54,7 @@ namespace SqlSugar
public virtual string SqlTemplateBatchSelect { public virtual string SqlTemplateBatchSelect {
get { get {
return "N'{0}' AS {1}"; return "{0} AS {1}";
} }
} }
@ -62,7 +62,7 @@ namespace SqlSugar
{ {
get 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 groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
var isSingle = groupList.Count() == 1; 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) if (isSingle)
{ {
string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it => Builder.SqlParameterKeyWord + it.ColumnName)); string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it => Builder.SqlParameterKeyWord + it.ColumnName));
@ -108,7 +108,7 @@ namespace SqlSugar
} }
else { else {
StringBuilder batchInsetrSql = new StringBuilder(); StringBuilder batchInsetrSql = new StringBuilder();
batchInsetrSql.AppendFormat(GetTableNameString, columnsString); batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString);
int i = 0; int i = 0;
foreach (var columns in groupList) foreach (var columns in groupList)
{ {
@ -116,11 +116,33 @@ namespace SqlSugar
if (!isFirst) { if (!isFirst) {
batchInsetrSql.Append(SqlTemplateBatchUnion); 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; ++i;
} }
return batchInsetrSql.ToString(); 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() + "'";
}
}
}
} }
} }

1
SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

@ -154,6 +154,7 @@ namespace SqlSugar
insertItem.Add(columnInfo); insertItem.Add(columnInfo);
} }
this.InsertBuilder.DbColumnInfoList.AddRange(insertItem); this.InsertBuilder.DbColumnInfoList.AddRange(insertItem);
++i;
} }
} }
private string GetDbColumnName(string entityName) private string GetDbColumnName(string entityName)

Loading…
Cancel
Save