Add: Updateable.PageSize

This commit is contained in:
sunkaixuan 2023-08-24 14:37:58 +08:00
parent 2d253bcf78
commit e4e53ba25c
4 changed files with 107 additions and 1 deletions

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

@ -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>
{