|
|
@ -2,11 +2,93 @@ |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Text; |
|
|
|
using System.Text.RegularExpressions; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
namespace SqlSugar |
|
|
|
{ |
|
|
|
public class SqlServerUpdateBuilder: UpdateBuilder |
|
|
|
{ |
|
|
|
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList) |
|
|
|
{ |
|
|
|
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key"); |
|
|
|
int pageSize = 200; |
|
|
|
int pageIndex = 1; |
|
|
|
int totalRecord = groupList.Count; |
|
|
|
int pageCount = (totalRecord + pageSize - 1) / pageSize; |
|
|
|
StringBuilder batchUpdateSql = new StringBuilder(); |
|
|
|
while (pageCount >= pageIndex) |
|
|
|
{ |
|
|
|
StringBuilder updateTable = new StringBuilder(); |
|
|
|
string setValues = string.Join(",", groupList.First().Where(it => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Select(it => |
|
|
|
{ |
|
|
|
if (SetValues.IsValuable()) |
|
|
|
{ |
|
|
|
var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName)); |
|
|
|
if (setValue != null && setValue.Any()) |
|
|
|
{ |
|
|
|
return setValue.First().Value; |
|
|
|
} |
|
|
|
} |
|
|
|
var result = string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(it.DbColumnName)); |
|
|
|
return result; |
|
|
|
})); |
|
|
|
batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString); |
|
|
|
int i = 0; |
|
|
|
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()) |
|
|
|
{ |
|
|
|
var isFirst = i == 0; |
|
|
|
if (!isFirst) |
|
|
|
{ |
|
|
|
updateTable.Append(SqlTemplateBatchUnion); |
|
|
|
} |
|
|
|
updateTable.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(base.SqlTemplateBatchSelect, GetValue(it), Builder.GetTranslationColumnName(it.DbColumnName))))); |
|
|
|
++i; |
|
|
|
} |
|
|
|
pageIndex++; |
|
|
|
updateTable.Append("\r\n"); |
|
|
|
string whereString = null; |
|
|
|
if (this.WhereValues.HasValue()) |
|
|
|
{ |
|
|
|
foreach (var item in WhereValues) |
|
|
|
{ |
|
|
|
var isFirst = whereString == null; |
|
|
|
whereString += (isFirst ? null : " AND "); |
|
|
|
whereString += Regex.Replace(item, "\\" + this.Builder.SqlTranslationLeft, "S." + this.Builder.SqlTranslationLeft); |
|
|
|
} |
|
|
|
} |
|
|
|
if (PrimaryKeys.HasValue()) |
|
|
|
{ |
|
|
|
foreach (var item in PrimaryKeys) |
|
|
|
{ |
|
|
|
var isFirst = whereString == null; |
|
|
|
whereString += (isFirst ? null : " AND "); |
|
|
|
whereString += string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(item)); |
|
|
|
} |
|
|
|
} |
|
|
|
batchUpdateSql.AppendFormat(SqlTemplateJoin, updateTable, whereString); |
|
|
|
} |
|
|
|
return batchUpdateSql.ToString(); |
|
|
|
} |
|
|
|
|
|
|
|
private object GetValue(DbColumnInfo it) |
|
|
|
{ |
|
|
|
if (it.SqlParameterDbType!=null&&it.SqlParameterDbType.Equals(System.Data.DbType.AnsiString)) |
|
|
|
{ |
|
|
|
var value = FormatValue(it.Value); |
|
|
|
if (value is string&&value.ObjToString().StartsWith("N'")) |
|
|
|
{ |
|
|
|
return value.ObjToString().TrimStart('N'); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return value; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return FormatValue(it.Value); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|