Browse Source

Expression .Value support

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
63e3be4986
  1. 45
      Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Where.cs
  2. 27
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs
  3. 50
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs

45
Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Where.cs

@ -22,6 +22,8 @@ namespace OrmTest.UnitTest
for (int i = 0; i < base.Count; i++) for (int i = 0; i < base.Count; i++)
{ {
whereSingle23();
whereSingle22();
whereSingle21(); whereSingle21();
whereSingle20(); whereSingle20();
whereSingle19(); whereSingle19();
@ -339,15 +341,42 @@ namespace OrmTest.UnitTest
private void whereSingle21() private void whereSingle21()
{ {
//Expression<Func<DataTestInfo2, bool>> exp = it => it.Bool2.Value; Expression<Func<DataTestInfo2, bool>> exp = it => it.Bool2.Value;
//SqlServerExpressionContext expContext = new SqlServerExpressionContext(); SqlServerExpressionContext expContext = new SqlServerExpressionContext();
//expContext.Resolve(exp, ResolveExpressType.WhereSingle); expContext.Resolve(exp, ResolveExpressType.WhereSingle);
//var value = expContext.Result.GetString(); var value = expContext.Result.GetString();
//var pars = expContext.Parameters; var pars = expContext.Parameters;
//base.Check(value, pars, "( 1 = 2 )", new List<SugarParameter>() base.Check(value, pars, "( [Bool2]=1 )", new List<SugarParameter>()
//{ {
}, "whereSingle21");
}
private void whereSingle22()
{
Expression<Func<DataTestInfo2, bool>> exp = it => !it.Bool2.Value;
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(value, pars, "NOT ( [Bool2]=1 ) ", new List<SugarParameter>()
{
//}, "whereSingle21"); }, "whereSingle22");
}
private void whereSingle23()
{
decimal? val = 1;
Expression<Func<DataTestInfo, bool>> exp = it => it.Decimal2==val.Value;
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.Resolve(exp, ResolveExpressType.WhereSingle);
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(value, pars, "( [Decimal2] = @Const0 )", new List<SugarParameter>()
{
new SugarParameter("@Const0",val)
}, "whereSingle23");
} }
} }

27
Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs

@ -50,7 +50,15 @@ namespace SqlSugar
{ {
return operatorValue == "&&" || operatorValue == "||"; return operatorValue == "&&" || operatorValue == "||";
} }
public static bool IsComparisonOperator(BinaryExpression expression)
public static bool IsLogicOperator(Expression expression)
{
return expression.NodeType == ExpressionType.And ||
expression.NodeType == ExpressionType.AndAlso ||
expression.NodeType == ExpressionType.Or ||
expression.NodeType == ExpressionType.OrElse;
}
public static bool IsComparisonOperator(Expression expression)
{ {
return expression.NodeType != ExpressionType.And && return expression.NodeType != ExpressionType.And &&
expression.NodeType != ExpressionType.AndAlso && expression.NodeType != ExpressionType.AndAlso &&
@ -143,6 +151,23 @@ namespace SqlSugar
return reval; return reval;
} }
public static bool IsConstExpression(MemberExpression memberExpr)
{
var result = false;
while (memberExpr!=null&&memberExpr.Expression != null)
{
var isConst = memberExpr.Expression is ConstantExpression;
if (isConst)
{
result = true;
break;
}
memberExpr = memberExpr.Expression as MemberExpression;
}
return result;
}
public static object GetPropertyValue(MemberExpression memberExpr) public static object GetPropertyValue(MemberExpression memberExpr)
{ {
object reval = null; object reval = null;

50
Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs

@ -14,11 +14,18 @@ namespace SqlSugar
var isLeft = parameter.IsLeft; var isLeft = parameter.IsLeft;
var isSetTempData = baseParameter.CommonTempData.IsValuable() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result); var isSetTempData = baseParameter.CommonTempData.IsValuable() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);
var expression = base.Expression as MemberExpression; var expression = base.Expression as MemberExpression;
if (expression.Member.Name == "Value" && expression.Member.GetType().Name == "RuntimePropertyInfo") var isValue = expression.Member.Name == "Value" && expression.Member.GetType().Name == "RuntimePropertyInfo";
var isBool = expression.Type == PubConst.BoolType;
var isValueBool = isValue && isBool && parameter.BaseExpression == null;
if (isValueBool)
{
isValue = false;
}
else if (isValue)
{ {
expression = expression.Expression as MemberExpression; expression = expression.Expression as MemberExpression;
} }
else if (expression.Expression != null && expression.Expression.NodeType != ExpressionType.Parameter) else if (expression.Expression != null && expression.Expression.NodeType != ExpressionType.Parameter && !isValueBool)
{ {
var value = ExpressionTool.GetMemberValue(expression.Member, expression); var value = ExpressionTool.GetMemberValue(expression.Member, expression);
if (isSetTempData) if (isSetTempData)
@ -58,30 +65,33 @@ namespace SqlSugar
} }
break; break;
case ResolveExpressType.WhereSingle: case ResolveExpressType.WhereSingle:
case ResolveExpressType.WhereMultiple:
var isSingle = parameter.Context.ResolveType == ResolveExpressType.WhereSingle;
if (isSetTempData) if (isSetTempData)
{ {
fieldName = GetSingleName(parameter, expression, null); fieldName = GetName(parameter, expression, null,isSingle);
baseParameter.CommonTempData = fieldName; baseParameter.CommonTempData = fieldName;
} }
else else
{ {
fieldName = GetSingleName(parameter, expression, isLeft); if (isValueBool)
if (expression.Type == PubConst.BoolType && baseParameter.OperatorValue.IsNullOrEmpty())
{ {
fieldName = "( " + fieldName + "=1 )"; fieldName = GetName(parameter, expression.Expression as MemberExpression, isLeft,isSingle);
}
fieldName = AppendMember(parameter, isLeft, fieldName);
} }
break; else if (ExpressionTool.IsConstExpression(expression))
case ResolveExpressType.WhereMultiple:
if (isSetTempData)
{ {
fieldName = GetMultipleName(parameter, expression, null); var value = ExpressionTool.GetMemberValue(expression.Member, expression);
baseParameter.CommonTempData = fieldName; base.AppendValue(parameter, isLeft, value);
return;
} }
else else
{ {
fieldName = GetMultipleName(parameter, expression, isLeft); fieldName = GetName(parameter, expression, isLeft,isSingle);
}
if (expression.Type == PubConst.BoolType && baseParameter.OperatorValue.IsNullOrEmpty())
{
fieldName = "( " + fieldName + "=1 )";
}
fieldName = AppendMember(parameter, isLeft, fieldName); fieldName = AppendMember(parameter, isLeft, fieldName);
} }
break; break;
@ -128,6 +138,18 @@ namespace SqlSugar
return fieldName; return fieldName;
} }
private string GetName(ExpressionParameter parameter, MemberExpression expression, bool? isLeft, bool isSingle)
{
if (isSingle)
{
return GetSingleName(parameter, expression, IsLeft);
}
else
{
return GetMultipleName(parameter,expression,IsLeft);
}
}
private string GetMultipleName(ExpressionParameter parameter, MemberExpression expression, bool? isLeft) private string GetMultipleName(ExpressionParameter parameter, MemberExpression expression, bool? isLeft)
{ {
string shortName = expression.Expression.ToString(); string shortName = expression.Expression.ToString();

Loading…
Cancel
Save