mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-08 02:14:53 +08:00
Split table
This commit is contained in:
@@ -17,6 +17,7 @@ namespace SqlSugar
|
|||||||
Context = this.Context,
|
Context = this.Context,
|
||||||
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()
|
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()
|
||||||
};
|
};
|
||||||
|
helper.CheckPrimaryKey();
|
||||||
var tables = helper.GetTables();
|
var tables = helper.GetTables();
|
||||||
//var oldMapingTables = this.Context.MappingTables;
|
//var oldMapingTables = this.Context.MappingTables;
|
||||||
if (tables.Count >0)
|
if (tables.Count >0)
|
||||||
|
|||||||
@@ -406,19 +406,61 @@ namespace SqlSugar
|
|||||||
result.AddSubList(tree);
|
result.AddSubList(tree);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public SplitInsertable SplitTable(SplitType splitType)
|
public SplitInsertable<T> SplitTable(SplitType splitType)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
SplitTableHelper helper = new SplitTableHelper()
|
||||||
|
{
|
||||||
|
Context = this.Context,
|
||||||
|
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()
|
||||||
|
};
|
||||||
|
helper.CheckPrimaryKey();
|
||||||
|
var table=helper.GetTableNameByDate(helper.GetTableGetDate(this.Context.GetDate(),splitType));
|
||||||
|
SplitInsertable<T> result = new SplitInsertable<T>();
|
||||||
|
result.Context = this.Context;
|
||||||
|
result.EntityInfo = this.EntityInfo;
|
||||||
|
result.TableNames = new List<string>() { table};
|
||||||
|
result.Inserable = this;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SplitInsertable SplitTable(SplitType splitType, Expression<Func<T, DateTime>> splitFieldName)
|
public SplitInsertable<T> SplitTable(SplitType splitType, Expression<Func<T, DateTime>> splitFieldName)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
SplitTableHelper helper = new SplitTableHelper()
|
||||||
|
{
|
||||||
|
Context = this.Context,
|
||||||
|
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()
|
||||||
|
};
|
||||||
|
helper.CheckPrimaryKey();
|
||||||
|
SplitInsertable<T> result = new SplitInsertable<T>();
|
||||||
|
result.Context = this.Context;
|
||||||
|
result.EntityInfo = this.EntityInfo;
|
||||||
|
result.TableNames = new List<string>();
|
||||||
|
List<DateTime> times = new List<DateTime>();
|
||||||
|
foreach (var item in times)
|
||||||
|
{
|
||||||
|
result.TableNames.Add(helper.GetTableNameByDate(helper.GetTableGetDate(item, splitType)));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SplitInsertable SplitTable(SplitType splitType, Expression<Func<T, DateTime?>> splitFieldName)
|
public SplitInsertable<T> SplitTable(SplitType splitType, Expression<Func<T, DateTime?>> splitFieldName)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
SplitTableHelper helper = new SplitTableHelper()
|
||||||
|
{
|
||||||
|
Context=this.Context,
|
||||||
|
EntityInfo=this.Context.EntityMaintenance.GetEntityInfo<T>()
|
||||||
|
};
|
||||||
|
helper.CheckPrimaryKey();
|
||||||
|
SplitInsertable<T> result = new SplitInsertable<T>();
|
||||||
|
result.Context = this.Context;
|
||||||
|
result.EntityInfo = this.EntityInfo;
|
||||||
|
result.TableNames = new List<string>();
|
||||||
|
List<DateTime> times = new List<DateTime>();
|
||||||
|
foreach (var item in times)
|
||||||
|
{
|
||||||
|
result.TableNames.Add(helper.GetTableNameByDate(helper.GetTableGetDate(item, splitType)));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -6,11 +6,37 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public class SplitInsertable
|
public class SplitInsertable<T>
|
||||||
{
|
{
|
||||||
public object ExecuteCommand()
|
public SqlSugarProvider Context;
|
||||||
|
public EntityInfo EntityInfo;
|
||||||
|
internal IInsertable<T> Inserable { get; set; }
|
||||||
|
internal List<string> TableNames { get; set; }
|
||||||
|
|
||||||
|
public int ExecuteCommand()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
CreateTable();
|
||||||
|
if (TableNames.Count == 1)
|
||||||
|
{
|
||||||
|
return Inserable.AS(TableNames.First()).ExecuteCommand();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CreateTable()
|
||||||
|
{
|
||||||
|
foreach (var item in TableNames)
|
||||||
|
{
|
||||||
|
if (!this.Context.DbMaintenance.IsAnyTable(item, false))
|
||||||
|
{
|
||||||
|
this.Context.MappingTables.Add(EntityInfo.EntityName, item);
|
||||||
|
this.Context.CodeFirst.InitTables<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.Context.MappingTables.Add(EntityInfo.EntityName, EntityInfo.DbTableName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,9 +46,9 @@ namespace SqlSugar
|
|||||||
MySqlBlukCopy<T> UseMySql();
|
MySqlBlukCopy<T> UseMySql();
|
||||||
OracleBlukCopy UseOracle();
|
OracleBlukCopy UseOracle();
|
||||||
|
|
||||||
SplitInsertable SplitTable(SplitType splitType);
|
SplitInsertable<T> SplitTable(SplitType splitType);
|
||||||
SplitInsertable SplitTable(SplitType splitType,Expression<Func<T,DateTime>> splitFieldName);
|
SplitInsertable<T> SplitTable(SplitType splitType,Expression<Func<T,DateTime>> splitFieldName);
|
||||||
SplitInsertable SplitTable(SplitType splitType, Expression<Func<T, DateTime?>> splitFieldName);
|
SplitInsertable<T> SplitTable(SplitType splitType, Expression<Func<T, DateTime?>> splitFieldName);
|
||||||
|
|
||||||
void AddQueue();
|
void AddQueue();
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,21 @@ namespace SqlSugar
|
|||||||
result = result.OrderByDescending(it => it.Date).ToList();
|
result = result.OrderByDescending(it => it.Date).ToList();
|
||||||
this.Context.Ado.IsEnableLogEvent = oldIsEnableLogEvent;
|
this.Context.Ado.IsEnableLogEvent = oldIsEnableLogEvent;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GetDefaultTableName()
|
||||||
|
{
|
||||||
|
var date = this.Context.GetDate();
|
||||||
|
return GetTableNameByDate(date);
|
||||||
|
}
|
||||||
|
public string GetTableNameByDate(DateTime date)
|
||||||
|
{
|
||||||
|
return EntityInfo.DbTableName.Replace("{year}", date.Year + "").Replace("{day}", PadLeft2(date.Day + "")).Replace("{month}", PadLeft2(date.Month + ""));
|
||||||
|
}
|
||||||
|
public void CheckPrimaryKey()
|
||||||
|
{
|
||||||
|
Check.Exception(EntityInfo.Columns.Any(it => it.IsIdentity == true), ErrorMessage.GetThrowMessage("Split table can't IsIdentity=true", "分表禁止使用自增列"));
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Common Helper
|
#region Common Helper
|
||||||
@@ -127,13 +141,6 @@ namespace SqlSugar
|
|||||||
return Convert.ToDateTime($"{year}-{month}-{day}");
|
return Convert.ToDateTime($"{year}-{month}-{day}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetDefaultTableName()
|
|
||||||
{
|
|
||||||
var date = this.Context.GetDate();
|
|
||||||
var result = EntityInfo.DbTableName.Replace("{year}", date.Year + "").Replace("{day}", PadLeft2(date.Day + "")).Replace("{month}", PadLeft2(date.Month + ""));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string PadLeft2(string str)
|
private string PadLeft2(string str)
|
||||||
{
|
{
|
||||||
if (str.Length < 2)
|
if (str.Length < 2)
|
||||||
@@ -159,22 +166,22 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Date Helper
|
#region Date Helper
|
||||||
public static DateTime GetMondayDate()
|
private DateTime GetMondayDate()
|
||||||
{
|
{
|
||||||
return GetMondayDate(DateTime.Now);
|
return GetMondayDate(DateTime.Now);
|
||||||
}
|
}
|
||||||
public static DateTime GetSundayDate()
|
private DateTime GetSundayDate()
|
||||||
{
|
{
|
||||||
return GetSundayDate(DateTime.Now);
|
return GetSundayDate(DateTime.Now);
|
||||||
}
|
}
|
||||||
public static DateTime GetMondayDate(DateTime someDate)
|
private DateTime GetMondayDate(DateTime someDate)
|
||||||
{
|
{
|
||||||
int i = someDate.DayOfWeek - DayOfWeek.Monday;
|
int i = someDate.DayOfWeek - DayOfWeek.Monday;
|
||||||
if (i == -1) i = 6;
|
if (i == -1) i = 6;
|
||||||
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
|
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
|
||||||
return someDate.Subtract(ts);
|
return someDate.Subtract(ts);
|
||||||
}
|
}
|
||||||
public static DateTime GetSundayDate(DateTime someDate)
|
private DateTime GetSundayDate(DateTime someDate)
|
||||||
{
|
{
|
||||||
int i = someDate.DayOfWeek - DayOfWeek.Sunday;
|
int i = someDate.DayOfWeek - DayOfWeek.Sunday;
|
||||||
if (i != 0) i = 7 - i;
|
if (i != 0) i = 7 - i;
|
||||||
|
|||||||
Reference in New Issue
Block a user