diff --git a/Src/Asp.Net/DmTest/Demo/DemoN_SplitTable.cs b/Src/Asp.Net/DmTest/Demo/DemoN_SplitTable.cs new file mode 100644 index 000000000..80415eac4 --- /dev/null +++ b/Src/Asp.Net/DmTest/Demo/DemoN_SplitTable.cs @@ -0,0 +1,101 @@ +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.Dm, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true + }); + db.Aop.OnLogExecuted = (s, p) => + { + Console.WriteLine(s); + Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))); + }; + + //初始化分表 + db.CodeFirst.SplitTables().InitTables(); + + Console.WriteLine(); + + ////根据最近3个表进行查询 + var list = db.Queryable() + .SplitTable(DateTime.Now.Date.AddYears(-1), DateTime.Now) + .ToList(); + + Console.WriteLine(); + + //根据时间选出的表进行查询 + var list2 = db.Queryable().SplitTable(tabs => tabs.Where(it=> it.Date>=DateTime.Now.AddYears(-2))).ToList(); + + Console.WriteLine(); + + //删除数据只在最近3张表执行操作 + var x = db.Deleteable().Where(it=>it.Pk==Guid.NewGuid()).SplitTable(tabs => tabs.Take(3)).ExecuteCommand(); + + Console.WriteLine(); + + var tableName = db.SplitHelper().GetTableName(DateTime.Now.AddDays(0)); + + var listNull= db.Queryable().SplitTable(ta => ta.InTableNames(tableName)).ToList(); + + var tableName2 = db.SplitHelper(new OrderSpliteTest() { Time=DateTime.Now}).GetTableNames(); + var tableName3 = db.SplitHelper(new List { + new OrderSpliteTest() { Time = DateTime.Now }, + new OrderSpliteTest() { Time = DateTime.Now }, + new OrderSpliteTest() { Time = DateTime.Now.AddMonths(-10) } + }).GetTableNames(); + var x2 = db.Updateable() + .SetColumns(it=>it.Name=="a") + .Where(it => it.Pk == Guid.NewGuid()) + .SplitTable(tabs => tabs.InTableNames(tableName2)) + .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(); + + //分表支持BulkCopy + //db.Fastest().SplitTable().BulkCopy(new List { + // new OrderSpliteTest() { Pk=Guid.NewGuid(),Name ="a", Time = DateTime.Now }, + // new OrderSpliteTest() {Pk=Guid.NewGuid(),Name ="a", Time = DateTime.Now }, + // new OrderSpliteTest() {Pk=Guid.NewGuid(),Name ="a", Time = DateTime.Now.AddMonths(-10) } + //}); + + //db.Fastest().SplitTable().BulkUpdate(db.Queryable().SplitTable(it=>it).ToList()); + //db.Fastest().SplitTable().BulkUpdate(db.Queryable().SplitTable(it => it).ToList(),new string[] { "pk"},new string[] { "name"}); + Console.WriteLine("#### CodeFirst end ####"); + } + + [SplitTable(SplitType.Day)] + [SqlSugar.SugarTable("Taxxx0101_{year}{month}{day}")] + public class OrderSpliteTest + { + [SugarColumn(IsPrimaryKey =true)] + public Guid Pk{ get; set; } + [SugarColumn(Length =200)] + public string Name { get; set; } + [SugarColumn(IsNullable =true)] + [SplitField] + public DateTime Time { get; set; } + } + } +} diff --git a/Src/Asp.Net/DmTest/DmTest.csproj b/Src/Asp.Net/DmTest/DmTest.csproj index 5f5e9aa1d..415f4961f 100644 --- a/Src/Asp.Net/DmTest/DmTest.csproj +++ b/Src/Asp.Net/DmTest/DmTest.csproj @@ -63,6 +63,7 @@ + diff --git a/Src/Asp.Net/OracleTest/Program.cs b/Src/Asp.Net/OracleTest/Program.cs index 2ab9e9e79..6dcacc73d 100644 --- a/Src/Asp.Net/OracleTest/Program.cs +++ b/Src/Asp.Net/OracleTest/Program.cs @@ -11,8 +11,9 @@ namespace OrmTest /// static void Main(string[] args) { - + //Demo + DemoN_SplitTable.Init(); Demo0_SqlSugarClient.Init(); Demo1_Queryable.Init(); Demo2_Updateable.Init();