This commit is contained in:
sunkaixuan
2017-05-21 02:14:27 +08:00
parent 5b11efc5b3
commit dc5adfc6a7
2 changed files with 20 additions and 18 deletions

View File

@@ -20,7 +20,7 @@ namespace OrmTest.UnitTest
{ {
var db = GetInstance(); var db = GetInstance();
var updateObj = new Student() { Id = 1, Name = "jack", CreateTime = DateTime.Now }; var updateObj = new Student() { Id = 1, Name = "jack", CreateTime = DateTime.Now };
var updateObjs = new List<Student>() { updateObj }.ToArray(); var updateObjs = new List<Student>() { updateObj,new Student() { Id=2,Name="sun" } }.ToArray();
db.IgnoreColumns.Add("TestId", "Student"); db.IgnoreColumns.Add("TestId", "Student");
//db.MappingColumns.Add("id","dbid", "Student"); //db.MappingColumns.Add("id","dbid", "Student");

View File

@@ -110,19 +110,21 @@ 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;
if (isSingle)
{
string columnsString = string.Join(",", groupList.First().Select(it => string columnsString = string.Join(",", groupList.First().Select(it =>
{ {
if (SetValues.IsValuable()) { if (SetValues.IsValuable())
{
var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName)); var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName));
if (setValue != null&& setValue.Any()) { if (setValue != null && setValue.Any())
{
return setValue.First().Value; return setValue.First().Value;
} }
} }
var result = Builder.GetTranslationColumnName(it.DbColumnName) + "=" + this.Context.Ado.SqlParameterKeyWord + it.DbColumnName; var result = Builder.GetTranslationColumnName(it.DbColumnName) + "=" + this.Context.Ado.SqlParameterKeyWord + it.DbColumnName;
return result; return result;
})); }));
if (isSingle)
{
string whereString = null; string whereString = null;
if (this.WhereValues.IsValuable()) if (this.WhereValues.IsValuable())
{ {
@@ -148,29 +150,29 @@ namespace SqlSugar
{ {
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key"); Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key");
var whereString = string.Join(",", PrimaryKeys.Select(it => string.Format("T.{0}=S.{0}", Builder.GetTranslationColumnName(it)))); var whereString = string.Join(",", PrimaryKeys.Select(it => string.Format("T.{0}=S.{0}", Builder.GetTranslationColumnName(it))));
StringBuilder batchInsetrSql = new StringBuilder(); StringBuilder batchUpdateSql = new StringBuilder();
int pageSize = 200; int pageSize = 200;
int pageIndex = 1; int pageIndex = 1;
int totalRecord = groupList.Count; int totalRecord = groupList.Count;
int pageCount = (totalRecord + pageSize - 1) / pageSize; int pageCount = (totalRecord + pageSize - 1) / pageSize;
while (pageCount >= pageIndex) while (pageCount >= pageIndex)
{ {
batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString); batchUpdateSql.AppendFormat(SqlTemplateBatch, GetTableNameString, "");
int i = 0; int i = 0;
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()) foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
{ {
var isFirst = i == 0; var isFirst = i == 0;
if (!isFirst) if (!isFirst)
{ {
batchInsetrSql.Append(SqlTemplateBatchUnion); batchUpdateSql.Append(SqlTemplateBatchUnion);
} }
batchInsetrSql.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), it.DbColumnName)))); batchUpdateSql.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), it.DbColumnName))));
++i; ++i;
} }
pageIndex++; pageIndex++;
batchInsetrSql.Append("\r\nGO\r\n"); batchUpdateSql.Append("\r\nGO\r\n");
} }
return batchInsetrSql.ToString(); return batchUpdateSql.ToString();
} }
} }