Synchronization code

This commit is contained in:
sunkaixuan 2023-08-30 18:43:18 +08:00
parent f40910cafb
commit d659e56ff0
3 changed files with 100 additions and 0 deletions

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 DeleteablePage<T> where T:class,new()
{
public T[] DataList { get; set; }
public ISqlSugarClient 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.Deleteable(pageItem).AS(TableName).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).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.Deleteable(pageItem).AS(TableName).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).ExecuteCommandAsync();
});
if (isNoTran)
{
await this.Context.Ado.CommitTranAsync();
}
}
catch (Exception)
{
if (isNoTran)
{
await this.Context.Ado.RollbackTranAsync();
}
throw;
}
return result;
}
}
}

View File

@ -524,6 +524,18 @@ namespace SqlSugar
return this;
}
public DeleteablePage<T> PageSize(int pageSize)
{
Check.ExceptionEasy(this.DeleteObjects == null, "PageSize can only be deleted as a List<Class> entity collection", "Deleteable.PageSize()只能是List<Class>实体集合方式删除,并且集合不能为null");
DeleteablePage<T> result = new DeleteablePage<T>();
result.DataList = this.DeleteObjects.ToArray();
result.Context = this.Context;
result.DiffModel = this.diffModel;
result.IsEnableDiffLogEvent= this.IsEnableDiffLogEvent;
result.TableName = this.DeleteBuilder.AsName;
result.PageSize = pageSize;
return result;
}
public IDeleteable<T> With(string lockString)
{
if (this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer)

View File

@ -22,6 +22,7 @@ namespace SqlSugar
IDeleteable<T> Where(T deleteObj);
IDeleteable<T> Where(Expression<Func<T, bool>> expression);
IDeleteable<T> Where(List<T> deleteObjs);
DeleteablePage<T> PageSize(int pageSize);
IDeleteable<T> In<PkType>(PkType primaryKeyValue);
IDeleteable<T> In<PkType>(PkType[] primaryKeyValues);
IDeleteable<T> In<PkType>(List<PkType> primaryKeyValues);