Browse Source

Add Updateable().SplitTable(+0)

pull/18/head
sunkaixuan 3 years ago
parent
commit
7d9a026d94
  1. 2
      Src/Asp.Net/SqlServerTest/Demo/DemoN_SplitTable.cs
  2. 64
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/SplitTableUpdateByObjectProvider.cs
  3. 13
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs
  4. 2
      Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs
  5. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

2
Src/Asp.Net/SqlServerTest/Demo/DemoN_SplitTable.cs

@ -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 },
new OrderSpliteTest() {Pk=Guid.NewGuid(),Name ="a", Time = DateTime.Now.AddMonths(-10) } 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());
db.Fastest<OrderSpliteTest>().SplitTable().BulkUpdate(db.Queryable<OrderSpliteTest>().SplitTable(it => it).ToList(),new string[] { "pk"},new string[] { "name"}); db.Fastest<OrderSpliteTest>().SplitTable().BulkUpdate(db.Queryable<OrderSpliteTest>().SplitTable(it => it).ToList(),new string[] { "pk"},new string[] { "name"});

64
Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/SplitTableUpdateByObjectProvider.cs

@ -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; }
}
}
}

13
Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs

@ -103,6 +103,19 @@ namespace SqlSugar
result.updateobj= this; result.updateobj= this;
return result; 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() public IUpdateable<T> RemoveDataCache()
{ {
this.RemoveCacheFunc = () => this.RemoveCacheFunc = () =>

2
Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs

@ -95,6 +95,6 @@ namespace SqlSugar
KeyValuePair<string,List<SugarParameter>> ToSql(); KeyValuePair<string,List<SugarParameter>> ToSql();
void AddQueue(); void AddQueue();
SplitTableUpdateProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc); SplitTableUpdateProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc);
SplitTableUpdateByObjectProvider<T> SplitTable();
} }
} }

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -99,6 +99,7 @@
<Compile Include="Abstract\QueryableProvider\NavigatManager.cs" /> <Compile Include="Abstract\QueryableProvider\NavigatManager.cs" />
<Compile Include="Abstract\SaveableProvider\StorageableDataTable.cs" /> <Compile Include="Abstract\SaveableProvider\StorageableDataTable.cs" />
<Compile Include="Abstract\SugarProvider\SqlSugarCoreProvider.cs" /> <Compile Include="Abstract\SugarProvider\SqlSugarCoreProvider.cs" />
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
<Compile Include="Entities\SugarAbMapping.cs" /> <Compile Include="Entities\SugarAbMapping.cs" />
<Compile Include="Entities\JoinMapper.cs" /> <Compile Include="Entities\JoinMapper.cs" />
<Compile Include="Enum\NavigatType.cs" /> <Compile Include="Enum\NavigatType.cs" />

Loading…
Cancel
Save