Browse Source

Update exp to sql

pull/16/MERGE
sunkaixuan 3 years ago
parent
commit
c4ea537902
  1. 1
      Src/Asp.Net/SqlServerTest/SqlServerTest.csproj
  2. 1
      Src/Asp.Net/SqlServerTest/UnitTest/Main.cs
  3. 24
      Src/Asp.Net/SqlServerTest/UnitTest/UCustom017.cs
  4. 60
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs

1
Src/Asp.Net/SqlServerTest/SqlServerTest.csproj

@ -99,6 +99,7 @@
<Compile Include="UnitTest\Models\TestModel.cs" />
<Compile Include="UnitTest\Models\UserEntity.cs" />
<Compile Include="UnitTest\Models\UserRoleEntity.cs" />
<Compile Include="UnitTest\UCustom017.cs" />
<Compile Include="UnitTest\UCustom016.cs" />
<Compile Include="UnitTest\UCustom015.cs" />
<Compile Include="UnitTest\UCustom014.cs" />

1
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();

24
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<Order>()
.Where(it => it.Id==SqlFunc.Subqueryable<Order>().Where(z=>z.Id==it.Id).GroupBy(z=>z.Id).Select(x=>x.Id) )
.ToList();
}
}
}

60
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;
}
}
}

Loading…
Cancel
Save