diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs index 97d7a88b6..e82eb7912 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/Main.cs @@ -31,6 +31,9 @@ namespace OrmTest } public static void Init() { + UCustom01.Init(); + UCustom02.Init(); + UCustom03.Init(); Bulk(); Filter(); Insert(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UCustom01.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UCustom01.cs new file mode 100644 index 000000000..dc40e4a92 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UCustom01.cs @@ -0,0 +1,68 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UCustom01 + { + + public static void Init() + { + var db = new SqlSugarScope(new SqlSugar.ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true + }); + db.Aop.OnLogExecuted = (s, p) => + { + Console.WriteLine(s); + }; + //建表 + if (!db.DbMaintenance.IsAnyTable("User_Test001", false)) + { + db.CodeFirst.InitTables(); + } + if (!db.DbMaintenance.IsAnyTable("UserRole_Test001", false)) + { + db.CodeFirst.InitTables(); + } + + //用例代码 + var result = db.Queryable((u, ur) => new object[] { + + JoinType.Left,u.ID==ur.UserID + + + + }).Select((u, ur) => new + + { + + customName = SqlFunc.Subqueryable().Where(s => s.UserName == u.UserName).Select(s => s.UserName+"") + + + + }).ToPageList(1, 10); + + } + [SugarTable("unitUser_Test001")] + public class User_Test001 + { + + public int ID { get; set; } + public string UserName { get; set; } + } + [SugarTable("unitUserRole_Test001")] + public class UserRole_Test001 + { + + public int ID { get; set; } + public int UserID { get; set; } + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UCustom02.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UCustom02.cs new file mode 100644 index 000000000..f245a1113 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UCustom02.cs @@ -0,0 +1,191 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; +namespace OrmTest +{ + [SugarTable("UnitTest0")] + + public class Test0 + + { + + [SugarColumn(IsNullable = false, IsPrimaryKey = true)] + + public long Id { get; set; } + + + + public string Title { get; set; } + + } + + [SugarTable("UnitTest_1")] + + public class Test1 + + { + + [SugarColumn(IsNullable = false, IsPrimaryKey = true)] + + public long Id { get; set; } + + + + [SugarColumn(ColumnName = "test0id")] + + public long Test0Id { get; set; } + + + + public string Title1 { get; set; } + + } + + + + [SugarTable("UnitTest_2")] + + public class Test2 + + { + + [SugarColumn(IsNullable = false, IsPrimaryKey = true)] + + public long Id { get; set; } + + + + [SugarColumn(ColumnName = "test0i_d")] + + public long Test0Id { get; set; } + + + + public string Title2 { get; set; } + + } + + + + [SugarTable("UnitTest_3")] + + public class Test3 + + { + + [SugarColumn(IsNullable = false, IsPrimaryKey = true)] + + public long Id { get; set; } + + + + [SugarColumn(ColumnName = "test0_id")] + + public long Test0Id { get; set; } + + + + public string Title3 { get; set; } + + } + + + + public class UCustom02 + + { + + public static void Init() + + { + + + var db = NewUnitTest.Db; ; + + + //用来打印Sql方便你调式 + + db.Aop.OnLogExecuting = (sql, pars) => + + { + + Console.WriteLine(sql + "\r\n" + + + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); + + Console.WriteLine(); + + }; + + db.Aop.OnError = (sql) => + + { + + string ss = sql.Sql; + + }; + + db.Aop.OnLogExecuting = (sql, p) => + + { + + Console.WriteLine(sql); + + }; + + db.Aop.OnLogExecuted = (sql, p) => + + { + + Console.WriteLine(sql); + + }; + + + + db.CodeFirst.InitTables(typeof(Test0), typeof(Test1), typeof(Test2), typeof(Test3)); + + + + db.Deleteable().ExecuteCommand(); + + db.Deleteable().ExecuteCommand(); + + db.Deleteable().ExecuteCommand(); + + db.Deleteable().ExecuteCommand(); + + db.Insertable(new Test0 { Id = 1, Title = "111111" }).ExecuteCommand(); + + db.Insertable(new Test1 { Id = 1, Test0Id = 1, Title1 = "111111" }).ExecuteCommand(); + + db.Insertable(new Test2 { Id = 1, Test0Id = 1, Title2 = "222222222" }).ExecuteCommand(); + + db.Insertable(new Test3 { Id = 1, Test0Id = 1, Title3 = "333333333" }).ExecuteCommand(); + + + + var cacheQueryT = db.Queryable().Select(x => new + + { + + id = x.Id, + + title = x.Title, + + title1 = + SqlFunc.Subqueryable().Where(w => w.Test0Id == x.Id && + SqlFunc.Subqueryable().Where(t => t.Test0Id == w.Test0Id && + SqlFunc.Subqueryable().Where(t1 => t1.Test0Id == w.Test0Id).Any()).Any()).Select(w => w.Title1) + + }).ToList(); + + } + + } + + +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UCustom03.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UCustom03.cs new file mode 100644 index 000000000..4de32b618 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/UnitTest/UCustom03.cs @@ -0,0 +1,312 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; +namespace OrmTest +{ + public class UCustom03 + { + public static void Init() + { + Demo8(); + Demo7(); + Demo6(); + Demo5(); + Demo4(); + Demo3(); + Demo2(); + Demo1(); + } + + private static void Demo5() + { + var db = NewUnitTest.Db; + List conModels = new List(); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NoEqual, FieldValue = "1" }); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNot, FieldValue = null }); + var json = db.Context.Utilities.SerializeObject(conModels); + var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json); + var list6 = db.Queryable().Where(conditionalModels).ToList(); + var json2 = db.Context.Utilities.SerializeObject(conditionalModels); + if (json != json2) + { + throw new Exception("unit error"); + } + } + + private static void Demo1() + { + var db = NewUnitTest.Db; + List conModels = new List(); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NoEqual, FieldValue = "1" }); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNot, FieldValue = null });// id is not null + + conModels.Add(new ConditionalTree() + { + ConditionalList = new List>()// (id=1 or id=2 and id=1) + { + //new KeyValuePair( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }), + new KeyValuePair (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + new KeyValuePair ( WhereType.And, new ConditionalTree(){ + ConditionalList=new List>(){ + new KeyValuePair(WhereType.And,new ConditionalModel(){ + FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" + }) + + } + }) + } + }); + var json = db.Context.Utilities.SerializeObject(conModels); + var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json); + var list6 = db.Queryable().Where(conditionalModels).ToList(); + var json2 = db.Context.Utilities.SerializeObject(conditionalModels); + if (json != json2) + { + throw new Exception("unit error"); + } + + + } + + private static void Demo2() + { + var db = NewUnitTest.Db; + List conModels = new List(); + conModels.Add(new ConditionalTree() + { + ConditionalList = new List>()// (id=1 or id=2 and id=1) + { + //new KeyValuePair( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }), + new KeyValuePair (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + new KeyValuePair ( WhereType.And, new ConditionalTree(){ + ConditionalList=new List>(){ + new KeyValuePair(WhereType.And,new ConditionalModel(){ + FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" + }) + + } + }) + } + }); + + var json = db.Context.Utilities.SerializeObject(conModels); + var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json); + var list6 = db.Queryable().Where(conditionalModels).ToList(); + var json2 = db.Context.Utilities.SerializeObject(conditionalModels); + if (json != json2) + { + throw new Exception("unit error"); + } + + + } + + + private static void Demo3() + { + var db = NewUnitTest.Db; + List conModels = new List(); + conModels.Add(new ConditionalTree() + { + ConditionalList = new List>()// (id=1 or id=2 and id=1) + { + //new KeyValuePair( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }), + new KeyValuePair (WhereType.Null,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + new KeyValuePair ( WhereType.And, new ConditionalTree(){ + ConditionalList=new List>(){ + new KeyValuePair(WhereType.Null,new ConditionalModel(){ + FieldName="ID", ConditionalType=ConditionalType.Equal, FieldValue="1" + }), + new KeyValuePair(WhereType.Or,new ConditionalModel(){ + FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" + }) + + } + }) + } + }); + var json = db.Context.Utilities.SerializeObject(conModels); + var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json); + var list6 = db.Queryable().Where(conditionalModels).ToList(); + var json2 = db.Context.Utilities.SerializeObject(conditionalModels); + if (json != json2) + { + throw new Exception("unit error"); + } + + } + + private static void Demo4() + { + var db = NewUnitTest.Db; + List conModels = new List(); + conModels.Add(new ConditionalTree() + { + ConditionalList = new List>()// (id=1 or id=2 and id=1) + { + //new KeyValuePair( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }), + new KeyValuePair (WhereType.Null,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + new KeyValuePair (WhereType.And,new ConditionalModel() { FieldName = "name", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + new KeyValuePair ( + WhereType.And, new ConditionalTree(){ + ConditionalList=new List>() + { + new KeyValuePair(WhereType.Null,new ConditionalModel(){ + FieldName="price", ConditionalType=ConditionalType.Equal, FieldValue="1" + }), + new KeyValuePair(WhereType.And,new ConditionalModel(){ + FieldName = "CustomId", ConditionalType = ConditionalType.Equal, FieldValue = "1" + }) + + } + }) + } + }); + var json = db.Context.Utilities.SerializeObject(conModels); + + var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json); + var list6 = db.Queryable().Where(conditionalModels).ToList(); + + var json2 = db.Context.Utilities.SerializeObject(conditionalModels); + + if (json != json2) + { + throw new Exception("unit error"); + } + } + + private static void Demo6() + { + var db = NewUnitTest.Db; + List conModels = new List(); + conModels.Add(new ConditionalTree() + { + ConditionalList = new List>()// (id=1 or id=2 and id=1) + { + //new KeyValuePair( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }), + new KeyValuePair (WhereType.Null,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + new KeyValuePair (WhereType.And,new ConditionalModel() { FieldName = "name", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + new KeyValuePair ( + WhereType.And, new ConditionalTree(){ + ConditionalList=new List>() + { + new KeyValuePair(WhereType.Null,new ConditionalModel(){ + FieldName="price", ConditionalType=ConditionalType.Equal, FieldValue="1" + }), + new KeyValuePair(WhereType.And,new ConditionalModel(){ + FieldName = "CustomId", ConditionalType = ConditionalType.Equal, FieldValue = "1" + }) + + } + }) + } + }); + var json = db.Context.Utilities.SerializeObject(conModels); + + var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json); + var list6 = db.Queryable().Where(it=>it.Id==1).Where(conditionalModels).ToList(); + + var json2 = db.Context.Utilities.SerializeObject(conditionalModels); + + if (json != json2) + { + throw new Exception("unit error"); + } + } + + private static void Demo7() + { + var db = NewUnitTest.Db; + List conModels = new List(); + conModels.Add(new ConditionalTree() + { + ConditionalList = new List>()// (id=1 or id=2 and id=1) + { + //new KeyValuePair( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }), + new KeyValuePair (WhereType.Null,new ConditionalModel() { FieldName = "name", ConditionalType = ConditionalType.Equal, FieldValue = "name" }), + + new KeyValuePair ( WhereType.And, new ConditionalTree(){ + ConditionalList=new List>(){ + new KeyValuePair(WhereType.Null,new ConditionalModel(){ + FieldName="customid", ConditionalType=ConditionalType.Equal, FieldValue="1" + }), + new KeyValuePair(WhereType.Or,new ConditionalModel(){ + FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" + }), + new KeyValuePair(WhereType.Or,new ConditionalTree(){ + ConditionalList=new List>(){ + new KeyValuePair(WhereType.Null,new ConditionalModel(){ + FieldName="customid", ConditionalType=ConditionalType.Equal, FieldValue="1" + } + + ), + + } + }) + + + } + }), + new KeyValuePair (WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + } + }); + var json = db.Context.Utilities.SerializeObject(conModels); + var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json); + var list6 = db.Queryable().Where(conditionalModels).ToList(); + var json2 = db.Context.Utilities.SerializeObject(conditionalModels); + if (json != json2) + { + throw new Exception("unit error"); + } + } + + private static void Demo8() + { + var db = NewUnitTest.Db; + List conModels = new List(); + conModels.Add(new ConditionalTree() + { + ConditionalList = new List>()// (id=1 or id=2 and id=1) + { + //new KeyValuePair( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }), + new KeyValuePair (WhereType.Null,new ConditionalModel() { FieldName = "name", ConditionalType = ConditionalType.Equal, FieldValue = "name" }), + + new KeyValuePair ( WhereType.And, new ConditionalTree(){ + ConditionalList=new List>(){ + new KeyValuePair(WhereType.Null,new ConditionalModel(){ + FieldName="customid", ConditionalType=ConditionalType.Equal, FieldValue="1" + }), + new KeyValuePair(WhereType.And,new ConditionalModel(){ + FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" + }), + new KeyValuePair(WhereType.Or,new ConditionalTree(){ + ConditionalList=new List>(){ + new KeyValuePair(WhereType.Null,new ConditionalModel(){ + FieldName="customid", ConditionalType=ConditionalType.Equal, FieldValue="1" + } + + ), + + } + }) + + + } + }), + new KeyValuePair (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), + } + }); + var json = db.Context.Utilities.SerializeObject(conModels); + var conditionalModels = db.Context.Utilities.JsonToConditionalModels(json); + var list6 = db.Queryable().Where(conditionalModels).ToList(); + var json2 = db.Context.Utilities.SerializeObject(conditionalModels); + if (json != json2) + { + throw new Exception("unit error"); + } + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/ContextMethods.cs index d4442f5a0..db992fb85 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/ContextMethods.cs @@ -719,7 +719,7 @@ namespace SqlSugar { IConditionalModel conditionalModel = new ConditionalModel() { - ConditionalType = (ConditionalType)Convert.ToInt32(item["ConditionalType"]), + ConditionalType = (ConditionalType)Convert.ToInt32(item["ConditionalType"].Value()), FieldName = item["FieldName"] + "", FieldValue = item["FieldValue"].Value()==null?null: item["FieldValue"].ToString() }; @@ -735,7 +735,7 @@ namespace SqlSugar var values = item.Values().First(); foreach (var jToken in values) { - WhereType type = (WhereType)Convert.ToInt32(jToken["Key"]); + WhereType type = (WhereType)Convert.ToInt32(jToken["Key"].Value()); IConditionalModel conditionalModel = null; var value = jToken["Value"]; if (value.ToString().Contains("ConditionalList")) @@ -749,7 +749,7 @@ namespace SqlSugar { conditionalModel = new ConditionalModel() { - ConditionalType = (ConditionalType)Convert.ToInt32(value["ConditionalType"]), + ConditionalType = (ConditionalType)Convert.ToInt32(value["ConditionalType"].Value()), FieldName = value["FieldName"] + "", FieldValue = value["FieldValue"].Value() == null ? null : value["FieldValue"].ToString() };