Update split table

This commit is contained in:
sunkaixuan 2022-04-25 15:54:40 +08:00
parent f8674a8fbe
commit 6c84575a33
2 changed files with 77 additions and 0 deletions

View File

@ -22,6 +22,7 @@ namespace SqlSugar
public DiffLogModel diffModel { get; set; }
public List<string> tempPrimaryKeys { get; set; }
internal Action RemoveCacheFunc { get; set; }
internal List<T> DeleteObjects { get; set; }
public EntityInfo EntityInfo
{
get
@ -87,6 +88,7 @@ namespace SqlSugar
public IDeleteable<T> Where(List<T> deleteObjs)
{
this.DeleteObjects = deleteObjs;
if (deleteObjs == null || deleteObjs.Count() == 0)
{
Where(SqlBuilder.SqlFalse);
@ -292,6 +294,19 @@ namespace SqlSugar
result.deleteobj = this;
return result;
}
public SplitTableDeleteByObjectProvider<T> SplitTable()
{
SplitTableDeleteByObjectProvider<T> result = new SplitTableDeleteByObjectProvider<T>();
result.Context = this.Context;
Check.ExceptionEasy(this.DeleteObjects == null, "SplitTable() +0 only List<T> can be deleted", "SplitTable()无参数重载只支持根据实体集合删除");
result.deleteObjects = this.DeleteObjects.ToArray();
SplitTableContext helper = new SplitTableContext((SqlSugarProvider)Context)
{
EntityInfo = this.EntityInfo
};
result.deleteobj = this;
return result;
}
public LogicDeleteProvider<T> IsLogic()
{
LogicDeleteProvider<T> result = new LogicDeleteProvider<T>();

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SplitTableDeleteByObjectProvider<T> where T : class, new()
{
public ISqlSugarClient Context;
public DeleteableProvider<T> deleteobj;
public T [] deleteObjects { get; set; }
public int ExecuteCommand()
{
List<GroupModel> groupModels;
int result;
GroupDataList(deleteObjects, out groupModels, out result);
foreach (var item in groupModels.GroupBy(it => it.GroupName))
{
var addList = item.Select(it => it.Item).ToList();
result += this.Context.Deleteable<T>().Where(addList).AS(item.Key).ExecuteCommand();
}
return result;
}
public async Task<int> ExecuteCommandAsync()
{
List<GroupModel> groupModels;
int result;
GroupDataList(deleteObjects, out groupModels, out result);
foreach (var item in groupModels.GroupBy(it => it.GroupName))
{
var addList = item.Select(it => it.Item).ToList();
result +=await this.Context.Deleteable<T>().Where(addList).AS(item.Key).ExecuteCommandAsync();
}
return result;
}
private void GroupDataList(T[] datas, out List<GroupModel> groupModels, out int result)
{
var attribute = typeof(T).GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute;
Check.Exception(attribute == null, $"{typeof(T).Name} need SplitTableAttribute");
groupModels = new List<GroupModel>();
var db = this.Context;
foreach (var item in datas)
{
var value = db.SplitHelper<T>().GetValue(attribute.SplitType, item);
var tableName = db.SplitHelper<T>().GetTableName(attribute.SplitType, value);
groupModels.Add(new GroupModel() { GroupName = tableName, Item = item });
}
result = 0;
}
internal class GroupModel
{
public string GroupName { get; set; }
public T Item { get; set; }
}
}
}