mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-08 02:14:53 +08:00
Split table
This commit is contained in:
@@ -28,6 +28,12 @@ namespace OrmTest
|
|||||||
var list=db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Take(3)).ToList();
|
var list=db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Take(3)).ToList();
|
||||||
|
|
||||||
var x = db.Deleteable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid()).SplitTable(tabs => tabs.Take(3)).ExecuteCommand();
|
var x = db.Deleteable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid()).SplitTable(tabs => tabs.Take(3)).ExecuteCommand();
|
||||||
|
|
||||||
|
var x2 = db.Updateable<OrderSpliteTest>()
|
||||||
|
.SetColumns(it=>it.Name=="a")
|
||||||
|
.Where(it => it.Pk == Guid.NewGuid())
|
||||||
|
.SplitTable(tabs => tabs.Take(3))
|
||||||
|
.ExecuteCommand();
|
||||||
Console.WriteLine("#### CodeFirst end ####");
|
Console.WriteLine("#### CodeFirst end ####");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,51 @@ namespace SqlSugar
|
|||||||
public DeleteableProvider<T> deleteobj;
|
public DeleteableProvider<T> deleteobj;
|
||||||
|
|
||||||
public IEnumerable<SplitTableInfo> Tables { get; set; }
|
public IEnumerable<SplitTableInfo> Tables { get; set; }
|
||||||
|
|
||||||
public int ExecuteCommand()
|
public int ExecuteCommand()
|
||||||
|
{
|
||||||
|
if (this.Context.Ado.Transaction == null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.Context.Ado.BeginTran();
|
||||||
|
var result = _ExecuteCommand();
|
||||||
|
this.Context.Ado.CommitTran();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.Context.Ado.RollbackTran();
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _ExecuteCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public async Task<int> ExecuteCommandAsync()
|
||||||
|
{
|
||||||
|
if (this.Context.Ado.Transaction == null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.Context.Ado.BeginTran();
|
||||||
|
var result = await _ExecuteCommandAsync();
|
||||||
|
this.Context.Ado.BeginTran();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.Context.Ado.RollbackTran();
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return await _ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int _ExecuteCommand()
|
||||||
{
|
{
|
||||||
var result = 0;
|
var result = 0;
|
||||||
var sqlobj = deleteobj.ToSql();
|
var sqlobj = deleteobj.ToSql();
|
||||||
@@ -27,7 +70,7 @@ namespace SqlSugar
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> ExecuteCommandAsync()
|
public async Task<int> _ExecuteCommandAsync()
|
||||||
{
|
{
|
||||||
var result = 0;
|
var result = 0;
|
||||||
var sqlobj = deleteobj.ToSql();
|
var sqlobj = deleteobj.ToSql();
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public class SplitTableUpdateProvider<T> where T : class, new()
|
||||||
|
{
|
||||||
|
public SqlSugarProvider Context;
|
||||||
|
public UpdateableProvider<T> updateobj;
|
||||||
|
|
||||||
|
public IEnumerable<SplitTableInfo> Tables { get; set; }
|
||||||
|
|
||||||
|
public int ExecuteCommand()
|
||||||
|
{
|
||||||
|
if (this.Context.Ado.Transaction == null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.Context.Ado.BeginTran();
|
||||||
|
var result = _ExecuteCommand();
|
||||||
|
this.Context.Ado.CommitTran();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.Context.Ado.RollbackTran();
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _ExecuteCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public async Task<int> ExecuteCommandAsync()
|
||||||
|
{
|
||||||
|
if (this.Context.Ado.Transaction == null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.Context.Ado.BeginTran();
|
||||||
|
var result = await _ExecuteCommandAsync();
|
||||||
|
this.Context.Ado.BeginTran();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.Context.Ado.RollbackTran();
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return await _ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private int _ExecuteCommand()
|
||||||
|
{
|
||||||
|
var result = 0;
|
||||||
|
var sqlobj = updateobj.ToSql();
|
||||||
|
|
||||||
|
foreach (var item in Tables)
|
||||||
|
{
|
||||||
|
var newsqlobj = GetSqlObj(sqlobj, item.TableName);
|
||||||
|
result += this.Context.Ado.ExecuteCommand(newsqlobj.Key, newsqlobj.Value);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<int> _ExecuteCommandAsync()
|
||||||
|
{
|
||||||
|
var result = 0;
|
||||||
|
var sqlobj = updateobj.ToSql();
|
||||||
|
foreach (var item in Tables)
|
||||||
|
{
|
||||||
|
var newsqlobj = GetSqlObj(sqlobj, item.TableName);
|
||||||
|
result += await this.Context.Ado.ExecuteCommandAsync(newsqlobj.Key, newsqlobj.Value);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private KeyValuePair<string, List<SugarParameter>> GetSqlObj(KeyValuePair<string, List<SugarParameter>> keyValuePair, string asName)
|
||||||
|
{
|
||||||
|
List<SugarParameter> pars = new List<SugarParameter>();
|
||||||
|
string sql = keyValuePair.Key;
|
||||||
|
if (keyValuePair.Value != null)
|
||||||
|
{
|
||||||
|
pars = keyValuePair.Value.Select(it => new SugarParameter(it.ParameterName, it.Value)).ToList();
|
||||||
|
}
|
||||||
|
sql = Regex.Replace(sql, updateobj.EntityInfo.DbTableName, asName, RegexOptions.IgnoreCase);
|
||||||
|
return new KeyValuePair<string, List<SugarParameter>>(sql, pars);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -89,6 +89,21 @@ namespace SqlSugar
|
|||||||
this.UpdateBuilder.TableWithString = lockString;
|
this.UpdateBuilder.TableWithString = lockString;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public SplitTableUpdateProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
|
||||||
|
{
|
||||||
|
this.Context.MappingTables.Add(this.EntityInfo.EntityName, this.EntityInfo.DbTableName);
|
||||||
|
SplitTableUpdateProvider<T> result = new SplitTableUpdateProvider<T>();
|
||||||
|
result.Context = this.Context;
|
||||||
|
SplitTableHelper helper = new SplitTableHelper()
|
||||||
|
{
|
||||||
|
Context = (SqlSugarProvider)Context,
|
||||||
|
EntityInfo = this.EntityInfo
|
||||||
|
};
|
||||||
|
var tables = getTableNamesFunc(helper.GetTables());
|
||||||
|
result.Tables = tables;
|
||||||
|
result.updateobj= this;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
public IUpdateable<T> RemoveDataCache()
|
public IUpdateable<T> RemoveDataCache()
|
||||||
{
|
{
|
||||||
this.RemoveCacheFunc = () =>
|
this.RemoveCacheFunc = () =>
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ namespace SqlSugar
|
|||||||
IUpdateable<T> CallEntityMethod(Expression<Action<T>> method);
|
IUpdateable<T> CallEntityMethod(Expression<Action<T>> method);
|
||||||
KeyValuePair<string,List<SugarParameter>> ToSql();
|
KeyValuePair<string,List<SugarParameter>> ToSql();
|
||||||
void AddQueue();
|
void AddQueue();
|
||||||
|
SplitTableUpdateProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,6 +91,7 @@
|
|||||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||||
<Compile Include="Abstract\InsertableProvider\SplitInsertable.cs" />
|
<Compile Include="Abstract\InsertableProvider\SplitInsertable.cs" />
|
||||||
|
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateProvider.cs" />
|
||||||
<Compile Include="Entities\SqlSguarTransaction.cs" />
|
<Compile Include="Entities\SqlSguarTransaction.cs" />
|
||||||
<Compile Include="Enum\LanguageType.cs" />
|
<Compile Include="Enum\LanguageType.cs" />
|
||||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubAs.cs" />
|
<Compile Include="ExpressionsToSql\Subquery\Items\SubAs.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user