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. 33
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs
  3. 54
      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++)
{
whereSingle23();
whereSingle22();
whereSingle21();
whereSingle20();
whereSingle19();
@ -339,15 +341,42 @@ namespace OrmTest.UnitTest
private void whereSingle21()
{
//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, "( 1 = 2 )", new List<SugarParameter>()
//{
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, "( [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");
}
}

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

@ -48,9 +48,17 @@ namespace SqlSugar
}
public static bool IsLogicOperator(string operatorValue)
{
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 &&
expression.NodeType != ExpressionType.AndAlso &&
@ -120,7 +128,7 @@ namespace SqlSugar
{
object reval = null;
FieldInfo field = (FieldInfo)memberExpr.Member;
Check.Exception(field.IsPrivate,string.Format(" Field \"{0}\" can't be private ",field.Name));
Check.Exception(field.IsPrivate, string.Format(" Field \"{0}\" can't be private ", field.Name));
reval = field.GetValue(memberExpr.Member);
if (reval != null && reval.GetType().IsClass() && reval.GetType() != ExpressionConst.StringType)
{
@ -143,6 +151,23 @@ namespace SqlSugar
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)
{
object reval = null;
@ -191,7 +216,7 @@ namespace SqlSugar
public static bool IsEntity(Type type)
{
return type.IsClass() && type!=ExpressionConst.StringType;
return type.IsClass() && type != ExpressionConst.StringType;
}
public static bool IsValueType(Type type)

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

@ -14,11 +14,18 @@ namespace SqlSugar
var isLeft = parameter.IsLeft;
var isSetTempData = baseParameter.CommonTempData.IsValuable() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);
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;
}
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);
if (isSetTempData)
@ -58,14 +65,29 @@ namespace SqlSugar
}
break;
case ResolveExpressType.WhereSingle:
case ResolveExpressType.WhereMultiple:
var isSingle = parameter.Context.ResolveType == ResolveExpressType.WhereSingle;
if (isSetTempData)
{
fieldName = GetSingleName(parameter, expression, null);
fieldName = GetName(parameter, expression, null,isSingle);
baseParameter.CommonTempData = fieldName;
}
else
{
fieldName = GetSingleName(parameter, expression, isLeft);
if (isValueBool)
{
fieldName = GetName(parameter, expression.Expression as MemberExpression, isLeft,isSingle);
}
else if (ExpressionTool.IsConstExpression(expression))
{
var value = ExpressionTool.GetMemberValue(expression.Member, expression);
base.AppendValue(parameter, isLeft, value);
return;
}
else
{
fieldName = GetName(parameter, expression, isLeft,isSingle);
}
if (expression.Type == PubConst.BoolType && baseParameter.OperatorValue.IsNullOrEmpty())
{
fieldName = "( " + fieldName + "=1 )";
@ -73,18 +95,6 @@ namespace SqlSugar
fieldName = AppendMember(parameter, isLeft, fieldName);
}
break;
case ResolveExpressType.WhereMultiple:
if (isSetTempData)
{
fieldName = GetMultipleName(parameter, expression, null);
baseParameter.CommonTempData = fieldName;
}
else
{
fieldName = GetMultipleName(parameter, expression, isLeft);
fieldName = AppendMember(parameter, isLeft, fieldName);
}
break;
case ResolveExpressType.FieldSingle:
fieldName = GetSingleName(parameter, expression, isLeft);
base.Context.Result.Append(fieldName);
@ -128,6 +138,18 @@ namespace SqlSugar
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)
{
string shortName = expression.Expression.ToString();

Loading…
Cancel
Save