Ejemplo n.º 1
0
        public object Combine(object value)
        {
            if (!(value is IJSONDocument))
            {
                throw new Exception("At MaxDataCombiner: Document needs to be in IJSONDocument format");
            }

            IJSONDocument document = (IJSONDocument)value;

            IJSONDocument updateDoc = new JSONDocument();

            foreach (var attribute in _attributes)
            {
                object value1 = document[_userDefinedName];
                object value2 = _document[_userDefinedName];
                int    comparer;

                if (value1 is IComparable)
                {
                    comparer = JSONComparer.Compare((IComparable)value1, value2);
                }
                else
                {
                    comparer = JsonWrapper.Wrap(value1).CompareTo(JsonWrapper.Wrap(value2));
                }

                if (comparer > 0)
                {
                    updateDoc.Add(_userDefinedName, value1);
                }
            }
            JsonDocumentUtil.Update(_document, updateDoc);
            //_document.Update(updateDoc);
            return(_document);
        }
Ejemplo n.º 2
0
        public object Combine(object value)
        {
            if (!(value is IJSONDocument))
            {
                throw new Exception("At AverageDataCombiner: Document needs to be in IJSONDocument format");
            }

            bool          isUpdateApplicable = false;
            IJSONDocument document           = (IJSONDocument)value;
            IJSONDocument updateDoc          = new JSONDocument();

            foreach (var attribute in _attributes)
            {
                IJSONDocument sumAndCount = new JSONDocument();
                if (document.GetAttributeDataType(_userDefinedName) == ExtendedJSONDataTypes.Array)
                {
                    if (_document.Contains("$" + _userDefinedName) && _document.GetAttributeDataType("$" + _userDefinedName) == ExtendedJSONDataTypes.Array)
                    {
                        double[] sumAndCountDoc = document.GetArray <double>(_userDefinedName);
                        double   sum            = sumAndCountDoc[0];
                        double   count          = sumAndCountDoc[1];

                        double[] existingSumAndCount = _document.GetArray <double>("$" + _userDefinedName);
                        double   sum1   = existingSumAndCount[0];
                        double   count1 = existingSumAndCount[1];

                        double combinedSum   = sum + sum1;
                        double combinedCount = count + count1;

                        sumAndCountDoc[0] = combinedSum;
                        sumAndCountDoc[1] = combinedCount;

                        updateDoc.Add("$" + _userDefinedName, sumAndCountDoc);
                        updateDoc.Add(_userDefinedName, combinedSum / combinedCount);
                        isUpdateApplicable = true;
                    }
                }
            }

            if (isUpdateApplicable)
            {
                JsonDocumentUtil.Update(_document, updateDoc);
            }
            return(_document);
        }
Ejemplo n.º 3
0
        public object Combine(object value)
        {
            if (!(value is IJSONDocument))
            {
                throw new Exception("At SumDataCombiner: Document needs to be in IJSONDocument format");
            }

            IJSONDocument document  = (IJSONDocument)value;
            IJSONDocument updateDoc = new JSONDocument();

            foreach (var attribute in _attributes)
            {
                double value1 = document.GetAsDouble(_userDefinedName);
                double value2 = _document.GetAsDouble(_userDefinedName);
                updateDoc.Add(_userDefinedName, value1 + value2);
            }
            JsonDocumentUtil.Update(_document, updateDoc);
            //_document.Update(updateDoc);
            return(_document);
        }
Ejemplo n.º 4
0
        public bool Evaluate(out IJsonValue value, IJSONDocument document)
        {
            value = null;
            JSONDocument doc = new JSONDocument();

            foreach (var pair in _document)
            {
                IJsonValue outValue;
                if (pair.Value.Evaluate(out outValue, document))
                {
                    doc.Add(pair.Key, outValue.Value);
                }
                else
                {
                    return(false);
                }
            }

            if (doc.Count > 0)
            {
                value = new ObjectJsonValue(doc);
            }
            return(true);
        }
Ejemplo n.º 5
0
        private QueryCriteria AddQueryCriteria(IDqlObject query, IList <IParameter> parameters, IQueryStore queryStore)
        {
            var queryCriteria = new QueryCriteria {
                Store = queryStore
            };

            if (query is SelectObject)
            {
                var selectQuery = query as SelectObject;
                if (selectQuery.WherePredicate != null && parameters != null)
                {
                    selectQuery.WherePredicate.AssignConstants(parameters);
                    selectQuery.WherePredicate.AssignScalarFunctions();
                }

                var functions = new List <Function>();
                foreach (var projectionValue in selectQuery.Projections)
                {
                    if (selectQuery.IsDistinct)
                    {
                        IEvaluable projValue = projectionValue as IEvaluable;
                        queryCriteria.AddDistinctField(projValue);
                    }

                    var evaluable = projectionValue as IEvaluable;
                    if (parameters != null && evaluable != null)
                    {
                        evaluable.AssignConstants(parameters);
                    }

                    if (evaluable != null)
                    {
                        if (projectionValue is AllEvaluable)
                        {
                            queryCriteria.GetAllFields = true;
                        }
                        else
                        {
                            queryCriteria.AddProjection(evaluable.ToString(), evaluable);
                        }

                        if (evaluable.Functions != null)
                        {
                            functions.AddRange(evaluable.Functions);
                        }
                    }
                }

                foreach (var function in functions)
                {
                    if (!AssignAggregateFunction(function, queryCriteria))
                    {
                        if (!AssignScalarFunction(function))
                        {
                            throw new QuerySystemException(ErrorCodes.Query.INVALID_FUNCTION_NAME_SPECIFIED,
                                                           new[] { function.FunctionNameActual });
                        }
                    }
                }

                if (selectQuery.GroupValue != null)
                {
                    foreach (var projectionValue in selectQuery.GroupValue)
                    {
                        queryCriteria.AddGroupByField(projectionValue);
                    }
                }

                if (queryCriteria.GroupByField == null && queryCriteria.ContainsAggregations &&
                    queryCriteria.Aggregations.Count > 0)
                {
                    queryCriteria.GroupByField = new AllField(Field.FieldType.Grouped);
                }

                if (selectQuery.OrderValue != null)
                {
                    foreach (var projectionValue in selectQuery.OrderValue)
                    {
                        var attribute = projectionValue;
                        if (attribute != null)
                        {
                            queryCriteria.AddOrderByField(attribute,
                                                          attribute is BinaryExpression?
                                                          ((BinaryExpression)attribute).SortOrder: SortOrder.ASC);
                        }
                    }
                }

                long limit = -1;
                if (selectQuery.Limit != null)
                {
                    limit = long.Parse(selectQuery.Limit.InString);
                }
                if (limit > -1)
                {
                    long skip = -1;

                    if (selectQuery.Skip != null)
                    {
                        skip = long.Parse(selectQuery.Skip.InString);
                    }

                    if (skip > 0)
                    {
                        limit += skip;  //: Adding this to avoid bug at query router (while applying skip query)
                    }
                    queryCriteria.AddLimit(limit);
                }
            }
            else if (query is UpdateObject)
            {
                var updateQuery = query as UpdateObject;
                if (updateQuery.WherePredicate != null && parameters != null)
                {
                    updateQuery.WherePredicate.AssignConstants(parameters);
                }
                updateQuery.Updator.AssignConstants(parameters);

                foreach (IUpdation update in updateQuery.Updator.Updations)
                {
                    foreach (var function in update.GetFunctions())
                    {
                        foreach (var argument in function.Arguments)
                        {
                            if (argument.EvaluationType != EvaluationType.Constant)
                            {
                                throw new QuerySystemException(ErrorCodes.Query.INVALID_CONSTANT_FUNCTION_SPECIFIED);
                            }
                        }

                        if (!AssignScalarFunction(function))
                        {
                            throw new QuerySystemException(ErrorCodes.Query.INVALID_FUNCTION_NAME_SPECIFIED,
                                                           new[] { function.FunctionNameActual });
                        }
                    }
                }

                queryCriteria.DocumentUpdate = updateQuery.Updator;
                queryCriteria.UpdateOption   = UpdateOption.Update;
            }
            else if (query is InsertObject)
            {
                var document = new JSONDocument();
                foreach (KeyValuePair <Attribute, IEvaluable> pair in ((InsertObject)query).ValuesToInsert)
                {
                    pair.Value.AssignConstants(parameters);

                    foreach (var function in pair.Value.Functions)
                    {
                        foreach (var argument in function.Arguments)
                        {
                            if (argument.EvaluationType != EvaluationType.Constant)
                            {
                                throw new QuerySystemException(ErrorCodes.Query.INVALID_CONSTANT_FUNCTION_SPECIFIED);
                            }
                        }

                        if (!AssignScalarFunction(function))
                        {
                            throw new QuerySystemException(ErrorCodes.Query.INVALID_FUNCTION_NAME_SPECIFIED,
                                                           new[] { function.FunctionNameActual });
                        }
                    }

                    IJsonValue jsonValue;
                    if (!pair.Value.Evaluate(out jsonValue, null))
                    {
                        throw new QuerySystemException(ErrorCodes.Query.INVALID_CONSTANT_BINARY_EXPRESSION_SPECIFIED);
                    }
                    document.Add(pair.Key.ToString(), jsonValue.Value);
                }
                queryCriteria.NewDocument  = document;
                queryCriteria.UpdateOption = UpdateOption.Insert;
            }
            else if (query is DeleteObject)
            {
                var deleteQuery = query as DeleteObject;
                if (deleteQuery.WherePredicate != null && parameters != null)
                {
                    deleteQuery.WherePredicate.AssignConstants(parameters);
                }
                queryCriteria.UpdateOption = UpdateOption.Delete;
            }
            return(queryCriteria);
        }