Update Core

This commit is contained in:
sunkaixuan 2022-04-25 15:14:29 +08:00
parent f11b38f4e8
commit f8674a8fbe
5 changed files with 101 additions and 3 deletions

View File

@ -0,0 +1,77 @@
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).IgnoreColumns(GetIgnoreColumns()).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).IgnoreColumns(GetIgnoreColumns()).AS(item.Key).ExecuteCommandAsync();
}
return result;
}
private string [] GetIgnoreColumns()
{
if (this.updateobj.UpdateBuilder.DbColumnInfoList.Any())
{
var columns=this.updateobj.UpdateBuilder.DbColumnInfoList.Select(it => it.DbColumnName).Distinct().ToList();
return this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(x => !columns.Any(y => y.EqualCase(x.DbColumnName))).Select(it => it.DbColumnName).ToArray();
}
else
{
return null;
}
}
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; }
}
}
}

View File

@ -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 = () =>

View File

@ -109,9 +109,12 @@ namespace SqlSugar
mappingA = queryable.QueryBuilder.Builder.GetTranslationColumnName(mappingA);
mappingB = queryable.QueryBuilder.Builder.GetTranslationColumnName(mappingB);
var bTableName = queryable.QueryBuilder.Builder.GetTranslationTableName(this.ProPertyEntity.DbTableName);
mapper.Sql = $" (select count(1) from {bTableName} {this.ProPertyEntity.DbTableName}_1 where {this.ProPertyEntity.DbTableName}_1.{bPk} in (select {mappingA} from {mappingTableName} where {mappingB} = {ShorName}.{aPk} )) ";
mapper.Sql = $" (select count(1) from {bTableName} {this.ProPertyEntity.DbTableName}_1 where {this.ProPertyEntity.DbTableName}_1.{bPk} in (select {mappingB} from {mappingTableName} where {mappingA} = {ShorName}.{aPk} ) )";
if (this.whereSql.HasValue())
mapper.Sql = mapper.Sql + " AND " + this.whereSql;
{
mapper.Sql = mapper.Sql.TrimEnd(')');
mapper.Sql = mapper.Sql + " AND " + this.whereSql+")";
}
mapper.Sql = $" ({mapper.Sql}) ";
mapper.Sql = GetMethodSql(mapper.Sql);
return mapper;

View File

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

View File

@ -54,6 +54,11 @@
<Folder Include="Abstract\AdoProvider\Abstract\SqlBuilderProvider\" />
<Folder Include="Abstract\AdoProvider\Abstract\SugarProvider\" />
<Folder Include="Abstract\AdoProvider\Abstract\UpdateProvider\" />
<Folder Include="CaseWhen\" />
<Folder Include="Common\" />
<Folder Include="DbMethods\" />
<Folder Include="ResolveItems\" />
<Folder Include="Subquery\Items\" />
</ItemGroup>
</Project>