private void AnalyzeGroupByClause(QupidCollection collection, GroupByClause group) { if (group != null) { if (group.Property.IsAggregate) { _compiler.ErrorManager.AddError("Cannot 'group by' an aggregate property " + group.Property.Path); return; } var shortName = collection.ConvertToShortPath(group.Property.Path); if (shortName == null) { _compiler.ErrorManager.AddError("The 'group by' property (" + group.Property.Path + ") is invalid."); return; } var dbProp = collection.GetProperty(group.Property.Path); if (dbProp.Type == "DateTime") { _compiler.ErrorManager.AddWarning("You really want to group by a DateTime value? Really?"); } group.Property.Alias = "_id"; group.Property.AnalyzedName = shortName; group.AggregateByProperty = group.Property; if (_query.SelectProperties.Properties.Count(pr => pr.IsAggregate) > 1) { Fail("You can only include one 'COUNT' or 'SUM' property in your 'select'"); return; } group.AggregationProperty = _query.SelectProperties.Properties.SingleOrDefault(pr => pr.IsAggregate); if (group.AggregationProperty == null) { _compiler.ErrorManager.AddWarning("The aggregation property was not specified. Include a '.COUNT' or '.SUM' in your select clause"); } else if (group.AggregationProperty.AggregateType != AggregateTypes.Count) { var aggShortName = collection.ConvertToShortPath(group.AggregationProperty.Path); if (aggShortName == null) { Fail("The Sum property is invalid - please append '.SUM' to the end of a valid numeric property name (ex: Donations.TotalAmount.SUM)."); return; } group.AggregationProperty.AnalyzedName = aggShortName; } //get only the first segment of the index (because the groupby must match that part currently) var indexFirstProperties = collection.Indices.Select(i => i.ShortProperties.First()); var usesIndex = indexFirstProperties.Contains(shortName); if (!usesIndex && collection.NumberOfRows < _compiler.MaxCollectionSizeWithNoIndex) { _compiler.ErrorManager.AddWarning("Group by (" + group.Property.Path + ") doesn't use an index. Running with caution"); } else if (!usesIndex) { Fail("Group by (" + group.Property.Path + ") doesn't use an index. Collection too large to allow"); } } }
public QupidQuery(PropertyList pl, string collection, List<WhereClause> where, UnwindClause unwind, GroupByClause groupBy, HavingClause have, WithClause with) { CollectionName = collection; SelectProperties = pl; WhereClauses = where; UnwindClause = unwind; GroupByClause = groupBy; HavingClause = have; WithClause = with; }