From 7288921bd7ba80f10dae08e8e36fcb59fa758e6c Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 24 Aug 2023 16:14:27 +0800 Subject: [PATCH] db.Storageable.PageSize(100) --- .../Abstract/SaveableProvider/Storageable.cs | 11 ++ .../SaveableProvider/StorageablePage.cs | 150 ++++++++++++++++++ .../StorageableSplitProvider.cs | 106 +++++++++++-- .../SqlSugar/Interface/IStorageable.cs | 1 + 4 files changed, 258 insertions(+), 10 deletions(-) create mode 100644 Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageablePage.cs diff --git a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs index 12ce79e6d..cabe79878 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs @@ -88,6 +88,17 @@ namespace SqlSugar whereFuncs.Add(new KeyValuePair, bool>, string>(StorageType.Other, conditions, message)); return this; } + public StorageablePage PageSize(int PageSize,Action ActionCallBack=null) + { + StorageablePage page = new StorageablePage(); + 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 SplitTable() { StorageableSplitProvider result = new StorageableSplitProvider(); diff --git a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageablePage.cs b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageablePage.cs new file mode 100644 index 000000000..d667af460 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageablePage.cs @@ -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 where T : class,new() + { + public SqlSugarProvider Context { get; set; } + public List Data { get; set; } + public int PageSize { get; internal set; } + public Action ActionCallBack { get; internal set; } + public string TableName { get; internal set; } + public Expression> 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 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 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; + } + + } +} diff --git a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageableSplitProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageableSplitProvider.cs index f51dea464..9e032f3c8 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageableSplitProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageableSplitProvider.cs @@ -9,17 +9,23 @@ namespace SqlSugar { public class StorageableSplitProvider where T:class,new() { - public Storageable SaveInfo { get; internal set; } - public SqlSugarProvider Context { get; internal set; } - public List List { get; internal set; } - public EntityInfo EntityInfo { get; internal set; } - public int PageSize = 1000; + internal Storageable SaveInfo { get; set; } + internal SqlSugarProvider Context { get; set; } + internal List List { get; set; } + internal EntityInfo EntityInfo { get; set; } + internal int pageSize = 1000; + internal Action ActionCallBack =null; + public StorageableSplitProvider PageSize(int size, Action 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 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 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 _ExecuteCommandAsync(List 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 _ExecuteSqlBulkCopyAsync(List list) + { + int resultValue = 0; + List 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 list) + { + int resultValue = 0; + List 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 datas, out List groupModels, out int result) { var attribute = typeof(T).GetCustomAttribute() as SplitTableAttribute; diff --git a/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs index 33769eec0..6fa69b9f7 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs @@ -32,6 +32,7 @@ namespace SqlSugar Task ExecuteSqlBulkCopyAsync(); IStorageable DefaultAddElseUpdate(); StorageableSplitProvider SplitTable(); + StorageablePage PageSize(int PaegSize, Action ActionCallBack = null); } public class StorageableInfo where T : class, new()