예제 #1
0
파일: DbDataView.cs 프로젝트: shaficmd/data
        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);
            }
        }
예제 #2
0
파일: DbDataView.cs 프로젝트: shaficmd/data
        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());
        }