Synchronization code

This commit is contained in:
sunkaixuan 2024-06-21 19:14:33 +08:00
parent c94e2eb60a
commit 056f21597f
2 changed files with 107 additions and 0 deletions

View File

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SqlSugar
@ -18,9 +20,11 @@ namespace SqlSugar
var size = GetPageSize(20, count);
Context.Utilities.PageEach(list.ToList(), size, item =>
{
Before(item.ToList());
List<SugarParameter> allParamter = new List<SugarParameter>();
var sql=GetSql(item);
result+=Context.Ado.ExecuteCommand(sql.Key, sql.Value);
After(item.ToList());
});
return result<0?count:result;
}
@ -32,13 +36,82 @@ namespace SqlSugar
var size = GetPageSize(20, count);
await Context.Utilities.PageEachAsync(list.ToList(), size,async item =>
{
Before(item.ToList());
List<SugarParameter> allParamter = new List<SugarParameter>();
var sql = GetSql(item);
result +=await Context.Ado.ExecuteCommandAsync(sql.Key, sql.Value);
After(item.ToList());
});
return result < 0 ? count : result;
}
private void Before(List<T> updateObjects)
{
if (this.Updateable.IsEnableDiffLogEvent && updateObjects.Count > 0)
{
var isDisableMasterSlaveSeparation = this.Updateable.Ado.IsDisableMasterSlaveSeparation;
this.Updateable.Ado.IsDisableMasterSlaveSeparation = true;
var parameters = Updateable.UpdateBuilder.Parameters;
if (parameters == null)
parameters = new List<SugarParameter>();
Updateable.diffModel.BeforeData = GetDiffTable(updateObjects);
Updateable.diffModel.Sql = this.Updateable.UpdateBuilder.ToSqlString();
Updateable.diffModel.Parameters = parameters.ToArray();
this.Updateable.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
}
}
protected void After(List<T> updateObjects)
{
if (this.Updateable.IsEnableDiffLogEvent && updateObjects.Count > 0)
{
var isDisableMasterSlaveSeparation = this.Updateable.Ado.IsDisableMasterSlaveSeparation;
this.Updateable.Ado.IsDisableMasterSlaveSeparation = true;
Updateable.diffModel.AfterData = GetDiffTable(updateObjects);
Updateable.diffModel.Time = this.Context.Ado.SqlExecutionTime;
if (this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent != null)
this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent(Updateable.diffModel);
this.Updateable.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
}
if (this.Updateable.RemoveCacheFunc != null)
{
this.Updateable.RemoveCacheFunc();
}
}
private List<DiffLogTableInfo> GetDiffTable(List<T> updateObjects)
{
var builder = Updateable.UpdateBuilder.Builder;
var tableWithString = builder.GetTranslationColumnName(Updateable.UpdateBuilder.TableName);
var wheres = Updateable.WhereColumnList ?? Updateable.UpdateBuilder.PrimaryKeys;
if (wheres == null)
{
wheres = Updateable.UpdateBuilder.DbColumnInfoList
.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName).Distinct().ToList();
}
var sqlDb = this.Context.CopyNew();
sqlDb.Aop.DataExecuting = null;
var dataColumns = sqlDb.Updateable(updateObjects).UpdateBuilder.DbColumnInfoList;
List<SugarParameter> parameters = new List<SugarParameter>();
StringBuilder allWhereString = new StringBuilder();
string columnStr = string.Join(",", dataColumns.Select(x => x.DbColumnName).Distinct().ToList());
foreach (var item in dataColumns.GroupBy(it => it.TableId))
{
StringBuilder whereString = new StringBuilder();
foreach (var whereItem in wheres)
{
var pk = item.FirstOrDefault(it => it.DbColumnName.EqualCase(whereItem));
var paraterName = FormatValue(pk.PropertyType, pk.DbColumnName, pk.Value, parameters);
whereString.Append($" {pk.DbColumnName} = {paraterName} AND");
}
allWhereString.Append($" {Regex.Replace(whereString.ToString(), "AND$", "")} OR");
}
string key = $"SELECT {columnStr} FROM {tableWithString} WHERE {Regex.Replace(allWhereString.ToString(), "OR$", "")}";
var dt = sqlDb.Ado.GetDataTable(key, parameters);
return Updateable.GetTableDiff(dt);
}
#region Values Helper
public KeyValuePair<string, SugarParameter[]> GetSql(List<T> updateObjects)

View File

@ -757,6 +757,40 @@ namespace SqlSugar
return result;
}
internal List<DiffLogTableInfo> GetTableDiff(DataTable dt)
{
List<DiffLogTableInfo> result = new List<DiffLogTableInfo>();
if (dt.Rows != null && dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
DiffLogTableInfo item = new DiffLogTableInfo();
item.TableDescription = this.EntityInfo.TableDescription;
item.TableName = this.EntityInfo.DbTableName;
item.Columns = new List<DiffLogColumnInfo>();
foreach (DataColumn col in dt.Columns)
{
try
{
var sugarColumn = this.EntityInfo.Columns.Where(it => it.DbColumnName != null).First(it =>
it.DbColumnName.Equals(col.ColumnName, StringComparison.CurrentCultureIgnoreCase));
DiffLogColumnInfo addItem = new DiffLogColumnInfo();
addItem.Value = row[col.ColumnName];
addItem.ColumnName = col.ColumnName;
addItem.IsPrimaryKey = sugarColumn.IsPrimarykey;
addItem.ColumnDescription = sugarColumn.ColumnDescription;
item.Columns.Add(addItem);
}
catch (Exception ex)
{
Check.ExceptionEasy(col.ColumnName + " No corresponding entity attribute found in difference log ." + ex.Message, col.ColumnName + "在差异日志中可能没有找到相应的实体属性,详细:" + ex.Message);
}
}
result.Add(item);
}
}
return result;
}
private List<DiffLogTableInfo> GetDiffTable(string sql, List<SugarParameter> parameters)
{
List<DiffLogTableInfo> result = new List<DiffLogTableInfo>();