Beispiel #1
0
        internal override void Execute(QueryContext queryContext, Predicate nextPredicate)
        {
            if (ChildPredicate != null)
            {
                ChildPredicate.Execute(queryContext, nextPredicate);
            }

            CacheEntry entry = null;

            IComparable max         = null;
            bool        initialized = false;
            Type        type        = null;

            foreach (string key in queryContext.InternalQueryResult)
            {
                if (queryContext.CancellationToken != null && queryContext.CancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException(ExceptionsResource.OperationFailed);
                }
                CacheEntry  cacheentry = queryContext.Cache.GetEntryInternal(key, false);
                IComparable current    = (IComparable)queryContext.Index.GetAttributeValue(key, AttributeName, cacheentry.IndexInfo);

                if (current != null)
                {
                    type = current.GetType();
                    if (type == typeof(bool))
                    {
                        throw new Exception("MAX cannot be applied to Boolean data type.");
                    }

                    if (!initialized)
                    {
                        max         = current;
                        initialized = true;
                    }

                    if (current.CompareTo(max) > 0)
                    {
                        max = current;
                    }
                }
            }
            if (type != null)
            {
                if ((type != typeof(DateTime)) && (type != typeof(string)) && (type != typeof(char)))
                {
                    if (max != null)
                    {
                        base.SetResult(queryContext, AggregateFunctionType.MAX, Convert.ToDecimal(max));
                        return;
                    }
                }
            }
            base.SetResult(queryContext, AggregateFunctionType.MAX, max);
        }
Beispiel #2
0
        internal override void Execute(QueryContext queryContext, Predicate nextPredicate)
        {
            if (ChildPredicate != null)
            {
                ChildPredicate.Execute(queryContext, nextPredicate);
            }

            decimal count = queryContext.InternalQueryResult.Count;

            base.SetResult(queryContext, AggregateFunctionType.COUNT, count);
        }
Beispiel #3
0
        internal override void Execute(QueryContext queryContext, Predicate nextPredicate)
        {
            if (ChildPredicate != null)
            {
                ChildPredicate.Execute(queryContext, nextPredicate);
            }

            queryContext.Tree.Reduce();
            CacheEntry entry = null;

            IComparable max         = null;
            bool        initialized = false;
            Type        type        = null;

            foreach (string key in queryContext.Tree.LeftList)
            {
                IComparable current = (IComparable)queryContext.Index.GetAttributeValue(key, AttributeName);

                if (current != null)
                {
                    type = current.GetType();
                    if (type == typeof(bool))
                    {
                        throw new Exception("MAX cannot be applied to Boolean data type.");
                    }

                    if (!initialized)
                    {
                        max         = current;
                        initialized = true;
                    }

                    if (current.CompareTo(max) > 0)
                    {
                        max = current;
                    }
                }
            }
            if (type != null)
            {
                if ((type != typeof(DateTime)) && (type != typeof(string)) && (type != typeof(char)))
                {
                    if (max != null)
                    {
                        base.SetResult(queryContext, AggregateFunctionType.MAX, Convert.ToDecimal(max));
                        return;
                    }
                }
            }
            base.SetResult(queryContext, AggregateFunctionType.MAX, max);
        }
Beispiel #4
0
        internal override void Execute(QueryContext queryContext, Predicate nextPredicate)
        {
            if (ChildPredicate != null)
            {
                ChildPredicate.Execute(queryContext, nextPredicate);
            }

            CacheEntry entry = null;

            bool        initialized = false;
            IComparable min         = null;
            Type        type        = null;

            foreach (string key in queryContext.InternalQueryResult)
            {
                CacheEntry  cacheentry = queryContext.Cache.GetEntryInternal(key, false);
                IComparable current    = (IComparable)queryContext.Index.GetAttributeValue(key, AttributeName, cacheentry.IndexInfo);

                if (current != null)
                {
                    type = current.GetType();
                    if (type == typeof(bool))
                    {
                        throw new Exception("MIN cannot be applied to Boolean data type.");
                    }

                    if (!initialized)
                    {
                        min         = current;
                        initialized = true;
                    }
                    if (current.CompareTo(min) < 0)
                    {
                        min = current;
                    }
                }
            }
            if (type != null)
            {
                if ((type != typeof(DateTime)) && (type != typeof(string)) && (type != typeof(char)))
                {
                    if (min != null)
                    {
                        base.SetResult(queryContext, AggregateFunctionType.MIN, Convert.ToDecimal(min));
                        return;
                    }
                }
            }
            base.SetResult(queryContext, AggregateFunctionType.MIN, min);
        }
        internal override void Execute(QueryContext queryContext, Predicate nextPredicate)
        {
            if (ChildPredicate != null)
            {
                ChildPredicate.Execute(queryContext, nextPredicate);
            }

            CacheEntry entry = null;

            decimal sum = 0;

            if (queryContext.InternalQueryResult.Count > 0)
            {
                if (queryContext.CancellationToken != null && queryContext.CancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException(ExceptionsResource.OperationFailed);
                }
                foreach (string key in queryContext.InternalQueryResult)
                {
                    if (queryContext.CancellationToken != null && queryContext.CancellationToken.IsCancellationRequested)
                    {
                        throw new OperationCanceledException(ExceptionsResource.OperationFailed);
                    }
                    CacheEntry cacheentry  = queryContext.Cache.GetEntryInternal(key, false);
                    object     attribValue = queryContext.Index.GetAttributeValue(key, AttributeName, cacheentry.IndexInfo);
                    if (attribValue != null)
                    {
                        Type type = attribValue.GetType();
                        if ((type == typeof(bool)) || (type == typeof(DateTime)) || (type == typeof(string)) || (type == typeof(char)))
                        {
                            throw new Exception("AVG can only be applied to integral data types.");
                        }

                        sum += Convert.ToDecimal(attribValue);
                    }
                }

                AverageResult avgResult = new AverageResult();
                avgResult.Sum   = sum;
                avgResult.Count = queryContext.InternalQueryResult.Count;
                //put the count and the sum
                base.SetResult(queryContext, AggregateFunctionType.AVG, avgResult);
            }
            else
            {
                base.SetResult(queryContext, AggregateFunctionType.AVG, null);
            }
        }
Beispiel #6
0
        internal override void Execute(QueryContext queryContext, Predicate nextPredicate)
        {
            if (ChildPredicate != null)
            {
                ChildPredicate.Execute(queryContext, nextPredicate);
            }

            queryContext.Tree.Reduce();
            CacheEntry entry = null;

            decimal sum = 0;

            if (queryContext.Tree.LeftList.Count > 0)
            {
                foreach (string key in queryContext.Tree.LeftList)
                {
                    object attribValue = queryContext.Index.GetAttributeValue(key, AttributeName);
                    if (attribValue != null)
                    {
                        Type type = attribValue.GetType();
                        if ((type == typeof(bool)) || (type == typeof(DateTime)) || (type == typeof(string)) || (type == typeof(char)))
                        {
                            throw new Exception("AVG can only be applied to integral data types.");
                        }

                        sum += Convert.ToDecimal(attribValue);
                    }
                }

                AverageResult avgResult = new AverageResult();
                avgResult.Sum   = sum;
                avgResult.Count = queryContext.Tree.LeftList.Count;
                //put the count and the sum
                base.SetResult(queryContext, AggregateFunctionType.AVG, avgResult);
            }
            else
            {
                base.SetResult(queryContext, AggregateFunctionType.AVG, null);
            }
        }
Beispiel #7
0
        internal override void Execute(QueryContext queryContext, Predicate nextPredicate)
        {
            if (ChildPredicate != null)
            {
                ChildPredicate.Execute(queryContext, nextPredicate);
            }

            queryContext.Tree.Reduce();
            CacheEntry entry = null;

            decimal sum = 0;

            if (queryContext.Tree.LeftList.Count > 0)
            {
                foreach (string key in queryContext.Tree.LeftList)
                {
                    CacheEntry cacheentry  = queryContext.Cache.GetEntryInternal(key, false);
                    object     attribValue = queryContext.Index.GetAttributeValue(key, AttributeName, cacheentry.IndexInfo);

                    if (attribValue != null)
                    {
                        Type type = attribValue.GetType();
                        if ((type == typeof(bool)) || (type == typeof(DateTime)) || (type == typeof(string)) || (type == typeof(char)))
                        {
                            throw new Exception("SUM can only be applied to integral data types.");
                        }

                        sum += Convert.ToDecimal(attribValue);
                    }
                }

                base.SetResult(queryContext, AggregateFunctionType.SUM, Decimal.Round(sum, 4));
            }
            else
            {
                base.SetResult(queryContext, AggregateFunctionType.SUM, null);
            }
        }
        internal override void Execute(QueryContext queryContext, Predicate nextPredicate)
        {
            ChildPredicate.Execute(queryContext, nextPredicate);

            RecordSet resultRecordSet = new RecordSet();

            RecordColumn keyColumn = new RecordColumn(QueryKeyWords.KeyColumn);

            keyColumn.ColumnType            = ColumnType.KeyColumn;
            keyColumn.AggregateFunctionType = AggregateFunctionType.NOTAPPLICABLE;
            keyColumn.DataType = ColumnDataType.String;
            keyColumn.IsHidden = false;
            keyColumn.IsFilled = true;

            resultRecordSet.Columns.Add(keyColumn);

            RecordColumn valueColumn = new RecordColumn(QueryKeyWords.ValueColumn);

            valueColumn.ColumnType            = ColumnType.ValueColumn;
            valueColumn.AggregateFunctionType = AggregateFunctionType.NOTAPPLICABLE;
            valueColumn.DataType = ColumnDataType.CompressedValueEntry;
            valueColumn.IsHidden = true;
            valueColumn.IsFilled = false;

            resultRecordSet.Columns.Add(valueColumn);

            foreach (OrderByArgument orderBy in _orderByArguments)
            {
                RecordColumn column = new RecordColumn(orderBy.AttributeName);
                column.AggregateFunctionType = AggregateFunctionType.NOTAPPLICABLE;
                column.ColumnType            = ColumnType.AttributeColumn;
                column.IsFilled = true;
                column.IsHidden = true;

                resultRecordSet.Columns.Add(column);
            }

            MultiRootTree sortingTree = new MultiRootTree(_orderByArguments, false);

            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 < _orderByArguments.Count; i++)
                    {
                        if (queryContext.CancellationToken != null && queryContext.CancellationToken.IsCancellationRequested)
                        {
                            throw new OperationCanceledException(ExceptionsResource.OperationFailed);
                        }
                        string     attribute  = _orderByArguments[i].AttributeName;
                        CacheEntry cacheentry = queryContext.Cache.GetEntryInternal(key, false);
                        if (cacheentry == null)
                        {
                            invalidGroupKey = true;
                            break;
                        }

                        object attribValue = queryContext.Index.GetAttributeValue(key, attribute, cacheentry.IndexInfo);
                        if (attribValue == null)
                        {
                            invalidGroupKey = true;
                            break;
                        }
                        keyValues.Values[attribute] = attribValue;
                    }

                    if (!invalidGroupKey)
                    {
                        sortingTree.Add(keyValues);
                    }
                }
            }

            //generates RecordSet from tree.
            sortingTree.ToRecordSet(resultRecordSet);

            ReaderResultSet readerResult = new ReaderResultSet();

            readerResult.IsGrouped              = false;
            readerResult.OrderByArguments       = _orderByArguments;
            readerResult.RecordSet              = resultRecordSet;
            queryContext.ResultSet.Type         = QueryType.OrderByQuery;
            queryContext.ResultSet.ReaderResult = readerResult;
        }
        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;
        }