mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Add Updateable().SplitTable(+0)
This commit is contained in:
parent
fc6f217a7f
commit
7d9a026d94
@ -84,6 +84,8 @@ namespace OrmTest
|
||||
new OrderSpliteTest() {Pk=Guid.NewGuid(),Name ="a", Time = DateTime.Now },
|
||||
new OrderSpliteTest() {Pk=Guid.NewGuid(),Name ="a", Time = DateTime.Now.AddMonths(-10) }
|
||||
});
|
||||
var updateList = db.Queryable<OrderSpliteTest>().SplitTable(x1 => x1).Take(10).ToList();
|
||||
db.Updateable(updateList).IgnoreColumns(it=>it.Name).SplitTable().ExecuteCommand();
|
||||
|
||||
db.Fastest<OrderSpliteTest>().SplitTable().BulkUpdate(db.Queryable<OrderSpliteTest>().SplitTable(it=>it).ToList());
|
||||
db.Fastest<OrderSpliteTest>().SplitTable().BulkUpdate(db.Queryable<OrderSpliteTest>().SplitTable(it => it).ToList(),new string[] { "pk"},new string[] { "name"});
|
||||
|
@ -0,0 +1,64 @@
|
||||
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 SplitTableUpdateByObjectProvider<T> where T : class, new()
|
||||
{
|
||||
public SqlSugarProvider Context;
|
||||
public UpdateableProvider<T> updateobj;
|
||||
public T[] UpdateObjects { get; set; }
|
||||
|
||||
public IEnumerable<SplitTableInfo> Tables { get; set; }
|
||||
|
||||
public int ExecuteCommand()
|
||||
{
|
||||
List<GroupModel> groupModels;
|
||||
int result;
|
||||
GroupDataList(UpdateObjects, out groupModels, out result);
|
||||
foreach (var item in groupModels.GroupBy(it => it.GroupName))
|
||||
{
|
||||
var addList = item.Select(it => it.Item).ToList();
|
||||
result += this.Context.Updateable(addList).AS(item.Key).ExecuteCommand();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public async Task<int> ExecuteCommandAsync()
|
||||
{
|
||||
List<GroupModel> groupModels;
|
||||
int result;
|
||||
GroupDataList(UpdateObjects, 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.Updateable(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; }
|
||||
}
|
||||
}
|
||||
}
|
@ -103,6 +103,19 @@ namespace SqlSugar
|
||||
result.updateobj= this;
|
||||
return result;
|
||||
}
|
||||
public SplitTableUpdateByObjectProvider<T> SplitTable()
|
||||
{
|
||||
Check.ExceptionEasy(UpdateParameterIsNull, "SplitTable() not supported db.Updateable<T>(),use db.Updateable(list)", ".SplitTable()不支持 db.Updateable<T>()方式更新,请使用 db.Updateable(list) 对象方式更新, 或者使用 .SplitTable(+1)重载");
|
||||
SplitTableUpdateByObjectProvider<T> result = new SplitTableUpdateByObjectProvider<T>();
|
||||
result.Context = this.Context;
|
||||
result.UpdateObjects = this.UpdateObjs;
|
||||
SplitTableContext helper = new SplitTableContext(Context)
|
||||
{
|
||||
EntityInfo = this.EntityInfo
|
||||
};
|
||||
result.updateobj = this;
|
||||
return result;
|
||||
}
|
||||
public IUpdateable<T> RemoveDataCache()
|
||||
{
|
||||
this.RemoveCacheFunc = () =>
|
||||
|
@ -95,6 +95,6 @@ namespace SqlSugar
|
||||
KeyValuePair<string,List<SugarParameter>> ToSql();
|
||||
void AddQueue();
|
||||
SplitTableUpdateProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc);
|
||||
|
||||
SplitTableUpdateByObjectProvider<T> SplitTable();
|
||||
}
|
||||
}
|
||||
|
@ -99,6 +99,7 @@
|
||||
<Compile Include="Abstract\QueryableProvider\NavigatManager.cs" />
|
||||
<Compile Include="Abstract\SaveableProvider\StorageableDataTable.cs" />
|
||||
<Compile Include="Abstract\SugarProvider\SqlSugarCoreProvider.cs" />
|
||||
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
|
||||
<Compile Include="Entities\SugarAbMapping.cs" />
|
||||
<Compile Include="Entities\JoinMapper.cs" />
|
||||
<Compile Include="Enum\NavigatType.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user