Browse Source

Split table

pull/9/head
sunkaixuna 3 years ago
parent
commit
bfddebcdef
  1. 9
      Src/Asp.Net/SqlServerTest/Demo/DemoN_SplitTable.cs
  2. 4
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs
  3. 6
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  4. 2
      Src/Asp.Net/SqlSugar/Interface/IQueryable.cs
  5. 100
      Src/Asp.Net/SqlSugar/SpliteTable/SplitHelper.cs
  6. 20
      Src/Asp.Net/SqlSugar/SpliteTable/SplitTableInfo.cs
  7. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

9
Src/Asp.Net/SqlServerTest/Demo/DemoN_SplitTable.cs

@ -20,12 +20,17 @@ namespace OrmTest
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
db.Aop.OnLogExecuted = (s, p) =>
{
Console.WriteLine(s);
};
db.CodeFirst.SplitTables().InitTables<OrderSpliteTest>();
db.Queryable<OrderSpliteTest>().SplitTable(it => it.Take(3)).ToList();
var list=db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Take(3)).ToList();
Console.WriteLine("#### CodeFirst end ####");
}
[SqlSugar.SugarTable("Order{year}{month}{day}")]
[SqlSugar.SugarTable("Taxxx0101{year}{month}{day}")]
public class OrderSpliteTest
{
[SugarColumn(IsPrimaryKey =true)]

4
Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs

@ -11,7 +11,7 @@ namespace SqlSugar
public SqlSugarProvider Context;
public void InitTables<T>()
{
this.Context.InitMappingInfo<T>();
var oldMapping = this.Context.Utilities.TranslateCopy(this.Context.MappingTables);
SplitTableHelper helper = new SplitTableHelper()
{
Context = this.Context,
@ -23,7 +23,7 @@ namespace SqlSugar
{
foreach (var item in tables)
{
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, item);
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, item.TableName);
this.Context.CodeFirst.InitTables(typeof(T));
}
}

6
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -834,7 +834,7 @@ namespace SqlSugar
}
return result;
}
public ISugarQueryable<T> SplitTable(Func<List<string>, IEnumerable<string>> getTableNamesFunc)
public ISugarQueryable<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
{
SplitTableHelper helper = new SplitTableHelper()
{
@ -845,9 +845,9 @@ namespace SqlSugar
List<ISugarQueryable<object>> tableQueryables = new List<ISugarQueryable<object>>();
foreach (var item in tables)
{
tableQueryables.Add(this.Context.Queryable<object>().AS(item));
tableQueryables.Add(this.Context.Queryable<object>().AS(item.TableName));
}
var asName = this.Context.UnionAll(tableQueryables.ToArray()).Select<T>().ToSql().Key;
var asName = this.Context.UnionAll(tableQueryables.ToArray()).QueryBuilder.GetTableNameString;
return this.AS(asName);
}
public ISugarQueryable<T> Distinct()

2
Src/Asp.Net/SqlSugar/Interface/IQueryable.cs

@ -179,7 +179,7 @@ namespace SqlSugar
DataTable ToPivotTable<TColumn, TRow, TData>(Func<T, TColumn> columnSelector,Expression<Func<T, TRow>> rowSelector,Func<IEnumerable<T>, TData> dataSelector);
List<dynamic> ToPivotList<TColumn, TRow, TData>(Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector);
string ToPivotJson<TColumn, TRow, TData>(Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector);
ISugarQueryable<T> SplitTable(Func<List<string>,IEnumerable<string>> getTableNamesFunc);
ISugarQueryable<T> SplitTable(Func<List<SplitTableInfo>,IEnumerable<SplitTableInfo>> getTableNamesFunc);
}
public partial interface ISugarQueryable<T, T2> : ISugarQueryable<T>
{

100
Src/Asp.Net/SqlSugar/SpliteTable/SplitHelper.cs

@ -11,18 +11,110 @@ namespace SqlSugar
{
public SqlSugarProvider Context { get; set; }
public EntityInfo EntityInfo { get; set; }
public List<string> GetTables()
public List<SplitTableInfo> GetTables()
{
var tableInfos = this.Context.DbMaintenance.GetTableInfoList(false);
var regex = EntityInfo.DbTableName.Replace("{year}", "[0-9]{4}").Replace("{day}", "[0-9]{2}").Replace("{month}", "[0-9]{4}");
SplitTableHelper.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 currentTables = tableInfos.Where(it => Regex.IsMatch(it.Name, regex, RegexOptions.IgnoreCase)).Select(it => it.Name).Reverse().ToList();
return currentTables;
List<SplitTableInfo> result = new List<SplitTableInfo>();
foreach (var item in currentTables)
{
SplitTableInfo tableInfo = new SplitTableInfo();
tableInfo.TableName = item;
var math = Regex.Match(item, regex);
var group1 = math.Groups[1].Value;
var group2 = math.Groups[2].Value;
var group3 = math.Groups[3].Value;
tableInfo.Date = GetDate(group1,group2,group3, EntityInfo.DbTableName);
result.Add(tableInfo);
}
result = result.OrderByDescending(it => it.Date).ToList();
return result;
}
#region Helper
private DateTime GetDate(string group1, string group2, string group3, string dbTableName)
{
var yearIndex = dbTableName.IndexOf("{year}");
var dayIndex = dbTableName.IndexOf("{day}");
var monthIndex = dbTableName.IndexOf("{month}");
List<SplitTableSort> tables = new List<SplitTableSort>();
tables.Add(new SplitTableSort() { Name = "{year}", Sort = yearIndex });
tables.Add(new SplitTableSort() { Name = "{day}", Sort = dayIndex });
tables.Add(new SplitTableSort() { Name = "{month}", Sort = monthIndex });
tables = tables.OrderBy(it => it.Sort).ToList();
var year = "";
var month = "";
var day = "";
if (tables[0].Name == "{year}")
{
year = group1;
}
if (tables[1].Name == "{year}")
{
year = group2;
}
if (tables[2].Name == "{year}")
{
year = group3;
}
if (tables[0].Name == "{month}")
{
month = group1;
}
if (tables[1].Name == "{month}")
{
month = group2;
}
if (tables[2].Name == "{month}")
{
month = group3;
}
if (tables[0].Name == "{day}")
{
day = group1;
}
if (tables[1].Name == "{day}")
{
day = group2;
}
if (tables[2].Name == "{day}")
{
day = group3;
}
return Convert.ToDateTime($"{year}-{month}-{day}");
}
public string GetDefaultTableName()
{
throw new NotImplementedException();
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)
{
return str.PadLeft(2, '0');
}
else
{
return str;
}
}
private static void CheckTableName(string dbTableName)
{
Check.Exception(!dbTableName.Contains("{year}"), ErrorMessage.GetThrowMessage("table name need {year}", "分表表名需要占位符 {year}"));
Check.Exception(!dbTableName.Contains("{month}"), ErrorMessage.GetThrowMessage("table name need {month}", "分表表名需要占位符 {month} "));
Check.Exception(!dbTableName.Contains("{day}"), ErrorMessage.GetThrowMessage("table name need {day}", "分表表名需要占位符{day}"));
Check.Exception(Regex.Matches(dbTableName, @"\{year\}").Count > 1, ErrorMessage.GetThrowMessage(" There can only be one {year}", " 只能有一个 {year}"));
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}"));
}
#endregion
}
}

20
Src/Asp.Net/SqlSugar/SpliteTable/SplitTableInfo.cs

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SplitTableInfo
{
public string TableName { get; set; }
public DateTime Date { get; set; }
}
internal class SplitTableSort
{
public string Name { get; set; }
public int Sort { get; set; }
}
}

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -212,6 +212,7 @@
<Compile Include="DistributedSystem\Snowflake\InvalidSystemClock.cs" />
<Compile Include="DistributedSystem\Snowflake\TimeExtensions.cs" />
<Compile Include="SpliteTable\SplitHelper.cs" />
<Compile Include="SpliteTable\SplitTableInfo.cs" />
<Compile Include="SpliteTable\SplitType.cs" />
<Compile Include="SqlSugarClient.cs" />
<Compile Include="Utilities\CallContext.cs" />

Loading…
Cancel
Save