Split table

This commit is contained in:
sunkaixuna
2021-10-29 12:32:47 +08:00
parent 5d6f2dda5a
commit dafbfd1d95
6 changed files with 29 additions and 24 deletions

View File

@@ -417,6 +417,8 @@ namespace SqlSugar
SplitInsertable<T> result = new SplitInsertable<T>(); SplitInsertable<T> result = new SplitInsertable<T>();
result.Context = this.Context; result.Context = this.Context;
result.EntityInfo = this.EntityInfo; result.EntityInfo = this.EntityInfo;
result.Helper = helper;
result.SplitType = splitType;
result.TableNames = new List<string>(); result.TableNames = new List<string>();
foreach (var item in this.InsertObjs) foreach (var item in this.InsertObjs)
{ {

View File

@@ -9,7 +9,9 @@ namespace SqlSugar
public class SplitInsertable<T> public class SplitInsertable<T>
{ {
public SqlSugarProvider Context; public SqlSugarProvider Context;
internal SplitTableContext Helper;
public EntityInfo EntityInfo; public EntityInfo EntityInfo;
public SplitType SplitType;
internal IInsertable<T> Inserable { get; set; } internal IInsertable<T> Inserable { get; set; }
internal List<string> TableNames { get; set; } internal List<string> TableNames { get; set; }

View File

@@ -836,7 +836,7 @@ namespace SqlSugar
} }
public ISugarQueryable<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc) public ISugarQueryable<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
{ {
SplitTableHelper helper = new SplitTableHelper() SplitTableContext helper = new SplitTableContext()
{ {
Context=Context, Context=Context,
EntityInfo=this.EntityInfo EntityInfo=this.EntityInfo

View File

@@ -10,7 +10,8 @@ namespace SqlSugar
{ {
public class DateSplitTableService : ISplitTableService public class DateSplitTableService : ISplitTableService
{ {
public List<SplitTableInfo> GetAllTables(ISqlSugarClient db, EntityInfo EntityInfo,List<DbTableInfo> tableInfos) #region Core
public List<SplitTableInfo> GetAllTables(ISqlSugarClient db, EntityInfo EntityInfo, List<DbTableInfo> tableInfos)
{ {
CheckTableName(EntityInfo.DbTableName); CheckTableName(EntityInfo.DbTableName);
var regex = EntityInfo.DbTableName.Replace("{year}", "([0-9]{2,4})").Replace("{day}", "([0-9]{1,2})").Replace("{month}", "([0-9]{1,2})"); var regex = EntityInfo.DbTableName.Replace("{year}", "([0-9]{2,4})").Replace("{day}", "([0-9]{1,2})").Replace("{month}", "([0-9]{1,2})");
@@ -34,30 +35,28 @@ namespace SqlSugar
} }
public string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo) public string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo)
{ {
return GetTableName(db,EntityInfo,SplitType.Day); return GetTableName(db, EntityInfo, SplitType.Day);
} }
public string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo, SplitType splitType)
public string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo,SplitType splitType)
{ {
var date =db.GetDate(); var date = db.GetDate();
return GetTableNameByDate(EntityInfo,splitType,date); return GetTableNameByDate(EntityInfo, splitType, date);
} }
public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object fieldValue) public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object fieldValue)
{ {
var value= Convert.ToDateTime(fieldValue); var value = Convert.ToDateTime(fieldValue);
return GetTableNameByDate(entityInfo,splitType, value); return GetTableNameByDate(entityInfo, splitType, value);
} }
public object GetFieldValue(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object entityValue) public object GetFieldValue(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object entityValue)
{ {
var splitColumn=entityInfo.Columns.FirstOrDefault(it => it.PropertyInfo.PropertyType.GetCustomAttribute<SplitFieldAttribute>()!=null); var splitColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyInfo.PropertyType.GetCustomAttribute<SplitFieldAttribute>() != null);
if (splitColumn == null) if (splitColumn == null)
{ {
return db.GetDate(); return db.GetDate();
} }
else else
{ {
var value= splitColumn.PropertyInfo.GetValue(entityValue, null); var value = splitColumn.PropertyInfo.GetValue(entityValue, null);
if (value == null) if (value == null)
{ {
return db.GetDate(); return db.GetDate();
@@ -76,7 +75,6 @@ namespace SqlSugar
} }
} }
} }
public void VerifySplitType(SplitType splitType) public void VerifySplitType(SplitType splitType)
{ {
switch (splitType) switch (splitType)
@@ -92,10 +90,12 @@ namespace SqlSugar
case SplitType.Year: case SplitType.Year:
break; break;
default: default:
throw new Exception("DateSplitTableService no support "+ splitType.ToString()); throw new Exception("DateSplitTableService no support " + splitType.ToString());
} }
} }
#endregion
#region Common Helper #region Common Helper
private string GetTableNameByDate(EntityInfo EntityInfo,SplitType splitType,DateTime date) private string GetTableNameByDate(EntityInfo EntityInfo,SplitType splitType,DateTime date)
{ {
@@ -237,5 +237,14 @@ namespace SqlSugar
} }
#endregion #endregion
#region Private Models
internal class SplitTableSort
{
public string Name { get; set; }
public int Sort { get; set; }
}
#endregion
} }
} }

View File

@@ -53,7 +53,5 @@ namespace SqlSugar
{ {
Check.Exception(EntityInfo.Columns.Any(it => it.IsIdentity == true), ErrorMessage.GetThrowMessage("Split table can't IsIdentity=true", "分表禁止使用自增列")); Check.Exception(EntityInfo.Columns.Any(it => it.IsIdentity == true), ErrorMessage.GetThrowMessage("Split table can't IsIdentity=true", "分表禁止使用自增列"));
} }
} }
} }

View File

@@ -16,10 +16,4 @@ namespace SqlSugar
public int Int { get; set; } public int Int { get; set; }
public Byte[] ByteArray { get; set; } public Byte[] ByteArray { get; set; }
} }
internal class SplitTableSort
{
public string Name { get; set; }
public int Sort { get; set; }
}
} }