public string Compile(IColumnSelector value, SelectQuery query, IQueryParameterManager parameters)
        {
            var select = value as ColumnDatePartSelector;


            var format = "DATEADD({0},0, DATEDIFF({0},0, {1}{2}{3}))";

            if (select.Aggregate != Aggregate.None)
            {
                var isInGroupBy = AggregateHelpers.IsInGroupBy(query, select);

                // if its in the group by, dont aggregate it
                if (!isInGroupBy)
                {
                    return(string.Format("{5}(" + format + ") AS {4}",
                                         select.DatePart.ToSqlString(),
                                         select.TableAlias,
                                         string.IsNullOrWhiteSpace(select.TableAlias) ? null : ".",
                                         select.Field.Name,
                                         select.Alias,
                                         select.Aggregate.ToSqlString()
                                         ));
                }
            }

            return(string.Format(format + " AS {4}",
                                 select.DatePart.ToSqlString(),
                                 select.TableAlias,
                                 string.IsNullOrWhiteSpace(select.TableAlias) ? null : ".",
                                 select.Field.Name,
                                 select.Alias
                                 ));
        }
        public string Compile(IColumnSelector value, SelectQuery query, IQueryParameterManager parameters)
        {
            var select = value as ColumnSelector;

            if (select.Aggregate != Aggregate.None)
            {
                var isInGroupBy = AggregateHelpers.IsInGroupBy(query, select);

                // if its in the group by, dont aggregate it
                if (!isInGroupBy)
                {
                    return(string.Format("{0}({1}{2}{3}{4}) AS {5}",
                                         select.Aggregate.ToSqlString(),
                                         select.Aggregate == Aggregate.Bit || select.Aggregate == Aggregate.BitMax ? "0+" : null, // fix bit field aggregation for nulls
                                         select.TableAlias,
                                         string.IsNullOrWhiteSpace(select.TableAlias) ? null : ".",
                                         select.Field.Name,
                                         select.Alias));
                }
            }

            return(string.Format("{0}{1}{2} AS {3}",
                                 select.TableAlias,
                                 string.IsNullOrWhiteSpace(select.TableAlias) ? null : ".",
                                 select.Field.Name,
                                 select.Alias));
        }
        public virtual string CompileOrderBy(SelectQuery selectQuery)
        {
            if (!selectQuery.OrderByColumns.Any())
            {
                return(null);
            }

            string result = "ORDER BY ";

            foreach (var orderBy in selectQuery.OrderByColumns)
            {
                var fieldSelector = orderBy.FullName;

                bool isInGroupBy = false;
                // need to deal with aggregates
                if (orderBy.Aggregate != Aggregate.None)
                {
                    isInGroupBy = AggregateHelpers.IsInGroupBy(selectQuery, orderBy);
                }
                if (!isInGroupBy && selectQuery.GroupByColumns.Count > 0)
                {
                    fieldSelector = string.Format("{0}({1}{2}{3}{4})",
                                                  orderBy.Aggregate.ToSqlString(),
                                                  orderBy.Aggregate == Aggregate.Bit || orderBy.Aggregate == Aggregate.BitMax ? "0+" : null, // fix bit field aggregation for nulls
                                                  orderBy.TableAlias,
                                                  string.IsNullOrWhiteSpace(orderBy.TableAlias) ? null : ".",
                                                  orderBy.Field.Name
                                                  );
                }


                result += string.Format("\n\t {0} {1} ,",
                                        fieldSelector,
                                        orderBy.Direction.ToSqlString()
                                        );
            }

            result = result.TrimEnd(',');

            return(result);
        }