private MultiRootTree(int levels, List <OrderByArgument> orderingAttributes) { _levels = levels; _orderingAttributes = orderingAttributes; OrderByArgument currentOrderAttribute = orderingAttributes[orderingAttributes.Count - _levels]; _currentAttribute = currentOrderAttribute.AttributeName; _sortingOrder = currentOrderAttribute.Order; if (_sortingOrder == Order.ASC) { _nodeStore = new SortedDictionary <object, object>(); } else { _nodeStore = new SortedDictionary <object, object>(new ReverseComparer <object>()); } }
internal override void Execute(QueryContext queryContext, Predicate nextPredicate) { RecordSet resultRecordSet = new RecordSet(); foreach (string columnName in _groupByValueList.ObjectAttributes) { RecordColumn column = new RecordColumn(columnName); column.AggregateFunctionType = AggregateFunctionType.NOTAPPLICABLE; column.ColumnType = ColumnType.AttributeColumn; column.IsFilled = true; column.IsHidden = false; resultRecordSet.Columns.Add(column); } foreach (AggregateFunctionPredicate afp in _groupByValueList.AggregateFunctions) { string columnName = afp.GetFunctionType().ToString() + "(" + afp.AttributeName + ")"; if (resultRecordSet.Columns.Contains(columnName)) { throw new ArgumentException("Invalid query. Same value cannot be selected twice."); } RecordColumn column = new RecordColumn(columnName); column.IsHidden = false; column.ColumnType = ColumnType.AggregateResultColumn; column.AggregateFunctionType = afp.GetFunctionType(); column.IsFilled = true; resultRecordSet.Columns.Add(column); afp.ChildPredicate = null; } ChildPredicate.Execute(queryContext, nextPredicate); if (_orderingAttributes == null) { _orderingAttributes = new List <OrderByArgument>(_groupingAttributes.Count); foreach (string groupby in _groupingAttributes) { OrderByArgument oba = new OrderByArgument(); oba.AttributeName = groupby; _orderingAttributes.Add(oba); } } MultiRootTree groupTree = new MultiRootTree(_orderingAttributes, true); if (queryContext.InternalQueryResult.Count > 0) { foreach (string key in queryContext.InternalQueryResult) { if (queryContext.CancellationToken != null && queryContext.CancellationToken.IsCancellationRequested) { throw new OperationCanceledException(ExceptionsResource.OperationFailed); } KeyValuesContainer keyValues = new KeyValuesContainer(); keyValues.Key = key; bool invalidGroupKey = false; for (int i = 0; i < _orderingAttributes.Count; i++) { if (queryContext.CancellationToken != null && queryContext.CancellationToken.IsCancellationRequested) { throw new OperationCanceledException(ExceptionsResource.OperationFailed); } string attribute = _orderingAttributes[i].AttributeName; CacheEntry cacheentry = queryContext.Cache.GetEntryInternal(key, false); object attribValue = queryContext.Index.GetAttributeValue(key, attribute, cacheentry.IndexInfo); if (attribValue == null) { invalidGroupKey = true; break; } keyValues.Values[attribute] = attribValue; } if (!invalidGroupKey) { groupTree.Add(keyValues); } } } //add remaining attributes in Group By clause as hidden columns foreach (string attribute in _groupingAttributes) { if (!resultRecordSet.Columns.Contains(attribute)) { RecordColumn column = new RecordColumn(attribute); column.AggregateFunctionType = AggregateFunctionType.NOTAPPLICABLE; column.ColumnType = ColumnType.AttributeColumn; column.IsHidden = true; column.IsFilled = true; resultRecordSet.Columns.Add(column); } } //generates RecordSet from tree. groupTree.ToRecordSet(resultRecordSet); for (int rowID = 0; rowID < resultRecordSet.Rows.Count; rowID++) { if (queryContext.CancellationToken != null && queryContext.CancellationToken.IsCancellationRequested) { throw new OperationCanceledException(ExceptionsResource.OperationFailed); } List <string> keysList = resultRecordSet.Rows[rowID].Tag as List <string>; int j = 0; queryContext.InternalQueryResult = new Common.Queries.ListQueryResult(queryContext.KeyFilter, queryContext.CompoundFilter, keysList);//Union(keysList as IEnumerable<string>); foreach (AggregateFunctionPredicate afp in this._groupByValueList.AggregateFunctions) { if (queryContext.CancellationToken != null && queryContext.CancellationToken.IsCancellationRequested) { throw new OperationCanceledException(ExceptionsResource.OperationFailed); } afp.Execute(queryContext, null); int columnId = _groupByValueList.ObjectAttributes.Count + j++; if (resultRecordSet.Columns[columnId].DataType == ColumnDataType.Object) { resultRecordSet.Columns[columnId].DataType = RecordSet.ToColumnDataType(queryContext.ResultSet.AggregateFunctionResult.Value); } resultRecordSet.Rows[rowID][columnId] = queryContext.ResultSet.AggregateFunctionResult.Value; } } ReaderResultSet readerResult = new ReaderResultSet(); readerResult.IsGrouped = true; readerResult.OrderByArguments = _orderingAttributes; readerResult.RecordSet = resultRecordSet; queryContext.ResultSet.Type = QueryType.GroupByAggregateFunction; queryContext.ResultSet.ReaderResult = readerResult; }