Example #1
0
 private static WhereClip ToWhereClipChild(System.Linq.Expressions.Expression exprBody, WhereType wtype = WhereType.Where)
 {
     if (exprBody is BinaryExpression)
     {
         return(ConvertBinary((BinaryExpression)exprBody, wtype));
     }
     if (exprBody is MethodCallExpression)
     {
         return(ConvertMethodCall((MethodCallExpression)exprBody));
     }
     if (exprBody is UnaryExpression)
     {
         return(ConvertUnary((UnaryExpression)exprBody));
     }
     if (IsBoolFieldOrProperty(exprBody))
     {
         var key = ((MemberExpression)exprBody).Member.Name;
         return(new WhereClip());
     }
     if (exprBody is ConstantExpression)
     {
         var key = ((ConstantExpression)exprBody).Value;
         if (DataUtils.ConvertValue <bool>(key))
         {
             return(new WhereClip(" 1=1 "));
         }
         return(new WhereClip(" 1=2 "));
     }
     throw new Exception("暂时不支持的Where条件Lambda表达式写法!请使用经典写法!");
 }
Example #2
0
 /// <summary>
 /// 返回单个值
 /// </summary>
 /// <returns></returns>
 public TResult ToScalar <TResult>()
 {
     return(DataUtils.ConvertValue <TResult>(ToScalar()));
 }
Example #3
0
        private static WhereClip LeftAndRight(BinaryExpression e, QueryOperator co, WhereType wtype = WhereType.Where)
        {
            ColumnFunction   leftFunction;
            ColumnFunction   rightFunction;
            MemberExpression leftMe = null;
            MemberExpression rightMe;

            System.Linq.Expressions.Expression expLeft  = e.Left;
            System.Linq.Expressions.Expression expRight = e.Right;
            if (e.Left.NodeType == ExpressionType.Convert)
            {
                expLeft = ((UnaryExpression)e.Left).Operand;
            }
            if (e.Right.NodeType == ExpressionType.Convert)
            {
                expRight = ((UnaryExpression)e.Right).Operand;
            }

            //DebugInfoGenerator aa +=        ;
            //System.Linq.Expressions.Expression.Lambda(expRight).Compile(debugInfoGenerator);

            var isAgain = false;

Again:
            if (expLeft.NodeType == ExpressionType.Constant ||
                (expLeft.NodeType == ExpressionType.MemberAccess && ((MemberExpression)expLeft).Expression == null) || isAgain)
            {
                if (expRight.NodeType == ExpressionType.Constant ||
                    (expRight.NodeType == ExpressionType.MemberAccess && ((MemberExpression)expRight).Expression == null))
                {
                    //var leftValue = GetValue(expLeft);
                    //var rightValue = GetValue(expRight);
                    //if (leftValue.Equals(rightValue))
                    //{
                    //    return new WhereClip(" 1=1 ");
                    //}
                    if (DataUtils.ConvertValue <bool>(fastEvaluator.Eval(e)))
                    {
                        return(new WhereClip(" 1=2 "));
                    }
                    return(new WhereClip(" 1=1 "));
                }
                else
                {
                    var keyRightName = GetMemberName(expRight, out rightFunction, out rightMe);

                    if (expLeft.NodeType == ExpressionType.MemberAccess)
                    {
                        var left = (MemberExpression)expLeft;
                        if (left.Expression != null && (wtype == WhereType.JoinWhere || left.Expression.ToString() == rightMe.Expression.ToString()))
                        {
                            ColumnFunction functionLeft;
                            var            keyLeft = GetMemberName(expLeft, out functionLeft, out left);
                            if (keyRightName.Contains("$"))
                            {
                                return(new WhereClip(new Field(keyLeft, left.Expression.Type.Name), GetValue(expRight), co));
                            }
                            else
                            {
                                return(new WhereClip(new Field(keyRightName, rightMe.Expression.Type.Name), new Field(keyLeft, left.Expression.Type.Name), co));
                            }
                        }
                    }
                    object value = GetValue(expLeft);
                    if (keyRightName.Contains("$"))
                    {
                        if (DataUtils.ConvertValue <bool>(fastEvaluator.Eval(e)))
                        {
                            return(new WhereClip(" 1=2 "));
                        }
                        return(new WhereClip(" 1=1 "));
                    }
                    if (value == null)
                    {
                        if (co == QueryOperator.Equal)
                        {
                            return(new Field(keyRightName, rightMe.Expression.Type.Name).IsNull());
                        }
                        if (co == QueryOperator.NotEqual)
                        {
                            return(new Field(keyRightName, rightMe.Expression.Type.Name).IsNotNull());
                        }
                        throw new Exception("null值只支持等于或不等于!");
                    }
                    return(new WhereClip(new Field(keyRightName, rightMe.Expression.Type.Name), value, co));
                }
            }
            else
            {
                var key = "";
                try
                {
                    key = GetMemberName(expLeft, out leftFunction, out leftMe);
                    if (key.Contains("$"))
                    {
                        isAgain = true;
                        goto Again;
                    }
                }
                catch (Exception)
                {
                    isAgain = true;
                    goto Again;
                }
                if (expRight.NodeType == ExpressionType.MemberAccess)
                {
                    var right = (MemberExpression)expRight;
                    if (right.Expression != null && (wtype == WhereType.JoinWhere || right.Expression == leftMe.Expression))
                    {
                        ColumnFunction functionRight;
                        var            keyRight = GetMemberName(expRight, out functionRight, out right);
                        return(new WhereClip(new Field(key, leftMe.Expression.Type.Name), new Field(keyRight, right.Expression.Type.Name), co));
                    }
                }
                object value = GetValue(expRight);
                if (value == null)
                {
                    if (co == QueryOperator.Equal)
                    {
                        return(new Field(key, leftMe.Expression.Type.Name).IsNull());
                    }
                    if (co == QueryOperator.NotEqual)
                    {
                        return(new Field(key, leftMe.Expression.Type.Name).IsNotNull());
                    }
                    throw new Exception("null值只支持等于或不等于!");
                }
                return(new WhereClip(new Field(key, leftMe.Expression.Type.Name), value, co));
            }
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="be"></param>
        /// <param name="co"></param>
        /// <param name="wtype"></param>
        /// <returns></returns>
        private static WhereClip LeftAndRight(BinaryExpression be, QueryOperator co, WhereType wtype = WhereType.Where)
        {
            ColumnFunction   leftFunction;
            ColumnFunction   rightFunction;
            MemberExpression leftMe = null;
            MemberExpression rightMe;

            System.Linq.Expressions.Expression expLeft  = be.Left;
            System.Linq.Expressions.Expression expRight = be.Right;
            if (be.Left.NodeType == ExpressionType.Convert)
            {
                expLeft = ((UnaryExpression)be.Left).Operand;
            }
            if (be.Right.NodeType == ExpressionType.Convert)
            {
                expRight = ((UnaryExpression)be.Right).Operand;
            }
            var isAgain = false;

Again:
            if (expLeft.NodeType == ExpressionType.Constant ||
                (expLeft.NodeType == ExpressionType.MemberAccess && ((MemberExpression)expLeft).Expression == null) || isAgain)
            {
                if (expRight.NodeType == ExpressionType.Constant ||
                    (expRight.NodeType == ExpressionType.MemberAccess && ((MemberExpression)expRight).Expression == null))
                {
                    return(DataUtils.ConvertValue <bool>(fastEvaluator.Eval(be))
                        ? new WhereClip(" 1=2 ")
                        : new WhereClip(" 1=1 "));
                }
                else
                {
                    var keyRightName = GetMemberName(expRight, out rightFunction, out rightMe);

                    if (expLeft.NodeType == ExpressionType.MemberAccess)
                    {
                        var left = (MemberExpression)expLeft;
                        if (left.Expression != null && (wtype == WhereType.JoinWhere || left.Expression.ToString() == rightMe.Expression.ToString()))
                        {
                            ColumnFunction functionLeft;
                            var            keyLeft = GetMemberName(expLeft, out functionLeft, out left);
                            if (keyRightName[0].Contains("$"))
                            {
                                return(new WhereClip(CreateField(keyLeft, left.Expression.Type), GetValue(expRight), co));
                            }
                            else
                            {
                                return(new WhereClip(CreateField(keyRightName, rightMe.Expression.Type), CreateField(keyLeft, left.Expression.Type), co));
                            }
                        }
                    }
                    object value = GetValue(expLeft);
                    if (keyRightName[0].Contains("$"))
                    {
                        if (DataUtils.ConvertValue <bool>(fastEvaluator.Eval(be)))
                        {
                            return(new WhereClip(" 1=2 "));
                        }
                        return(new WhereClip(" 1=1 "));
                    }
                    if (value != null)
                    {
                        return(new WhereClip(CreateField(keyRightName, rightMe.Expression.Type), value, co));
                    }
                    switch (co)
                    {
                    case QueryOperator.Equal:
                        return(CreateField(keyRightName, rightMe.Expression.Type).IsNull());

                    case QueryOperator.NotEqual:
                        return(CreateField(keyRightName, rightMe.Expression.Type).IsNotNull());
                    }
                    throw new Exception("null值只支持等于或不等于!出错比较符:" + co.ToString());
                }
            }
            else
            {
                string[] key;
                try
                {
                    key = GetMemberName(expLeft, out leftFunction, out leftMe);
                    if (key[0].Contains("$"))
                    {
                        isAgain = true;
                        goto Again;
                    }
                }
                catch (Exception)
                {
                    isAgain = true;
                    goto Again;
                }
                if (expRight.NodeType == ExpressionType.MemberAccess)
                {
                    var right = (MemberExpression)expRight;
                    if (right.Expression != null && (wtype == WhereType.JoinWhere || right.Expression == leftMe.Expression))
                    {
                        ColumnFunction functionRight;
                        var            keyRight = GetMemberName(expRight, out functionRight, out right);
                        return(new WhereClip(
                                   CreateField(key, leftMe.Expression.Type),
                                   CreateField(keyRight, right.Expression.Type)
                                   , co));
                    }
                }
                object value = GetValue(expRight);
                if (value == null)
                {
                    if (co == QueryOperator.Equal)
                    {
                        return(CreateField(key, leftMe.Expression.Type).IsNull());
                    }
                    if (co == QueryOperator.NotEqual)
                    {
                        return(CreateField(key, leftMe.Expression.Type).IsNotNull());
                    }
                    throw new Exception("null值只支持等于或不等于!");
                }
                return(new WhereClip(CreateField(key, leftMe.Expression.Type), value, co));
            }
        }