Browse Source

Support split table

pull/9/head
sunkaixuna 3 years ago
parent
commit
859e7d34d4
  1. 36
      Src/Asp.Net/SqlServerTest/Demo/DemoN_SplitTable.cs
  2. 1
      Src/Asp.Net/SqlServerTest/Program.cs
  3. 1
      Src/Asp.Net/SqlServerTest/SqlServerTest.csproj
  4. 7
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
  5. 38
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs
  6. 13
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SplitInsertable.cs
  7. 17
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  8. 1
      Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs
  9. 1
      Src/Asp.Net/SqlSugar/Interface/IQueryable.cs
  10. 3
      Src/Asp.Net/SqlSugar/Interface/Insertable.cs
  11. 28
      Src/Asp.Net/SqlSugar/SpliteTable/SplitHelper.cs
  12. 15
      Src/Asp.Net/SqlSugar/SpliteTable/SplitType.cs
  13. 4
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

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

@ -0,0 +1,36 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public class DemoN_SplitTable
{
public static void Init()
{
Console.WriteLine("");
Console.WriteLine("#### CodeFirst Start ####");
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = Config.ConnectionString,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
db.CodeFirst.SplitTables().InitTables<OrderSpliteTest>();
db.Queryable<OrderSpliteTest>().SplitTable(it => it.Take(3)).ToList();
Console.WriteLine("#### CodeFirst end ####");
}
[SqlSugar.SugarTable("Order{year}{month}{day}")]
public class OrderSpliteTest
{
[SugarColumn(IsPrimaryKey =true)]
public Guid Pk{ get; set; }
public string Name { get; set; }
}
}
}

1
Src/Asp.Net/SqlServerTest/Program.cs

@ -19,6 +19,7 @@ namespace OrmTest
Demo3_Insertable.Init();
Demo4_Deleteable.Init();
Demo5_SqlQueryable.Init();
DemoN_SplitTable.Init();
Demo6_Queue.Init();
Demo7_Ado.Init();
Demo8_Saveable.Init();

1
Src/Asp.Net/SqlServerTest/SqlServerTest.csproj

@ -82,6 +82,7 @@
<Compile Include="Demo\DemoJ_Report.cs" />
<Compile Include="Demo\DemoL_Snowflake.cs" />
<Compile Include="Demo\DemoM_UnitOfWork.cs" />
<Compile Include="Demo\DemoN_SplitTable.cs" />
<Compile Include="Models\DataDictionary.cs" />
<Compile Include="Models\Custom.cs" />
<Compile Include="Models\EntityMapper.cs" />

7
Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs

@ -24,6 +24,13 @@ namespace SqlSugar
#endregion
#region Public methods
public SplitCodeFirstProvider SplitTables()
{
var result = new SplitCodeFirstProvider();
result.Context = this.Context;
return result;
}
public virtual ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue)
{
this.IsBackupTable = true;

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

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SplitCodeFirstProvider
{
public SqlSugarProvider Context;
public void InitTables<T>()
{
this.Context.InitMappingInfo<T>();
SplitTableHelper helper = new SplitTableHelper()
{
Context = this.Context,
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()
};
var tables = helper.GetTables();
var oldMapingTables = this.Context.MappingTables;
if (tables.Count >0)
{
foreach (var item in tables)
{
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, item);
this.Context.CodeFirst.InitTables(typeof(T));
}
}
else
{
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, helper.GetDefaultTableName());
this.Context.CodeFirst.InitTables(typeof(T));
}
this.Context.MappingTables = oldMapingTables;
}
}
}

13
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SplitInsertable.cs

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SplitInsertable
{
}
}

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

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

1
Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs

@ -17,5 +17,6 @@ namespace SqlSugar
void InitTables<T, T2>();
void InitTables<T, T2, T3>();
void InitTables<T, T2, T3, T4>();
SplitCodeFirstProvider SplitTables();
}
}

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

@ -179,6 +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);
}
public partial interface ISugarQueryable<T, T2> : ISugarQueryable<T>
{

3
Src/Asp.Net/SqlSugar/Interface/Insertable.cs

@ -45,6 +45,9 @@ namespace SqlSugar
SqlServerBlukCopy UseSqlServer();
MySqlBlukCopy<T> UseMySql();
OracleBlukCopy UseOracle();
//SplitInsertable SplitTable(SplitType splitType);
void AddQueue();
}

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

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SqlSugar
{
internal class SplitTableHelper
{
public SqlSugarProvider Context { get; set; }
public EntityInfo EntityInfo { get; set; }
public List<string> 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}");
var currentTables = tableInfos.Where(it => Regex.IsMatch(it.Name, regex, RegexOptions.IgnoreCase)).Select(it => it.Name).Reverse().ToList();
return currentTables;
}
public string GetDefaultTableName()
{
throw new NotImplementedException();
}
}
}

15
Src/Asp.Net/SqlSugar/SpliteTable/SplitType.cs

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public enum SplitType
{
Day=0,
Week=1,
Month=1
}
}

4
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -73,6 +73,7 @@
<Compile Include="Abstract\CacheProvider\CacheProvider.cs" />
<Compile Include="Abstract\CodeFirstProvider\CodeFirstProvider.cs" />
<Compile Include="Abstract\AdoProvider\AdoAccessory.cs" />
<Compile Include="Abstract\CodeFirstProvider\SplitCodeFirstProvider.cs" />
<Compile Include="Abstract\DbBindProvider\DbBindAccessory.cs" />
<Compile Include="Abstract\DbBindProvider\DbBindProvider.cs" />
<Compile Include="Abstract\DbBindProvider\IDataReaderEntityBuilder.cs" />
@ -88,6 +89,7 @@
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
<Compile Include="Abstract\InsertableProvider\SplitInsertable.cs" />
<Compile Include="Entities\SqlSguarTransaction.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubAs.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubHaving.cs" />
@ -208,6 +210,8 @@
<Compile Include="DistributedSystem\Snowflake\IdWorker.cs" />
<Compile Include="DistributedSystem\Snowflake\InvalidSystemClock.cs" />
<Compile Include="DistributedSystem\Snowflake\TimeExtensions.cs" />
<Compile Include="SpliteTable\SplitHelper.cs" />
<Compile Include="SpliteTable\SplitType.cs" />
<Compile Include="SqlSugarClient.cs" />
<Compile Include="Utilities\CallContext.cs" />
<Compile Include="Utilities\CallContextAsync.cs" />

Loading…
Cancel
Save