From c4ea537902527c97967d8e63e7497d8525516a8c Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 15 Apr 2022 23:52:19 +0800 Subject: [PATCH] Update exp to sql --- .../SqlServerTest/SqlServerTest.csproj | 1 + Src/Asp.Net/SqlServerTest/UnitTest/Main.cs | 1 + .../SqlServerTest/UnitTest/UCustom017.cs | 24 ++++++++ .../ResolveItems/BinaryExpressionResolve.cs | 60 +++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 Src/Asp.Net/SqlServerTest/UnitTest/UCustom017.cs diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 1140804a6..d3b925018 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -99,6 +99,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs index 9cb5f183e..34ce5477d 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + UCustom017.Init(); UCustom016.Init(); UCustom015.Init(); UCustom014.Init(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom017.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom017.cs new file mode 100644 index 000000000..38c14d154 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom017.cs @@ -0,0 +1,24 @@ +using OrmTest.UnitTest.Models; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UCustom017 + { + + public static void Init() + { + var db = NewUnitTest.Db; + db.Queryable() + .Where(it => it.Id==SqlFunc.Subqueryable().Where(z=>z.Id==it.Id).GroupBy(z=>z.Id).Select(x=>x.Id) ) + .ToList(); + + } + + } +} diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs index e8634d07e..035b689ee 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; +using System.Text.RegularExpressions; + namespace SqlSugar { public class BinaryExpressionResolve : BaseResolve @@ -26,6 +28,13 @@ namespace SqlSugar { var expression = this.Expression as BinaryExpression; var operatorValue = parameter.OperatorValue = ExpressionTool.GetOperator(expression.NodeType); + + if (IsGroupSubquery(expression.Right,operatorValue)) + { + InSubGroupBy(expression); + return; + } + var isEqual = expression.NodeType == ExpressionType.Equal; var isComparisonOperator = ExpressionTool.IsComparisonOperator(expression); base.ExactExpression = expression; @@ -88,5 +97,56 @@ namespace SqlSugar base.Context.Result.Append(" " + ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index + " "); } } + + + private void InSubGroupBy(BinaryExpression expression) + { + var leftSql = GetNewExpressionValue(expression.Left); + var rightExpression = expression.Right as MethodCallExpression; + var selector = GetNewExpressionValue(rightExpression.Arguments[0]); + var rightSql = GetNewExpressionValue(rightExpression.Object).Replace("SELECT FROM", $"SELECT {selector} FROM"); + if (this.Context.IsSingle&&this.Context.SingleTableNameSubqueryShortName==null) + { + var leftExp = expression.Left; + if (leftExp is UnaryExpression) + { + leftExp = (leftExp as UnaryExpression).Operand; + } + var p = (leftExp as MemberExpression); + this.Context.SingleTableNameSubqueryShortName=p.Expression.ToString(); + } + base.Context.Result.Append($" {leftSql} in ({rightSql}) "); + } + + private bool IsGroupSubquery(Expression rightExpression, string operatorValue) + { + if (operatorValue != "=") + { + return false; + } + if (rightExpression == null) + { + return false; + } + if ((rightExpression is MethodCallExpression) == false) + { + return false; + } + var method = (rightExpression as MethodCallExpression); + if (method.Method.Name != "Select") + { + return false; + } + var methodString = method.ToString(); + if (methodString.IndexOf("GroupBy(")==0) + { + return false; + } + if (Regex.Matches(methodString, @"Subqueryable\(").Count!=1) + { + return false; + } + return true; + } } }