Browse Source

-

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
2a7b832462
  1. 21
      Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs
  2. 26
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

21
Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Method.cs

@ -725,18 +725,15 @@ namespace OrmTest.UnitTest
private void IIF4()
{
//Expression<Func<DataTestInfo2, bool>> exp = it => SqlFunc.IIF(true, it.Bool1, it.Bool2) == true;
//SqlServerExpressionContext expContext = new SqlServerExpressionContext();
//expContext.Resolve(exp, ResolveExpressType.WhereSingle);
//var value = expContext.Result.GetString();
//var pars = expContext.Parameters;
//base.Check(value, pars, "(( CASE WHEN ([Name] like '%'+@MethodConst0+'%') THEN @MethodConst1 ELSE @MethodConst2 END ) = @Const3 )", new List<SugarParameter>()
//{
// new SugarParameter("@MethodConst0","a"),
// new SugarParameter("@MethodConst1",true),
// new SugarParameter("@MethodConst2",false),
// new SugarParameter("@Const3",true)
//}, "IIF4 error");
Expression<Func<DataTestInfo2, bool>> exp = it => SqlFunc.IIF(true, it.Bool1, it.Bool2) == true;
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(value, pars, "(( CASE WHEN ( 1 = 1 ) THEN [Bool1] ELSE [Bool2] END ) = @Const0 )", new List<SugarParameter>()
{
new SugarParameter("@Const0",true)
}, "IIF4 error");
}
}
}

26
Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

@ -146,19 +146,33 @@ namespace SqlSugar
private void AppendItem(ExpressionParameter parameter, string name, IEnumerable<Expression> args, MethodCallExpressionModel model, Expression item)
{
var isBinaryExpression = item is BinaryExpression || item is MethodCallExpression;
var isIFFBoolMember = name == "IIF" && (args.Last() is MemberExpression) && (args.Last() as MemberExpression).Type == PubConst.BoolType;
var isIFFUnary = name == "IIF" && (args.Last() is UnaryExpression) && (args.Last() as UnaryExpression).Operand.Type == PubConst.BoolType;
var isIFFBoolBinary = name == "IIF" && (args.Last() is BinaryExpression) && (args.Last() as BinaryExpression).Type == PubConst.BoolType;
if (isIFFUnary && item != args.First())
var isConst = item is ConstantExpression;
var isIIF= name == "IIF";
var isIFFBoolMember = isIIF && (item is MemberExpression) && (item as MemberExpression).Type == PubConst.BoolType;
var isIFFUnary = isIIF && (item is UnaryExpression) && (item as UnaryExpression).Operand.Type == PubConst.BoolType;
var isIFFBoolBinary = isIIF && (item is BinaryExpression) && (item as BinaryExpression).Type == PubConst.BoolType;
var isFirst = item == args.First();
if (isFirst&& isIIF && isConst)
{
var value = (item as ConstantExpression).Value.ObjToBool() ? this.Context.DbMehtods.True() : this.Context.DbMehtods.False();
var methodCallExpressionArgs = new MethodCallExpressionArgs()
{
IsMember =true,
MemberName = value,
MemberValue= value
};
model.Args.Add(methodCallExpressionArgs);
}
else if (isIFFUnary && !isFirst)
{
AppendModelByIIFMember(parameter, model, (item as UnaryExpression).Operand);
}
else if (isIFFBoolMember && item != args.First())
else if (isIFFBoolMember && !isFirst)
{
AppendModelByIIFMember(parameter, model, item);
}
else if (isIFFBoolBinary && item != args.First())
else if (isIFFBoolBinary && !isFirst)
{
AppendModelByIIFBinary(parameter, model, item);

Loading…
Cancel
Save