db.Storageable.PageSize(100)

This commit is contained in:
sunkaixuan
2023-08-24 16:14:27 +08:00
parent bfc2b54173
commit 7288921bd7
4 changed files with 258 additions and 10 deletions

View File

@@ -88,6 +88,17 @@ namespace SqlSugar
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Other, conditions, message)); whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Other, conditions, message));
return this; return this;
} }
public StorageablePage<T> PageSize(int PageSize,Action<int> ActionCallBack=null)
{
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() public StorageableSplitProvider<T> SplitTable()
{ {
StorageableSplitProvider<T> result = new StorageableSplitProvider<T>(); 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 class StorageableSplitProvider<T> where T:class,new()
{ {
public Storageable<T> SaveInfo { get; internal set; } internal Storageable<T> SaveInfo { get; set; }
public SqlSugarProvider Context { get; internal set; } internal SqlSugarProvider Context { get; set; }
public List<T> List { get; internal set; } internal List<T> List { get; set; }
public EntityInfo EntityInfo { get; internal set; } internal EntityInfo EntityInfo { get; set; }
public int PageSize = 1000; 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() public int ExecuteCommand()
{ {
if (List.Count > PageSize) if (List.Count > pageSize)
{ {
var result = 0; var result = 0;
this.Context.Utilities.PageEach(List, PageSize, pageItem => this.Context.Utilities.PageEach(List, pageSize, pageItem =>
{ {
result+= _ExecuteCommand(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() public async Task<int> ExecuteCommandAsync()
{ {
if (List.Count > PageSize) if (List.Count > pageSize)
{ {
var result = 0; var result = 0;
this.Context.Utilities.PageEach(List, PageSize, async pageItem => this.Context.Utilities.PageEach(List, pageSize, async pageItem =>
{ {
result +=await _ExecuteCommandAsync(pageItem); result +=await _ExecuteCommandAsync(pageItem);
if (ActionCallBack != null)
{
ActionCallBack(result);
}
}); });
return result; return result;
} }
@@ -51,6 +79,29 @@ namespace SqlSugar
return await _ExecuteCommandAsync(list); 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) private async Task<int> _ExecuteCommandAsync(List<T> list)
{ {
int resultValue = 0; int resultValue = 0;
@@ -61,6 +112,10 @@ namespace SqlSugar
{ {
var addList = item.Select(it => it.Item).ToList(); var addList = item.Select(it => it.Item).ToList();
resultValue +=await this.Context.Storageable(addList).ExecuteCommandAsync(); resultValue +=await this.Context.Storageable(addList).ExecuteCommandAsync();
if (ActionCallBack != null)
{
ActionCallBack(resultValue);
}
} }
return result; return result;
} }
@@ -78,6 +133,37 @@ namespace SqlSugar
return result; 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) private void GroupDataList(List<T> datas, out List<GroupModel> groupModels, out int result)
{ {
var attribute = typeof(T).GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute; var attribute = typeof(T).GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute;

View File

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