コード例 #1
0
        public static ConditionCollection VisitStatementConditions <T>(Expression <Func <T, bool> > conditions, DatabaseMapper mapper, bool aliasTables)
        {
            // Build a new query
            var queryParser   = QueryParser.CreateDefault();
            var queryExecutor = new StatementExecutor();
            var query         = new StatementQuery <T>(queryParser, queryExecutor);

            // Create an expression to select from the query with the conditions so that we have a sequence for Re-Linq to parse
            var expression = Expression.Call(
                typeof(Queryable),
                "Where",
                new Type[] { query.ElementType },
                query.Expression,
                conditions);

            // Parse the expression with Re-Linq
            var queryModel = queryParser.GetParsedQuery(expression);

            // Get the conditions from the query model
            var visitor = new StatementCreator(mapper, aliasTables);

            visitor.SelectStatement = new SelectStatement();
            queryModel.Accept(visitor);
            return(visitor.SelectStatement.Conditions);
        }
コード例 #2
0
        public static SelectStatement Visit(QueryModel queryModel, DatabaseMapper mapper, bool aliasTables)
        {
            var visitor = new StatementCreator(mapper, aliasTables);

            queryModel.Accept(visitor);
            return(visitor.SelectStatement);
        }
コード例 #3
0
        public override Statement CreateStatement(DatabaseMapper mapper)
        {
            var select = new SelectStatement();

            select.Source = new Table(mapper.GetTableName(this.Source.Type), this.Source.Alias);
            select.SourceFields.AddRange(this.SourceFields.Select(s => PropertyToSourceField(s, mapper)));
            select.SourceFields.AddRange(this.AggregateFields.Select(s => PropertyToAggregate(s, mapper)));
            select.IsAny      = this.IsAny;
            select.IsAll      = this.IsAll;
            select.IsDistinct = this.IsDistinct;
            select.StartIndex = this.StartIndex;
            select.Limit      = this.Limit;
            if (this.Conditions != null)
            {
                // TODO: Need to handle columns from multiple tables...
                var aliasTables = !string.IsNullOrEmpty(this.Source.Alias);
                foreach (var condition in StatementCreator.VisitStatementConditions(this.Conditions, mapper, aliasTables))
                {
                    select.Conditions.Add(condition);
                }
            }
            select.OrderByFields.AddRange(this.OrderByFields.Select(s => PropertyToOrderBy(s, mapper)));
            select.GroupByFields.AddRange(this.GroupByFields.Select(s => PropertyToGroupBy(s, mapper)));
            return(select);
        }
コード例 #4
0
        public override Statement CreateStatement(DatabaseMapper mapper)
        {
            var delete = new DeleteStatement();

            delete.Target = new Table(mapper.GetTableName(this.Target));
            foreach (var condition in StatementCreator.VisitStatementConditions(this.Conditions, mapper, false))
            {
                delete.Conditions.Add(condition);
            }
            return(delete);
        }
コード例 #5
0
        public UpdateStatement CreateStatement(DatabaseMapper mapper)
        {
            var update = new UpdateStatement();

            update.Target = new Table(mapper.GetTableName(this.Target));
            update.SetValues.AddRange(this.SetValues.Select(sv => new SetValue(new Column(mapper.GetTableName(sv.Item1.DeclaringType), mapper.GetColumnName(sv.Item1)), sv.Item2)));
            foreach (var condition in StatementCreator.VisitStatementConditions(this.Conditions, mapper, false))
            {
                update.Conditions.Add(condition);
            }
            return(update);
        }
コード例 #6
0
        public override Statement CreateStatement(DatabaseMapper mapper)
        {
            var update = new UpdateStatement();

            update.Target = new Table(mapper.GetTableName(this.Target));
            update.SetValues.AddRange(this.SetValues.Select(sv => PropertyToSetValue(sv, mapper)));
            foreach (var condition in StatementCreator.VisitStatementConditions(this.Conditions, mapper, false))
            {
                update.Conditions.Add(condition);
            }
            return(update);
        }
コード例 #7
0
        public SelectStatement CreateStatement(DatabaseMapper mapper)
        {
            var select = new SelectStatement();

            select.Source = new Table(mapper.GetTableName(this.Source.Type), this.Source.Alias);
            select.SourceFields.AddRange(this.SourceFields.Select(s => new Column(TableNameOrAlias(mapper, s.DeclaringType), mapper.GetColumnName(s))));
            select.SourceFields.AddRange(this.AggregateFields.Select(s => new Aggregate(s.Item2, new Column(s.Item1 != null ? TableNameOrAlias(mapper, s.Item1.DeclaringType) : "", s.Item1 != null ? mapper.GetColumnName(s.Item1) : "*"))));
            select.IsAny      = this.IsAny;
            select.IsAll      = this.IsAll;
            select.IsDistinct = this.IsDistinct;
            select.StartIndex = this.StartIndex;
            select.Limit      = this.Limit;
            if (this.Conditions != null)
            {
                // TODO: Need to handle columns from multiple tables...
                var aliasTables = !string.IsNullOrEmpty(this.Source.Alias);
                foreach (var condition in StatementCreator.VisitStatementConditions(this.Conditions, mapper, aliasTables))
                {
                    select.Conditions.Add(condition);
                }
            }
            select.OrderByFields.AddRange(this.OrderByFields.Select(s => new OrderByExpression(new Column(TableNameOrAlias(mapper, s.Item1.DeclaringType), mapper.GetColumnName(s.Item1)), s.Item2)));
            return(select);
        }