diff --git a/Src/Asp.Net/PgSqlTest/Demo/Demo0_SqlSugarClient.cs b/Src/Asp.Net/PgSqlTest/Demo/Demo0_SqlSugarClient.cs new file mode 100644 index 000000000..81a556044 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/Demo0_SqlSugarClient.cs @@ -0,0 +1,273 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; +namespace OrmTest +{ + public class Demo0_SqlSugarClient + { + + public static void Init() + { + SqlSugarClient();//Create db + DbContext();//Optimizing SqlSugarClient usage + SingletonPattern();//Singleten Pattern + DistributedTransactionExample(); + CustomAttribute(); + } + + private static void SqlSugarClient() + { + //Create db + Console.WriteLine("#### SqlSugarClient Start ####"); + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + 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))); + } + } + }); + + //if no exist create datebase SQLSUGAR4XTEST (bin/database/) + db.DbMaintenance.CreateDatabase(); + + //Use db + var dt = db.Ado.GetDataTable("select 1"); + + //create table OrderDetail + db.CodeFirst.InitTables(typeof(OrderItem)); + + db.Insertable(new OrderItem() { OrderId = 1, Price = 0 }).ExecuteCommand(); + Console.WriteLine("#### SqlSugarClient End ####"); + + } + + private static void DbContext() + { + Console.WriteLine(""); + Console.WriteLine("#### DbContext Start ####"); + var insertObj = new Order { Name = "jack", CreateTime = DateTime.Now }; + var InsertObjs = new Order[] { insertObj }; + + DbContext context = new DbContext(); + + context.Db.CodeFirst.InitTables();//Create Tables + ; + var orderDb = context.OrderDb; + + //Select + var data1 = orderDb.GetById(1); + var data2 = orderDb.GetList(); + var data3 = orderDb.GetList(it => it.Id == 1); + var data4 = orderDb.GetSingle(it => it.Id == 1); + var p = new PageModel() { PageIndex = 1, PageSize = 2 }; + var data5 = orderDb.GetPageList(it => it.Name == "xx", p); + Console.Write(p.PageCount); + var data6 = orderDb.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc); + Console.Write(p.PageCount); + List conModels = new List(); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" , FieldValueConvertFunc=it => Convert.ToInt32(it)});//id=1 + var data7 = orderDb.GetPageList(conModels, p, it => it.Name, OrderByType.Asc); + orderDb.AsQueryable().Where(x => x.Id == 1).ToList(); + + //Insert + orderDb.Insert(insertObj); + orderDb.InsertRange(InsertObjs); + var id = orderDb.InsertReturnIdentity(insertObj); + orderDb.AsInsertable(insertObj).ExecuteCommand(); + + + //Delete + orderDb.Delete(insertObj); + orderDb.DeleteById(1); + orderDb.DeleteById(new int[] { 1, 2 }); + orderDb.Delete(it => it.Id == 1); + orderDb.AsDeleteable().Where(it => it.Id == 1).ExecuteCommand(); + + //Update + orderDb.Update(insertObj); + orderDb.UpdateRange(InsertObjs); + orderDb.Update(it => new Order() { Name = "a", }, it => it.Id == 1); + orderDb.AsUpdateable(insertObj).UpdateColumns(it => new { it.Name }).ExecuteCommand(); + + //Use Inherit DbContext + OrderDal dal = new OrderDal(); + var data = dal.GetById(1); + var list = dal.GetList(); + + Console.WriteLine("#### DbContext End ####"); + } + + private static void CustomAttribute() + { + Console.WriteLine(""); + Console.WriteLine("#### Custom Attribute Start ####"); + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.PostgreSQL, + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute, + ConfigureExternalServices = new ConfigureExternalServices() + { + EntityService = (property, column) => + { + + var attributes = property.GetCustomAttributes(true);//get all attributes + + if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey + { + column.IsPrimarykey = true; + } + }, + EntityNameService = (type, entity) => + { + var attributes = type.GetCustomAttributes(true); + if (attributes.Any(it => it is TableAttribute)) + { + entity.DbTableName = (attributes.First(it => it is TableAttribute) as TableAttribute).Name; + } + } + } + }); + db.CodeFirst.InitTables();//Create Table + + db.Insertable(new MyCustomAttributeTable() { Id = Guid.NewGuid().ToString(), Name = "Name" }).ExecuteCommand(); + var list = db.Queryable().ToList(); + + Console.WriteLine("#### Custom Attribute End ####"); + } + + + private static void SingletonPattern() + { + Console.WriteLine(""); + Console.WriteLine("#### Singleton Pattern Start ####"); + Console.WriteLine("Db_Id:" + singleDb.ContextID); + Console.WriteLine("Db_Id:" + singleDb.ContextID); + var task = new Task(() => + { + Console.WriteLine("Task DbId:" + singleDb.ContextID); + new Task(() => + { + Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID); + Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID); + + }).Start(); + Console.WriteLine("Task DbId:" + singleDb.ContextID); + }); + task.Start(); + task.Wait(); + System.Threading.Thread.Sleep(500); + Console.WriteLine(string.Join(",", singleDb.TempItems.Keys)); + + Console.WriteLine("#### Singleton Pattern end ####"); + } + + static SqlSugarClient singleDb = new SqlSugarClient( + new ConnectionConfig() + { + ConfigId = 1, + DbType = DbType.PostgreSQL, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + AopEvents = new AopEvents() + { + OnLogExecuting = (sql, p) => { Console.WriteLine(sql); } + } + }); + + + private static void DistributedTransactionExample() + { + //see sqlservertest + } + } + + /// + /// DbContext Example 1 + /// + public class DbContext + { + + public SqlSugarClient Db; + public DbContext() + { + Db = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.PostgreSQL, + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute, + AopEvents = new AopEvents() + { + OnLogExecuting = (sql, p) => + { + Console.WriteLine(sql); + } + } + }); + } + public SimpleClient OrderDb => new SimpleClient(Db); + public SimpleClient OrderItemDb => new SimpleClient(Db); + } + + + public class OrderDal : DbContext + { + + } + /// + /// DbContext Example 2 + /// + /// + public class DbContext where T : class, new() + { + + public SqlSugarClient Db; + public DbContext() + { + Db = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.PostgreSQL, + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute, + AopEvents = new AopEvents() + { + OnLogExecuting = (sql, p) => + { + Console.WriteLine(sql); + } + } + }); + } + public SimpleClient CurrentDb => new SimpleClient(Db); + public virtual T GetById(int id) + { + return CurrentDb.GetById(id); + } + public virtual List GetList() + { + return CurrentDb.GetList(); + } + public virtual bool Delete(int id) + { + return CurrentDb.DeleteById(id); + } + } + +} diff --git a/Src/Asp.Net/PgSqlTest/Demo/Demo1_Queryable.cs b/Src/Asp.Net/PgSqlTest/Demo/Demo1_Queryable.cs new file mode 100644 index 000000000..194e2f07e --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/Demo1_Queryable.cs @@ -0,0 +1,289 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Data; +using System.Dynamic; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + public class Demo1_Queryable + { + + public static void Init() + { + EasyExamples(); + QueryConditions(); + //JoinTable(); + Async(); + NoEntity(); + Mapper(); + SqlFuncTest(); + Subquery(); + ReturnType(); + } + + private static void EasyExamples() + { + Console.WriteLine(""); + Console.WriteLine("#### Examples Start ####"); + var db = GetInstance(); + var dbTime = db.GetDate(); + var getAll = db.Queryable().ToList(); + var getOrderBy = db.Queryable().OrderBy(it => it.Name,OrderByType.Desc).ToList(); + var getOrderBy2 = db.Queryable().OrderBy(it => it.Id).OrderBy(it => it.Name, OrderByType.Desc).ToList(); + var getOrderBy3 = db.Queryable().OrderBy(it =>new { it.Name,it.Id}).ToList(); + var getRandom = db.Queryable().OrderBy(it => SqlFunc.GetRandom()).First(); + var getByPrimaryKey = db.Queryable().InSingle(2); + var getSingleOrDefault = db.Queryable().Where(it => it.Id == 1).Single(); + var getFirstOrDefault = db.Queryable().First(); + var getByWhere = db.Queryable().Where(it => it.Id == 1 || it.Name == "a").ToList(); + var getByWhere2 = db.Queryable().Where(it => it.Id == DateTime.Now.Year).ToList(); + var getByFuns = db.Queryable().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList(); + var getByFuns2 = db.Queryable().GroupBy(it => it.Name).Select(it => SqlFunc.AggregateDistinctCount(it.Price)).ToList(); + Console.WriteLine("#### Examples End ####"); + } + + private static void ReturnType() + { + Console.WriteLine(""); + Console.WriteLine("#### ReturnType Start ####"); + var db = GetInstance(); + List list = db.Queryable().ToList(); + + Order item = db.Queryable().First(it => it.Id == 1); + + DataTable dataTable = db.Queryable().Select(it => it.Id).ToDataTable(); + + var json = db.Queryable().ToJson(); + + List listInt = db.Queryable().Select(it => it.Id).ToList(); + + var dynamic = db.Queryable().Select().ToList(); + + var viewModel = db.Queryable((o, i, c) => new JoinQueryInfos( + JoinType.Left, o.Id == i.OrderId , + JoinType.Left, o.CustomId == c.Id + )) + .Select().ToList(); + + var newDynamic = db.Queryable((o, i, c) => new JoinQueryInfos( + JoinType.Left, o.Id == i.OrderId, + JoinType.Left, o.CustomId == c.Id + )) + .Select((o, i, c) => new { orderName = o.Name, cusName=c.Name }).ToList(); + + var newClass = db.Queryable((o, i, c) => new JoinQueryInfos( + JoinType.Left, o.Id == i.OrderId, + JoinType.Left, o.CustomId == c.Id + )) + .Select((o, i, c) => new ViewOrder { Name=o.Name, CustomName=c.Name }).ToList(); + + + var oneClass = db.Queryable((o, i, c) => new JoinQueryInfos( + JoinType.Left, o.Id == i.OrderId, + JoinType.Left, o.CustomId == c.Id + )) + .Select((o, i, c) => c).ToList(); + + var twoClass = db.Queryable((o, i, c) => new JoinQueryInfos( + JoinType.Left, o.Id == i.OrderId, + JoinType.Left, o.CustomId == c.Id + )) + .Select((o, i, c) => new { o,i}).ToList(); + + List> ListDic = db.Queryable((o, i, c) => new JoinQueryInfos( + JoinType.Left, o.Id == i.OrderId, + JoinType.Left, o.CustomId == c.Id + )) + .Select().ToList().Select(it => it.ToDictionary(x => x.Key, x => x.Value)).ToList(); + Console.WriteLine("#### ReturnType End ####"); + } + + private static void Subquery() + { + Console.WriteLine(""); + Console.WriteLine("#### Subquery Start ####"); + var db = GetInstance(); + + var list = db.Queryable().Take(10).Select(it => new + { + customName=SqlFunc.Subqueryable().Where("it.CustomId=id").Select(s=>s.Name), + customName2 = SqlFunc.Subqueryable().Where("it.CustomId = id").Where(s => true).Select(s => s.Name) + }).ToList(); + + var list2 = db.Queryable().Where(it => SqlFunc.Subqueryable().Where(i => i.OrderId == it.Id).Any()).ToList(); + + Console.WriteLine("#### Subquery End ####"); + } + + private static void SqlFuncTest() + { + Console.WriteLine(""); + Console.WriteLine("#### SqlFunc Start ####"); + var db = GetInstance(); + var index= db.Queryable().Select(it => SqlFunc.Contains(it.Name, "cccacc")).First(); + + Console.WriteLine("#### SqlFunc End ####"); + } + + private static void Mapper() + { + Console.WriteLine(""); + Console.WriteLine("#### Mapper Start ####"); + var db = GetInstance(); + //Creater Table + db.CodeFirst.InitTables(typeof(Tree)); + db.DbMaintenance.TruncateTable("tree"); + db.Insertable(new Tree() { Id = 1, Name = "root" }).ExecuteCommand(); + db.Insertable(new Tree() { Id = 11, Name = "child1",ParentId=1 }).ExecuteCommand(); + db.Insertable(new Tree() { Id = 12, Name = "child2",ParentId=1 }).ExecuteCommand(); + db.Insertable(new Tree() { Id = 2, Name = "root" }).ExecuteCommand(); + db.Insertable(new Tree() { Id = 22, Name = "child3", ParentId = 2 }).ExecuteCommand(); + + var list=db.Queryable() + //parent=(select * from parent where id=it.parentid) + .Mapper(it=>it.Parent,it=>it.ParentId, it=>it.Parent.Id) + //Child=(select * from parent where ParentId=it.id) + .Mapper(it => it.Child, it => it.Id, it => it.Parent.ParentId) + .ToList(); + + + Console.WriteLine("#### End Start ####"); + } + + private static void NoEntity() + { + Console.WriteLine(""); + Console.WriteLine("#### No Entity Start ####"); + var db = GetInstance(); + + var list = db.Queryable().AS("order").Where("id=@id", new { id = 1 }).ToList(); + + var list2 = db.Queryable("o").AS("order").AddJoinInfo("OrderDetail", "i", "o.id=i.OrderId").Where("id=id", new { id = 1 }).Select("o.*").ToList(); + Console.WriteLine("#### No Entity End ####"); + } + + private static void JoinTable() + { + Console.WriteLine(""); + Console.WriteLine("#### Join Table Start ####"); + var db = GetInstance(); + + //Simple join + var list = db.Queryable((o, i, c) => o.Id == i.OrderId&&c.Id == o.CustomId) + .Select() + .ToList(); + + //Join table + var list2 = db.Queryable((o, i, c) => new JoinQueryInfos( + JoinType.Left, o.Id == i.OrderId, + JoinType.Left, c.Id == o.CustomId + )) + .Select().ToList(); + + //Join queryable + var query1 = db.Queryable((o, i) => new JoinQueryInfos( + JoinType.Left, o.Id == i.OrderId + )) + .Where(o => o.Name == "jack"); + + var query2 = db.Queryable(); + var list3=db.Queryable(query1, query2,JoinType.Left, (p1, p2) => p1.CustomId == p2.Id).Select().ToList(); + + Console.WriteLine("#### Join Table End ####"); + } + + private static void QueryConditions() + { + Console.WriteLine(""); + Console.WriteLine("#### Query Conditions Start ####"); + + SqlSugarClient db = GetInstance(); + + /*** By expression***/ + + //id=@id + var list = db.Queryable().Where(it => it.Id == 1).ToList(); + //id=@id or name like '%'+@name+'%' + var list2 = db.Queryable().Where(it => it.Id == 1 || it.Name.Contains("jack")).ToList(); + + + //Create expression + var exp = Expressionable.Create() + .And(it => it.Id == 1) + .Or(it => it.Name.Contains("jack")).ToExpression(); + var list3 = db.Queryable().Where(exp).ToList(); + + + + + /*** By dynamic***/ + + //id=1 + var conModels = new List(); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" , FieldValueConvertFunc = it => Convert.ToInt32(it) });//id=1 + var student = db.Queryable().Where(conModels).ToList(); + + //Complex use case + List Order = new List(); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1", FieldValueConvertFunc=it=>Convert.ToInt32(it) });//id=1 + + var list6 = db.Queryable().Where(conModels).ToList(); + + /*** Conditional builder ***/ + + // use whereif + string name = ""; + int id = 1; + var query = db.Queryable() + .WhereIF(!string.IsNullOrEmpty(name), it => it.Name.Contains(name)) + .WhereIF(id > 0, it => it.Id == id).ToList(); + //clone new Queryable + var query2 = db.Queryable().Where(it => it.Id == 1); + var list7 = query2.Clone().Where(it => it.Name == "jack").ToList();//id=1 and name = jack + var list8 = query2.Clone().Where(it => it.Name == "tom").ToList();//id=1 and name = tom + + Console.WriteLine("#### Condition Screening End ####"); + + + + } + + private static void Async() + { + Console.WriteLine(""); + Console.WriteLine("#### Async Start ####"); + + SqlSugarClient db = GetInstance(); + var task1 = db.Queryable().FirstAsync(); + task1.Wait(); + var task2 = db.Queryable().Where(it => it.Id == 1).ToListAsync(); + + + task2.Wait(); + + Console.WriteLine("#### Async End ####"); + } + + private static SqlSugarClient GetInstance() + { + return new SqlSugarClient(new ConnectionConfig() + { + DbType = SqlSugar.DbType.PostgreSQL, + 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))); + } + } + }); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Demo/Demo2_Updateable.cs b/Src/Asp.Net/PgSqlTest/Demo/Demo2_Updateable.cs new file mode 100644 index 000000000..ae5259aef --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/Demo2_Updateable.cs @@ -0,0 +1,107 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class Demo2_Updateable + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### Updateable Start ####"); + + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + 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))); + } + } + }); + + + + /*** 1.entity or List ***/ + + var updateObj = new Order() { Id = 1, Name = "order1" }; + var updateObjs = new List { + new Order() { Id = 11, Name = "order11" }, + new Order() { Id = 12, Name = "order12" } + }; + + //update all columns by primary key + var result = db.Updateable(updateObj).ExecuteCommand();//update single + var result2 = db.Updateable(updateObjs).ExecuteCommand();//update List + + //Ignore Name and Price + var result3 = db.Updateable(updateObj).IgnoreColumns(it => new { it.CreateTime, it.Price }).ExecuteCommand(); + + //only update Name and CreateTime + var result4 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name, it.CreateTime }).ExecuteCommand(); + + //If there is no primary key + var result5 = db.Updateable(updateObj).WhereColumns(it => new { it.Id }).ExecuteCommand();//update single by id + var result6 = db.Updateable(updateObjs).WhereColumns(it => new { it.Id }).ExecuteCommand();//update List by id + + + + + /*** 2.by expression ***/ + + //update name,createtime + var result7 = db.Updateable(it => new Order() { Name = "a", CreateTime = DateTime.Now }).Where(it => it.Id == 11).ExecuteCommand(); + var result71 = db.Updateable().SetColumns(it => new Order() { Name = "a", CreateTime = DateTime.Now }).Where(it => it.Id == 11).ExecuteCommand(); + //only update name + var result8 = db.Updateable(it => it.Name == "Name").Where(it => it.Id == 1).ExecuteCommand(); + var result81 = db.Updateable().SetColumns(it => it.Name == "Name").Where(it => it.Id == 1).ExecuteCommand(); + // + + + + + /*** 3.by Dictionary ***/ + var dt = new Dictionary(); + dt.Add("id", 1); + dt.Add("name", null); + dt.Add("createTime", DateTime.Now); + var dtList = new List>(); + dtList.Add(dt); + + var t66 = db.Updateable(dt).AS("student").WhereColumns("id").ExecuteCommand(); + var t666 = db.Updateable(dtList).AS("student").WhereColumns("id").ExecuteCommand(); + + + + /*** 4.Other instructions ***/ + + var caseValue = "1"; + //Do not update NULL columns + db.Updateable(updateObj).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); + + //if 1 update name else if 2 update name,createtime + db.Updateable(updateObj) + .UpdateColumnsIF(caseValue == "1", it => new { it.Name }) + .UpdateColumnsIF(caseValue == "2", it => new { it.Name, it.CreateTime }) + .ExecuteCommand(); + //Use Lock + db.Updateable(updateObj).With(SqlWith.UpdLock).ExecuteCommand(); + + //Where Sql + db.Updateable(updateObj).Where("id=@x", new { x = 1 }).ExecuteCommand(); + + Console.WriteLine("#### Updateable End ####"); + } + + } +} \ No newline at end of file diff --git a/Src/Asp.Net/PgSqlTest/Demo/Demo3_Insertable.cs b/Src/Asp.Net/PgSqlTest/Demo/Demo3_Insertable.cs new file mode 100644 index 000000000..292ed577d --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/Demo3_Insertable.cs @@ -0,0 +1,56 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class Demo3_Insertable + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### Insertable Start ####"); + + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + 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))); + } + } + }); + + var insertObj = new Order() { Id = 1, Name = "order1" }; + var updateObjs = new List { + new Order() { Id = 11, Name = "order11" }, + new Order() { Id = 12, Name = "order12" } + }; + + //Ignore Price + db.Insertable(insertObj).IgnoreColumns(it => new { it.Price }).ExecuteReturnIdentity();//get identity + db.Insertable(insertObj).IgnoreColumns("Name", "TestId").ExecuteReturnIdentity(); + + //Only insert Name and Price + db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.Price }).ExecuteReturnIdentity(); + db.Insertable(insertObj).InsertColumns("Name", "SchoolId").ExecuteReturnIdentity(); + + //ignore null columns + db.Insertable(insertObj).IgnoreColumns(ignoreNullColumn: true).ExecuteCommand();//get change row count + + //Use Lock + db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand(); + + Console.WriteLine("#### Insertable End ####"); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Demo/Demo4_Deleteable.cs b/Src/Asp.Net/PgSqlTest/Demo/Demo4_Deleteable.cs new file mode 100644 index 000000000..39fe7ab67 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/Demo4_Deleteable.cs @@ -0,0 +1,48 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class Demo4_Deleteable + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### Deleteable Start ####"); + + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + 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))); + } + } + }); + //by entity + db.Deleteable().Where(new Order() { Id = 1 }).ExecuteCommand(); + + //by primary key + db.Deleteable().In(1).ExecuteCommand(); + + //by primary key array + db.Deleteable().In(new int[] { 1, 2 }).ExecuteCommand(); + + //by expression + db.Deleteable().Where(it => it.Id == 1).ExecuteCommand(); + + Console.WriteLine("#### Deleteable End ####"); + + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Demo/Demo5_SqlQueryable.cs b/Src/Asp.Net/PgSqlTest/Demo/Demo5_SqlQueryable.cs new file mode 100644 index 000000000..8aa49913f --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/Demo5_SqlQueryable.cs @@ -0,0 +1,37 @@ +using OrmTest.Models; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class Demo5_SqlQueryable + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### SqlQueryable Start ####"); + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true + }); + + int total = 0; + var list = db.SqlQueryable("select * from student").ToPageList(1, 2, ref total); + + + //by expression + var list2 = db.SqlQueryable("select * from student").Where(it => it.Id == 1).ToPageList(1, 2); + //by sql + var list3 = db.SqlQueryable("select * from student").Where("id=@id", new { id = 1 }).ToPageList(1, 2); + + Console.WriteLine("#### SqlQueryable End ####"); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Demo/Demo6_Queue.cs b/Src/Asp.Net/PgSqlTest/Demo/Demo6_Queue.cs new file mode 100644 index 000000000..829ccf7d7 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/Demo6_Queue.cs @@ -0,0 +1,52 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class Demo6_Queue + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### Queue Start ####"); + + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + 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.Insertable(new Order() { Name = "a" }).AddQueue(); + db.Insertable(new Order() { Name = "b" }).AddQueue(); + db.SaveQueues(); + + + db.Insertable(new Order() { Name = "a" }).AddQueue(); + db.Insertable(new Order() { Name = "b" }).AddQueue(); + db.Insertable(new Order() { Name = "c" }).AddQueue(); + db.Insertable(new Order() { Name = "d" }).AddQueue(); + var ar = db.SaveQueuesAsync(); + ar.Wait(); + + db.Queryable().AddQueue(); + db.Queryable().AddQueue(); + db.AddQueue("select * from [Order] where id=@id", new { id = 10000 }); + var result2 = db.SaveQueues(); + + Console.WriteLine("#### Queue End ####"); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Demo/DemoD_DbFirst.cs b/Src/Asp.Net/PgSqlTest/Demo/DemoD_DbFirst.cs new file mode 100644 index 000000000..7062f5b05 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/DemoD_DbFirst.cs @@ -0,0 +1,75 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + public class DemoD_DbFirst + { + public static void Init() + { + Console.WriteLine(); + Console.WriteLine("#### DbFirst Start ####"); + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true + }); + + db.DbFirst.CreateClassFile("c:\\Demo\\1", "Models"); + + + db.DbFirst.Where("Student").CreateClassFile("c:\\Demo\\2", "Models"); + + + db.DbFirst.Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\3", "Models"); + + + db.DbFirst.Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\4", "Models"); + + + db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\5", "Models"); + + + db.DbFirst.IsCreateDefaultValue().CreateClassFile("c:\\Demo\\6", "Demo.Models"); + + + db.DbFirst. SettingClassTemplate(old => { return old;}) + .SettingNamespaceTemplate(old =>{ return old;}) + .SettingPropertyDescriptionTemplate(old => + { + return @" /// + /// Desc_New:{PropertyDescription} + /// Default_New:{DefaultValue} + /// Nullable_New:{IsNullable} + /// "; + }) + .SettingPropertyTemplate(old =>{return old;}) + .SettingConstructorTemplate(old =>{return old; }) + .CreateClassFile("c:\\Demo\\7"); + + + + foreach (var item in db.DbMaintenance.GetTableInfoList()) + { + string entityName = item.Name.ToUpper();/*Format class name*/ + db.MappingTables.Add(entityName , item.Name); + foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(item.Name)) + { + db.MappingColumns.Add(col.DbColumnName.ToUpper() /*Format class property name*/, col.DbColumnName, entityName); + } + } + db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\8", "Models"); + + + //Use Razor Template + //db.DbFirst.UseRazorAnalysis(RazorFirst.DefaultRazorClassTemplate).CreateClassFile(""); + + Console.WriteLine("#### DbFirst End ####"); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Demo/DemoE_CodeFirst.cs b/Src/Asp.Net/PgSqlTest/Demo/DemoE_CodeFirst.cs new file mode 100644 index 000000000..178a60a71 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/DemoE_CodeFirst.cs @@ -0,0 +1,39 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + public class DemoE_CodeFirst + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### CodeFirst Start ####"); + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + ConnectionString = "PORT=5432;DATABASE=MyDbTest;HOST=localhost;PASSWORD=haosql;USER ID=postgres", + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true + }); + db.DbMaintenance.CreateDatabase(@"c:\"); + db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create CodeFirstTable1 + db.Insertable(new CodeFirstTable1() { Name = "a", Text="a" }).ExecuteCommand(); + var list = db.Queryable().ToList(); + Console.WriteLine("#### CodeFirst end ####"); + } + } + + public class CodeFirstTable1 + { + [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] + public int Id { get; set; } + public string Name { get; set; } + public string Text { get; set; } + [SugarColumn(IsNullable = true)] + public DateTime CreateTime { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Demo/Democ_GobalFilter.cs b/Src/Asp.Net/PgSqlTest/Demo/Democ_GobalFilter.cs new file mode 100644 index 000000000..d615c7bc9 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Demo/Democ_GobalFilter.cs @@ -0,0 +1,77 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + public class Democ_GobalFilter + { + public static void Init() + { + Console.WriteLine(""); + Console.WriteLine("#### Filter Start ####"); + var db = GetInstance(); + + + var sql = db.Queryable().ToSql(); + //SELECT [Id],[Name],[Price],[CreateTime] FROM [Order] WHERE isDelete=0 + Console.WriteLine(sql); + + + var sql2 = db.Queryable((main,ot)=> main.Id==ot.OrderId).ToSql(); + //SELECT [Id],[Name],[Price],[CreateTime] FROM [Order] main ,[OrderDetail] ot WHERE ( [main].[Id] = [ot].[OrderId] ) AND main.isDelete=0 + Console.WriteLine(sql2); + + + var sql3 = db.Queryable().Filter("Myfilter").ToSql();// Myfilter+Gobal + //SELECT [Id],[Name],[Price],[CreateTime] FROM [Order] WHERE Name='jack' AND isDelete=0 + Console.WriteLine(sql3); + + var sql4 = db.Queryable().Filter("Myfilter",isDisabledGobalFilter:true).ToSql();//only Myfilter + //SELECT [Id],[Name],[Price],[CreateTime] FROM [Order] WHERE Name='jack' + Console.WriteLine(sql4); + Console.WriteLine("#### Filter End ####"); + } + + + public static SqlSugarClient GetInstance() + { + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.PostgreSQL, ConnectionString = Config.ConnectionString, IsAutoCloseConnection = true }); + + //single table query gobal filter + db.QueryFilter.Add(new SqlFilterItem() + { + FilterValue = filterDb => + { + //Writable logic + return new SqlFilterResult() { Sql = " isDelete=0" };//Global string perform best + } + }); + + //Multi-table query gobal filter + db.QueryFilter.Add(new SqlFilterItem() + { + FilterValue = filterDb => + { + //Writable logic + return new SqlFilterResult() { Sql = " main.isDelete=0" }; + }, + IsJoinQuery=true + }); + + //Specific filters + db.QueryFilter.Add(new SqlFilterItem() + { + FilterName= "Myfilter", + FilterValue = filterDb => + { + //Writable logic + return new SqlFilterResult() { Sql = "Name='jack'" }; + } + }); + return db; + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Models/CarType.cs b/Src/Asp.Net/PgSqlTest/Models/CarType.cs new file mode 100644 index 000000000..00dc12710 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Models/CarType.cs @@ -0,0 +1,7 @@ +namespace OrmTest +{ + public class CarType + { + public bool State { get; set; } + } +} \ No newline at end of file diff --git a/Src/Asp.Net/PgSqlTest/Models/Custom.cs b/Src/Asp.Net/PgSqlTest/Models/Custom.cs new file mode 100644 index 000000000..3b8871c57 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Models/Custom.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class Custom + { + public int Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Models/Mappers.cs b/Src/Asp.Net/PgSqlTest/Models/Mappers.cs new file mode 100644 index 000000000..d2878de64 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Models/Mappers.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class Tree + { + [SqlSugar.SugarColumn(IsPrimaryKey =true)] + public int Id { get; set; } + public string Name { get; set; } + public int ParentId { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public Tree Parent { get; set; } + [SqlSugar.SugarColumn(IsIgnore = true)] + public List Child { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Models/MyCustomAttributeTable.cs b/Src/Asp.Net/PgSqlTest/Models/MyCustomAttributeTable.cs new file mode 100644 index 000000000..11359d062 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Models/MyCustomAttributeTable.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + [Table("CustomAttributeTable")] + //[SugarTable("CustomAttributeTable")] + public class MyCustomAttributeTable + { + + [Key] + //[SugarColumn(IsPrimaryKey =true)] + public string Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Models/Order.cs b/Src/Asp.Net/PgSqlTest/Models/Order.cs new file mode 100644 index 000000000..c03613669 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Models/Order.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + + public class Order + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + public string Name { get; set; } + public decimal Price { get; set; } + [SqlSugar.SugarColumn(IsNullable = true)] + public DateTime CreateTime { get; set; } + [SqlSugar.SugarColumn(IsNullable =true)] + public int CustomId { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Models/OrderItem.cs b/Src/Asp.Net/PgSqlTest/Models/OrderItem.cs new file mode 100644 index 000000000..ae3262c7c --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Models/OrderItem.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + [SqlSugar.SugarTable("OrderDetail")] + public class OrderItem + { + [SqlSugar.SugarColumn(IsPrimaryKey =true, IsIdentity =true)] + public int ItemId { get; set; } + public int OrderId { get; set; } + public decimal? Price { get; set; } + [SqlSugar.SugarColumn(IsNullable = true)] + public DateTime? CreateTime { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Models/TestTree.cs b/Src/Asp.Net/PgSqlTest/Models/TestTree.cs new file mode 100644 index 000000000..b8250828a --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Models/TestTree.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class TestTree + { + [SqlSugar.SugarColumn(ColumnDataType = "hierarchyid")] + public string TreeId { get; set; } + [SqlSugar.SugarColumn(ColumnDataType = "Geography")] + public string GId { get; set; } + public string Name { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Models/ViewOrder.cs b/Src/Asp.Net/PgSqlTest/Models/ViewOrder.cs new file mode 100644 index 000000000..fcd465747 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/Models/ViewOrder.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class ViewOrder:Order + { + public string CustomName { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/Demos/1_Query.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/1_Query.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/1_Query.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/1_Query.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/2_Update.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/2_Update.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/2_Update.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/2_Update.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/3_Insert.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/3_Insert.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/3_Insert.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/3_Insert.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/4_Delete.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/4_Delete.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/4_Delete.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/4_Delete.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/5_CodeFirst.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/5_CodeFirst.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/5_CodeFirst.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/5_CodeFirst.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/5_DbFirst.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/5_DbFirst.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/5_DbFirst.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/5_DbFirst.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/6_ComplexModel.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/6_ComplexModel.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/6_ComplexModel.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/6_ComplexModel.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/7_Filter.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/7_Filter.cs similarity index 98% rename from Src/Asp.Net/PgSqlTest/Demos/7_Filter.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/7_Filter.cs index 6f42fba06..512b790cd 100644 --- a/Src/Asp.Net/PgSqlTest/Demos/7_Filter.cs +++ b/Src/Asp.Net/PgSqlTest/OldTest/Demos/7_Filter.cs @@ -41,7 +41,7 @@ namespace OrmTest.Demo public static SqlSugarClient GetInstance1() { - SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true }); + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.PostgreSQL, IsAutoCloseConnection = true }); db.QueryFilter .Add(new SqlFilterItem() { diff --git a/Src/Asp.Net/PgSqlTest/Demos/8_JoinSql.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/8_JoinSql.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/8_JoinSql.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/8_JoinSql.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/9_Aop.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/9_Aop.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/9_Aop.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/9_Aop.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/A_MasterSlave.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/A_MasterSlave.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/A_MasterSlave.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/A_MasterSlave.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/B_SharedConnection.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/B_SharedConnection.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/B_SharedConnection.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/B_SharedConnection.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/CS_TeacherStudent.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/CS_TeacherStudent.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/CS_TeacherStudent.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/CS_TeacherStudent.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/C_ExtSqlFun.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/C_ExtSqlFun.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/C_ExtSqlFun.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/C_ExtSqlFun.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/D_QueryableView.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/D_QueryableView.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/D_QueryableView.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/D_QueryableView.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/E_Attribute.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/E_Attribute.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/E_Attribute.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/E_Attribute.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/F_VersionValidation.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/F_VersionValidation.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/F_VersionValidation.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/F_VersionValidation.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/G_Mapper.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/G_Mapper.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/G_Mapper.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/G_Mapper.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/H_ExtEntity.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/H_ExtEntity.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/H_ExtEntity.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/H_ExtEntity.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/H_Queue.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/H_Queue.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/H_Queue.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/H_Queue.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/PerformanceTest.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/PerformanceTest.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/PerformanceTest.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/PerformanceTest.cs diff --git a/Src/Asp.Net/PgSqlTest/Demos/Z_DemoBase.cs b/Src/Asp.Net/PgSqlTest/OldTest/Demos/Z_DemoBase.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Demos/Z_DemoBase.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Demos/Z_DemoBase.cs diff --git a/Src/Asp.Net/PgSqlTest/Models/DataTestInfo.cs b/Src/Asp.Net/PgSqlTest/OldTest/Models/DataTestInfo.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Models/DataTestInfo.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Models/DataTestInfo.cs diff --git a/Src/Asp.Net/PgSqlTest/Models/DataTestInfo2.cs b/Src/Asp.Net/PgSqlTest/OldTest/Models/DataTestInfo2.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Models/DataTestInfo2.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Models/DataTestInfo2.cs diff --git a/Src/Asp.Net/PgSqlTest/Models/Enum.cs b/Src/Asp.Net/PgSqlTest/OldTest/Models/Enum.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Models/Enum.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Models/Enum.cs diff --git a/Src/Asp.Net/PgSqlTest/Models/School.cs b/Src/Asp.Net/PgSqlTest/OldTest/Models/School.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Models/School.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Models/School.cs diff --git a/Src/Asp.Net/PgSqlTest/Models/Student.cs b/Src/Asp.Net/PgSqlTest/OldTest/Models/Student.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Models/Student.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Models/Student.cs diff --git a/Src/Asp.Net/PgSqlTest/Models/ViewModelStudent.cs b/Src/Asp.Net/PgSqlTest/OldTest/Models/ViewModelStudent.cs similarity index 100% rename from Src/Asp.Net/PgSqlTest/Models/ViewModelStudent.cs rename to Src/Asp.Net/PgSqlTest/OldTest/Models/ViewModelStudent.cs diff --git a/Src/Asp.Net/PgSqlTest/OldTest/Program.cs b/Src/Asp.Net/PgSqlTest/OldTest/Program.cs new file mode 100644 index 000000000..4b7c93370 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/OldTest/Program.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Linq.Expressions; +using SqlSugar; +using OrmTest.Models; +using System.Data.SqlClient; + + +namespace OrmTest +{ + public class OldTestMain + { + public static void Init() + { + Demo.CodeFirst.Init(); + Demo.DbFirst.Init(); + Demo.Aop.Init(); + Demo.Query.Init(); + Demo.Insert.Init(); + Demo.Delete.Init(); + Demo.Update.Init(); + Demo.MasterSlave.Init(); + Demo.SharedConnection.Init(); + Demo.ExtSqlFun.Init(); + //Demo.QueryableView.Init(); + Demo.AttributeDemo.Init(); + Demo.Mapper.Init(); + Demo.ExtEntity.Init(); + Demo.Queue.Init(); + + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj index 91e5bded5..93e025e30 100644 --- a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj +++ b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj @@ -60,37 +60,67 @@ + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Src/Asp.Net/PgSqlTest/Program.cs b/Src/Asp.Net/PgSqlTest/Program.cs index c635b0f0b..893b1d331 100644 --- a/Src/Asp.Net/PgSqlTest/Program.cs +++ b/Src/Asp.Net/PgSqlTest/Program.cs @@ -1,14 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using System.Linq.Expressions; -using SqlSugar; -using OrmTest.Models; -using System.Data.SqlClient; - namespace OrmTest { @@ -16,22 +6,25 @@ namespace OrmTest { static void Main(string[] args) { - Demo.CodeFirst.Init(); - Demo.DbFirst.Init(); - Demo.Aop.Init(); - Demo.Query.Init(); - Demo.Insert.Init(); - Demo.Delete.Init(); - Demo.Update.Init(); - Demo.MasterSlave.Init(); - Demo.SharedConnection.Init(); - Demo.ExtSqlFun.Init(); - //Demo.QueryableView.Init(); - Demo.AttributeDemo.Init(); - Demo.Mapper.Init(); - Demo.ExtEntity.Init(); - Demo.Queue.Init(); + // OldTestMain.Init(); + //Demo + Demo0_SqlSugarClient.Init(); + Demo1_Queryable.Init(); + Demo2_Updateable.Init(); + Democ_GobalFilter.Init(); + DemoD_DbFirst.Init(); + DemoE_CodeFirst.Init(); + Demo5_SqlQueryable.Init(); + Demo6_Queue.Init(); + + //Unit test + NewUnitTest.Init(); + + Console.WriteLine("all successfully."); + Console.ReadKey(); } + + } } diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs b/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs new file mode 100644 index 000000000..a18614ff5 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs @@ -0,0 +1,39 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + public partial class NewUnitTest + { + public static SqlSugarClient Db=> new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + 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))); + } + } + }); + public static void Init() + { + CodeFirst(); + Updateable(); + Json(); + Ado(); + Queryable(); + QueryableAsync(); + Thread(); + Thread2(); + Thread3(); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UAdo.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UAdo.cs new file mode 100644 index 000000000..5f3a431cb --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UAdo.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + public static void Ado() + { + + var task1 = Db.Ado.GetScalarAsync("select 1"); + task1.Wait(); + UValidate.Check(1, task1.Result, "ado"); + + var task2 = Db.Ado.GetIntAsync("select 2"); + task2.Wait(); + UValidate.Check(2, task2.Result, "ado"); + + + var task3 = Db.Ado.GetLongAsync("select 3"); + task3.Wait(); + UValidate.Check(3, task3.Result, "ado"); + + + var task4 = Db.Ado.GetDataTableAsync("select 4 as id"); + task4.Wait(); + UValidate.Check(4, task4.Result.Rows[0]["id"], "ado"); + + + var task5 = Db.Ado.GetInt("select @id as id",new { id=5}); + UValidate.Check(5, task5, "ado"); + + + + var task6 = Db.Ado.SqlQuery("select @id as id", new { id = 5 }); + UValidate.Check(5, task6[0].id, "ado"); + + + var task7 = Db.Ado.SqlQueryAsync("select @id as id", new { id = 7 }); + task7.Wait(); + UValidate.Check(7, task7.Result[0].id, "ado"); + + + var task8 = Db.Ado.SqlQueryAsync("select 8 as id"); + task8.Wait(); + UValidate.Check(8, task8.Result[0].id, "ado"); + + var task9=Db.Ado.SqlQuery("select * from [order];select * from OrderDetail"); + + var task10 = Db.Ado.SqlQueryAsync("select * from [order];select * from OrderDetail"); + task10.Wait(); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UCodeFirst.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UCodeFirst.cs new file mode 100644 index 000000000..3c853a7d5 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UCodeFirst.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + public static void CodeFirst() + { + if (Db.DbMaintenance.IsAnyTable("UnitCodeTest1", false)) + Db.DbMaintenance.DropTable("UnitCodeTest1"); + Db.CodeFirst.InitTables(); + } + public class UnitCodeTest1 + { + [SqlSugar.SugarColumn(IndexGroupNameList = new string[] { "group1" })] + public int Id { get; set; } + [SqlSugar.SugarColumn(DefaultValue="getdate()", IndexGroupNameList =new string[] {"group1" } )] + public DateTime? CreateDate { get; set; } + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UJson.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UJson.cs new file mode 100644 index 000000000..8b8f664a0 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UJson.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + + public static void Json() + { + Db.CodeFirst.InitTables(); + Db.DbMaintenance.TruncateTable(); + Db.Insertable(new JsonTest() { Order = new Order { Id = 1, Name = "order1" } }).ExecuteCommand(); + var list = Db.Queryable().ToList(); + UValidate.Check("order1", list.First().Order.Name, "Json"); + Db.Updateable(new JsonTest() { Id = 1, Order = new Order { Id = 2, Name = "order2" } }).ExecuteCommand(); + list= Db.Queryable().ToList(); + UValidate.Check("order2", list.First().Order.Name, "Json"); + var list2 = Db.Queryable().ToList(); + } + } + + + public class JsonTest + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + [SqlSugar.SugarColumn(ColumnDataType = "varchar(max)", IsJson = true)] + public Order Order { get; set; } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UQueryable.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UQueryable.cs new file mode 100644 index 000000000..62bc9d064 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UQueryable.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + public static void Queryable() { + + var pageindex = 1; + var pagesize = 10; + var total = 0; + var totalPage = 0; + var list=Db.Queryable().ToPageList(pageindex, pagesize, ref total, ref totalPage); + + //Db.CodeFirst.InitTables(typeof(CarType)); + //Db.Updateable() + // .SetColumns(it => new CarType { State = SqlSugar.SqlFunc.IIF(it.State == true, false, true) }).Where(it => true) + // .ExecuteCommand(); + + //Db.CodeFirst.InitTables(typeof(TestTree)); + //Db.DbMaintenance.TruncateTable(); + //Db.Ado.ExecuteCommand("insert testtree values(hierarchyid::GetRoot(),geography :: STGeomFromText ('POINT(55.9271035250276 -3.29431266523898)',4326),'name')"); + //var list2 = Db.Queryable().ToList(); + + Db.CodeFirst.InitTables(); + Db.Queryable().Where(it => it.Id.HasValue).ToList(); + } + + + public class GuidTable + { + public Guid? Id { get; set; } + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UQueryableAsync.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UQueryableAsync.cs new file mode 100644 index 000000000..7ebce71e8 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UQueryableAsync.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; +namespace OrmTest +{ + public partial class NewUnitTest + { + public static void QueryableAsync() + { + q1(); + q2(); + q3(); + } + + private static void q1() + { + RefAsync total = 0; + var count = Db.Queryable().Count(); + Task t = Db.Queryable().ToPageListAsync(1, 2, total); + t.Wait(); + UValidate.Check(count, total.Value, "QueryableAsync"); + } + private static void q2() + { + RefAsync total = 0; + var count = Db.Queryable().Count(); + Task t = Db.Queryable().ToDataTablePageAsync(1, 2, total); + t.Wait(); + UValidate.Check(count, total.Value, "QueryableAsync"); + } + private static void q3() + { + RefAsync total = 0; + var count = Db.Queryable().Count(); + Task t = Db.Queryable().ToJsonPageAsync(1, 2, total); + t.Wait(); + UValidate.Check(count, total.Value, "QueryableAsync"); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UThread.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UThread.cs new file mode 100644 index 000000000..53998c258 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UThread.cs @@ -0,0 +1,378 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + + public static SqlSugarClient simpleDb => new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + 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))); + } + } + }); + public static SqlSugarClient ssDb => new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + IsShardSameThread = true, + AopEvents = new AopEvents + { + OnLogExecuting = (sql, p) => + { + Console.WriteLine(sql); + Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))); + } + } + }); + public static SqlSugarClient singleDb = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + 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))); + } + } + }); + public static SqlSugarClient singleAndSsDb = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + IsShardSameThread = true, + AopEvents = new AopEvents + { + OnLogExecuting = (sql, p) => + { + Console.WriteLine(sql); + Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))); + } + } + }); + public static void Thread() + { + Simple(); + IsShardSameThread(); + Single(); + SingleAndIsShardSameThread(); + SimpleAsync(); + IsShardSameThreadAsync(); + SingleAsync(); + SingleAndIsShardSameThreadAsync(); + + } + + private static void Simple() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void SingleAndIsShardSameThread() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleAndSsDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleAndSsDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleAndSsDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void Single() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void IsShardSameThread() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + Db.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + Db.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + Db.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + + + private static void SimpleAsync() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); ; + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void SingleAndIsShardSameThreadAsync() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleAndSsDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleAndSsDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleAndSsDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void SingleAsync() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + singleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void IsShardSameThreadAsync() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + Db.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + Db.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + Db.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UThread2.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UThread2.cs new file mode 100644 index 000000000..5fe4c12dd --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UThread2.cs @@ -0,0 +1,316 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + + public static void Thread2() + { + Simple2(); + IsShardSameThread2(); + Single2(); + SingleAndIsShardSameThread2(); + SimpleAsync2(); + IsShardSameThreadAsync2(); + SingleAsync2(); + SingleAndIsShardSameThreadAsync2(); + + } + + private static void Simple2() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void SingleAndIsShardSameThread2() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void Single2() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void IsShardSameThread2() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToList(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + + + private static void SimpleAsync2() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); ; + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void SingleAndIsShardSameThreadAsync2() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void SingleAsync2() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + + private static void IsShardSameThreadAsync2() + { + var t1 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(1); + } + + }); + var t2 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(10); + } + + }); + var t3 = new Task(() => + { + for (int i = 0; i < 100; i++) + { + simpleDb.Queryable().Take(10).ToListAsync().Wait(); + System.Threading.Thread.Sleep(6); + } + + }); + t1.Start(); + t2.Start(); + t3.Start(); + + Task.WaitAll(t1, t2, t3); + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UThread3.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UThread3.cs new file mode 100644 index 000000000..04794aeba --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UThread3.cs @@ -0,0 +1,117 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + + public static void Thread3() + { + Console.WriteLine("Thread3"); + SimpleAsync3(); + IsShardSameThreadAsync3(); + SingleAsync3(); + SingleAndIsShardSameThreadAsync3(); + + } + + + + private static async Task SimpleAsync3() + { + + for (int i = 0; i < 100; i++) + { + await simpleDb.Queryable().Take(10).ToListAsync(); + } + + for (int i = 0; i < 100; i++) + { + await simpleDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync(); + } + + List orders = new List(); + for (int i = 0; i < 100; i++) + { + orders = await simpleDb.Queryable().Take(10).ToListAsync(); + } + if (orders.Count > 0) + { + Console.WriteLine("async is ok"); + } + } + + private static async Task SingleAndIsShardSameThreadAsync3() + { + for (int i = 0; i < 100; i++) + { + await singleAndSsDb.Queryable().Take(10).ToListAsync(); + } + + for (int i = 0; i < 100; i++) + { + await singleAndSsDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync(); + } + List orders = new List(); + for (int i = 0; i < 100; i++) + { + orders = await singleAndSsDb.Queryable().Take(10).ToListAsync(); + } + if (orders.Count > 0) + { + Console.WriteLine("async is ok"); + } + } + + private static async Task SingleAsync3() + { + for (int i = 0; i < 100; i++) + { + await singleDb.Queryable().Take(10).ToListAsync(); + } + + for (int i = 0; i < 100; i++) + { + await singleDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync(); + } + + List orders = new List(); + for (int i = 0; i < 100; i++) + { + orders = await singleDb.Queryable().Take(10).ToListAsync(); + } + if (orders.Count > 0) + { + Console.WriteLine("async is ok"); + } + } + + private static async Task IsShardSameThreadAsync3() + { + for (int i = 0; i < 100; i++) + { + await ssDb.Queryable().Take(10).ToListAsync(); + } + + for (int i = 0; i < 100; i++) + { + await ssDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync(); + } + + List orders = new List(); + for (int i = 0; i < 100; i++) + { + orders = await ssDb.Queryable().Take(10).ToListAsync(); + } + if (orders.Count > 0) + { + Console.WriteLine("async is ok"); + } + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/UValidate.cs b/Src/Asp.Net/PgSqlTest/UnitTest/UValidate.cs new file mode 100644 index 000000000..c766e9b02 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/UValidate.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UValidate + { + public static void Check(object a, object b, object name) + { + if (a?.ToString() != b?.ToString()) + { + throw new Exception(name + " error"); + } + } + } +} diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/Updateable.cs b/Src/Asp.Net/PgSqlTest/UnitTest/Updateable.cs new file mode 100644 index 000000000..881eca2da --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/Updateable.cs @@ -0,0 +1,90 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + public static void Updateable() + { + Db.CodeFirst.InitTables(typeof(SYS_USER)); + Db.DbMaintenance.TruncateTable(); + Db.Insertable(new SYS_USER() { USER_ID=1,USER_ACCOUNT = "a", USER_PWD = "b", USER_NAME = "c", PWD_LASTCHTIME = DateTime.Now, PWD_ERRORCOUNT = 1, PWD_LASTERRTIME = DateTime.Now }).ExecuteCommand(); + Db.Updateable(new SYS_USER() { USER_ID=1, PWD_LASTERRTIME = null }).WhereColumns(it=> new{ it.PWD_ERRORCOUNT, it.PWD_LASTERRTIME }).ExecuteCommand(); + + } + } + /// + /// 普通用户表 + /// + [Serializable] + public class SYS_USER + { + private System.Int64? _USER_ID; + /// + /// GUID主键 + /// + [SugarColumn(IsPrimaryKey = true, IsIdentity = false)] + public System.Int64? USER_ID { get { return this._USER_ID; } set { this._USER_ID = value; } } + + private System.String _USER_ACCOUNT; + /// + /// 用户账号,不可重名,即使是假删除了,亦不可重复 + /// + public System.String USER_ACCOUNT { get { return this._USER_ACCOUNT; } set { this._USER_ACCOUNT = value; } } + + private System.String _USER_PWD; + /// + /// 用户密码 + /// + public System.String USER_PWD + { + get { return this._USER_PWD; } + set { this._USER_PWD = value; } + } + /// + /// 不允许用户密码序列化,可以反序列化 + /// + /// + public bool ShouldSerializeUSER_PWD() + { + return false; + } + + private System.String _USER_NAME; + /// + /// 用户姓名 + /// + + public System.String USER_NAME { get { return this._USER_NAME; } set { this._USER_NAME = value; } } + + private System.Int32 _USER_STATUS; + /// + /// 用户状体:10正常;20锁定;99已删除 + /// + public System.Int32 USER_STATUS { get { return this._USER_STATUS; } set { this._USER_STATUS = value; } } + + private System.DateTime _PWD_LASTCHTIME; + /// + /// 最后一次密码更新时间 + /// + public System.DateTime PWD_LASTCHTIME { get { return this._PWD_LASTCHTIME; } set { this._PWD_LASTCHTIME = value; } } + + private System.Int32? _PWD_ERRORCOUNT; + /// + /// 密码错误次数,达到定义的次数就锁定 + /// + public System.Int32? PWD_ERRORCOUNT { get { return this._PWD_ERRORCOUNT; } set { this._PWD_ERRORCOUNT = value ?? 0; } } + + private System.DateTime? _PWD_LASTERRTIME = null; + /// + /// 密码最后一次错误时间,满足定义的时间差之后,可自动解锁,如20分钟后自动解锁,亦作为累次错误次数的时间差比对基础 + /// 允许为空 + /// + public System.DateTime? PWD_LASTERRTIME { get { return this._PWD_LASTERRTIME; } set { this._PWD_LASTERRTIME = value; } } + } +}