From 161d29c4a67b1bdfcd7a6310ea0cf0447bb553b3 Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Sun, 31 Oct 2021 17:51:47 +0800 Subject: [PATCH] Update demo --- .../SqlSeverTest/Demo/DemoL_Snowflake.cs | 49 +++++++++++ .../SqlSeverTest/Demo/DemoM_UnitOfWork.cs | 72 ++++++++++++++++ .../SqlSeverTest/Demo/DemoN_SplitTable.cs | 86 +++++++++++++++++++ .../SqlSeverTest/SqlSeverTest/Program.cs | 3 + 4 files changed, 210 insertions(+) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoL_Snowflake.cs create mode 100644 Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoM_UnitOfWork.cs create mode 100644 Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoN_SplitTable.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoL_Snowflake.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoL_Snowflake.cs new file mode 100644 index 000000000..7111083d9 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoL_Snowflake.cs @@ -0,0 +1,49 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class DemoL_Snowflake + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### DemoL_Snowflake ####"); + + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.SqlServer, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + AopEvents = new AopEvents + { + OnLogExecuting = (sql, p) => + { + Console.WriteLine(sql); + Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))); + } + } + }); + db.CodeFirst.InitTables(); + Console.WriteLine(db.Queryable().Count()); + var id= db.Insertable(new SnowflakeModel() + { + Name="哈哈" + }).ExecuteReturnSnowflakeId(); + var ids = db.Insertable(db.Queryable().Take(10).ToList()).ExecuteReturnSnowflakeIdList(); + Console.WriteLine(db.Queryable().Count()); + } + } + public class SnowflakeModel + { + [SugarColumn(IsPrimaryKey =true)] + public long Id { get; set; } + + public string Name{get;set; } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoM_UnitOfWork.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoM_UnitOfWork.cs new file mode 100644 index 000000000..2c8006cc1 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoM_UnitOfWork.cs @@ -0,0 +1,72 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class DemoM_UnitOfWork + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### DemoM_UnitOfWork ####"); + + + DbContext.Db.UseTran(() => + { + + var id = DbContext.CustomDal.InsertReturnIdentity(new Custom() { Id = 1, Name = "guid" }); + var id2 = DbContext.OrderDal.InsertReturnIdentity(new Order() { Name = "guid2", Price = 0, CreateTime = DateTime.Now, CustomId = 1 }); + throw new Exception(""); + }, + e => + { + //throw e; + }); + Console.WriteLine(""); + Console.WriteLine("#### Saveable End ####"); + } + public class DbContext + { + public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig() + { + DbType = SqlSugar.DbType.SqlServer, + ConnectionString = Config.ConnectionString, + IsAutoCloseConnection = true + }, db => { + //单例参数配置,所有上下文生效 + db.Aop.OnLogExecuting = (s, p) => + { + Console.WriteLine(s); + }; + }); + + public static DbSet OrderDal => new DbSet(); + public static DbSet CustomDal => new DbSet(); + + + public class DbSet : SimpleClient where T : class, new() + { + public DbSet(ISqlSugarClient context = null) : base(context)//需要有构造参数 + { + base.Context = DbContext.Db; + } + + /// + /// 扩展方法,自带方法不能满足的时候可以添加新方法 + /// + /// + public List CommQuery(string json) + { + //base.Context.Queryable().ToList();可以拿到SqlSugarClient 做复杂操作 + return null; + } + + } + } + + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoN_SplitTable.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoN_SplitTable.cs new file mode 100644 index 000000000..ca5521b9f --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/DemoN_SplitTable.cs @@ -0,0 +1,86 @@ +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.Aop.OnLogExecuted = (s, p) => + { + Console.WriteLine(s); + }; + + //初始化分表 + db.CodeFirst.SplitTables().InitTables(); + + Console.WriteLine(); + + //根据最近3个表进行查询 + var list=db.Queryable().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().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(-1)); + 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(); + Console.WriteLine("#### CodeFirst end ####"); + } + + [SplitTable(SplitType.Day)] + [SqlSugar.SugarTable("Taxxx0101_{year}{month}{day}")] + public class OrderSpliteTest + { + [SugarColumn(IsPrimaryKey =true)] + public Guid Pk{ get; set; } + public string Name { get; set; } + [SugarColumn(IsNullable =true)] + [SplitField] + public DateTime Time { get; set; } + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs index 5afe2f00c..571653449 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs @@ -19,6 +19,9 @@ namespace OrmTest Demo2_Updateable.Init(); Demo3_Insertable.Init(); Demo4_Deleteable.Init(); + DemoN_SplitTable.Init(); + DemoM_UnitOfWork.Init(); + DemoL_Snowflake.Init(); Demo5_SqlQueryable.Init(); Demo6_Queue.Init(); Demo7_Ado.Init();