Пример #1
0
#pragma warning restore 649

        /// <summary>
        /// Process a result operator. If this result is amenable to be made into a function, then
        /// do so.
        /// </summary>
        /// <param name="resultOperator"></param>
        /// <param name="queryModel"></param>
        /// <param name="index"></param>
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            // Look for a single-result processor

            var processor = _operators.FindScalarROProcessor(resultOperator.GetType());

            if (processor != null)
            {
                var result = processor.ProcessResultOperator(resultOperator, queryModel, _codeEnv, _codeContext, MEFContainer);
                if (result != null)
                {
                    _codeEnv.SetResult(result);
                    _scoping.Add(_codeContext.Add(queryModel, result));
                }
                return;
            }

            // Look for a sequence processor

            var collectionProcessor = _operators.FindCollectionROProcessor(resultOperator.GetType());

            if (collectionProcessor != null)
            {
                collectionProcessor.ProcessResultOperator(resultOperator, queryModel, _codeEnv, _codeContext, MEFContainer);
                _codeEnv.ResetResult();
                return;
            }

            ///
            /// Uh oh - no idea how to do this!
            ///

            throw new InvalidOperationException("LINQToTTree can't translate the operator '" + resultOperator.ToString() + "'");
        }
Пример #2
0
        /// <summary>
        /// Visits the result operator.
        /// </summary>
        /// <param name="resultOperator">The result operator.</param>
        /// <param name="queryModel">The query model.</param>
        /// <param name="index">The index.</param>
        /// <remarks></remarks>
        public override void VisitResultOperator(ResultOperatorBase resultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            if (typeof(CountResultOperator).IsAssignableFrom(resultOperator.GetType()))
            {
                _queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.Count, 0);
            }

            if (typeof(SingleResultOperator).IsAssignableFrom(resultOperator.GetType()))
            {
                var op     = resultOperator as SingleResultOperator;
                var filter = op.ReturnDefaultWhenEmpty ? ResultFilterType.SingleOrDefault : ResultFilterType.Single;
                _queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, filter, 0);
            }

            if (typeof(TakeResultOperator).IsAssignableFrom(resultOperator.GetType()))
            {
                var firstResultOperator = resultOperator as TakeResultOperator;
                var countExpression     = firstResultOperator.Count as ConstantExpression;
                var count = Convert.ToInt32(countExpression.Value);
                _queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.Take, count);
            }

            var skipResultOp = resultOperator as SkipResultOperator;

            if (skipResultOp != null)
            {
                var countExpression = skipResultOp.Count as ConstantExpression;
                var count           = Convert.ToInt32(countExpression.Value);
                _queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.Skip, count);
            }

            if (resultOperator is AnyResultOperator)
            {
                _queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.Any, 0);
            }

            var allResultOp = resultOperator as AllResultOperator;

            if (allResultOp != null)
            {
                var criteriaExpression = this.GetCriteriaExpression(queryModel, allResultOp.Predicate);
                _queryDescription.SetCriteria(criteriaExpression);
                _queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.All, 0);
            }

            var firstResultOp = resultOperator as FirstResultOperator;

            if (firstResultOp != null)
            {
                this._queryDescription.SetResultFilterClause(
                    queryModel.MainFromClause.ItemType,
                    firstResultOp.ReturnDefaultWhenEmpty ? ResultFilterType.FirstOrDefault : ResultFilterType.First,
                    0);
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
        /// <summary>
        /// The visit result operator.
        /// </summary>
        /// <param name="resultOperator">
        /// The result operator.
        /// </param>
        /// <param name="queryModel">
        /// The query model.
        /// </param>
        /// <param name="index">
        /// The index.
        /// </param>
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            if (resultOperator.GetType() == typeof(TakeResultOperator))
            {
                _resultLcs.ReturnTop = ((TakeResultOperator)resultOperator).GetConstantCount();
                if (_resultLcs.RowNumber != null)
                {
                    _resultLcs.RowNumber.EndRow = _resultLcs.RowNumber.StartRow + _resultLcs.ReturnTop - 1;
                    _resultLcs.ReturnTop        = 0;
                }
            }
            else if (resultOperator.GetType() == typeof(FirstResultOperator))
            {
                var resOp = (FirstResultOperator)resultOperator;
                _resultLcs.ReturnType = resOp.ReturnDefaultWhenEmpty
                                            ? LcsReturnType.Object
                                            : LcsReturnType.ObjectRequired;
            }
            else if (resultOperator.GetType() == typeof(AnyResultOperator))
            {
                _resultLcs.ReturnType = LcsReturnType.Any;
            }
            else if (resultOperator.GetType() == typeof(AllResultOperator))
            {
                var a = (AllResultOperator)resultOperator;
                FillLcsLimitFunction(a.Predicate);
                _resultLcs.ReturnType = LcsReturnType.All;
            }
            else if (resultOperator.GetType() == typeof(CountResultOperator))
            {
                _resultLcs.ReturnType = LcsReturnType.Count;
            }
            else if (resultOperator is SkipResultOperator)
            {
                var minNumberExpression = ((SkipResultOperator)resultOperator).Count as ConstantExpression;

                if (minNumberExpression != null)
                {
                    int maxNumber = _resultLcs.ReturnTop == 0 ? int.MaxValue : _resultLcs.ReturnTop;
                    if (_resultLcs.RowNumber == null)
                    {
                        _resultLcs.RowNumber = new RowNumberDef((int)minNumberExpression.Value + 1, maxNumber);
                    }
                    else
                    {
                        _resultLcs.RowNumber.StartRow = _resultLcs.RowNumber.StartRow + (int)minNumberExpression.Value;
                    }

                    _resultLcs.ReturnTop = 0;
                }
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
Пример #4
0
        public void Process(ResultOperatorBase resultOperator, QueryModelVisitor queryModel, IntermediateHqlTree tree)
        {
            ResultOperatorProcessorBase processor;

            if (_map.TryGetValue(resultOperator.GetType(), out processor))
            {
                processor.Process(resultOperator, queryModel, tree);
            }
            else
            {
                throw new NotSupportedException(string.Format("The {0} result operator is not current supported",
                                                              resultOperator.GetType().Name));
            }
        }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            base.VisitResultOperator(resultOperator, queryModel, index);

            switch (resultOperator)
            {
            case TakeResultOperator takeResultOperator:
                var takeVariable = GetFluxExpression(takeResultOperator.Count, resultOperator);
                _context.QueryAggregator.AddLimitN(takeVariable);
                break;

            case SkipResultOperator skipResultOperator:
                var skipVariable = GetFluxExpression(skipResultOperator.Count, resultOperator);
                _context.QueryAggregator.AddLimitOffset(skipVariable);
                break;

            case AnyResultOperator _:
                break;

            case LongCountResultOperator _:
            case CountResultOperator _:
                _context.QueryAggregator.AddResultFunction(ResultFunction.Count);
                break;

            default:
                throw new NotSupportedException($"{resultOperator.GetType().Name} is not supported.");
            }
        }
Пример #6
0
#pragma warning restore 649

        /// <summary>
        /// Process a result operator. If this result is amenable to be made into a function, then
        /// do so.
        /// </summary>
        /// <param name="resultOperator"></param>
        /// <param name="queryModel"></param>
        /// <param name="index"></param>
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            // Look for a single-result processor

            var processor = _operators.FindScalarROProcessor(resultOperator.GetType());
            if (processor != null)
            {
                var result = processor.ProcessResultOperator(resultOperator, queryModel, _codeEnv, _codeContext, MEFContainer);
                if (result != null)
                {
                    _codeEnv.SetResult(result);
                    _scoping.Add(_codeContext.Add(queryModel, result));
                }
                return;
            }

            // Look for a sequence processor

            var collectionProcessor = _operators.FindCollectionROProcessor(resultOperator.GetType());
            if (collectionProcessor != null)
            {
                collectionProcessor.ProcessResultOperator(resultOperator, queryModel, _codeEnv, _codeContext, MEFContainer);
                _codeEnv.ResetResult();
                return;
            }

            ///
            /// Uh oh - no idea how to do this!
            /// 

            throw new InvalidOperationException("LINQToTTree can't translate the operator '" + resultOperator.ToString() + "'");
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            var relationalQueryModelVisitor = (RelationalQueryModelVisitor)entityQueryModelVisitor;
            var selectExpression            = relationalQueryModelVisitor.TryGetQuery(queryModel.MainFromClause);

            if (!relationalQueryModelVisitor.RequiresClientResultOperator &&
                selectExpression != null &&
                selectExpression.Projection.Count == 1 &&
                _restrictedOperators.TryGetValue(resultOperator.GetType(), out var restrictedTypes))
            {
                PrepareSelectExpressionForAggregate(selectExpression);

                if (!(selectExpression.Projection[0].RemoveConvert() is SelectExpression))
                {
                    if (restrictedTypes.Contains(queryModel.SelectClause.Selector.Type.UnwrapNullableType()))
                    {
                        relationalQueryModelVisitor.RequiresClientResultOperator = true;

                        return(_resultOperatorHandler.HandleResultOperator(
                                   entityQueryModelVisitor,
                                   resultOperator,
                                   queryModel));
                    }
                }
            }

            return(base.HandleResultOperator(entityQueryModelVisitor, resultOperator, queryModel));
        }
Пример #8
0
        public virtual Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            Check.NotNull(entityQueryModelVisitor, "entityQueryModelVisitor");
            Check.NotNull(resultOperator, "resultOperator");
            Check.NotNull(queryModel, "queryModel");

            var relationalQueryModelVisitor
                = (RelationalQueryModelVisitor)entityQueryModelVisitor;

            var selectExpression
                = relationalQueryModelVisitor
                  .TryGetQuery(queryModel.MainFromClause);

            var handlerContext
                = new HandlerContext(
                      _resultOperatorHandler,
                      relationalQueryModelVisitor,
                      resultOperator,
                      queryModel,
                      selectExpression);

            Func <HandlerContext, Expression> resultHandler;

            if (relationalQueryModelVisitor.RequiresClientFilter ||
                !_resultHandlers.TryGetValue(resultOperator.GetType(), out resultHandler) ||
                selectExpression == null)
            {
                return(handlerContext.EvalOnClient);
            }

            return(resultHandler(handlerContext));
        }
Пример #9
0
        /// <summary>
        ///     Handles the result operator.
        /// </summary>
        /// <param name="entityQueryModelVisitor"> The entity query model visitor. </param>
        /// <param name="resultOperator"> The result operator. </param>
        /// <param name="queryModel"> The query model. </param>
        /// <returns>
        ///     An compiled query expression fragment representing the result operator.
        /// </returns>
        public virtual Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            Check.NotNull(entityQueryModelVisitor, nameof(entityQueryModelVisitor));
            Check.NotNull(resultOperator, nameof(resultOperator));
            Check.NotNull(queryModel, nameof(queryModel));

            if (!_handlers.TryGetValue(resultOperator.GetType(), out var handler))
            {
                throw new NotImplementedException(resultOperator.GetType().ToString());
            }

            return(handler(entityQueryModelVisitor, resultOperator, queryModel));
        }
Пример #10
0
        public virtual Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            IStreamedDataInfo streamedDataInfo,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            Check.NotNull(entityQueryModelVisitor, "entityQueryModelVisitor");
            Check.NotNull(streamedDataInfo, "streamedDataInfo");
            Check.NotNull(resultOperator, "resultOperator");
            Check.NotNull(queryModel, "queryModel");

            var relationalQueryModelVisitor
                = (RelationalQueryModelVisitor)entityQueryModelVisitor;

            var sqlSelect
                = relationalQueryModelVisitor.TryGetSqlSelect(queryModel.MainFromClause);

            Func <SqlSelect, ResultOperatorBase, bool> resultHandler;

            if (!_resultHandlers.TryGetValue(resultOperator.GetType(), out resultHandler) ||
                sqlSelect == null ||
                resultHandler(sqlSelect, resultOperator))
            {
                return(_resultOperatorHandler
                       .HandleResultOperator(
                           entityQueryModelVisitor,
                           streamedDataInfo,
                           resultOperator,
                           queryModel));
            }

            return(null);
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            var relationalQueryModelVisitor
                = (RelationalQueryModelVisitor)entityQueryModelVisitor;

            var selectExpression
                = relationalQueryModelVisitor
                  .TryGetQuery(queryModel.MainFromClause);

            var handlerContext
                = new HandlerContext(
                      _resultOperatorHandler,
                      _model,
                      _sqlTranslatingExpressionVisitorFactory,
                      _selectExpressionFactory,
                      relationalQueryModelVisitor,
                      resultOperator,
                      queryModel,
                      selectExpression);

            return(relationalQueryModelVisitor.RequiresClientEval ||
                   relationalQueryModelVisitor.RequiresClientSelectMany ||
                   relationalQueryModelVisitor.RequiresClientJoin ||
                   relationalQueryModelVisitor.RequiresClientFilter ||
                   relationalQueryModelVisitor.RequiresClientOrderBy ||
                   relationalQueryModelVisitor.RequiresClientResultOperator ||
                   relationalQueryModelVisitor.RequiresStreamingGroupResultOperator ||
                   !_resultHandlers.TryGetValue(resultOperator.GetType(), out var resultHandler) ||
                   selectExpression == null
                ? handlerContext.EvalOnClient()
                : resultHandler(handlerContext));
        }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            switch (resultOperator)
            {
            case DistinctResultOperator ro:
                _query["DISTINCT"] = true;
                return;

            case AverageResultOperator ro:
            {
                var select = (_query["WHAT"] as List <object>);
                select.Insert(0, "AVG()");
                _query["WHAT"] = new[] { select };
                return;
            }

            case CountResultOperator ro:
            {
                var select = (_query["WHAT"] as List <object>);
                select.Insert(0, "COUNT()");
                _query["WHAT"] = new[] { select };
                return;
            }

            case MinResultOperator ro:
            {
                var select = (_query["WHAT"] as List <object>);
                select.Insert(0, "MIN()");
                _query["WHAT"] = new[] { select };
                return;
            }

            case MaxResultOperator ro:
            {
                var select = (_query["WHAT"] as List <object>);
                select.Insert(0, "MAX()");
                _query["WHAT"] = new[] { select };
                return;
            }

            case SumResultOperator ro:
            {
                var select = (_query["WHAT"] as List <object>);
                select.Insert(0, "SUM()");
                _query["WHAT"] = new[] { select };
                return;
            }

            case TakeResultOperator ro:
                _query["LIMIT"] = ro.GetConstantCount();
                return;

            case SkipResultOperator ro:
                _query["OFFSET"] = ro.GetConstantCount();
                return;
            }

            throw new NotSupportedException($"Result operator {resultOperator.GetType().Name.Replace("ResultOperator","")} not supported");
        }
        public virtual Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            Check.NotNull(entityQueryModelVisitor, nameof(entityQueryModelVisitor));
            Check.NotNull(resultOperator, nameof(resultOperator));
            Check.NotNull(queryModel, nameof(queryModel));

            ResultHandler handler;
            if (!_handlers.TryGetValue(resultOperator.GetType(), out handler))
            {
                throw new NotImplementedException(resultOperator.GetType().ToString());
            }

            return handler(entityQueryModelVisitor, resultOperator, queryModel);
        }
 /// <summary>
 /// Visits the result operator.
 /// </summary>
 /// <param name="resultOperator">The result operator.</param>
 /// <param name="queryModel">The query model.</param>
 /// <param name="index">The index.</param>
 public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
 {
     if (resultOperator is CountResultOperator)
     {
         this.IsCount = true;
     }
     else
         throw new NotSupportedException(string.Format("Operator {0} is not supported.", resultOperator.GetType()));
 }
        public override Expression HandleResultOperator(EntityQueryModelVisitor entityQueryModelVisitor, ResultOperatorBase resultOperator, QueryModel queryModel)
        {
            if (_handlers.TryGetValue(resultOperator.GetType(), out var handler))
            {
                return(handler(entityQueryModelVisitor, resultOperator, queryModel));
            }

            return(base.HandleResultOperator(entityQueryModelVisitor, resultOperator, queryModel));
        }
Пример #16
0
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            if (resultOperator is FetchOneRequest)
            {
                ProcessFetchRequest(resultOperator as FetchRequestBase, queryModel);
            }
            else if (resultOperator is FetchManyRequest)
            {
                ProcessFetchRequest(resultOperator as FetchRequestBase, queryModel);
            }
            else if (resultOperator is SumResultOperator)
            {
                _selectPart = string.Format("SUM({0})", _selectPart);
            }
            else if (resultOperator is CountResultOperator)
            {
                // TJT: FIXME
                _selectPart = "COUNT(*)";
            }
            else if (resultOperator is AnyResultOperator)
            {
                _selectPart = string.Format("CASE COUNT({0}) WHEN 0 THEN 0 ELSE 1 END", _selectPart);
            }
            else if (resultOperator is AverageResultOperator)
            {
                _selectPart = string.Format("AVG({0})", _selectPart);
            }
            else if (resultOperator is MinResultOperator)
            {
                _selectPart = string.Format("MIN({0})", _selectPart);
            }
            else if (resultOperator is MaxResultOperator)
            {
                _selectPart = string.Format("MAX({0})", _selectPart);
            }
            else if (resultOperator is FirstResultOperator)
            {
                _limitPart = "1";
            }
            else if (resultOperator is SingleResultOperator)
            {
                // if we get more then one we throw exception
                _limitPart = "2";
            }
            else if (resultOperator is LastResultOperator)
            {
                throw new NotSupportedException("Last is not supported, reverse the order and use First");
            }
            else
            {
                throw new NotSupportedException(resultOperator.GetType().Name + " is not supported");
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            var relationalQueryModelVisitor
                = (RelationalQueryModelVisitor)entityQueryModelVisitor;

            var selectExpression
                = relationalQueryModelVisitor
                  .TryGetQuery(queryModel.MainFromClause);

            var handlerContext
                = new HandlerContext(
                      _resultOperatorHandler,
                      _model,
                      _sqlTranslatingExpressionVisitorFactory,
                      _selectExpressionFactory,
                      relationalQueryModelVisitor,
                      resultOperator,
                      queryModel,
                      selectExpression);

            Func <HandlerContext, Expression> resultHandler;

            if (relationalQueryModelVisitor.RequiresClientEval ||
                relationalQueryModelVisitor.RequiresClientSelectMany ||
                relationalQueryModelVisitor.RequiresClientJoin ||
                relationalQueryModelVisitor.RequiresClientFilter ||
                relationalQueryModelVisitor.RequiresClientOrderBy ||
                relationalQueryModelVisitor.RequiresClientResultOperator ||
                !_resultHandlers.TryGetValue(resultOperator.GetType(), out resultHandler) ||
                selectExpression == null)
            {
                return(handlerContext.EvalOnClient());
            }

            if (relationalQueryModelVisitor.RequiresClientSingleColumnResultOperator &&
                !(resultOperator is SkipResultOperator ||
                  resultOperator is TakeResultOperator ||
                  resultOperator is FirstResultOperator ||
                  resultOperator is SingleResultOperator ||
                  resultOperator is CountResultOperator ||
                  resultOperator is AllResultOperator ||
                  resultOperator is AnyResultOperator ||
                  resultOperator is GroupResultOperator))
            {
                return(handlerContext.EvalOnClient());
            }

            return(resultHandler(handlerContext));
        }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            var handler = resultOperators.GetItem(resultOperator.GetType());

            if (handler != null)
            {
                handler.Accept(resultOperator, model);
            }
            else
            {
                model.ApplyUnsupported(resultOperator);
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            var handler = resultOperators.GetItem(resultOperator.GetType());

            if (handler != null)
            {
                handler.Accept(resultOperator, model);
            }
            else
            {
                model.ApplyUnsupported(resultOperator);
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
        /// <summary>Visits a result operator.</summary>
        /// <param name="resultOperator">Result operator to be visited.</param>
        /// <param name="queryModel">Query model containing given from clause.</param>
        /// <param name="index">Index of the visited result operator in the result operators collection.</param>
        public override void VisitResultOperator(ResultOperatorBase resultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            MethodInfo visitResultOperatorMethod = GetType().GetMethod("Visit" + resultOperator.GetType().Name, BindingFlags.Instance | BindingFlags.NonPublic);

            if (visitResultOperatorMethod != null)
            {
                visitResultOperatorMethod.Invoke(this, new object[] { resultOperator, queryModel, index });
            }
            else
            {
                throw new NotSupportedException(System.String.Format("Expressions of type '{0}' are not supported.", resultOperator.GetType().Name.Replace("ResultOperator", System.String.Empty)));
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
Пример #21
0
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            var operatorType = resultOperator.GetType();

            if (AggretatingOperators.ContainsKey(operatorType))
            {
                _queryParts.SetSelectPartAsScalar(AggretatingOperators[operatorType]);
                base.VisitResultOperator(resultOperator, queryModel, index);
            }

            else if (SetOperators.ContainsKey(operatorType))
            {
                dynamic subQueryResultOperator = Convert.ChangeType(resultOperator, operatorType);
                this.VisitSubQueryExpression(subQueryResultOperator.Source2); // Source2 is a SubQueryExpression.
                _queryParts.AddSubQueryLinkAction(SetOperators[operatorType]);
                base.VisitResultOperator(resultOperator, queryModel, index);
            }

            else if (resultOperator is TakeResultOperator || resultOperator is SkipResultOperator)
            {
                var limitter        = resultOperator is TakeResultOperator ? "LIMIT" : "OFFSET";
                var constExpression = resultOperator is TakeResultOperator ?
                                      (resultOperator as TakeResultOperator).Count :
                                      (resultOperator as SkipResultOperator).Count;

                _queryParts.AddPagingPart(limitter, GetPsqlExpression(constExpression));
                base.VisitResultOperator(resultOperator, queryModel, index);
            }

            else if (resultOperator is AnyResultOperator)
            {
                _queryParts.AddSubQueryLinkAction("EXISTS ({0})");
            }

            else if (resultOperator is AllResultOperator)
            {
                var subQuery = (resultOperator as AllResultOperator);
                _queryParts.AddWherePart($"NOT ({GetPsqlExpression(subQuery.Predicate)})");
                _queryParts.AddSubQueryLinkAction("NOT EXISTS ({0})");
            }

            else
            {
                throw new NotImplementedException(
                          $"This LINQ provider does not provide the {resultOperator} result operator.");
            }
        }
Пример #22
0
            public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
            {
                if (resultOperator is SumResultOperator)
                {
                    m_selectPart = string.Format("SUM({0})", m_selectPart);
                }
                else if (resultOperator is CountResultOperator)
                {
                    m_selectPart = string.Format("COUNT({0})", m_selectPart);
                }
                else if (resultOperator is AnyResultOperator)
                {
                    m_selectPart = string.Format("CASE COUNT({0}) WHEN 0 THEN 0 ELSE 1 END", m_selectPart);
                }
                else if (resultOperator is AverageResultOperator)
                {
                    m_selectPart = string.Format("AVG({0})", m_selectPart);
                }
                else if (resultOperator is MinResultOperator)
                {
                    m_selectPart = string.Format("MIN({0})", m_selectPart);
                }
                else if (resultOperator is MaxResultOperator)
                {
                    m_selectPart = string.Format("MAX({0})", m_selectPart);
                }
                else if (resultOperator is FirstResultOperator)
                {
                    m_selectPart = string.Format("TOP(1) {0}", m_selectPart);
                }
                else if (resultOperator is SingleResultOperator)
                {
                    // if we get more then one we throw exception
                    m_selectPart = string.Format("TOP(2) {0}", m_selectPart);
                }
                else if (resultOperator is LastResultOperator)
                {
                    throw new NotSupportedException("Last is not supported, reverse the order and use First");
                }
                else
                {
                    throw new NotSupportedException(resultOperator.GetType().Name + " is not supported");
                }

                base.VisitResultOperator(resultOperator, queryModel, index);
            }
        public virtual Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            Check.NotNull(entityQueryModelVisitor, "entityQueryModelVisitor");
            Check.NotNull(resultOperator, "resultOperator");
            Check.NotNull(queryModel, "queryModel");

            ResultHandler handler;

            if (!_handlers.TryGetValue(resultOperator.GetType(), out handler))
            {
                throw new NotImplementedException();
            }

            return(handler(entityQueryModelVisitor, resultOperator, queryModel));
        }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            var streamedDataInfo
                = resultOperator.GetOutputDataInfo(_streamedSequenceInfo);

            Func <Expression, Type, ResultOperatorBase, Expression> asyncHandler;

            if (!_asyncHandlers.TryGetValue(resultOperator.GetType(), out asyncHandler))
            {
                // TODO: Implement the rest...
                throw new NotImplementedException();
            }

            _expression
                = asyncHandler(_expression, _streamedSequenceInfo.ResultItemType, resultOperator);

            _streamedSequenceInfo = streamedDataInfo as StreamedSequenceInfo;
        }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            ArgumentUtility.CheckNotNull("resultOperator", resultOperator);
            ArgumentUtility.CheckNotNull("queryModel", queryModel);

            var operatorType = resultOperator.GetType();

            var resultOperatorHandler = _resultOperatorHandlerRegistry.GetItem(operatorType);

            if (resultOperatorHandler == null)
            {
                string message = string.Format(
                    "The result operator '{0}' is not supported and no custom handler has been registered.",
                    operatorType.Name);
                throw new NotSupportedException(message);
            }

            resultOperatorHandler.HandleResultOperator(resultOperator, _sqlStatementBuilder, _generator, _stage, _context);
        }
Пример #26
0
        public virtual Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            Check.NotNull(entityQueryModelVisitor, "entityQueryModelVisitor");
            Check.NotNull(resultOperator, "resultOperator");
            Check.NotNull(queryModel, "queryModel");

            Func <Expression, Type, ResultOperatorBase, Expression> asyncHandler;

            if (!_asyncHandlers.TryGetValue(resultOperator.GetType(), out asyncHandler))
            {
                // TODO: Implement the rest...
                throw new NotImplementedException();
            }

            return(asyncHandler(
                       entityQueryModelVisitor.Expression,
                       entityQueryModelVisitor.StreamedSequenceInfo.ResultItemType,
                       resultOperator));
        }
Пример #27
0
        /// <summary>
        /// Processes result operator (overall query aggregation function).
        /// </summary>
        private void VisitResultOperator(ResultOperatorBase resultOperator)
        {
            LogWriter.WriteLine($"VisitResultOperator: {resultOperator}, type {resultOperator.GetType().Name}");

            switch (resultOperator)
            {
            case MinResultOperator _:
                _state.SetFunction("Min", null);
                break;

            case MaxResultOperator _:
                _state.SetFunction("Max", null);
                break;

            case AverageResultOperator _:
                _state.SetFunction("Average", null);
                break;

            case CountResultOperator _:
                _state.SetFunction("Count", null);
                break;

            case SumResultOperator _:
                _state.SetFunction("Sum", null);
                break;

            case ContainsResultOperator containsResult:
                Visit(containsResult.Item);

                var columnPath = (string)_arguments.Pop();
                var value      = _arguments.Pop();

                _state.SetColumn(columnPath);
                _state.SetComparison(FilterComparisonType.Equal, value);
                break;
            }
        }
Пример #28
0
        public virtual Expression HandleResultOperator(
            EntityQueryModelVisitor entityQueryModelVisitor,
            ResultOperatorBase resultOperator,
            QueryModel queryModel)
        {
            var relationalQueryModelVisitor
                = (RelationalQueryModelVisitor)entityQueryModelVisitor;

            var selectExpression
                = relationalQueryModelVisitor
                  .TryGetQuery(queryModel.MainFromClause);

            var handlerContext
                = new HandlerContext(
                      _resultOperatorHandler,
                      _model,
                      _relationalAnnotationProvider,
                      _sqlTranslatingExpressionVisitorFactory,
                      _selectExpressionFactory,
                      relationalQueryModelVisitor,
                      resultOperator,
                      queryModel,
                      selectExpression);

            Func <HandlerContext, Expression> resultHandler;

            if (relationalQueryModelVisitor.RequiresClientFilter ||
                relationalQueryModelVisitor.RequiresClientResultOperator ||
                relationalQueryModelVisitor.RequiresClientSelectMany ||
                !_resultHandlers.TryGetValue(resultOperator.GetType(), out resultHandler) ||
                selectExpression == null)
            {
                return(handlerContext.EvalOnClient());
            }

            return(resultHandler(handlerContext));
        }
        /// <summary>
        /// Visits the result operator.
        /// </summary>
        /// <param name="resultOperator">The result operator.</param>
        /// <param name="queryModel">The query model.</param>
        /// <param name="index">The index.</param>
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            if (resultOperator is SkipResultOperator)
            {
                if (index > 0 && this.querySpec.Limit > 0)
                    throw new NotSupportedException("Skip operators must come before Take operators.");

                var constantExpression = ((SkipResultOperator)resultOperator).Count as ConstantExpression;
                if (constantExpression == null)
                    throw new NotSupportedException("Only constant skip counts are supported.");
                this.querySpec.Skip = (int)constantExpression.Value;
            }
            else if (resultOperator is TakeResultOperator)
            {
                var constantExpression = ((TakeResultOperator)resultOperator).Count as ConstantExpression;
                if (constantExpression == null)
                    throw new NotSupportedException("Only constant take counts are supported.");
                this.querySpec.Limit = (int)constantExpression.Value;
            }
            else if (resultOperator is FirstResultOperator || resultOperator is SingleResultOperator)
            {
                this.querySpec.Limit = 1;
            }
            else
                throw new NotSupportedException(string.Format("Operator {0} is not supported.", resultOperator.GetType()));
        }
Пример #30
0
        private void ProcessClientSideResultOperator(ResultOperatorBase resultOperator)
        {
			if (resultOperator is NonAggregatingGroupBy)
			{
				ProcessNonAggregatingGroupBy((NonAggregatingGroupBy) resultOperator);
			}
            else if (resultOperator is ClientSideSelect)
            {
                ProcessClientSideSelect((ClientSideSelect) resultOperator);
            }
			else
			{
				throw new NotSupportedException(string.Format("The {0} result operator is not current supported",
				                                              resultOperator.GetType().Name));
			}
        }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            if (ResultOperatorHandlers.ContainsKey(resultOperator.GetType()))
            {
                var method = ResultOperatorHandlers[resultOperator.GetType()];
                _aggegrator.Add(method.Invoke(resultOperator, _values));
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
Пример #32
0
		public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
		{
            if (typeof(ClientSideTransformOperator).IsAssignableFrom(resultOperator.GetType()))
            {
                if (_resultOperatorProcessingMode == ResultOperatorProcessingMode.ProcessClientSide)
                {
                    ProcessClientSideResultOperator(resultOperator);
                }
            }
            else
            {
                if (_resultOperatorProcessingMode == ResultOperatorProcessingMode.ProcessServerSide)
                {
                    ProcessServerSideResultOperator(resultOperator);
                }
            }

			base.VisitResultOperator(resultOperator, queryModel, index);
		}
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            if (resultOperator is AnyResultOperator ||
                resultOperator is CountResultOperator ||
                resultOperator is LongCountResultOperator ||
                resultOperator is FirstResultOperator ||
                resultOperator is SingleResultOperator)
            {
                return;
            }

            var takeOperator = resultOperator as TakeResultOperator;
            if (takeOperator != null)
            {
                var count = takeOperator.GetConstantCount();
                if (Count != null)
                {
                    count = Math.Min(count, Count.Value);
                }

                Count = count;
                return;
            }

            var skipOperator = resultOperator as SkipResultOperator;
            if (skipOperator != null)
            {
                var startIndex = skipOperator.GetConstantCount();
                if (Count != null)
                {
                    Count = Math.Max(Count.Value - startIndex, 0);
                }

                StartIndex = (StartIndex ?? 1) + startIndex;
                return;
            }

            var fetchOperator = resultOperator as FetchResultOperator;
            if (fetchOperator != null)
            {
                var paths = PropertyPathExtractionVisitor.ExtractPaths(fetchOperator.Selector, _nodeTypeProvider, false, false, _namingScheme, "/");
                if (paths.Count > 0)
                {
                    if (Include != null)
                    {
                        Include += ",";
                    }
                    Include += string.Join(",", paths.ToArray());
                }

                return;
            }

            var withPrecedenceOperator = resultOperator as WithPrecedenceResultOperator;
            if (withPrecedenceOperator != null)
            {
                Precedence = Math.Min(Precedence ?? int.MaxValue, withPrecedenceOperator.Precedence);
                return;
            }

            var withExtensionArgOperator = resultOperator as WithExtensionArgResultOperator;
            if (withExtensionArgOperator != null)
            {
                ExtensionArgs[withExtensionArgOperator.Name] = withExtensionArgOperator.Value;
                return;
            }

            #if !NET_3_5
            if (resultOperator is ToCollectionAsyncResultOperator)
            {
                return;
            }
            #endif

            throw new NotSupportedException(string.Format("Result operator '{0}' not supported", resultOperator.GetType()));
        }
        /// <summary>Visits a result operator.</summary>
        /// <param name="resultOperator">Result operator to be visited.</param>
        /// <param name="queryModel">Query model containing given from clause.</param>
        /// <param name="index">Index of the visited result operator in the result operators collection.</param>
        public override void VisitResultOperator(ResultOperatorBase resultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            MethodInfo visitResultOperatorMethod = GetType().GetMethod("Visit" + resultOperator.GetType().Name, BindingFlags.Instance | BindingFlags.NonPublic);
            if (visitResultOperatorMethod != null)
            {
                visitResultOperatorMethod.Invoke(this, new object[] { resultOperator, queryModel, index });
            }
            else
            {
                throw new NotSupportedException(System.String.Format("Expressions of type '{0}' are not supported.", resultOperator.GetType().Name.Replace("ResultOperator", System.String.Empty)));
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            string value;
            if (resultOperatorMap.TryGetValue (resultOperator.GetType (), out value)) {
                queryParts.SelectPart = string.Format (value, queryParts.SelectPart);
            } else {
                throw new NotSupportedException ();
            }

            base.VisitResultOperator (resultOperator, queryModel, index);
        }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            if (resultOperator is AnyResultOperator ||
                resultOperator is CountResultOperator ||
                resultOperator is LongCountResultOperator ||
                resultOperator is FirstResultOperator ||
                resultOperator is SingleResultOperator)
            {
                return;
            }

            var takeOperator = resultOperator as TakeResultOperator;

            if (takeOperator != null)
            {
                var count = takeOperator.GetConstantCount();
                if (Count != null)
                {
                    count = Math.Min(count, Count.Value);
                }

                Count = count;
                return;
            }

            var skipOperator = resultOperator as SkipResultOperator;

            if (skipOperator != null)
            {
                var startIndex = skipOperator.GetConstantCount();
                if (Count != null)
                {
                    Count = Math.Max(Count.Value - startIndex, 0);
                }

                StartIndex = (StartIndex ?? 1) + startIndex;
                return;
            }

            var fetchOperator = resultOperator as FetchResultOperator;

            if (fetchOperator != null)
            {
                var paths = PropertyPathExtractionVisitor.ExtractPaths(fetchOperator.Selector, _nodeTypeProvider, false, false, _namingScheme, "/");
                if (paths.Count > 0)
                {
                    if (Include != null)
                    {
                        Include += ",";
                    }
                    Include += string.Join(",", paths.ToArray());
                }

                return;
            }

            var withPrecedenceOperator = resultOperator as WithPrecedenceResultOperator;

            if (withPrecedenceOperator != null)
            {
                Precedence = Math.Min(Precedence ?? int.MaxValue, withPrecedenceOperator.Precedence);
                return;
            }

            var withExtensionArgOperator = resultOperator as WithExtensionArgResultOperator;

            if (withExtensionArgOperator != null)
            {
                ExtensionArgs[withExtensionArgOperator.Name] = withExtensionArgOperator.Value;
                return;
            }

#if !NET_3_5
            if (resultOperator is ToCollectionAsyncResultOperator)
            {
                return;
            }
#endif

            throw new NotSupportedException(string.Format("Result operator '{0}' not supported", resultOperator.GetType()));
        }
Пример #37
0
 private void ProcessServerSideResultOperator(ResultOperatorBase resultOperator)
 {
     if (resultOperator is FirstResultOperator)
     {
         ProcessFirstOperator();
     }
     else if (resultOperator is TakeResultOperator)
     {
         ProcessTakeOperator((TakeResultOperator)resultOperator);
     }
     else if (resultOperator is SkipResultOperator)
     {
         ProcessSkipOperator((SkipResultOperator)resultOperator);
     }
     else if (resultOperator is GroupResultOperator)
     {
         ProcessGroupByOperator((GroupResultOperator)resultOperator);
     }
     else
     {
         throw new NotSupportedException(string.Format("The {0} result operator is not current supported",
                                                       resultOperator.GetType().Name));
     }
 }
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            if ((resultOperator is TakeResultOperator))
            {
                var takeResultOperator = resultOperator as TakeResultOperator;

                _queryPartsAggregator.AddLimitPart(" LIMIT {0}",
                    Convert.ToInt32(GetN1QlExpression(takeResultOperator.Count)));
            }
            else if (resultOperator is SkipResultOperator)
            {
                var skipResultOperator = resultOperator as SkipResultOperator;

                _queryPartsAggregator.AddOffsetPart(" OFFSET {0}",
                    Convert.ToInt32(GetN1QlExpression(skipResultOperator.Count)));
            }
            else if (resultOperator is FirstResultOperator)
            {
                // We can save query execution time with a short circuit for .First()

                _queryPartsAggregator.AddLimitPart(" LIMIT {0}", 1);
            }
            else if (resultOperator is SingleResultOperator)
            {
                // We can save query execution time with a short circuit for .Single()
                // But we have to get at least 2 results so we know if there was more than 1

                _queryPartsAggregator.AddLimitPart(" LIMIT {0}", 2);
            }
            else if (resultOperator is DistinctResultOperator)
            {
                var distinctResultOperator = resultOperator as DistinctResultOperator;
                _queryPartsAggregator.AddDistinctPart("DISTINCT ");
            }
            else if (resultOperator is ExplainResultOperator)
            {
                _queryPartsAggregator.ExplainPart = "EXPLAIN ";
            }
            else if (resultOperator is AnyResultOperator)
            {
                _queryPartsAggregator.QueryType =
                    _queryPartsAggregator.QueryType == N1QlQueryType.Array ? N1QlQueryType.ArrayAny :
                        _queryPartsAggregator.QueryType == N1QlQueryType.Subquery ? N1QlQueryType.SubqueryAny : N1QlQueryType.MainQueryAny;

                if (_queryPartsAggregator.QueryType == N1QlQueryType.SubqueryAny)
                {
                    // For any Any query this value won't be used
                    // But we'll generate it for consistency

                    _queryPartsAggregator.PropertyExtractionPart =
                        _queryGenerationContext.ExtentNameProvider.GetUnlinkedExtentName();
                }
            }
            else if (resultOperator is AllResultOperator)
            {
                _queryPartsAggregator.QueryType =
                    _queryPartsAggregator.QueryType == N1QlQueryType.Array ? N1QlQueryType.ArrayAll :
                        _queryPartsAggregator.QueryType == N1QlQueryType.Subquery ? N1QlQueryType.SubqueryAll : N1QlQueryType.MainQueryAll;

                bool prefixedExtents = false;
                if (_queryPartsAggregator.QueryType == N1QlQueryType.SubqueryAll)
                {
                    // We're putting allResultOperator.Predicate in the SATISFIES clause of an ALL clause
                    // Each extent of the subquery will be a property returned by the subquery
                    // So we need to prefix the references to the subquery in the predicate with the iterator name from the ALL clause

                    _queryPartsAggregator.PropertyExtractionPart =
                        _queryGenerationContext.ExtentNameProvider.GetUnlinkedExtentName();

                    prefixedExtents = true;
                    _queryGenerationContext.ExtentNameProvider.Prefix = _queryPartsAggregator.PropertyExtractionPart + ".";
                }
                else if (_queryPartsAggregator.QueryType == N1QlQueryType.ArrayAll)
                {
                    // We're dealing with an array-type subquery
                    // If there is any pre-filtering on the array using a Where clause, these statements will be
                    // referencing the query source using the default extent name.  When we apply the SATISFIES clause
                    // we'll need to use a new extent name to reference the results of the internal WHERE clause.

                    _queryPartsAggregator.PropertyExtractionPart =
                        _queryGenerationContext.ExtentNameProvider.GenerateNewExtentName(queryModel.MainFromClause);
                }

                var allResultOperator = (AllResultOperator) resultOperator;
                _queryPartsAggregator.WhereAllPart = GetN1QlExpression(allResultOperator.Predicate);

                if (prefixedExtents)
                {
                    _queryGenerationContext.ExtentNameProvider.Prefix = null;
                }
            }
            else if (resultOperator is ContainsResultOperator)
            {
                if (_queryPartsAggregator.QueryType != N1QlQueryType.Array)
                {
                    throw new NotSupportedException("Contains is only supported in N1QL against nested or constant arrays.");
                }

                var containsResultOperator = (ContainsResultOperator) resultOperator;

                // Use a wrapping function to wrap the subquery with an IN statement

                _queryPartsAggregator.AddWrappingFunction(GetN1QlExpression(containsResultOperator.Item) + " IN ");
            }
            else if (resultOperator is GroupResultOperator)
            {
                VisitGroupResultOperator((GroupResultOperator)resultOperator, queryModel);
            }
            else if (resultOperator is AverageResultOperator)
            {
                _queryPartsAggregator.AggregateFunction = "AVG";
                _isAggregated = true;
            }
            else if ((resultOperator is CountResultOperator) || (resultOperator is LongCountResultOperator))
            {
                _queryPartsAggregator.AggregateFunction = "COUNT";
                _isAggregated = true;
            }
            else if (resultOperator is MaxResultOperator)
            {
                _queryPartsAggregator.AggregateFunction = "MAX";
                _isAggregated = true;
            }
            else if (resultOperator is MinResultOperator)
            {
                _queryPartsAggregator.AggregateFunction = "MIN";
                _isAggregated = true;
            }
            else if (resultOperator is SumResultOperator)
            {
                _queryPartsAggregator.AggregateFunction = "SUM";
                _isAggregated = true;
            }
            else if (resultOperator is UnionResultOperator)
            {
                EnsureNotArraySubquery();

                var source = ((UnionResultOperator) resultOperator).Source2 as SubQueryExpression;
                if (source == null)
                {
                    throw new NotSupportedException("Union is only support against query sources.");
                }

                VisitUnion(source, true);
            }
            else if (resultOperator is ConcatResultOperator)
            {
                EnsureNotArraySubquery();

                var source = ((ConcatResultOperator)resultOperator).Source2 as SubQueryExpression;
                if (source == null)
                {
                    throw new NotSupportedException("Concat is only support against query sources.");
                }

                VisitUnion(source, false);
            }
            else
            {
                throw new NotSupportedException(string.Format("{0} is not supported.", resultOperator.GetType().Name));
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
    public override void VisitResultOperator (ResultOperatorBase resultOperator, QueryModel queryModel, int index)
    {
      ArgumentUtility.CheckNotNull ("resultOperator", resultOperator);
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      var operatorType = resultOperator.GetType();
      
      var resultOperatorHandler = _resultOperatorHandlerRegistry.GetItem (operatorType);
      if (resultOperatorHandler == null)
      {
        string message = string.Format (
            "The result operator '{0}' is not supported and no custom handler has been registered.",
            operatorType.Name);
        throw new NotSupportedException (message);
      }

      resultOperatorHandler.HandleResultOperator (resultOperator, _sqlStatementBuilder, _generator, _stage, _context);
    }
Пример #40
0
        /// <inheritdoc />
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            LogWriter.WriteLine($"VisitResultOperator: {resultOperator}, type {resultOperator.GetType().Name}");

            switch (resultOperator)
            {
            // First()
            case FirstResultOperator _:
                _collector.Take = 1;
                break;

            // Count()
            case CountResultOperator _:
            case LongCountResultOperator _:
                _collector.SetResultAggregationType(AggregationTypeStrict.Count);
                break;

            // Take()
            case TakeResultOperator takeOperator:
                var takeExpr = takeOperator.Count;

                if (takeExpr.NodeType == ExpressionType.Constant)
                {
                    _collector.Take = (int)((ConstantExpression)takeExpr).Value;
                }
                else
                {
                    throw new NotSupportedException("Currently not supporting methods or variables in the Skip or Take clause.");
                }

                break;

            // Skip()
            case SkipResultOperator skipOperator:
                var skipExpr = skipOperator.Count;

                if (skipExpr.NodeType == ExpressionType.Constant)
                {
                    _collector.Skip = (int)((ConstantExpression)skipExpr).Value;
                }
                else
                {
                    throw new NotSupportedException("Currently not supporting methods or variables in the Skip or Take clause.");
                }

                break;

            // Min()
            case MinResultOperator _:
                _state.SetFunction("Min", null);
                break;

            // Max()
            case MaxResultOperator _:
                _state.SetFunction("Max", null);
                break;

            // Average()
            case AverageResultOperator _:
                _state.SetFunction("Average", null);
                break;

            // GroupBy()
            case GroupResultOperator groupOperator:
                using (_state.PushCollectorMode(QueryCollectionState.GroupBy))
                {
                    var key             = groupOperator.KeySelector;
                    var elementSelector = groupOperator.ElementSelector;

                    UpdateEntitySchemaQueryExpression(key);
                    UpdateEntitySchemaQueryExpression(elementSelector);

                    if (key is NewExpression newKeySelector)
                    {
                        int position = 0;
                        foreach (var memberInfo in newKeySelector.Members)
                        {
                            _state.SetColumnAlias(position++, memberInfo.Name);
                        }
                    }
                }

                break;
            }
        }
Пример #41
0
        /// <summary>
        /// Visits the result operator.
        /// </summary>
        /// <param name="resultOperator">The result operator.</param>
        /// <param name="queryModel">The query model.</param>
        /// <param name="index">The index.</param>
        /// <remarks></remarks>
        public override void VisitResultOperator(ResultOperatorBase resultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            if (typeof(CountResultOperator).IsAssignableFrom(resultOperator.GetType()))
            {
                //_queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.Count, 0);
                _queryDescription.AddResultFilter(queryModel.MainFromClause.ItemType, ResultFilterType.Count, 0);
            }

            if (typeof(SingleResultOperator).IsAssignableFrom(resultOperator.GetType()))
            {
                var op = resultOperator as SingleResultOperator;
                var filter = op.ReturnDefaultWhenEmpty ? ResultFilterType.SingleOrDefault : ResultFilterType.Single;
                //_queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, filter, 0);
                _queryDescription.AddResultFilter(queryModel.MainFromClause.ItemType, filter, 0);
            }

            if (typeof(TakeResultOperator).IsAssignableFrom(resultOperator.GetType()))
            {
                var firstResultOperator = resultOperator as TakeResultOperator;
                var countExpression = firstResultOperator.Count as ConstantExpression;
                var count = Convert.ToInt32(countExpression.Value);
                //_queryDescription.SetTakeResultFilter(queryModel.MainFromClause.ItemType, count);
                _queryDescription.AddTakeResultFilter(queryModel.MainFromClause.ItemType, count);
            }

            var skipResultOp = resultOperator as SkipResultOperator;
            if (skipResultOp != null)
            {
                var countExpression = skipResultOp.Count as ConstantExpression;
                var count = Convert.ToInt32(countExpression.Value);
                //_queryDescription.SetSkipResultFilter(queryModel.MainFromClause.ItemType, count);
                _queryDescription.AddSkipResultFilter(queryModel.MainFromClause.ItemType, count);
            }

            if (resultOperator is AnyResultOperator)
            {
                //_queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.Any, 0);
                _queryDescription.AddResultFilter(queryModel.MainFromClause.ItemType, ResultFilterType.Any, 0);
            }

            var allResultOp = resultOperator as AllResultOperator;
            if (allResultOp != null)
            {
                var criteriaExpression = this.GetCriteriaExpression(queryModel, allResultOp.Predicate);
                _queryDescription.SetCriteria(criteriaExpression);
                //_queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.All, 0);
                _queryDescription.AddResultFilter(queryModel.MainFromClause.ItemType, ResultFilterType.All, 0);
            }

            var firstResultOp = resultOperator as FirstResultOperator;
            if (firstResultOp != null)
            {
                //this._queryDescription.SetResultFilterClause(
                //    queryModel.MainFromClause.ItemType,
                //    firstResultOp.ReturnDefaultWhenEmpty ? ResultFilterType.FirstOrDefault : ResultFilterType.First,
                //    0);

                this._queryDescription.AddResultFilter(
                    queryModel.MainFromClause.ItemType,
                    firstResultOp.ReturnDefaultWhenEmpty ? ResultFilterType.FirstOrDefault : ResultFilterType.First,
                    0);
            }

            var lastResultOp = resultOperator as LastResultOperator;
            if (lastResultOp != null)
            {
                this._queryDescription.AddResultFilter(
                    queryModel.MainFromClause.ItemType,
                    lastResultOp.ReturnDefaultWhenEmpty ? ResultFilterType.LastOrDefault : ResultFilterType.Last,
                    0);
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }