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);
        }
        public static SelectStatement Visit(QueryModel queryModel, DatabaseMapper mapper, bool aliasTables)
        {
            var visitor = new StatementCreator(mapper, aliasTables);

            queryModel.Accept(visitor);
            return(visitor.SelectStatement);
        }
 private OrderByExpression PropertyToOrderBy(FieldOrder field, DatabaseMapper mapper)
 {
     return(new OrderByExpression(
                new Column(
                    TableNameOrAlias(mapper, field.Field.DeclaringType),
                    mapper.GetColumnName(field.Field)), field.Direction));
 }
Exemplo n.º 4
0
        public InsertStatement CreateStatement(DatabaseMapper mapper)
        {
            var insert = new InsertStatement();

            insert.Target = new Table(mapper.GetTableName(this.Target));
            insert.SetValues.AddRange(this.SetValues.Select(sv => new SetValue(new Column(mapper.GetColumnName(sv.Item1)), sv.Item2)));
            return(insert);
        }
 private SetValue PropertyToSetValue(FieldValue sv, DatabaseMapper mapper)
 {
     return(new SetValue(
                new Column(
                    mapper.GetTableName(sv.Field.DeclaringType),
                    mapper.GetColumnName(sv.Field)),
                sv.Value));
 }
Exemplo n.º 6
0
        public override Statement CreateStatement(DatabaseMapper mapper)
        {
            var insert = new InsertStatement();

            insert.Target = new Table(mapper.GetTableName(this.Target));
            insert.SetValues.AddRange(this.SetValues.Select(sv => PropertyToSetValue(sv, mapper)));
            return(insert);
        }
 private SourceExpression PropertyToAggregate(FieldAggregate field, DatabaseMapper mapper)
 {
     return(new Aggregate(
                field.Aggregate,
                new Column(
                    field.Field != null ? TableNameOrAlias(mapper, field.Field.DeclaringType) : "",
                    field.Field != null ? mapper.GetColumnName(field.Field) : "*")
                ));
 }
 private string TableNameOrAlias(DatabaseMapper mapper, Type t)
 {
     if (t == this.Source.Type && !string.IsNullOrEmpty(this.Source.Alias))
     {
         return(this.Source.Alias);
     }
     else
     {
         return(mapper.GetTableName(t));
     }
 }
 private SourceExpression PropertyToSourceField(PropertyInfo prop, DatabaseMapper mapper)
 {
     if (prop != null)
     {
         return(new Column(TableNameOrAlias(mapper, prop.DeclaringType), mapper.GetColumnName(prop)));
     }
     else
     {
         return(new ConstantPart(null));
     }
 }
Exemplo n.º 10
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);
        }
        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);
        }
        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);
        }
        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);
        }
 private Column PropertyToGroupBy(PropertyInfo prop, DatabaseMapper mapper)
 {
     return(new Column(
                TableNameOrAlias(mapper, prop.DeclaringType),
                mapper.GetColumnName(prop)));
 }
 public abstract Statement CreateStatement(DatabaseMapper mapper);
        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);
        }
 private StatementCreator(DatabaseMapper mapper, bool aliasTables)
 {
     this.Configuration   = mapper;
     this.AliasTables     = aliasTables;
     this.SelectStatement = new SelectStatement();
 }
Exemplo n.º 18
0
 public Command Build(DatabaseMapper mapper, ICommandBuilder builder)
 {
     builder.VisitStatement(this, mapper);
     return(new Command(this, builder.CommandText.ToString(), builder.ParameterValues.ToArray()));
 }
Exemplo n.º 19
0
 public Command Build(DatabaseMapper mapper)
 {
     return(Build(mapper, new SqlCommandBuilder()));
 }