Split table

This commit is contained in:
sunkaixuna
2021-10-25 22:35:18 +08:00
parent 84dd2bd5f0
commit 3821aff9fc
6 changed files with 91 additions and 4 deletions

View File

@@ -24,9 +24,12 @@ namespace OrmTest
{
Console.WriteLine(s);
};
db.CodeFirst.SplitTables().InitTables<OrderSpliteTest>();
var list=db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Take(3)).ToList();
var list2 = db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Where(it=> it.Date>=DateTime.Now.AddYears(-2))).ToList();
var x = db.Deleteable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid()).SplitTable(tabs => tabs.Take(3)).ExecuteCommand();
var x2 = db.Updateable<OrderSpliteTest>()
@@ -34,6 +37,9 @@ namespace OrmTest
.Where(it => it.Pk == Guid.NewGuid())
.SplitTable(tabs => tabs.Take(3))
.ExecuteCommand();
var x3 = db.Insertable(new OrderSpliteTest() { Name="A" }).SplitTable(SplitType.Day).ExecuteCommand();
var x4 = db.Insertable(new OrderSpliteTest() { Name = "A" }).SplitTable(SplitType.Day,it=>it.Time).ExecuteCommand();
Console.WriteLine("#### CodeFirst end ####");
}
@@ -43,6 +49,8 @@ namespace OrmTest
[SugarColumn(IsPrimaryKey =true)]
public Guid Pk{ get; set; }
public string Name { get; set; }
[SugarColumn(IsNullable =true)]
public DateTime Time { get; set; }
}
}
}

View File

@@ -406,6 +406,20 @@ namespace SqlSugar
result.AddSubList(tree);
return result;
}
public SplitInsertable SplitTable(SplitType splitType)
{
throw new NotImplementedException();
}
public SplitInsertable SplitTable(SplitType splitType, Expression<Func<T, DateTime>> splitFieldName)
{
throw new NotImplementedException();
}
public SplitInsertable SplitTable(SplitType splitType, Expression<Func<T, DateTime?>> splitFieldName)
{
throw new NotImplementedException();
}
#endregion

View File

@@ -8,6 +8,9 @@ namespace SqlSugar
{
public class SplitInsertable
{
public object ExecuteCommand()
{
throw new NotImplementedException();
}
}
}

View File

@@ -46,7 +46,9 @@ namespace SqlSugar
MySqlBlukCopy<T> UseMySql();
OracleBlukCopy UseOracle();
//SplitInsertable SplitTable(SplitType splitType);
SplitInsertable SplitTable(SplitType splitType);
SplitInsertable SplitTable(SplitType splitType,Expression<Func<T,DateTime>> splitFieldName);
SplitInsertable SplitTable(SplitType splitType, Expression<Func<T, DateTime?>> splitFieldName);
void AddQueue();

View File

@@ -11,6 +11,39 @@ namespace SqlSugar
{
public SqlSugarProvider Context { get; set; }
public EntityInfo EntityInfo { get; set; }
public DateTime GetTableGetDate(DateTime time,SplitType type)
{
switch (type)
{
case SplitType.Day:
return Convert.ToDateTime(time.ToString("yyyy-MM-dd"));
case SplitType.Week:
return GetMondayDate(time);
case SplitType.Month:
return Convert.ToDateTime(time.ToString("yyyy-MM-01"));
case SplitType.Season:
if (time.Month <= 3)
{
return Convert.ToDateTime(time.ToString("yyyy-01-01"));
}
else if (time.Month <= 6)
{
return Convert.ToDateTime(time.ToString("yyyy-04-01"));
}
else if (time.Month <= 9)
{
return Convert.ToDateTime(time.ToString("yyyy-07-01"));
}
else
{
return Convert.ToDateTime(time.ToString("yyyy-10-01"));
}
case SplitType.Year:
return Convert.ToDateTime(time.ToString("yyyy-01-01"));
default:
throw new Exception($"SplitType paramter error ");
}
}
public List<SplitTableInfo> GetTables()
{
@@ -118,7 +151,32 @@ namespace SqlSugar
Check.Exception(Regex.Matches(dbTableName, @"\{month\}").Count > 1, ErrorMessage.GetThrowMessage("There can only be one {{month}}", "只能有一个 {{month}} "));
Check.Exception(Regex.Matches(dbTableName, @"\{day\}").Count > 1, ErrorMessage.GetThrowMessage("There can only be one {{day}}", "只能有一个{{day}}"));
Check.Exception(Regex.IsMatch(dbTableName, @"\d\{|\}\d"), ErrorMessage.GetThrowMessage(" '{{' or '}}' can't be numbers nearby", "占位符相令一位不能是数字,比如 : 1{{day}}2 错误 , 正确: 1_{{day}}_2"));
}
}
#endregion
#region
public static DateTime GetMondayDate()
{
return GetMondayDate(DateTime.Now);
}
public static DateTime GetSundayDate()
{
return GetSundayDate(DateTime.Now);
}
public static 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)
{
int i = someDate.DayOfWeek - DayOfWeek.Sunday;
if (i != 0) i = 7 - i;
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
return someDate.Add(ts);
}
#endregion
}
}

View File

@@ -10,6 +10,8 @@ namespace SqlSugar
{
Day=0,
Week=1,
Month=1
Month =2,
Season=3,
Year=4
}
}