Browse Source

Split table

pull/9/head
sunkaixuna 3 years ago
parent
commit
501ac235f7
  1. 11
      Src/Asp.Net/SqlServerTest/Demo/DemoN_SplitTable.cs
  2. 10
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  3. 6
      Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs

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

@ -25,11 +25,18 @@ namespace OrmTest
Console.WriteLine(s);
};
//初始化分表
db.CodeFirst.SplitTables().InitTables<OrderSpliteTest>();
var list=db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Take(3)).ToList();
//根据最近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);
//根据时间选出的表进行查询
var list2 = db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Where(it=> it.Date>=DateTime.Now.AddYears(-2))).ToList();
//删除数据只在最近3张表执行操作
var x = db.Deleteable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid()).SplitTable(tabs => tabs.Take(3)).ExecuteCommand();
var x2 = db.Updateable<OrderSpliteTest>()
@ -38,7 +45,9 @@ namespace OrmTest
.SplitTable(tabs => tabs.Take(3))
.ExecuteCommand();
//按日分表
var x3 = db.Insertable(new OrderSpliteTest() { Name="A" }).SplitTable(SplitType.Day).ExecuteCommand();
//按日分表,根据time字段扔到对应的表中
var x4 = db.Insertable(new OrderSpliteTest() { Name = "A" }).SplitTable(SplitType.Day,it=>it.Time).ExecuteCommand();
Console.WriteLine("#### CodeFirst end ####");
}

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

@ -843,13 +843,15 @@ namespace SqlSugar
};
this.Context.MappingTables.Add(this.EntityInfo.EntityName, this.EntityInfo.DbTableName);
var tables = getTableNamesFunc(helper.GetTables());
List<ISugarQueryable<object>> tableQueryables = new List<ISugarQueryable<object>>();
List<ISugarQueryable<T>> tableQueryables = new List<ISugarQueryable<T>>();
foreach (var item in tables)
{
tableQueryables.Add(this.Context.Queryable<object>().AS(item.TableName));
tableQueryables.Add(this.Clone().AS(item.TableName));
}
var asName = this.Context.UnionAll(tableQueryables.ToArray()).QueryBuilder.GetTableNameString;
return this.AS(asName);
var unionall = this.Context._UnionAll(tableQueryables.ToArray());
//var values= unionall.QueryBuilder.GetSelectValue;
//unionall.QueryBuilder.SelectValue = values;
return unionall;
}
public ISugarQueryable<T> Distinct()
{

6
Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs

@ -504,6 +504,11 @@ namespace SqlSugar
#endregion
public virtual ISugarQueryable<T> UnionAll<T>(params ISugarQueryable<T>[] queryables) where T : class, new()
{
return _UnionAll(queryables);
}
internal ISugarQueryable<T> _UnionAll<T>(ISugarQueryable<T>[] queryables)
{
var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null ");
@ -533,6 +538,7 @@ namespace SqlSugar
return resulut.Select<T>(sqlBuilder.SqlSelectAll);
}
}
public virtual ISugarQueryable<T> UnionAll<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
{
Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null ");

Loading…
Cancel
Save