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() {
var db = GetInstance();
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.MappingColumns.Add("id","dbid", "Student");
@ -45,6 +45,8 @@ namespace OrmTest.UnitTest
var s7= db.Insertable<Student>(insertObj).With(SqlWith.UpdLock)
.InsertColumns(it => new { it.Name }).ToSql();
db.IgnoreColumns = new IgnoreComumnList();
db.IgnoreColumns.Add("TestId", "Student");
//Insert List<T>
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 {
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() + "'";
}
}
}
}
}

1
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)

Loading…
Cancel
Save