예제 #1
0
 internal QueryExpressionParser(IDbConnection connection)
 {
     this.connection = connection;
     this.context    = new ExpressionVisitorContext(connection)
     {
         UseTableAlias = true
     };
 }
예제 #2
0
        private void ParseGroupByExpression(MethodCallExpression node)
        {
            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)));
            }

            var groupVisitor = new GroupByExpressionVisitor(this.context);

            groupVisitor.Parse(node.Arguments[1]);
            this.context.Fragment.GroupPart = groupVisitor.Columns;
        }
예제 #3
0
        private void ParseSelectManyExpression(Expression node)
        {
            var methodExpression = node as MethodCallExpression;

            Visit(methodExpression.Arguments[0]);
            if (!this.context.Fragment.HasOnlyParts(QueryPart.From))
            {
                var oldContext = this.context;
                this.context = oldContext.CopyTo();
                var queryTable = new QueryTable(oldContext.WrapToFragment(), this.context.GetTableAlias(oldContext.ExportType));
                this.context.Fragment.FromPart.Add(queryTable);
            }

            var joinContext = this.context.CopyTo();
            var joinParser  = new QueryExpressionParser(this.connection, joinContext);

            joinParser.Parse(methodExpression.Arguments[1]);

            Table table = null;

            if (joinContext.Fragment.HasOnlyParts(QueryPart.From) && joinContext.Fragment.PartsCount(QueryPart.From) == 1)
            {
                table       = joinContext.Fragment.FromPart.First();
                table.Alias = this.context.GetTableAlias(joinContext.ExportType);
            }
            else if (joinContext.Fragment.HasAnyParts())
            {
                table = new QueryTable(joinContext.WrapToFragment(), this.context.GetTableAlias(joinContext.ExportType));
            }

            var joinVisitor = new JoinExpressionVisitor(this.context);

            joinVisitor.Parse(methodExpression.Arguments[2]);

            if (table != null)
            {
                this.context.Fragment.FromPart.Add(new JoinTable(table)
                {
                    JoinMode = JoinMode.Default
                });
            }
        }
예제 #4
0
        private void ParseJoinExpression(Expression node, JoinMode mode)
        {
            var methodExpression = node as MethodCallExpression;

            Visit(methodExpression.Arguments[0]);
            if (!this.context.Fragment.HasOnlyParts(QueryPart.From))
            {
                var oldContext = this.context;
                this.context = oldContext.CopyTo();
                var queryTable = new QueryTable(oldContext.WrapToFragment(), this.context.GetTableAlias(oldContext.ExportType));
                this.context.Fragment.FromPart.Add(queryTable);
            }

            var joinContext = this.context.CopyTo();
            var joinParser  = new QueryExpressionParser(this.connection, joinContext);

            joinParser.Parse(methodExpression.Arguments[1]);

            Table table;

            if (joinContext.Fragment.HasOnlyParts(QueryPart.From) && joinContext.Fragment.FromPart.Count == 1)
            {
                table       = joinContext.Fragment.FromPart.First();
                table.Alias = this.context.GetTableAlias(joinContext.ExportType);
            }
            else
            {
                table = new QueryTable(joinContext.WrapToFragment(), this.context.GetTableAlias(joinContext.ExportType));
            }

            var joinVisitor = new JoinExpressionVisitor(this.context);

            joinVisitor.Parse(methodExpression.Arguments[2], methodExpression.Arguments[3], methodExpression.Arguments[4]);
            this.context.Fragment.FromPart.Add(new JoinTable(table)
            {
                JoinMode = mode, JoinCondition = joinVisitor.Condition
            });
        }
예제 #5
0
 //public Token Token
 //{
 //    get;
 //    protected set;
 //}
 //public object ExtraObject
 //{
 //    get;
 //    protected set;
 //}
 internal ExpressionVisitorBase(ExpressionVisitorContext context)
 {
     this.Context = context;
 }
예제 #6
0
 internal MemberExpressionVisitor(ExpressionVisitorContext context, bool useColumnAlias)
     : base(context)
 {
     UseColumnAlias = useColumnAlias;
 }
예제 #7
0
 internal MemberExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
     UseColumnAlias = false;
 }
예제 #8
0
 internal BinaryExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
 }
예제 #9
0
 public SelectExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
 }
 internal MethodCallExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
 }
예제 #11
0
 internal MemberExpressionVisitor(ExpressionVisitorContext context, bool useColumnAlias)
     : base(context)
 {
     UseColumnAlias = useColumnAlias;
 }
예제 #12
0
 internal QueryExpressionParser(IDbConnection connection, ExpressionVisitorContext context)
 {
     this.connection = connection;
     this.context    = context;
 }
예제 #13
0
 public WhereExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
 }
예제 #14
0
 public JoinExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
     leftColumns  = new List <Column>();
     rightColumns = new List <Column>();
 }
예제 #15
0
 public GroupByExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
     Columns = new List <Column>();
 }
예제 #16
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);
        }
예제 #17
0
 internal MemberExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
     UseColumnAlias = false;
 }
예제 #18
0
        //public Token Token
        //{
        //    get;
        //    protected set;
        //}

        //public object ExtraObject
        //{
        //    get;
        //    protected set;
        //}

        internal ExpressionVisitorBase(ExpressionVisitorContext context)
        {
            this.Context = context;
        }
예제 #19
0
 internal MethodCallExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
 }
예제 #20
0
 internal BinaryExpressionVisitor(ExpressionVisitorContext context)
     : base(context)
 {
 }