mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Support split table
This commit is contained in:
parent
e67d1a88ce
commit
859e7d34d4
36
Src/Asp.Net/SqlServerTest/Demo/DemoN_SplitTable.cs
Normal file
36
Src/Asp.Net/SqlServerTest/Demo/DemoN_SplitTable.cs
Normal file
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@ namespace OrmTest
|
|||||||
Demo3_Insertable.Init();
|
Demo3_Insertable.Init();
|
||||||
Demo4_Deleteable.Init();
|
Demo4_Deleteable.Init();
|
||||||
Demo5_SqlQueryable.Init();
|
Demo5_SqlQueryable.Init();
|
||||||
|
DemoN_SplitTable.Init();
|
||||||
Demo6_Queue.Init();
|
Demo6_Queue.Init();
|
||||||
Demo7_Ado.Init();
|
Demo7_Ado.Init();
|
||||||
Demo8_Saveable.Init();
|
Demo8_Saveable.Init();
|
||||||
|
@ -82,6 +82,7 @@
|
|||||||
<Compile Include="Demo\DemoJ_Report.cs" />
|
<Compile Include="Demo\DemoJ_Report.cs" />
|
||||||
<Compile Include="Demo\DemoL_Snowflake.cs" />
|
<Compile Include="Demo\DemoL_Snowflake.cs" />
|
||||||
<Compile Include="Demo\DemoM_UnitOfWork.cs" />
|
<Compile Include="Demo\DemoM_UnitOfWork.cs" />
|
||||||
|
<Compile Include="Demo\DemoN_SplitTable.cs" />
|
||||||
<Compile Include="Models\DataDictionary.cs" />
|
<Compile Include="Models\DataDictionary.cs" />
|
||||||
<Compile Include="Models\Custom.cs" />
|
<Compile Include="Models\Custom.cs" />
|
||||||
<Compile Include="Models\EntityMapper.cs" />
|
<Compile Include="Models\EntityMapper.cs" />
|
||||||
|
@ -24,6 +24,13 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public methods
|
#region Public methods
|
||||||
|
public SplitCodeFirstProvider SplitTables()
|
||||||
|
{
|
||||||
|
var result = new SplitCodeFirstProvider();
|
||||||
|
result.Context = this.Context;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public virtual ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue)
|
public virtual ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue)
|
||||||
{
|
{
|
||||||
this.IsBackupTable = true;
|
this.IsBackupTable = true;
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -834,7 +834,22 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
return result;
|
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()
|
public ISugarQueryable<T> Distinct()
|
||||||
{
|
{
|
||||||
QueryBuilder.IsDistinct = true;
|
QueryBuilder.IsDistinct = true;
|
||||||
|
@ -17,5 +17,6 @@ namespace SqlSugar
|
|||||||
void InitTables<T, T2>();
|
void InitTables<T, T2>();
|
||||||
void InitTables<T, T2, T3>();
|
void InitTables<T, T2, T3>();
|
||||||
void InitTables<T, T2, T3, T4>();
|
void InitTables<T, T2, T3, T4>();
|
||||||
|
SplitCodeFirstProvider SplitTables();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
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);
|
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);
|
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>
|
public partial interface ISugarQueryable<T, T2> : ISugarQueryable<T>
|
||||||
{
|
{
|
||||||
|
@ -45,6 +45,9 @@ namespace SqlSugar
|
|||||||
SqlServerBlukCopy UseSqlServer();
|
SqlServerBlukCopy UseSqlServer();
|
||||||
MySqlBlukCopy<T> UseMySql();
|
MySqlBlukCopy<T> UseMySql();
|
||||||
OracleBlukCopy UseOracle();
|
OracleBlukCopy UseOracle();
|
||||||
|
|
||||||
|
//SplitInsertable SplitTable(SplitType splitType);
|
||||||
|
|
||||||
void AddQueue();
|
void AddQueue();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
28
Src/Asp.Net/SqlSugar/SpliteTable/SplitHelper.cs
Normal file
28
Src/Asp.Net/SqlSugar/SpliteTable/SplitHelper.cs
Normal file
@ -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
Normal file
15
Src/Asp.Net/SqlSugar/SpliteTable/SplitType.cs
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -73,6 +73,7 @@
|
|||||||
<Compile Include="Abstract\CacheProvider\CacheProvider.cs" />
|
<Compile Include="Abstract\CacheProvider\CacheProvider.cs" />
|
||||||
<Compile Include="Abstract\CodeFirstProvider\CodeFirstProvider.cs" />
|
<Compile Include="Abstract\CodeFirstProvider\CodeFirstProvider.cs" />
|
||||||
<Compile Include="Abstract\AdoProvider\AdoAccessory.cs" />
|
<Compile Include="Abstract\AdoProvider\AdoAccessory.cs" />
|
||||||
|
<Compile Include="Abstract\CodeFirstProvider\SplitCodeFirstProvider.cs" />
|
||||||
<Compile Include="Abstract\DbBindProvider\DbBindAccessory.cs" />
|
<Compile Include="Abstract\DbBindProvider\DbBindAccessory.cs" />
|
||||||
<Compile Include="Abstract\DbBindProvider\DbBindProvider.cs" />
|
<Compile Include="Abstract\DbBindProvider\DbBindProvider.cs" />
|
||||||
<Compile Include="Abstract\DbBindProvider\IDataReaderEntityBuilder.cs" />
|
<Compile Include="Abstract\DbBindProvider\IDataReaderEntityBuilder.cs" />
|
||||||
@ -88,6 +89,7 @@
|
|||||||
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
||||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||||
|
<Compile Include="Abstract\InsertableProvider\SplitInsertable.cs" />
|
||||||
<Compile Include="Entities\SqlSguarTransaction.cs" />
|
<Compile Include="Entities\SqlSguarTransaction.cs" />
|
||||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubAs.cs" />
|
<Compile Include="ExpressionsToSql\Subquery\Items\SubAs.cs" />
|
||||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubHaving.cs" />
|
<Compile Include="ExpressionsToSql\Subquery\Items\SubHaving.cs" />
|
||||||
@ -208,6 +210,8 @@
|
|||||||
<Compile Include="DistributedSystem\Snowflake\IdWorker.cs" />
|
<Compile Include="DistributedSystem\Snowflake\IdWorker.cs" />
|
||||||
<Compile Include="DistributedSystem\Snowflake\InvalidSystemClock.cs" />
|
<Compile Include="DistributedSystem\Snowflake\InvalidSystemClock.cs" />
|
||||||
<Compile Include="DistributedSystem\Snowflake\TimeExtensions.cs" />
|
<Compile Include="DistributedSystem\Snowflake\TimeExtensions.cs" />
|
||||||
|
<Compile Include="SpliteTable\SplitHelper.cs" />
|
||||||
|
<Compile Include="SpliteTable\SplitType.cs" />
|
||||||
<Compile Include="SqlSugarClient.cs" />
|
<Compile Include="SqlSugarClient.cs" />
|
||||||
<Compile Include="Utilities\CallContext.cs" />
|
<Compile Include="Utilities\CallContext.cs" />
|
||||||
<Compile Include="Utilities\CallContextAsync.cs" />
|
<Compile Include="Utilities\CallContextAsync.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user