mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-18 17:48:11 +08:00
Split table
This commit is contained in:
@@ -17,6 +17,7 @@ namespace SqlSugar
|
||||
Context = this.Context,
|
||||
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()
|
||||
};
|
||||
helper.CheckPrimaryKey();
|
||||
var tables = helper.GetTables();
|
||||
//var oldMapingTables = this.Context.MappingTables;
|
||||
if (tables.Count >0)
|
||||
|
@@ -406,19 +406,61 @@ namespace SqlSugar
|
||||
result.AddSubList(tree);
|
||||
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
|
||||
|
@@ -6,11 +6,37 @@ using System.Threading.Tasks;
|
||||
|
||||
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();
|
||||
OracleBlukCopy UseOracle();
|
||||
|
||||
SplitInsertable SplitTable(SplitType splitType);
|
||||
SplitInsertable SplitTable(SplitType splitType,Expression<Func<T,DateTime>> splitFieldName);
|
||||
SplitInsertable SplitTable(SplitType splitType, Expression<Func<T, DateTime?>> splitFieldName);
|
||||
SplitInsertable<T> SplitTable(SplitType splitType);
|
||||
SplitInsertable<T> SplitTable(SplitType splitType,Expression<Func<T,DateTime>> splitFieldName);
|
||||
SplitInsertable<T> SplitTable(SplitType splitType, Expression<Func<T, DateTime?>> splitFieldName);
|
||||
|
||||
void AddQueue();
|
||||
|
||||
|
@@ -71,7 +71,21 @@ namespace SqlSugar
|
||||
result = result.OrderByDescending(it => it.Date).ToList();
|
||||
this.Context.Ado.IsEnableLogEvent = oldIsEnableLogEvent;
|
||||
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
|
||||
|
||||
#region Common Helper
|
||||
@@ -127,13 +141,6 @@ namespace SqlSugar
|
||||
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)
|
||||
{
|
||||
if (str.Length < 2)
|
||||
@@ -159,22 +166,22 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Date Helper
|
||||
public static DateTime GetMondayDate()
|
||||
private DateTime GetMondayDate()
|
||||
{
|
||||
return GetMondayDate(DateTime.Now);
|
||||
}
|
||||
public static DateTime GetSundayDate()
|
||||
private DateTime GetSundayDate()
|
||||
{
|
||||
return GetSundayDate(DateTime.Now);
|
||||
}
|
||||
public static DateTime GetMondayDate(DateTime someDate)
|
||||
private DateTime GetMondayDate(DateTime someDate)
|
||||
{
|
||||
int i = someDate.DayOfWeek - DayOfWeek.Monday;
|
||||
if (i == -1) i = 6;
|
||||
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
|
||||
return someDate.Subtract(ts);
|
||||
}
|
||||
public static DateTime GetSundayDate(DateTime someDate)
|
||||
private DateTime GetSundayDate(DateTime someDate)
|
||||
{
|
||||
int i = someDate.DayOfWeek - DayOfWeek.Sunday;
|
||||
if (i != 0) i = 7 - i;
|
||||
|
Reference in New Issue
Block a user