mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
New Expression BUG
This commit is contained in:
parent
6386133359
commit
e3ee431c91
@ -18,6 +18,7 @@ namespace OrmTest
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// /***Unit Test***/
|
||||
new Select(1).Init();
|
||||
new Field(1).Init();
|
||||
new Where(1).Init();
|
||||
new Method(1).Init();
|
||||
|
@ -33,15 +33,18 @@ namespace OrmTest.UnitTest
|
||||
{
|
||||
Expression<Func<Student, School, object>> exp = (it, school) => new Student() { Name = "a", Id = it.Id, SchoolId = school.Id, TestId = it.Id + 1 };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.IsSingle = false;
|
||||
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(
|
||||
selectorValue,
|
||||
pars,
|
||||
@" @constant1 AS Name , it.Id AS Id , school.Id AS SchoolId , ( it.Id + 1 ) AS TestId ",
|
||||
@" @constant0 AS [Name] , [it].[Id] AS [Id] , [school].[Id] AS [SchoolId] , ( [it].[Id] + @Id1 ) AS [TestId] ",
|
||||
new List<SugarParameter>(){
|
||||
new SugarParameter("@constant1","a")},
|
||||
new SugarParameter("@constant0","a"),
|
||||
new SugarParameter("@Id1",1)
|
||||
},
|
||||
"Select.Multiple Error");
|
||||
}
|
||||
|
||||
@ -49,21 +52,23 @@ namespace OrmTest.UnitTest
|
||||
{
|
||||
Expression<Func<Student, School, object>> exp = (it, school) => new { Name = "a", Id = it.Id / 2, SchoolId = school.Id };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.IsSingle = false;
|
||||
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(
|
||||
selectorValue,
|
||||
pars,
|
||||
@" @constant1 AS Name , ( it.Id / 2 ) AS Id , school.Id AS SchoolId ",
|
||||
@" @constant0 AS [Name] , ( [it].[Id] / @Id1 ) AS [Id] , [school].[Id] AS [SchoolId] ",
|
||||
new List<SugarParameter>(){
|
||||
new SugarParameter("@constant1","a")},
|
||||
new SugarParameter("@constant0","a"),
|
||||
new SugarParameter("@Id1", 2)},
|
||||
"Select.MultipleDynamic Error");
|
||||
}
|
||||
private void single()
|
||||
{
|
||||
int p = 1;
|
||||
Expression<Func<Student, object>> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p,TestId=it.Id+1 };
|
||||
Expression<Func<Student, object>> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p,TestId=it.Id+11 };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
@ -71,17 +76,18 @@ namespace OrmTest.UnitTest
|
||||
base.Check(
|
||||
selectorValue,
|
||||
pars,
|
||||
@" @constant1 AS Name , Id AS Id , @constant3 AS SchoolId , ( Id + 1 ) AS TestId ",
|
||||
@" @constant0 AS [Name] , [Id] AS [Id] , @constant1 AS [SchoolId] , ( [Id] + @Id2 ) AS [TestId] ",
|
||||
new List<SugarParameter>(){
|
||||
new SugarParameter("@constant1","a"),
|
||||
new SugarParameter("@constant3",1)},
|
||||
new SugarParameter("@constant0","a"),
|
||||
new SugarParameter("@constant1",1),
|
||||
new SugarParameter("@Id2",11 ) },
|
||||
"Select.single Error");
|
||||
}
|
||||
|
||||
private void singleDynamic()
|
||||
{
|
||||
string a = "a";
|
||||
Expression<Func<Student, object>> exp = it => new { x = it.Id, shoolid = 1, name = a,p=it.Id*1 };
|
||||
Expression<Func<Student, object>> exp = it => new { x = it.Id, shoolid = 1, name = a,p=it.Id*2 };
|
||||
ExpressionContext expContext = new ExpressionContext();
|
||||
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
|
||||
var selectorValue = expContext.Result.GetString();
|
||||
@ -89,11 +95,12 @@ namespace OrmTest.UnitTest
|
||||
base.Check(
|
||||
selectorValue,
|
||||
pars,
|
||||
@" Id AS x , @constant2 AS shoolid , @constant3 AS name , ( Id * 1 ) AS p ",
|
||||
@" [Id] AS [x] , @constant0 AS [shoolid] , @constant1 AS [name] , ( [Id] * @Id2 ) AS [p] ",
|
||||
new List<SugarParameter>(){
|
||||
new SugarParameter("@constant2",1),
|
||||
new SugarParameter("@constant3","a")},
|
||||
"Select.single Error");
|
||||
new SugarParameter("@constant0",1),
|
||||
new SugarParameter("@constant1","a"),
|
||||
new SugarParameter("@Id2",2)},
|
||||
"Select.singleDynamic Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -106,7 +106,8 @@ namespace SqlSugar
|
||||
}
|
||||
return null;
|
||||
}
|
||||
protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue) {
|
||||
protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue)
|
||||
{
|
||||
|
||||
Context.ParameterIndex++;
|
||||
if (isLeft == true)
|
||||
@ -200,7 +201,7 @@ namespace SqlSugar
|
||||
protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContext();
|
||||
newContext.Resolve(item, this.Context.IsJoin?ResolveExpressType.WhereMultiple:ResolveExpressType.WhereSingle);
|
||||
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.IsValuable())
|
||||
@ -215,13 +216,14 @@ namespace SqlSugar
|
||||
return methodCallExpressionArgs;
|
||||
}
|
||||
|
||||
protected void ResolveNewExpressions(ExpressionParameter parameter, int i, Expression item, string memberName)
|
||||
protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string memberName)
|
||||
{
|
||||
if (item.NodeType == ExpressionType.Constant || (item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + i;
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(memberName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
|
||||
}
|
||||
@ -231,7 +233,7 @@ namespace SqlSugar
|
||||
this.Start();
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(memberName, parameter.CommonTempData.ObjToString()));
|
||||
}
|
||||
else if (item is MemberExpression || item is UnaryExpression)
|
||||
else if (item is MemberExpression)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
@ -245,20 +247,68 @@ namespace SqlSugar
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
}
|
||||
else if (item is UnaryExpression && ((UnaryExpression)item).Operand is MemberExpression)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
var expression = ((UnaryExpression)item).Operand as MemberExpression;
|
||||
if (expression.Expression == null)
|
||||
{
|
||||
this.Context.Result.CurrentParameter = parameter;
|
||||
this.Context.Result.IsLockCurrentParameter = true;
|
||||
parameter.IsAppendTempDate();
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
parameter.IsAppendResult();
|
||||
this.Context.Result.Append(this.Context.GetAsString(memberName, parameter.CommonTempData.ObjToString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
else if (expression.Expression is ConstantExpression)
|
||||
{
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(memberName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, ExpressionTool.GetMemberValue(expression.Member, expression)));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.CurrentParameter = parameter;
|
||||
this.Context.Result.IsLockCurrentParameter = true;
|
||||
parameter.IsAppendTempDate();
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
parameter.IsAppendResult();
|
||||
this.Context.Result.Append(this.Context.GetAsString(memberName, parameter.CommonTempData.ObjToString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (item is UnaryExpression && ((UnaryExpression)item).Operand is ConstantExpression)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
this.Expression = ((UnaryExpression)item).Operand;
|
||||
this.Start();
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(memberName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
|
||||
}
|
||||
}
|
||||
else if (item is BinaryExpression)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
this.Context.Result.CurrentParameter = parameter;
|
||||
this.Context.Result.IsLockCurrentParameter = true;
|
||||
parameter.IsAppendTempDate();
|
||||
this.Expression = item;
|
||||
parameter.CommonTempData = "simple";
|
||||
this.Start();
|
||||
parameter.CommonTempData = null;
|
||||
parameter.IsAppendResult();
|
||||
this.Context.Result.TrimEnd();
|
||||
this.Context.Result.Append(this.Context.GetAsString(memberName, parameter.CommonTempData.ObjToString()));
|
||||
var newContext = this.Context.GetCopyContext();
|
||||
var resolveExpressType = this.Context.IsSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple;
|
||||
newContext.Resolve(item, resolveExpressType);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.IsValuable())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
this.Context.Result.Append(this.Context.GetAsString(memberName, newContext.Result.GetString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
}
|
||||
@ -282,7 +332,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
var asName = this.Context.GetTranslationText(item.Type.Name + "." + property.Name );
|
||||
var asName = this.Context.GetTranslationText(item.Type.Name + "." + property.Name);
|
||||
var columnName = property.Name;
|
||||
if (Context.IsJoin)
|
||||
{
|
||||
@ -298,7 +348,6 @@ namespace SqlSugar
|
||||
else
|
||||
{
|
||||
Check.ThrowNotSupportedException(item.GetType().Name);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,60 +9,51 @@ namespace SqlSugar
|
||||
{
|
||||
public BinaryExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
if (parameter.BaseParameter.CommonTempData != null && parameter.BaseParameter.CommonTempData.Equals("simple"))
|
||||
var expression = this.Expression as BinaryExpression;
|
||||
var operatorValue = parameter.OperatorValue = ExpressionTool.GetOperator(expression.NodeType);
|
||||
var isEqual = expression.NodeType == ExpressionType.Equal;
|
||||
var isComparisonOperator = ExpressionTool.IsComparisonOperator(expression);
|
||||
base.ExactExpression = expression;
|
||||
var leftExpression = expression.Left;
|
||||
var rightExpression = expression.Right;
|
||||
var leftIsBinary = leftExpression is BinaryExpression;
|
||||
var rightBinary = rightExpression is BinaryExpression;
|
||||
var lbrs = leftIsBinary && !rightBinary;
|
||||
var lsrb = !leftIsBinary && rightBinary;
|
||||
var lbrb = rightBinary && leftIsBinary;
|
||||
var lsbs = !leftIsBinary && !rightBinary;
|
||||
var isAppend = !base.Context.Result.Contains(ExpressionConst.Format0);
|
||||
if (isAppend)
|
||||
{
|
||||
parameter.BaseParameter = parameter;
|
||||
new SimpleBinaryExpressionResolve(parameter);
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
base.Context.Result.Append(ExpressionConst.Format3);
|
||||
base.Context.Result.Append(ExpressionConst.Format0);
|
||||
}
|
||||
else
|
||||
{
|
||||
var expression = this.Expression as BinaryExpression;
|
||||
var operatorValue = parameter.OperatorValue = ExpressionTool.GetOperator(expression.NodeType);
|
||||
var isEqual = expression.NodeType == ExpressionType.Equal;
|
||||
var isComparisonOperator =ExpressionTool.IsComparisonOperator(expression);
|
||||
base.ExactExpression = expression;
|
||||
var leftExpression = expression.Left;
|
||||
var rightExpression = expression.Right;
|
||||
var leftIsBinary = leftExpression is BinaryExpression;
|
||||
var rightBinary = rightExpression is BinaryExpression;
|
||||
var lbrs = leftIsBinary && !rightBinary;
|
||||
var lsrb = !leftIsBinary && rightBinary;
|
||||
var lbrb = rightBinary && leftIsBinary;
|
||||
var lsbs = !leftIsBinary && !rightBinary;
|
||||
var isAppend = !base.Context.Result.Contains(ExpressionConst.Format0);
|
||||
if (isAppend)
|
||||
{
|
||||
base.Context.Result.Append(ExpressionConst.Format3);
|
||||
base.Context.Result.Append(ExpressionConst.Format0);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format0, ExpressionConst.Format3 + ExpressionConst.Format0);
|
||||
}
|
||||
parameter.LeftExpression = leftExpression;
|
||||
parameter.RightExpression = rightExpression;
|
||||
base.Expression = leftExpression;
|
||||
base.IsLeft = true;
|
||||
base.Start();
|
||||
base.IsLeft = false;
|
||||
base.Expression = rightExpression;
|
||||
base.Start();
|
||||
base.IsLeft = null;
|
||||
if (lsbs && parameter.ValueIsNull)
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format1 + parameter.Index, isEqual ? "IS" : "IS NOT");
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format1 + parameter.Index, operatorValue);
|
||||
base.Context.Result.Replace(ExpressionConst.Format1 + (parameter.Index + 1), operatorValue);
|
||||
}
|
||||
base.Context.Result.Append(ExpressionConst.Format4);
|
||||
if (parameter.BaseExpression is BinaryExpression && parameter.IsLeft == true)
|
||||
{
|
||||
base.Context.Result.Append(" " + ExpressionConst.Format1 + parameter.BaseParameter.Index + " ");
|
||||
}
|
||||
base.Context.Result.Replace(ExpressionConst.Format0, ExpressionConst.Format3 + ExpressionConst.Format0);
|
||||
}
|
||||
parameter.LeftExpression = leftExpression;
|
||||
parameter.RightExpression = rightExpression;
|
||||
base.Expression = leftExpression;
|
||||
base.IsLeft = true;
|
||||
base.Start();
|
||||
base.IsLeft = false;
|
||||
base.Expression = rightExpression;
|
||||
base.Start();
|
||||
base.IsLeft = null;
|
||||
if (lsbs && parameter.ValueIsNull)
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format1 + parameter.Index, isEqual ? "IS" : "IS NOT");
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format1 + parameter.Index, operatorValue);
|
||||
base.Context.Result.Replace(ExpressionConst.Format1 + (parameter.Index + 1), operatorValue);
|
||||
}
|
||||
base.Context.Result.Append(ExpressionConst.Format4);
|
||||
if (parameter.BaseExpression is BinaryExpression && parameter.IsLeft == true)
|
||||
{
|
||||
base.Context.Result.Append(" " + ExpressionConst.Format1 + parameter.BaseParameter.Index + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,10 +87,8 @@ namespace SqlSugar
|
||||
|
||||
private void Select(MemberInitExpression expression, ExpressionParameter parameter, bool isSingle)
|
||||
{
|
||||
int i = 0;
|
||||
foreach (MemberBinding binding in expression.Bindings)
|
||||
{
|
||||
++i;
|
||||
if (binding.BindingType != MemberBindingType.Assignment)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
@ -98,7 +96,7 @@ namespace SqlSugar
|
||||
MemberAssignment memberAssignment = (MemberAssignment)binding;
|
||||
var memberName = memberAssignment.Member.Name;
|
||||
var item = memberAssignment.Expression;
|
||||
ResolveNewExpressions(parameter, i, item, memberName);
|
||||
ResolveNewExpressions(parameter, item, memberName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ namespace SqlSugar
|
||||
{
|
||||
string memberName = expression.Members[i].Name;
|
||||
++i;
|
||||
ResolveNewExpressions(parameter, i, item, memberName);
|
||||
ResolveNewExpressions(parameter,item, memberName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,40 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq.Expressions;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SimpleBinaryExpressionResolve : BaseResolve
|
||||
{
|
||||
public SimpleBinaryExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
var expression = base.Expression as BinaryExpression;
|
||||
switch (parameter.Context.ResolveType)
|
||||
{
|
||||
case ResolveExpressType.SelectSingle:
|
||||
case ResolveExpressType.SelectMultiple:
|
||||
case ResolveExpressType.Update:
|
||||
|
||||
base.Expression = expression.Right;
|
||||
base.Start();
|
||||
var rightValue = parameter.CommonTempData;
|
||||
base.Expression = expression.Left;
|
||||
base.Start();
|
||||
var leftValue = parameter.Context.Result.CurrentParameter.CommonTempData;
|
||||
var operatorValue = ExpressionTool.GetOperator(expression.NodeType);
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
this.Context.Result.AppendFormat(ExpressionConst.BinaryFormatString, leftValue, operatorValue, rightValue.ObjToString());
|
||||
break;
|
||||
case ResolveExpressType.WhereSingle:
|
||||
case ResolveExpressType.WhereMultiple:
|
||||
case ResolveExpressType.FieldSingle:
|
||||
case ResolveExpressType.FieldMultiple:
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -166,7 +166,6 @@
|
||||
<Compile Include="ExpressionsToSql\ResolveItems\MethodCallExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ResolveItems\NewArrayExpessionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ResolveItems\NewExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ResolveItems\SimpleBinaryExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ResolveItems\TypeParameterExpressionReolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\ResolveItems\UnaryExpressionResolve.cs" />
|
||||
<Compile Include="ExpressionsToSql\Common\ExpResolveAccessory.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user