From cf4ec53150560d92fcb21b816d54ec70ad44c23a Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Sat, 25 Dec 2021 14:25:48 +0800 Subject: [PATCH] Update exp to sql --- .../SqlServerTest/SqlServerTest.csproj | 1 + Src/Asp.Net/SqlServerTest/UnitTest/Main.cs | 1 + .../SqlServerTest/UnitTest/UCustom02.cs | 191 ++++++++++++++++++ .../Subquery/Items/SubWhere.cs | 5 + .../ExpressionsToSql/Subquery/SubResolve.cs | 6 + 5 files changed, 204 insertions(+) create mode 100644 Src/Asp.Net/SqlServerTest/UnitTest/UCustom02.cs diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index d42947445..b14d50feb 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -95,6 +95,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs index 7fa771223..be4650733 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs @@ -32,6 +32,7 @@ namespace OrmTest public static void Init() { UCustom01.Init(); + UCustom02.Init(); SubQueryTest(); UConfig(); DeleteTest(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom02.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom02.cs new file mode 100644 index 000000000..f245a1113 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/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.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhere.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhere.cs index 0a231acf7..8010b8387 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhere.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhere.cs @@ -40,10 +40,15 @@ namespace SqlSugar public string GetValue(Expression expression) { var exp = expression as MethodCallExpression; + if (Regex.Matches( expression.ToString(), "Subqueryable").Count >= 2) + { + new SubSelect() { Context = this.Context }.SetShortName(exp, "+"); + } var argExp = exp.Arguments[0]; var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple); + var regex = @"^WHERE (\@Const\d+) $"; if (this.Context is OracleExpressionContext) { diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs index dcdd2f1a2..2e3ab1e6c 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs @@ -148,8 +148,14 @@ namespace SqlSugar private List GetSubItems() { + var isSubSubQuery = this.allMethods.Select(it => it.ToString()).Any(it => Regex.Matches(it, "Subquery").Count > 1); var isubList = this.allMethods.Select(exp => { + if (isSubSubQuery) + { + this.context.JoinIndex = 1; + this.context.SubQueryIndex = 0; + } var methodName = exp.Method.Name; var items = SubTools.SubItems(this.context); var item = items.First(s => s.Name == methodName);