mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-21 02:58:05 +08:00
Split table
This commit is contained in:
@@ -28,25 +28,38 @@ namespace OrmTest
|
||||
//初始化分表
|
||||
db.CodeFirst.SplitTables().InitTables<OrderSpliteTest>();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//根据最近3个表进行查询
|
||||
var list=db.Queryable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid())
|
||||
.SplitTable(tabs => tabs.Take(3))
|
||||
.Where(it=>it.Time==DateTime.Now).ToOffsetPage(1,2);
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//根据时间选出的表进行查询
|
||||
var list2 = db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Where(it=> it.Date>=DateTime.Now.AddYears(-2))).ToList();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//删除数据只在最近3张表执行操作
|
||||
var x = db.Deleteable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid()).SplitTable(tabs => tabs.Take(3)).ExecuteCommand();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
var tableName = db.SplitHelper<OrderSpliteTest>().GetTableName(DateTime.Now.AddDays(-1));
|
||||
var x2 = db.Updateable<OrderSpliteTest>()
|
||||
.SetColumns(it=>it.Name=="a")
|
||||
.Where(it => it.Pk == Guid.NewGuid())
|
||||
.SplitTable(tabs => tabs.Take(3))
|
||||
.SplitTable(tabs => tabs.InTableNames(tableName))
|
||||
.ExecuteCommand();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//按日分表
|
||||
var x3 = db.Insertable(new OrderSpliteTest() { Name="A" }).SplitTable().ExecuteCommand();
|
||||
|
||||
Console.WriteLine();
|
||||
////强制分表类型
|
||||
var x4 = db.Insertable(new OrderSpliteTest() { Name = "A" ,Time=DateTime.Now.AddDays(-1) }).SplitTable().ExecuteCommand();
|
||||
Console.WriteLine("#### CodeFirst end ####");
|
||||
|
@@ -43,6 +43,7 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Other methods
|
||||
SplitTableContext SplitHelper<T>();
|
||||
DateTime GetDate();
|
||||
//SimpleClient GetSimpleClient();
|
||||
SimpleClient<T> GetSimpleClient<T>() where T : class, new();
|
||||
|
@@ -1,19 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class SplitTableContext
|
||||
public class SplitTableContext
|
||||
{
|
||||
public SqlSugarProvider Context { get; set; }
|
||||
public EntityInfo EntityInfo { get; set; }
|
||||
public ISplitTableService Service { get; set; }
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
internal EntityInfo EntityInfo { get; set; }
|
||||
internal ISplitTableService Service { get; set; }
|
||||
private SplitTableContext() { }
|
||||
public SplitTableContext(SqlSugarProvider context)
|
||||
internal SplitTableContext(SqlSugarProvider context)
|
||||
{
|
||||
this.Context = context;
|
||||
if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null&&this.Context.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService!=null)
|
||||
@@ -35,23 +36,29 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
|
||||
internal string GetDefaultTableName()
|
||||
public string GetDefaultTableName()
|
||||
{
|
||||
return Service.GetTableName(this.Context,EntityInfo);
|
||||
}
|
||||
internal string GetTableName(SplitType splitType)
|
||||
public string GetTableName(SplitType splitType)
|
||||
{
|
||||
return Service.GetTableName(this.Context,EntityInfo, splitType);
|
||||
}
|
||||
internal string GetTableName(SplitType splitType, object fieldValue)
|
||||
public string GetTableName(SplitType splitType, object fieldValue)
|
||||
{
|
||||
return Service.GetTableName(this.Context,EntityInfo, splitType, fieldValue);
|
||||
}
|
||||
internal object GetValue(SplitType splitType, object entityValue)
|
||||
public string GetTableName(object fieldValue)
|
||||
{
|
||||
var attribute = EntityInfo.Type.GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute;
|
||||
Check.Exception(attribute == null, $" {EntityInfo.EntityName} need SplitTableAttribute");
|
||||
return Service.GetTableName(this.Context, EntityInfo, attribute.SplitType, fieldValue);
|
||||
}
|
||||
public object GetValue(SplitType splitType, object entityValue)
|
||||
{
|
||||
return Service.GetFieldValue(this.Context,EntityInfo, splitType, entityValue);
|
||||
}
|
||||
public void CheckPrimaryKey()
|
||||
internal void CheckPrimaryKey()
|
||||
{
|
||||
Check.Exception(EntityInfo.Columns.Any(it => it.IsIdentity == true), ErrorMessage.GetThrowMessage("Split table can't IsIdentity=true", "分表禁止使用自增列"));
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
{
|
||||
public class SplitTableInfo
|
||||
{
|
||||
public string TableName { get; set; }
|
||||
|
@@ -6,10 +6,11 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public static class SplitTypeExtensions
|
||||
public static class SplitTableInfoExtensions
|
||||
{
|
||||
|
||||
|
||||
|
||||
public static IEnumerable<SplitTableInfo> InTableNames(this List<SplitTableInfo> tables, params string[] tableNames)
|
||||
{
|
||||
return tables.Where(it => tableNames.Any(y => y.Equals(it.TableName, StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -846,6 +846,14 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
public SplitTableContext SplitHelper<T>()
|
||||
{
|
||||
var result = new SplitTableContext(this.Context)
|
||||
{
|
||||
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>()
|
||||
};
|
||||
return result;
|
||||
}
|
||||
private SqlSugarProvider GetContext()
|
||||
{
|
||||
SqlSugarProvider result = null;
|
||||
|
@@ -596,6 +596,10 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.Updateable(UpdateObjs);
|
||||
}
|
||||
public SplitTableContext SplitHelper<T>()
|
||||
{
|
||||
return ScopedContext.SplitHelper<T>();
|
||||
}
|
||||
public SqlSguarTransaction UseTran()
|
||||
{
|
||||
return ScopedContext.UseTran();
|
||||
|
Reference in New Issue
Block a user