Example #1
0
            public void ToSql(ISqlFormatter sql, Joins aliases, List <string> output)
            {
                var table = Key.To.Table.ToSql(sql);
                var alias = aliases[Key.To.Table];
                var left  = sql.IdentifierPair(alias, Key.To.Table.Id.DbName);
                var right = sql.IdentifierPair(aliases[Key.Table], Key.DbName);

                output.Add($"join {table} {alias} on {left} = {right}");

                foreach (var join in Joins)
                {
                    join.ToSql(sql, aliases, output);
                }
            }
Example #2
0
        public string ToSql(
            ISqlFormatter sql,
            IEnumerable <IColumn> selectColumns,
            IEnumerable <Filter> outerFilters,
            IFilterParameters filterParams,
            IEnumerable <Ordering> orderings = null,
            long?skip = null,
            int?take  = null)
        {
            var joins = new Joins();

            var selects = selectColumns?.Select((c, i) =>
                                                $"{sql.IdentifierPair(joins[c.Table], c.DbName)} Select{i}").ToList()
                          ?? new List <string>();

            var aggColumn = Column != null ? $"{Function}({joins.Aliased(Column, sql)})" : null;

            if (aggColumn != null)
            {
                selects.Add($"{aggColumn} Value0");
            }

            if (selects.Count == 0)
            {
                throw new InvalidOperationException("Must select something");
            }

            var filters = outerFilters.Concat(Filters).Select(f => new
            {
                Column = joins.Aliased(f.Column, sql),
                f.Operator,
                Param = filterParams[f]
            })
                          .ToList();

            var groupBy = selectColumns?.Select(x => new
            {
                Part = joins.Aliased(x, sql)
            })
                          .ToList();

            var skipAndTake = skip != null && take != null
                    ? sql.SkipAndTake(skip.Value, take.Value)
                    : null;

            var orderBy = skipAndTake == null ? null :
                          orderings.Any() ? string.Join(", ", orderings.Select(x => $"{joins.Aliased(x.Column, sql)} {x.Direction}")) :
                          aggColumn != null ? $"{aggColumn} desc" :
                          null;

            return(_template(new
            {
                selects,
                filters,
                joins = joins.ToSql(sql),
                groupBy,
                orderBy,
                skipAndTake
            }));
        }
Example #3
0
 public string ToSql(ISqlFormatter sql) => sql.IdentifierPair(Schema.DbName, DbName);
Example #4
0
 public string Aliased(IColumn column, ISqlFormatter sql)
 => sql.IdentifierPair(this[column.Table], column.DbName);