private Token ParseArgument(Expression argExp)
 {
     while ((argExp.NodeType == ExpressionType.Convert) || (argExp.NodeType == ExpressionType.ConvertChecked))
     {
         argExp = ((UnaryExpression)argExp).Operand;
     }
     if ((argExp.NodeType == ExpressionType.MemberAccess) || (argExp.NodeType == ExpressionType.Call))
     {
         var visitor1 = new MemberExpressionVisitor(Context);
         visitor1.Visit(argExp);
         if (visitor1.Token.Type == TokenType.Column)
         {
             _isColumn = true;
         }
         return(visitor1.Token);
     }
     if (argExp.NodeType != ExpressionType.Constant)
     {
         throw new Exception();
     }
     return(Token.Create(((ConstantExpression)argExp).Value));
 }
Example #2
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var expression = node.Arguments[0];

            if (expression.NodeType == ExpressionType.Call)
            {
                var expression5 = expression as MethodCallExpression;
                if (expression5.Method.Name == "NoLock")
                {
                    expression = expression5.Arguments[0];
                }
            }
            var key = "<>" + Path.GetRandomFileName();

            if (expression.NodeType == ExpressionType.Call)
            {
                var expression1 = (MethodCallExpression)expression;
                var visitor2    = new JoinExpressionVisitor(Context);
                visitor2.Visit(expression);
                if (node.Method.Name == "SelectMany")
                {
                    SelectExpression = node.Arguments[2];
                    key = ((LambdaExpression)((UnaryExpression)node.Arguments[2]).Operand).Parameters[1].Name;
                    var joins = visitor2.Joins;
                    var pair  = joins.LastOrDefault();
                    joins.Remove(pair.Key);
                    Joins.Add(key, pair.Value);
                    return(node);
                }
            }
            SelectExpression = node.Arguments[4];
            var expression2 = node.Arguments[1];

            if (expression2.NodeType == ExpressionType.Call)
            {
                var expression6 = (MethodCallExpression)expression2;
                if (expression6.Method.Name != "NoLock")
                {
                    throw new Exception();
                }
                expression2 = expression6.Arguments[0];
            }
            if (expression2.NodeType != ExpressionType.Constant)
            {
                throw new Exception();
            }
            var expression3 = node.Arguments[2];
            var expression4 = node.Arguments[3];
            var visitor     =
                new MemberExpressionVisitor(Context);

            visitor.Visit(expression3);
            if (visitor.Token.Type != TokenType.Column)
            {
                throw new Exception();
            }
            var join = new Join
            {
                Left = visitor.Token.Column
            };

            visitor = new MemberExpressionVisitor(Context);
            visitor.Visit(expression4);
            join.Right = visitor.Token.Column;
            var name = node.Method.Name;

            if (name != "Join")
            {
                if (name != "GroupJoin")
                {
                    throw new Exception();
                }
            }
            else
            {
                join.JoinType = JoinType.Inner;
                var expression8 = (LambdaExpression)((UnaryExpression)node.Arguments[4]).Operand;
                Joins.Add(key, join);
                return(node);
            }
            join.JoinType = JoinType.Left;
            var expression7 = (LambdaExpression)((UnaryExpression)node.Arguments[4]).Operand;

            Joins.Add(key, join);
            return(node);
        }
            private Token ParseNotExpression(UnaryExpression unaryExpression)
            {
                var operand = unaryExpression.Operand;

                if (operand is MemberExpression)
                {
                    var visitor = new MemberExpressionVisitor(Context);
                    visitor.Visit(operand);
                    var type = visitor.Token.Type;
                    if (type != TokenType.Column)
                    {
                        if (type != TokenType.Object)
                        {
                            throw new Exception();
                        }
                        if (!visitor.Token.IsBool())
                        {
                            throw new Exception("不支持");
                        }
                        return(Token.Create(!(bool)visitor.Token.Object));
                    }
                    if ((operand.Type == typeof(bool)) || (operand.Type == typeof(bool?)))
                    {
                        var condition1 = new Condition
                        {
                            CompareType = CompareType.Equal,
                            Left        = Token.Create(1),
                            Right       = Token.Create(1)
                        };
                        return(Token.Create(condition1));
                    }
                    var condition2 = new Condition
                    {
                        Left        = visitor.Token,
                        CompareType = CompareType.Not
                    };
                    return(Token.Create(condition2));
                }
                if (!(operand is MethodCallExpression))
                {
                    throw new Exception("不支持");
                }
                var visitor1 = new MethodCallExpressionVisitor(Context);

                visitor1.Visit(operand);
                var token = visitor1.Token;

                switch (token.Type)
                {
                case TokenType.Column:
                {
                    var condition3 = new Condition
                    {
                        Left        = token,
                        CompareType = CompareType.Not
                    };
                    return(Token.Create(condition3));
                }

                case TokenType.Object:
                    return(Token.Create(!(bool)token.Object));

                case TokenType.Condition:
                {
                    var condition4 = new Condition
                    {
                        Left        = Token.Create(token.Condition),
                        CompareType = CompareType.Not
                    };
                    return(Token.Create(condition4));
                }
                }
                throw new Exception();
            }
            private Token ParseMathBinary(BinaryExpression node)
            {
                ExpressionVisitorBase base2;
                ExpressionVisitorBase base3;

                if (node.Left is BinaryExpression)
                {
                    base2 = new BinaryExpressionVisitor(Context);
                }
                else
                {
                    base2 = new MemberExpressionVisitor(Context);
                }
                base2.Visit(node.Left);
                var token = base2.Token;

                if (node.Right is BinaryExpression)
                {
                    base3 = new BinaryExpressionVisitor(Context);
                }
                else
                {
                    base3 = new MemberExpressionVisitor(Context);
                }
                base3.Visit(node.Right);
                var token2 = base3.Token;

                if ((token.Type != TokenType.Object) || (token2.Type != TokenType.Object))
                {
                    if ((token.Type == TokenType.Column) && (token2.Type == TokenType.Object))
                    {
                        var condition1 = new Condition
                        {
                            Left        = token,
                            CompareType = SelectMathCompareType(node.NodeType),
                            Right       = token2
                        };
                        return(Token.Create(condition1));
                    }
                    if ((token.Type != TokenType.Condition) || (token2.Type != TokenType.Object))
                    {
                        throw new Exception();
                    }
                    var condition2 = new Condition
                    {
                        Left        = token,
                        CompareType = SelectMathCompareType(node.NodeType),
                        Right       = token2
                    };
                    return(Token.Create(condition2));
                }
                if ((token.Object == null) && (token2.Object == null))
                {
                    return(Token.Create(true));
                }
                if ((token == null) || (token2 == null))
                {
                    return(Token.Create(false));
                }
                if (token.Type != TokenType.Object)
                {
                    throw new Exception();
                }
                if (token.Object is string || token.Object is bool || token.Object is bool?)
                {
                    return(Token.Create(token.Equals(token2)));
                }
                if (!(token.Object is DateTime) && !(token.Object is DateTime?))
                {
                    if (!(token.Object is int) && !(token.Object is int?))
                    {
                        if (!(token.Object is short) && !(token.Object is short?))
                        {
                            if (!(token.Object is long) && !(token.Object is long?))
                            {
                                return(Token.Create(token.Object == token2.Object));
                            }
                            var num5 = Convert.ToInt64(token.Object);
                            var num6 = Convert.ToInt64(token2.Object);
                            switch (node.NodeType)
                            {
                            case ExpressionType.Equal:
                                return(Token.Create(num5 = num6));

                            case ExpressionType.GreaterThan:
                                return(Token.Create(num5 > num6));

                            case ExpressionType.GreaterThanOrEqual:
                                return(Token.Create(num5 >= num6));

                            case ExpressionType.LessThan:
                                return(Token.Create(num5 < num6));

                            case ExpressionType.LessThanOrEqual:
                                return(Token.Create(num5 <= num6));
                            }
                            throw new Exception();
                        }
                        var num3 = Convert.ToInt16(token.Object);
                        var num4 = Convert.ToInt16(token2.Object);
                        switch (node.NodeType)
                        {
                        case ExpressionType.Equal:
                            return(Token.Create(num3 = num4));

                        case ExpressionType.GreaterThan:
                            return(Token.Create(num3 > num4));

                        case ExpressionType.GreaterThanOrEqual:
                            return(Token.Create(num3 >= num4));

                        case ExpressionType.LessThan:
                            return(Token.Create(num3 < num4));

                        case ExpressionType.LessThanOrEqual:
                            return(Token.Create(num3 <= num4));
                        }
                        throw new Exception();
                    }
                    var num  = Convert.ToInt32(token.Object);
                    var num2 = Convert.ToInt32(token2.Object);
                    switch (node.NodeType)
                    {
                    case ExpressionType.Equal:
                        return(Token.Create(num = num2));

                    case ExpressionType.GreaterThan:
                        return(Token.Create(num > num2));

                    case ExpressionType.GreaterThanOrEqual:
                        return(Token.Create(num >= num2));

                    case ExpressionType.LessThan:
                        return(Token.Create(num < num2));

                    case ExpressionType.LessThanOrEqual:
                        return(Token.Create(num <= num2));
                    }
                    throw new Exception();
                }
                var time  = Convert.ToDateTime(token.Object);
                var time2 = Convert.ToDateTime(token2.Object);

                switch (node.NodeType)
                {
                case ExpressionType.Equal:
                    return(Token.Create(time = time2));

                case ExpressionType.GreaterThan:
                    return(Token.Create(time > time2));

                case ExpressionType.GreaterThanOrEqual:
                    return(Token.Create(time >= time2));

                case ExpressionType.LessThan:
                    return(Token.Create(time < time2));

                case ExpressionType.LessThanOrEqual:
                    return(Token.Create(time <= time2));
                }
                throw new Exception();
            }
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            ColumnConverter converter;
            var             source = new List <Token>();

            foreach (var expression in node.Arguments)
            {
                source.Add(ParseArgument(expression));
            }
            if (node.Object != null)
            {
                var visitor = new MemberExpressionVisitor(Context);
                visitor.Visit(node.Object);
                if ((visitor.Token.Type == TokenType.Column) && !_isColumn)
                {
                    Token = visitor.Token;
                    var memberInfo = node.Method;
                    var enumerable = from x in source select x.Object;
                    Token.Column.Converters.Push(new ColumnConverter(memberInfo, enumerable.ToList(), true));
                    return(node);
                }
                if ((visitor.Token.Type == TokenType.Object) && !_isColumn)
                {
                    Token =
                        Token.Create(node.Method.Invoke(visitor.Token.Object,
                                                        (from x in source select x.Object).ToArray()));
                    return(node);
                }
                if ((visitor.Token.Type != TokenType.Object) || !_isColumn)
                {
                    throw new Exception();
                }
                Token = source[0];
                var list4 = new List <object>
                {
                    visitor.Token.Object
                };
                list4.AddRange(source.Skip(1));
                Token.Column.Converters.Push(new ColumnConverter(node.Method, list4, false));
                return(node);
            }
            var method = node.Method;

            if (!_isColumn)
            {
                var list3     = new List <object>();
                var infoArray = node.Method.GetParameters().ToArray();
                BaseTypeConverter converter2 = null;
                for (var i = 0; i < infoArray.Length; i++)
                {
                    var info2 = infoArray[i];
                    var obj2  = source[i].Object;
                    if (TypeHelper.IsValueType(info2.ParameterType))
                    {
                        converter2 = new BaseTypeConverter(obj2, info2.ParameterType);
                        converter2.Process();
                        list3.Add(converter2.Result);
                    }
                    else
                    {
                        list3.Add(obj2);
                    }
                }
                Token = Token.Create(node.Method.Invoke(null, list3.ToArray()));
                return(node);
            }
            var parameters = new List <object>();
            var name       = method.Name;

            if (name != "ToDateTime")
            {
                if (name != "Contains")
                {
                    throw new Exception();
                }
            }
            else
            {
                Token = source[0];
                parameters.AddRange(source.Skip(1));
                converter = new ColumnConverter(method, parameters);
                Token.Column.Converters.Push(converter);
                return(node);
            }
            Token = source[1];
            parameters.Add(source[0].Object);
            parameters.AddRange(from x in source.Skip(2)
                                where x.Type == TokenType.Object
                                select x.Object);
            converter = new ColumnConverter(method, parameters);
            Token.Column.Converters.Push(converter);
            return(node);
        }