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

            visitor.Visit(node);
            if (visitor.Column.Type == typeof(bool))
            {
                var column = new ParameterColumn(this.Context.AddParameter(true))
                {
                    Type = typeof(bool)
                };
                this.Condition = new SingleCondition {
                    Left = visitor.Column, Relation = "=", Right = column
                };
            }
            else
            {
                this.Condition = new SingleCondition {
                    Right = visitor.Column
                };
            }
            return(node);
        }
Beispiel #3
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            switch (node.Method.Name)
            {
            case "First":
            case "FirstOrDefault":
                this.Visit(node.Arguments[0]);
                if (this.context.IsQueryResultChanged)
                {
                    var oldContext = this.context;
                    this.context = oldContext.CopyTo();
                    this.context.Fragment.FromPart.Add(new QueryTable(oldContext.WrapToFragment(), this.context.GetTableAlias(oldContext.ExportType)));
                }
                if (node.Arguments.Count == 2)
                {
                    ParseWhereExpresson(node.Arguments[1]);
                }
                this.context.Fragment.Take = 1;
                break;

            case "Last":
            case "LastOrDefault":
                this.Visit(node.Arguments[0]);
                if (this.context.IsQueryResultChanged)
                {
                    var oldContext = this.context;
                    this.context = oldContext.CopyTo();
                    this.context.Fragment.FromPart.Add(new QueryTable(oldContext.WrapToFragment(), this.context.GetTableAlias(oldContext.ExportType)));
                }
                if (node.Arguments.Count == 2)
                {
                    ParseWhereExpresson(node.Arguments[1]);
                }
                break;

            case "Where":
                this.Visit(node.Arguments[0]);
                if (this.context.IsQueryResultChanged)
                {
                    var oldContext = this.context;
                    this.context = oldContext.CopyTo();
                    this.context.Fragment.FromPart.Add(new QueryTable(oldContext.WrapToFragment(), this.context.GetTableAlias(oldContext.ExportType)));
                }
                ParseWhereExpresson(node.Arguments[1]);
                break;

            case "Select":
                this.Visit(node.Arguments[0]);
                var selectVisitor = new SelectExpressionVisitor(context);
                selectVisitor.Visit(node.Arguments[1]);
                this.context.Fragment.SelectPart = selectVisitor.Columns;
                break;

            case "Skip":
                this.Visit(node.Arguments[0]);
                this.context.Fragment.Skip = (int)(node.Arguments[1] as ConstantExpression).Value;
                break;

            case "Take":
                this.Visit(node.Arguments[0]);
                this.context.Fragment.Take = (int)(node.Arguments[1] as ConstantExpression).Value;
                break;

            case "OrderBy":
            case "OrderByDescending":
            case "ThenBy":
            case "ThenByDescending":
                this.Visit(node.Arguments[0]);
                var orderVisitor = new MemberExpressionVisitor(context);
                orderVisitor.Visit(node.Arguments[1]);
                this.context.Fragment.OrderPart.Add(new OrderCondition(orderVisitor.Column,
                                                                       node.Method.Name == "OrderBy" || node.Method.Name == "ThenBy" ? "asc" : "desc"));
                break;

            case "Any":
            case "Count":
                var parser = new QueryExpressionParser(this.connection, this.context.CopyTo());
                parser.Visit(node.Arguments[0]);
                this.context.Fragment.FromPart.Add(new QueryTable(parser.context.WrapToFragment(), this.context.GetTableAlias(parser.ExportType)));
                if (node.Arguments.Count > 1)
                {
                    ParseWhereExpresson(node.Arguments[1]);
                }
                this.context.Fragment.SelectPart.Add(new FunctionColumn()
                {
                    Formatter = "Count(*)"
                });
                break;

            case "GroupJoin":
                ParseJoinExpression(node, JoinMode.LeftJoin);
                break;

            case "Join":
                ParseJoinExpression(node, JoinMode.InnerJoin);
                break;

            case "SelectMany":
                ParseSelectManyExpression(node);
                break;

            case "GroupBy":
                ParseGroupByExpression(node);
                break;

            case "Predicate":
                if (node.Arguments[0].Type == typeof(IDbConnection))
                {
                    var elementType = node.Type.GetGenericArguments()[0];
                    this.context.ExportType = this.context.ImportType = elementType;
                    var typeMapper = TypeMapperCache.GetTypeMapper(elementType);
                    this.context.Fragment.FromPart.Add(new SingleTable(typeMapper.TableName, this.context.GetTableAlias(elementType)));
                }
                break;

            case "DefaultIfEmpty":
                break;

            default:
                break;
            }
            return(node);
        }
        private void ParseMethodMax(MethodCallExpression node)
        {
            var column = new FunctionColumn();
            column.Formatter = "Max({0})";

            var paramVisitor = new MemberExpressionVisitor(Context);
            paramVisitor.Visit(node.Arguments[1]);
            column.Add(paramVisitor.Column);
            this.Column = column;
        }
        private void ParseMethodContains(MethodCallExpression node)
        {
            var column = new FunctionColumn();
            var objectVisitor = new MemberExpressionVisitor(Context);
            var paramVisitor = new MemberExpressionVisitor(Context);
            if (node.Object != null && (node.Object.NodeType == ExpressionType.MemberAccess || node.Object.NodeType == ExpressionType.Constant))
            {
                objectVisitor.Visit(node.Object);

                paramVisitor.Visit(node.Arguments[0]);

            }
            else
            {
                objectVisitor.Visit(node.Arguments[1]);

                paramVisitor.Visit(node.Arguments[0]);
            }

            if (objectVisitor.Column.Type == typeof(string))
            {
                column.Formatter = "LOCATE({1}, {0}) > 0";
            }
            else
            {
                column.Formatter = "{0} in {1}";
            }
            column.Add(objectVisitor.Column);
            column.Add(paramVisitor.Column);
            this.Column = column;
        }