Synchronization code

This commit is contained in:
sunkaixuan 2023-08-24 16:22:04 +08:00
parent cb1f3b42ff
commit 10109de1c2
11 changed files with 540 additions and 12 deletions

View File

@ -0,0 +1,157 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class InsertablePage<T> where T:class,new()
{
public int PageSize { get; set; }
public SqlSugarProvider Context { get; set; }
public T[] DataList { get; set; }
public string TableName { get; internal set; }
public List<string> InsertColumns { get; internal set; }
public bool IsEnableDiffLogEvent { get; internal set; }
public DiffLogModel DiffModel { get; internal set; }
public int ExecuteCommand()
{
if (DataList.Count() == 1 && DataList.First() == null)
{
return 0;
}
if (PageSize == 0) { PageSize = 1000; }
var result = 0;
var isNoTran = this.Context.Ado.IsNoTran();
try
{
if (isNoTran)
{
this.Context.Ado.BeginTran();
}
this.Context.Utilities.PageEach(DataList, PageSize, pageItem =>
{
result += this.Context.Insertable(pageItem).AS(TableName).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteCommand();
});
if (isNoTran)
{
this.Context.Ado.CommitTran();
}
}
catch (Exception)
{
if (isNoTran)
{
this.Context.Ado.RollbackTran();
}
throw;
}
return result;
}
public async Task<int> ExecuteCommandAsync()
{
if (DataList.Count() == 1 && DataList.First() == null)
{
return 0;
}
if (PageSize == 0) { PageSize = 1000; }
var result = 0;
var isNoTran = this.Context.Ado.IsNoTran();
try
{
if (isNoTran)
{
await this.Context.Ado.BeginTranAsync();
}
await this.Context.Utilities.PageEachAsync(DataList, PageSize, async pageItem =>
{
result +=await this.Context.Insertable(pageItem).AS(TableName).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteCommandAsync();
});
if (isNoTran)
{
await this.Context.Ado.CommitTranAsync();
}
}
catch (Exception)
{
if (isNoTran)
{
await this.Context.Ado.RollbackTranAsync();
}
throw;
}
return result;
}
public List<long> ExecuteReturnSnowflakeIdList()
{
if (DataList.Count() == 1 && DataList.First() == null)
{
return new List<long>();
}
if (PageSize == 0) { PageSize = 1000; }
var result = new List<long>();
var isNoTran = this.Context.Ado.IsNoTran();
try
{
if (isNoTran)
{
this.Context.Ado.BeginTran();
}
this.Context.Utilities.PageEach(DataList, PageSize, pageItem =>
{
result.AddRange(this.Context.Insertable(pageItem).AS(TableName).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteReturnSnowflakeIdList());
});
if (isNoTran)
{
this.Context.Ado.CommitTran();
}
}
catch (Exception)
{
if (isNoTran)
{
this.Context.Ado.RollbackTran();
}
throw;
}
return result;
}
public async Task<List<long>> ExecuteReturnSnowflakeIdListAsync()
{
if (DataList.Count() == 1 && DataList.First() == null)
{
return new List<long>();
}
if (PageSize == 0) { PageSize = 1000; }
var result = new List<long>();
var isNoTran = this.Context.Ado.IsNoTran();
try
{
if (isNoTran)
{
await this.Context.Ado.BeginTranAsync();
}
await this.Context.Utilities.PageEachAsync(DataList, PageSize, async pageItem =>
{
result.AddRange(await this.Context.Insertable(pageItem).AS(TableName).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteReturnSnowflakeIdListAsync());
});
if (isNoTran)
{
await this.Context.Ado.CommitTranAsync();
}
}
catch (Exception)
{
if (isNoTran)
{
await this.Context.Ado.RollbackTranAsync();
}
throw;
}
return result;
}
}
}

View File

@ -465,7 +465,19 @@ namespace SqlSugar
#endregion
#region Setting
public InsertablePage<T> PageSize(int pageSize)
{
InsertablePage<T> result = new InsertablePage<T>();
result.PageSize = pageSize;
result.Context = this.Context;
result.DataList = this.InsertObjs;
result.TableName = this.InsertBuilder.AsName;
result.IsEnableDiffLogEvent = this.IsEnableDiffLogEvent;
result.DiffModel = this.diffModel;
if(this.InsertBuilder.DbColumnInfoList.Any())
result.InsertColumns = this.InsertBuilder.DbColumnInfoList.GroupBy(it => it.TableId).First().Select(it=>it.DbColumnName).ToList();
return result;
}
public IParameterInsertable<T> UseParameter()
{
var result = new ParameterInsertable<T>();

View File

@ -88,6 +88,21 @@ namespace SqlSugar
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Other, conditions, message));
return this;
}
public StorageablePage<T> PageSize(int PageSize,Action<int> ActionCallBack=null)
{
if (PageSize > 10000)
{
Check.ExceptionEasy("Advanced save page Settings should not exceed 10,000, and the reasonable number of pages is about 2000", "高级保存分页设置不要超过1万合理分页数在2000左右");
}
StorageablePage<T> page = new StorageablePage<T>();
page.Context = this.Context;
page.PageSize = PageSize;
page.Data = this.allDatas.Select(it => it.Item).ToList();
page.ActionCallBack = ActionCallBack;
page.TableName = this.asname;
page.whereExpression = this.whereExpression;
return page;
}
public StorageableSplitProvider<T> SplitTable()
{
StorageableSplitProvider<T> result = new StorageableSplitProvider<T>();

View File

@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class StorageablePage<T> where T : class,new()
{
public SqlSugarProvider Context { get; set; }
public List<T> Data { get; set; }
public int PageSize { get; internal set; }
public Action<int> ActionCallBack { get; internal set; }
public string TableName { get; internal set; }
public Expression<Func<T, object>> whereExpression { get; internal set; }
public int ExecuteCommand()
{
if (Data.Count() == 1 && Data.First() == null)
{
return 0;
}
if (PageSize == 0) { PageSize = 1000; }
var result = 0;
var isNoTran = this.Context.Ado.IsNoTran();
try
{
if (isNoTran)
{
this.Context.Ado.BeginTran();
}
this.Context.Utilities.PageEach(Data, PageSize, pageItem =>
{
result += this.Context.Storageable(pageItem).As(TableName).WhereColumns(whereExpression).ExecuteCommand();
if (ActionCallBack != null)
{
ActionCallBack(result);
}
});
if (isNoTran)
{
this.Context.Ado.CommitTran();
}
}
catch (Exception)
{
if (isNoTran)
{
this.Context.Ado.RollbackTran();
}
throw;
}
return result;
}
public async Task<int> ExecuteCommandAsync()
{
if (Data.Count() == 1 && Data.First() == null)
{
return 0;
}
if (PageSize == 0) { PageSize = 1000; }
var result = 0;
var isNoTran = this.Context.Ado.IsNoTran();
try
{
if (isNoTran)
{
await this.Context.Ado.BeginTranAsync();
}
await this.Context.Utilities.PageEachAsync(Data, PageSize, async pageItem =>
{
result += await this.Context.Storageable(pageItem).As(TableName).WhereColumns(whereExpression).ExecuteCommandAsync();
if (ActionCallBack != null)
{
ActionCallBack(result);
}
});
if (isNoTran)
{
await this.Context.Ado.CommitTranAsync();
}
}
catch (Exception)
{
if (isNoTran)
{
await this.Context.Ado.RollbackTranAsync();
}
throw;
}
return result;
}
public int ExecuteSqlBulkCopy()
{
if (Data.Count() == 1 && Data.First() == null)
{
return 0;
}
if (PageSize == 0) { PageSize = 1000; }
var result = 0;
var isNoTran = this.Context.Ado.IsNoTran();
try
{
this.Context.Utilities.PageEach(Data, PageSize, pageItem =>
{
result += this.Context.Storageable(pageItem).As(TableName).WhereColumns(whereExpression).ExecuteSqlBulkCopy();
if (ActionCallBack != null)
{
ActionCallBack(result);
}
});
}
catch (Exception)
{
throw;
}
return result;
}
public async Task<int> ExecuteSqlBulkCopyAsync()
{
if (Data.Count() == 1 && Data.First() == null)
{
return 0;
}
if (PageSize == 0) { PageSize = 1000; }
var result = 0;
var isNoTran = this.Context.Ado.IsNoTran();
try
{
await this.Context.Utilities.PageEachAsync(Data, PageSize, async pageItem =>
{
result += await this.Context.Storageable(pageItem).As(TableName).WhereColumns(whereExpression).ExecuteSqlBulkCopyAsync();
if (ActionCallBack != null)
{
ActionCallBack(result);
}
});
}
catch (Exception)
{
throw;
}
return result;
}
}
}

View File

@ -9,17 +9,23 @@ namespace SqlSugar
{
public class StorageableSplitProvider<T> where T:class,new()
{
public Storageable<T> SaveInfo { get; internal set; }
public SqlSugarProvider Context { get; internal set; }
public List<T> List { get; internal set; }
public EntityInfo EntityInfo { get; internal set; }
public int PageSize = 1000;
internal Storageable<T> SaveInfo { get; set; }
internal SqlSugarProvider Context { get; set; }
internal List<T> List { get; set; }
internal EntityInfo EntityInfo { get; set; }
internal int pageSize = 1000;
internal Action<int> ActionCallBack =null;
public StorageableSplitProvider<T> PageSize(int size, Action<int> ActionCallBack = null)
{
this.pageSize = size;
return this;
}
public int ExecuteCommand()
{
if (List.Count > PageSize)
if (List.Count > pageSize)
{
var result = 0;
this.Context.Utilities.PageEach(List, PageSize, pageItem =>
this.Context.Utilities.PageEach(List, pageSize, pageItem =>
{
result+= _ExecuteCommand(pageItem);
});
@ -32,16 +38,38 @@ namespace SqlSugar
}
}
public int ExecuteSqlBulkCopy()
{
if (List.Count > pageSize)
{
var result = 0;
this.Context.Utilities.PageEach(List, pageSize, pageItem =>
{
result += _ExecuteSqlBulkCopy(pageItem);
});
return result;
}
else
{
var list = List;
return _ExecuteSqlBulkCopy(list);
}
}
public async Task<int> ExecuteCommandAsync()
{
if (List.Count > PageSize)
if (List.Count > pageSize)
{
var result = 0;
this.Context.Utilities.PageEach(List, PageSize, async pageItem =>
this.Context.Utilities.PageEach(List, pageSize, async pageItem =>
{
result +=await _ExecuteCommandAsync(pageItem);
if (ActionCallBack != null)
{
ActionCallBack(result);
}
});
return result;
}
@ -51,6 +79,29 @@ namespace SqlSugar
return await _ExecuteCommandAsync(list);
}
}
public async Task<int> ExecuteSqlBulkCopyAsync()
{
if (List.Count > pageSize)
{
var result = 0;
this.Context.Utilities.PageEach(List, pageSize, async pageItem =>
{
result += await _ExecuteSqlBulkCopyAsync(pageItem);
if (ActionCallBack != null)
{
ActionCallBack(result);
}
});
return result;
}
else
{
var list = List;
return await _ExecuteSqlBulkCopyAsync(list);
}
}
private async Task<int> _ExecuteCommandAsync(List<T> list)
{
int resultValue = 0;
@ -61,6 +112,10 @@ namespace SqlSugar
{
var addList = item.Select(it => it.Item).ToList();
resultValue +=await this.Context.Storageable(addList).ExecuteCommandAsync();
if (ActionCallBack != null)
{
ActionCallBack(resultValue);
}
}
return result;
}
@ -78,6 +133,37 @@ namespace SqlSugar
return result;
}
private async Task<int> _ExecuteSqlBulkCopyAsync(List<T> list)
{
int resultValue = 0;
List<GroupModel> groupModels;
int result;
GroupDataList(list, out groupModels, out result);
foreach (var item in groupModels.GroupBy(it => it.GroupName))
{
var addList = item.Select(it => it.Item).ToList();
resultValue += await this.Context.Storageable(addList).ExecuteSqlBulkCopyAsync();
if (ActionCallBack != null)
{
ActionCallBack(resultValue);
}
}
return result;
}
private int _ExecuteSqlBulkCopy(List<T> list)
{
int resultValue = 0;
List<GroupModel> groupModels;
int result;
GroupDataList(list, out groupModels, out result);
foreach (var item in groupModels.GroupBy(it => it.GroupName))
{
var addList = item.Select(it => it.Item).ToList();
resultValue += this.Context.Storageable(addList).As(item.Key).ExecuteSqlBulkCopy();
}
return result;
}
private void GroupDataList(List<T> datas, out List<GroupModel> groupModels, out int result)
{
var attribute = typeof(T).GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute;

View File

@ -735,6 +735,9 @@ namespace SqlSugar
{
Check.Exception(UpdateParameterIsNull == false, ErrorMessage.GetThrowMessage(" no support SetColumns and Where", "根据对像更新 db.Updateabe(对象) 禁止使用 SetColumns和Where ,你可以使用WhereColumns 和 UpdateColumns。 更新分为2种方式 1.根据表达式更新 2.根据实体或者集合更新 具体用法请查看文档 "));
}
private void ThrowUpdateByExpressionByMesage(string message)
{
Check.Exception(UpdateParameterIsNull == true, ErrorMessage.GetThrowMessage(" no support "+ message, "根据对像更新 db.Updateabe(对象) 禁止使用 SetColumns和Where ,你可以使用 "+ message + "。 更新分为2种方式 1.根据表达式更新 2.根据实体或者集合更新 具体用法请查看文档 "));
}
}
}

View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class UpdateablePage<T> where T:class,new()
{
public T[] DataList { get; set; }
public SqlSugarProvider Context { get; set; }
public int PageSize { get; internal set; }
public string TableName { get; internal set; }
public bool IsEnableDiffLogEvent { get; internal set; }
public DiffLogModel DiffModel { get; internal set; }
public List<string> UpdateColumns { get; internal set; }
public int ExecuteCommand()
{
if (DataList.Count() == 1 && DataList.First() == null)
{
return 0;
}
if (PageSize == 0) { PageSize = 1000; }
var result = 0;
var isNoTran = this.Context.Ado.IsNoTran();
try
{
if (isNoTran)
{
this.Context.Ado.BeginTran();
}
this.Context.Utilities.PageEach(DataList, PageSize, pageItem =>
{
result += this.Context.Updateable(pageItem).AS(TableName).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).UpdateColumns(UpdateColumns.ToArray()).ExecuteCommand();
});
if (isNoTran)
{
this.Context.Ado.CommitTran();
}
}
catch (Exception)
{
if (isNoTran)
{
this.Context.Ado.RollbackTran();
}
throw;
}
return result;
}
public async Task<int> ExecuteCommandAsync()
{
if (DataList.Count() == 1 && DataList.First() == null)
{
return 0;
}
if (PageSize == 0) { PageSize = 1000; }
var result = 0;
var isNoTran = this.Context.Ado.IsNoTran();
try
{
if (isNoTran)
{
await this.Context.Ado.BeginTranAsync();
}
await this.Context.Utilities.PageEachAsync(DataList, PageSize, async pageItem =>
{
result += await this.Context.Updateable(pageItem).AS(TableName).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).UpdateColumns(UpdateColumns.ToArray()).ExecuteCommandAsync();
});
if (isNoTran)
{
await this.Context.Ado.CommitTranAsync();
}
}
catch (Exception)
{
if (isNoTran)
{
await this.Context.Ado.RollbackTranAsync();
}
throw;
}
return result;
}
}
}

View File

@ -173,6 +173,20 @@ namespace SqlSugar
#endregion
#region Common
public UpdateablePage<T> PageSize(int pageSize)
{
ThrowUpdateByExpressionByMesage(" PageSize(num) ");
UpdateablePage<T> result = new UpdateablePage<T>();
result.PageSize = pageSize;
result.Context = this.Context;
result.DataList = this.UpdateObjs;
result.TableName = this.UpdateBuilder.TableName;
result.IsEnableDiffLogEvent = this.IsEnableDiffLogEvent;
result.DiffModel = this.diffModel;
if (this.UpdateBuilder.DbColumnInfoList.Any())
result.UpdateColumns = this.UpdateBuilder.DbColumnInfoList.GroupBy(it => it.TableId).First().Select(it => it.DbColumnName).ToList();
return result;
}
public IUpdateable<T, T2> InnerJoin<T2>(Expression<Func<T, T2, bool>> joinExpress)
{
UpdateableProvider<T, T2> result = new UpdateableProvider<T, T2>();

View File

@ -32,6 +32,7 @@ namespace SqlSugar
Task<int> ExecuteSqlBulkCopyAsync();
IStorageable<T> DefaultAddElseUpdate();
StorageableSplitProvider<T> SplitTable();
StorageablePage<T> PageSize(int PaegSize, Action<int> ActionCallBack = null);
}
public class StorageableInfo<T> where T : class, new()

View File

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@ -114,6 +115,7 @@ namespace SqlSugar
IUpdateable<T> EnableQueryFilter();
IUpdateable<T> Clone();
IUpdateable<T,T2> InnerJoin<T2>(Expression<Func<T,T2,bool>> joinExpress);
UpdateablePage<T> PageSize(int pageSize);
}
public interface IUpdateable<T, T2>
{

View File

@ -62,5 +62,6 @@ namespace SqlSugar
void AddQueue();
IInsertable<T> MySqlIgnore();
IInsertable<T> OffIdentity();
InsertablePage<T> PageSize(int pageSize);
}
}