private Token ParseBinaryExpressionLeftOrRight(BinaryExpression node)
            {
                Token token = null;

                if (node.Left is MemberExpression)
                {
                    var visitor1 = new MemberExpressionVisitor(Context);
                    visitor1.Visit(node.Left);
                    token = visitor1.Token;
                    if (token.Type != TokenType.Column)
                    {
                        throw new Exception();
                    }
                    if (TypeHelper.GetUnderlyingType(token.Column.DataType) != ReflectorConsts.BoolType)
                    {
                        throw new Exception();
                    }
                    var token2     = token;
                    var token3     = Token.Create(1);
                    var condition1 = new Condition
                    {
                        CompareType = CompareType.Equal,
                        Left        = token2,
                        Right       = token3
                    };
                    return(Token.Create(condition1));
                }
                return(VisitBinaryNode(node.Left));
            }
            private Token VisitBinaryNode(Expression node)
            {
                if (node.NodeType == ExpressionType.Not)
                {
                    var token = VisitBinaryNode(((UnaryExpression)node).Operand);
                    switch (token.Type)
                    {
                    case TokenType.Column:
                    {
                        var condition1 = new Condition
                        {
                            Left        = token,
                            CompareType = CompareType.Not
                        };
                        return(Token.Create(condition1));
                    }

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

                    case TokenType.Condition:
                        token.Condition.Right = Token.Create(0);
                        return(token);
                    }
                    throw new Exception();
                }
                if (node is MemberExpression)
                {
                    var visitor1 = new MemberExpressionVisitor(Context);
                    visitor1.Visit(node);
                    var token2 = visitor1.Token;
                    var type   = token2.Type;
                    if (type != TokenType.Column)
                    {
                        if (type != TokenType.Object)
                        {
                            throw new Exception();
                        }
                        return(token2);
                    }
                    if (TypeHelper.GetUnderlyingType(token2.Column.DataType) != ReflectorConsts.BoolType)
                    {
                        throw new Exception();
                    }
                    var token3     = token2;
                    var token4     = Token.Create(1);
                    var condition2 = new Condition
                    {
                        CompareType = CompareType.Equal,
                        Left        = token3,
                        Right       = token4
                    };
                    return(Token.Create(condition2));
                }
                var visitor2 = new BinaryExpressionVisitor(Context);

                visitor2.Visit(node);
                return(visitor2.Token);
            }
        protected override Expression VisitMember(MemberExpression node)
        {
            var visitor = new MemberExpressionVisitor(Context);

            visitor.Visit(node);
            Token = visitor.Token;
            return(node);
        }
        private void VisitSortExpression(KeyValuePair <string, Expression> sortExpression)
        {
            var visitor = new MemberExpressionVisitor(_context);

            visitor.Visit(sortExpression.Value);
            if (visitor.Token.Type != TokenType.Column)
            {
                throw new Exception();
            }
            SortColumns.Add(new KeyValuePair <string, Column>(sortExpression.Key, visitor.Token.Column));
        }
Exemple #5
0
 protected override Expression VisitMember(MemberExpression node)
 {
     if (node.Expression == null)
     {
         var obj2 = GetValue(node, null);
         while (_memberInfos.Count > 0)
         {
             obj2 = GetValue((MemberExpression)_memberInfos.Pop(), obj2);
         }
         Token = Token.Create(obj2);
         return(node);
     }
     if (node.Member.DeclaringType == typeof(TimeSpan))
     {
         if (node.Expression.NodeType != ExpressionType.Subtract)
         {
             throw new Exception();
         }
         var expression1 = (BinaryExpression)node.Expression;
         var left        = expression1.Left;
         var right       = expression1.Right;
         var visitor     = new MemberExpressionVisitor(Context);
         visitor.Visit(left);
         var visitor2 = new MemberExpressionVisitor(Context);
         visitor2.Visit(right);
         if ((visitor.Token.Type == TokenType.Column) && (visitor2.Token.Type == TokenType.Object))
         {
             var time = (DateTime)visitor2.Token.Object;
             Token = visitor.Token;
             var list1 = new List <object>
             {
                 time
             };
             Token.Column.Converters.Push(new ColumnConverter(node.Member, list1, true));
             return(node);
         }
         if ((visitor.Token.Type != TokenType.Object) || (visitor2.Token.Type != TokenType.Column))
         {
             throw new Exception();
         }
         var time2 = (DateTime)visitor.Token.Object;
         Token = visitor2.Token;
         var parameters = new List <object>
         {
             time2
         };
         Token.Column.Converters.Push(new ColumnConverter(node.Member, parameters, false));
         return(node);
     }
     _memberInfos.Push(node);
     return(base.VisitMember(node));
 }
 private void VisitUpdateExpression(Expression updateExpression)
 {
     if (updateExpression != null)
     {
         var visitor = new MemberExpressionVisitor(_context);
         visitor.Visit(updateExpression);
         if (visitor.Token.Type != TokenType.Object)
         {
             throw new NotSupportedException("不支持");
         }
         UpdateResult = (Dictionary <string, object>)visitor.Token.Object;
     }
 }
 protected override Expression VisitNew(NewExpression node)
 {
     for (var i = 0; i < node.Arguments.Count; i++)
     {
         var visitor    = new MemberExpressionVisitor(Context);
         var expression = node.Arguments[i];
         var info       = node.Members[i];
         visitor.Visit(expression);
         visitor.SelectedColumn.Alias = info.Name;
         Columns.Add(visitor.SelectedColumn);
         Context.Columns.Add(info.Name, visitor.SelectedColumn);
     }
     return(node);
 }
 private void VisitAggreationExpression(KeyValuePair <string, Expression> aggreationExpression)
 {
     if (aggreationExpression.Value != null)
     {
         var visitor = new MemberExpressionVisitor(_context);
         visitor.Visit(aggreationExpression.Value);
         if (visitor.Token.Type != TokenType.Column)
         {
             throw new Exception("只能针对列进行聚合操作");
         }
         AggregationColumns.Add(aggreationExpression.Key, visitor.Token.Column);
     }
     else
     {
         AggregationColumns.Add(aggreationExpression.Key, null);
     }
 }
Exemple #9
0
        protected override Expression VisitMemberInit(MemberInitExpression node)
        {
            var dictionary = new Dictionary <string, object>();

            foreach (MemberAssignment assignment in node.Bindings)
            {
                var visitor = new MemberExpressionVisitor(Context);
                visitor.Visit(assignment.Expression);
                if (visitor.Token.Type != TokenType.Object)
                {
                    throw new NotSupportedException("不支持");
                }
                dictionary.Add(assignment.Member.Name, visitor.Token.Object);
            }
            Token = Token.Create(dictionary);
            return(node);
        }
 protected override Expression VisitMemberInit(MemberInitExpression node)
 {
     foreach (MemberAssignment assignment in node.Bindings)
     {
         var visitor = new MemberExpressionVisitor(Context);
         visitor.Visit(assignment.Expression);
         if ((visitor.SelectedColumn != null) &&
             (assignment.Member.GetCustomAttribute(ReflectorConsts.NonSelectAttributeType, false) ==
              null))
         {
             visitor.SelectedColumn.Alias = assignment.Member.Name;
             Columns.Add(visitor.SelectedColumn);
             Context.Columns.Add(assignment.Member.Name, visitor.SelectedColumn);
         }
     }
     return(node);
 }
        protected override Expression VisitParameter(ParameterExpression node)
        {
            var type       = node.Type;
            var expression = _memberExpressions.LastOrDefault();

            if (!TypeHelper.IsValueType(expression.Type))
            {
                while (!EntityConfigurationManager.IsEntity(type))
                {
                    type = _memberExpressions.Pop().Type;
                }
                ParseEntityType(type);
                return(node);
            }
            var visitor = new MemberExpressionVisitor(Context);

            visitor.Visit(expression);
            Columns.Add(visitor.SelectedColumn);
            return(node);
        }
 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));
 }
Exemple #13
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();
            }
        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);
        }