You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

91 lines
3.7 KiB

using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class MySqlUpdateBuilder : UpdateBuilder
{
public override string SqlTemplateBatch
{
get
{
return @"UPDATE {1} S {2} INNER JOIN ${{0}} SET {0} ";
}
}
public override string SqlTemplateJoin
{
get
{
return @" (
{0}
) T ON {1}
";
}
}
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
{
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key");
var pageSize = 200;
var pageIndex = 1;
var totalRecord = groupList.Count;
var pageCount = (totalRecord + pageSize - 1) / pageSize;
var batchUpdateSql = new StringBuilder();
while (pageCount >= pageIndex)
{
var updateTable = new StringBuilder();
var setValues = string.Join(",", groupList.First().Where(it => !it.IsPrimarykey && (!it.IsIdentity || (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);
var 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(SqlTemplateBatchSelect, FormatValue(it.Value),this.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 += item;
}
}
else 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));
}
}
var format= string.Format(SqlTemplateJoin, updateTable, whereString);
batchUpdateSql.Replace("${0}",format);
batchUpdateSql.Append(";");
}
return batchUpdateSql.ToString();
}
}
}