Ejemplo n.º 1
0
        string BuildGroupBy(Query query, ISqlExpressionBuilder sqlBuilder)
        {
            if (query.Fields == null || query.Fields.Length == 0 || !query.Fields.Any(isAggregateField))
            {
                return(null);
            }
            var groupByCols = new StringBuilder();

            foreach (var qField in query.Fields)
            {
                if (!isAggregateField(qField))
                {
                    var f = qField;
                    if (FieldMapping != null && FieldMapping.ContainsKey(f.Name))
                    {
                        f = new QField(f.Name, FieldMapping[f.Name]);
                    }

                    if (groupByCols.Length > 0)
                    {
                        groupByCols.Append(',');
                    }
                    groupByCols.Append(sqlBuilder.BuildValue((IQueryValue)f));
                }
            }
            return(groupByCols.Length > 0 ? groupByCols.ToString() : null);

            bool isAggregateField(QField fld)
            {
                if (fld is QAggregateField)
                {
                    return(true);
                }
                // special handling for count(*)
                if (fld.Expression != null && fld.Expression.ToLower() == QField.Count.Expression)
                {
                    return(true);
                }
                return(false);
            }
        }
Ejemplo n.º 2
0
        string BuildSelectColumns(Query query, ISqlExpressionBuilder sqlBuilder)
        {
            // Compose fields part
            if (query.Fields == null || query.Fields.Length == 0)
            {
                return(ApplyFieldMapping("*"));
            }

            var columns = new StringBuilder();

            foreach (var qField in query.Fields)
            {
                var f = qField;
                if (FieldMapping != null && FieldMapping.ContainsKey(f.Name))
                {
                    f = new QField(f.Name, FieldMapping[f.Name]);
                }

                var fld = sqlBuilder.BuildValue((IQueryValue)f);
                if (fld != f.Name && f.Expression != null)               // use "as" only for expression-fields
                // special handling for 'count(*)' mapping
                {
                    if (f.Expression.ToLower() == "count(*)")
                    {
                        fld = ApplyFieldMapping("count(*)");
                    }
                    fld = String.IsNullOrEmpty(f.Name) ? fld : String.Format("{0} as {1}", fld, sqlBuilder.BuildValue((QField)f.Name));
                }
                if (columns.Length > 0)
                {
                    columns.Append(',');
                }
                columns.Append(fld);
            }
            return(columns.ToString());
        }