Synchronization code

This commit is contained in:
sunkaixuan
2023-05-28 20:59:16 +08:00
parent 2eeeb074d3
commit 405978ba63
8 changed files with 43 additions and 0 deletions

View File

@@ -54,8 +54,10 @@ namespace SqlSugar
var mappingInfo=this.Context.MappingTables.FirstOrDefault(it => it.EntityName == entityType.Name);
if (mappingInfo == null)
{
UtilMethods.StartCustomSplitTable(this.Context,entityType);
this.Context.CodeFirst.SplitTables().InitTables(entityType);
this.Context.MappingTables.RemoveAll(it=>it.EntityName==entityType.Name);
UtilMethods.EndCustomSplitTable(this.Context, entityType);
return;
}
}

View File

@@ -350,6 +350,7 @@ namespace SqlSugar
}
public SplitTableDeleteProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
{
UtilMethods.StartCustomSplitTable(this.Context, typeof(T));
this.Context.MappingTables.Add(this.EntityInfo.EntityName, this.EntityInfo.DbTableName);
SplitTableDeleteProvider<T> result = new SplitTableDeleteProvider<T>();
result.Context = this.Context;
@@ -364,6 +365,7 @@ namespace SqlSugar
}
public SplitTableDeleteByObjectProvider<T> SplitTable()
{
UtilMethods.StartCustomSplitTable(this.Context, typeof(T));
SplitTableDeleteByObjectProvider<T> result = new SplitTableDeleteByObjectProvider<T>();
result.Context = this.Context;
Check.ExceptionEasy(this.DeleteObjects == null, "SplitTable() +0 only List<T> can be deleted", "SplitTable()无参数重载只支持根据实体集合删除");

View File

@@ -665,6 +665,7 @@ namespace SqlSugar
}
public SplitInsertable<T> SplitTable(SplitType splitType)
{
UtilMethods.StartCustomSplitTable(this.Context, typeof(T));
SplitTableContext helper = new SplitTableContext(Context)
{
EntityInfo = this.EntityInfo
@@ -688,6 +689,7 @@ namespace SqlSugar
public SplitInsertable<T> SplitTable()
{
UtilMethods.StartCustomSplitTable(this.Context, typeof(T));
var splitTableAttribute = typeof(T).GetCustomAttribute<SplitTableAttribute>();
if (splitTableAttribute != null)
{

View File

@@ -1262,11 +1262,13 @@ namespace SqlSugar
}
public ISugarQueryable<T> SplitTable()
{
UtilMethods.StartCustomSplitTable(this.Context, typeof(T));
//all table
return this.SplitTable(tag => tag);
}
public ISugarQueryable<T> SplitTable(DateTime beginTime, DateTime endTime)
{
UtilMethods.StartCustomSplitTable(this.Context, typeof(T));
var splitColumn = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyInfo.GetCustomAttribute<SplitFieldAttribute>() != null);
Check.ExceptionEasy(splitColumn==null,"[SplitFieldAttribute] need to be added to the table field", "需要在分表字段加上属性[SplitFieldAttribute]");
var columnName = this.SqlBuilder.GetTranslationColumnName(splitColumn.DbColumnName);
@@ -1307,6 +1309,7 @@ namespace SqlSugar
}
public ISugarQueryable<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
{
UtilMethods.StartCustomSplitTable(this.Context, typeof(T));
SplitTableContext helper = new SplitTableContext(Context)
{
EntityInfo = this.EntityInfo

View File

@@ -1599,6 +1599,7 @@ namespace SqlSugar
#region Split table
public SplitTableContext SplitHelper<T>() where T : class, new()
{
UtilMethods.StartCustomSplitTable(this, typeof(T));
var result = new SplitTableContext(this.Context)
{
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()
@@ -1607,6 +1608,7 @@ namespace SqlSugar
}
public SplitTableContext SplitHelper(Type entityType)
{
UtilMethods.StartCustomSplitTable(this,entityType);
var result = new SplitTableContext(this.Context)
{
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo(entityType)
@@ -1615,6 +1617,7 @@ namespace SqlSugar
}
public SplitTableContextResult<T> SplitHelper<T>(T data) where T : class, new()
{
UtilMethods.StartCustomSplitTable(this, typeof(T));
var result = new SplitTableContext(this.Context)
{
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()
@@ -1627,6 +1630,7 @@ namespace SqlSugar
}
public SplitTableContextResult<T> SplitHelper<T>(List<T> data) where T : class, new()
{
UtilMethods.StartCustomSplitTable(this, typeof(T));
var result = new SplitTableContext(this.Context)
{
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()

View File

@@ -221,6 +221,7 @@ namespace SqlSugar
}
public SplitTableUpdateProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
{
UtilMethods.StartCustomSplitTable(this.Context, typeof(T));
this.Context.MappingTables.Add(this.EntityInfo.EntityName, this.EntityInfo.DbTableName);
SplitTableUpdateProvider<T> result = new SplitTableUpdateProvider<T>();
result.Context = this.Context;
@@ -235,6 +236,7 @@ namespace SqlSugar
}
public SplitTableUpdateByObjectProvider<T> SplitTable()
{
UtilMethods.StartCustomSplitTable(this.Context, typeof(T));
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;

View File

@@ -10,10 +10,20 @@ namespace SqlSugar
public class SplitTableAttribute : Attribute
{
public SplitType SplitType { get; set; }
public Type CustomSplitTableService { get; set; }
public SplitTableAttribute(SplitType splitType)
{
this.SplitType = splitType;
}
public SplitTableAttribute(SplitType splitType,Type customSplitTableService)
{
this.SplitType = splitType;
if (!customSplitTableService.GetInterfaces().Any(it => it == typeof(ISplitTableService)))
{
Check.ExceptionEasy("customSplitTableService in SplitTableAttribute(SplitType splitType,Type customSplitTableService) must be inherited ISplitTableService", " SplitTableAttribute(SplitType splitType,Type customSplitTableService) 中的 customSplitTableService 必须继承 ISplitTableService");
}
this.CustomSplitTableService= customSplitTableService;
}
}
[AttributeUsage(AttributeTargets.Property, Inherited = true)]

View File

@@ -17,6 +17,24 @@ namespace SqlSugar
{
public class UtilMethods
{
internal static void EndCustomSplitTable(ISqlSugarClient context,Type entityType)
{
var splitTableAttribute = entityType.GetCustomAttribute<SplitTableAttribute>();
if (splitTableAttribute.CustomSplitTableService != null)
{
context.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService = null;
}
}
internal static void StartCustomSplitTable(ISqlSugarClient context, Type entityType)
{
var splitTableAttribute = entityType.GetCustomAttribute<SplitTableAttribute>();
if (splitTableAttribute.CustomSplitTableService != null)
{
context.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService
= (ISplitTableService)Activator.CreateInstance(splitTableAttribute.CustomSplitTableService);
}
}
public static void ConvertParameter(SugarParameter p, ISqlBuilder builder)
{
if (!p.ParameterName.StartsWith(builder.SqlParameterKeyWord))