Exemple #1
0
        public override void VisitAdditionalFromClause(AdditionalFromClause fromClause, QueryModel queryModel, int index)
        {
            var innerExpression
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(this)
                      .VisitExpression(fromClause.FromExpression));

            var itemParameter
                = Expression.Parameter(fromClause.ItemType);

            var scopeCreatorExpression
                = QuerySourceScope
                  .Create(fromClause, itemParameter, _querySourceScopeParameter);

            _expression
                = Expression.Call(
                      _linqOperatorProvider.SelectMany
                      .MakeGenericMethod(typeof(QuerySourceScope), typeof(QuerySourceScope)),
                      _expression,
                      Expression.Lambda(
                          Expression.Call(
                              _linqOperatorProvider.Select
                              .MakeGenericMethod(fromClause.ItemType, typeof(QuerySourceScope)),
                              innerExpression,
                              Expression.Lambda(
                                  scopeCreatorExpression,
                                  new[] { itemParameter })),
                          new[] { _querySourceScopeParameter }));

            _querySourceMapping.AddMapping(
                fromClause,
                QuerySourceScope.GetResult(_querySourceScopeParameter, fromClause));
        }
        public override void VisitMainFromClause(
            [NotNull] MainFromClause fromClause, [NotNull] QueryModel queryModel)
        {
            Check.NotNull(fromClause, "fromClause");
            Check.NotNull(queryModel, "queryModel");

            _expression
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(fromClause)
                      .VisitExpression(fromClause.FromExpression));

            var sequenceType = _expression.Type.GetSequenceType();

            var elementScoped
                = sequenceType.IsConstructedGenericType &&
                  sequenceType.GetGenericTypeDefinition() == typeof(QuerySourceScope <>);

            Type elementType;

            if (elementScoped)
            {
                elementType = sequenceType.GetTypeInfo().GenericTypeArguments[0];
            }
            else
            {
                elementType = sequenceType;

                var itemParameter = Expression.Parameter(elementType);

                _expression
                    = Expression.Call(
                          _queryCompilationContext.LinqOperatorProvider.Select
                          .MakeGenericMethod(elementType, typeof(QuerySourceScope)),
                          _expression,
                          Expression.Lambda(
                              QuerySourceScope
                              .Create(fromClause, itemParameter, QuerySourceScopeParameter),
                              new[] { itemParameter }));
            }

            _expression
                = Expression.Call(
                      _queryCompilationContext.LinqOperatorProvider.SelectMany
                      .MakeGenericMethod(typeof(QuerySourceScope), typeof(QuerySourceScope)),
                      Expression.Call(
                          _queryCompilationContext.LinqOperatorProvider.ToSequence
                          .MakeGenericMethod(typeof(QuerySourceScope)),
                          QuerySourceScopeParameter),
                      Expression.Lambda(_expression,
                                        new[] { QuerySourceScopeParameter }));

            _querySourceMapping.AddMapping(
                fromClause,
                QuerySourceScope.GetResult(QuerySourceScopeParameter, fromClause, elementType));
        }
        public override void VisitGroupJoinClause(
            [NotNull] GroupJoinClause groupJoinClause, [NotNull] QueryModel queryModel, int index)
        {
            Check.NotNull(groupJoinClause, "groupJoinClause");
            Check.NotNull(queryModel, "queryModel");

            var innerSequenceExpression
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(groupJoinClause.JoinClause)
                      .VisitExpression(groupJoinClause.JoinClause.InnerSequence));

            var innerElementType
                = innerSequenceExpression.Type.GetSequenceType();

            var itemParameter
                = Expression.Parameter(innerElementType);

            _querySourceMapping.AddMapping(groupJoinClause.JoinClause, itemParameter);

            var outerKeySelector
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(groupJoinClause)
                      .VisitExpression(groupJoinClause.JoinClause.OuterKeySelector));

            var innerKeySelector
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(groupJoinClause)
                      .VisitExpression(groupJoinClause.JoinClause.InnerKeySelector));

            var itemsParameter
                = Expression.Parameter(innerSequenceExpression.Type);

            var scopeCreatorExpression
                = QuerySourceScope
                  .Create(groupJoinClause, itemsParameter, _querySourceScopeParameter);

            _expression
                = Expression.Call(
                      _queryCompilationContext.LinqOperatorProvider.GroupJoin.MakeGenericMethod(
                          typeof(QuerySourceScope),
                          innerElementType,
                          outerKeySelector.Type,
                          typeof(QuerySourceScope)),
                      _expression,
                      innerSequenceExpression,
                      Expression.Lambda(outerKeySelector, _querySourceScopeParameter),
                      Expression.Lambda(innerKeySelector, itemParameter),
                      Expression.Lambda(
                          scopeCreatorExpression,
                          new[] { _querySourceScopeParameter, itemsParameter }));

            _querySourceMapping.AddMapping(
                groupJoinClause,
                QuerySourceScope.GetResult(_querySourceScopeParameter, groupJoinClause, innerSequenceExpression.Type));
        }
Exemple #4
0
        public override void VisitMainFromClause(
            [NotNull] MainFromClause fromClause, [NotNull] QueryModel queryModel)
        {
            Check.NotNull(fromClause, "fromClause");
            Check.NotNull(queryModel, "queryModel");

            _expression
                = ReplaceClauseReferences(fromClause.FromExpression, fromClause);

            var sequenceType = _expression.Type.GetSequenceType();

            var elementScoped
                = sequenceType.IsConstructedGenericType &&
                  sequenceType.GetGenericTypeDefinition() == typeof(QuerySourceScope <>);

            Type elementType;

            if (elementScoped)
            {
                elementType = sequenceType.GetTypeInfo().GenericTypeArguments[0];
            }
            else
            {
                elementType = sequenceType;
                _expression = CreateScope(_expression, elementType, fromClause);
            }

            _expression
                = Expression.Call(
                      LinqOperatorProvider.SelectMany
                      .MakeGenericMethod(typeof(QuerySourceScope), typeof(QuerySourceScope)),
                      Expression.Call(
                          LinqOperatorProvider.ToSequence
                          .MakeGenericMethod(typeof(QuerySourceScope)),
                          QuerySourceScopeParameter),
                      Expression.Lambda(_expression,
                                        new[] { QuerySourceScopeParameter }));

            _querySourceMapping.AddMapping(
                fromClause,
                QuerySourceScope.GetResult(QuerySourceScopeParameter, fromClause, elementType));

            if (fromClause.ItemType.GetTypeInfo().IsGenericType &&
                fromClause.ItemType.GetTypeInfo().GetGenericTypeDefinition() == typeof(IGrouping <,>))
            {
                queryModel.TransformExpressions(ReplaceClauseReferences);
            }
        }
Exemple #5
0
        public override void VisitGroupJoinClause(GroupJoinClause groupJoinClause, QueryModel queryModel, int index)
        {
            var itemParameter
                = Expression.Parameter(groupJoinClause.JoinClause.ItemType);

            _querySourceMapping.AddMapping(groupJoinClause.JoinClause, itemParameter);

            var innerSequence
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(null)
                      .VisitExpression(groupJoinClause.JoinClause.InnerSequence));

            var outerKeySelector
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(this)
                      .VisitExpression(groupJoinClause.JoinClause.OuterKeySelector));

            var innerKeySelector
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(this)
                      .VisitExpression(groupJoinClause.JoinClause.InnerKeySelector));

            var itemsParameter
                = Expression.Parameter(groupJoinClause.ItemType);

            var scopeCreatorExpression
                = QuerySourceScope
                  .Create(groupJoinClause, itemsParameter, _querySourceScopeParameter);

            _expression
                = Expression.Call(
                      _linqOperatorProvider.GroupJoin.MakeGenericMethod(
                          typeof(QuerySourceScope),
                          groupJoinClause.JoinClause.ItemType,
                          outerKeySelector.Type,
                          typeof(QuerySourceScope)),
                      _expression,
                      innerSequence,
                      Expression.Lambda(outerKeySelector, _querySourceScopeParameter),
                      Expression.Lambda(innerKeySelector, itemParameter),
                      Expression.Lambda(
                          scopeCreatorExpression,
                          new[] { _querySourceScopeParameter, itemsParameter }));

            _querySourceMapping.AddMapping(
                groupJoinClause,
                QuerySourceScope.GetResult(_querySourceScopeParameter, groupJoinClause));
        }
        public override void VisitMainFromClause(
            [NotNull] MainFromClause fromClause, [NotNull] QueryModel queryModel)
        {
            Check.NotNull(fromClause, nameof(fromClause));
            Check.NotNull(queryModel, nameof(queryModel));

            _expression
                = ReplaceClauseReferences(fromClause.FromExpression, fromClause);

            var sequenceType = _expression.Type.GetSequenceType();

            var elementScoped
                = sequenceType.IsConstructedGenericType &&
                  sequenceType.GetGenericTypeDefinition() == typeof(QuerySourceScope <>);

            Type elementType;

            if (elementScoped)
            {
                elementType = sequenceType.GetTypeInfo().GenericTypeArguments[0];
            }
            else
            {
                elementType = sequenceType;
                _expression = CreateScope(_expression, elementType, fromClause);
            }

            _expression
                = Expression.Call(
                      LinqOperatorProvider.SelectMany
                      .MakeGenericMethod(typeof(QuerySourceScope), typeof(QuerySourceScope)),
                      Expression.Call(
                          LinqOperatorProvider.ToSequence
                          .MakeGenericMethod(typeof(QuerySourceScope)),
                          QuerySourceScopeParameter),
                      Expression.Lambda(_expression, QuerySourceScopeParameter));

            var querySourceMapping = _queryCompilationContext.QuerySourceMapping;

            if (!querySourceMapping.ContainsMapping(fromClause))
            {
                querySourceMapping.AddMapping(
                    fromClause,
                    QuerySourceScope.GetResult(QuerySourceScopeParameter, fromClause, elementType));
            }
        }
        public override void VisitMainFromClause(
            [NotNull] MainFromClause fromClause, [NotNull] QueryModel queryModel)
        {
            Check.NotNull(fromClause, "fromClause");
            Check.NotNull(queryModel, "queryModel");

            _expression
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(fromClause)
                      .VisitExpression(fromClause.FromExpression));

            var elementType = _expression.Type.GetSequenceType();

            var itemParameter
                = Expression.Parameter(elementType);

            var scopeCreatorExpression
                = QuerySourceScope
                  .Create(fromClause, itemParameter, _querySourceScopeParameter);

            _expression
                = Expression.Call(
                      _queryCompilationContext.LinqOperatorProvider.SelectMany
                      .MakeGenericMethod(typeof(QuerySourceScope), typeof(QuerySourceScope)),
                      Expression.Call(
                          _queryCompilationContext.LinqOperatorProvider.ToSequence
                          .MakeGenericMethod(typeof(QuerySourceScope)),
                          _querySourceScopeParameter),
                      Expression.Lambda(
                          Expression.Call(
                              _queryCompilationContext.LinqOperatorProvider.Select
                              .MakeGenericMethod(elementType, typeof(QuerySourceScope)),
                              _expression,
                              Expression.Lambda(
                                  scopeCreatorExpression,
                                  new[] { itemParameter })),
                          new[] { _querySourceScopeParameter }));

            _querySourceMapping.AddMapping(
                fromClause,
                QuerySourceScope.GetResult(_querySourceScopeParameter, fromClause, elementType));
        }
Exemple #8
0
        public override void VisitAdditionalFromClause(
            [NotNull] AdditionalFromClause fromClause, [NotNull] QueryModel queryModel, int index)
        {
            Check.NotNull(fromClause, "fromClause");
            Check.NotNull(queryModel, "queryModel");

            var innerExpression
                = ReplaceClauseReferences(fromClause.FromExpression, fromClause);

            var innerSequenceType = innerExpression.Type.GetSequenceType();

            var innerElementScoped
                = innerSequenceType.IsConstructedGenericType &&
                  innerSequenceType.GetGenericTypeDefinition() == typeof(QuerySourceScope <>);

            Type innerElementType;

            if (innerElementScoped)
            {
                innerElementType
                    = innerSequenceType.GetTypeInfo().GenericTypeArguments[0];
            }
            else
            {
                innerElementType = innerSequenceType;
                innerExpression  = CreateScope(innerExpression, innerElementType, fromClause);
            }

            _expression
                = Expression.Call(
                      LinqOperatorProvider.SelectMany
                      .MakeGenericMethod(typeof(QuerySourceScope), typeof(QuerySourceScope)),
                      _expression,
                      Expression.Lambda(
                          innerExpression,
                          new[] { QuerySourceScopeParameter }));

            _querySourceMapping.AddMapping(
                fromClause,
                QuerySourceScope.GetResult(QuerySourceScopeParameter, fromClause, innerElementType));
        }
Exemple #9
0
        public override void VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
        {
            _expression
                = ReplaceClauseReferences(
                      CreateQueryingExpressionTreeVisitor(this)
                      .VisitExpression(fromClause.FromExpression));

            var itemParameter
                = Expression.Parameter(fromClause.ItemType);

            var parentScopeExpression
                = _parentQueryModelVisitor == null
                    ? (Expression)Expression.Default(typeof(QuerySourceScope))
                    : _querySourceScopeParameter;

            var scopeCreatorExpression
                = QuerySourceScope
                  .Create(fromClause, itemParameter, parentScopeExpression);

            _expression
                = Expression.Call(
                      _linqOperatorProvider.SelectMany
                      .MakeGenericMethod(typeof(QuerySourceScope), typeof(QuerySourceScope)),
                      Expression.Call(
                          _linqOperatorProvider.ToSequence.MakeGenericMethod(typeof(QuerySourceScope)),
                          parentScopeExpression),
                      Expression.Lambda(
                          Expression.Call(
                              _linqOperatorProvider.Select
                              .MakeGenericMethod(fromClause.ItemType, typeof(QuerySourceScope)),
                              _expression,
                              Expression.Lambda(
                                  scopeCreatorExpression,
                                  new[] { itemParameter })),
                          new[] { _querySourceScopeParameter }));

            _querySourceMapping.AddMapping(
                fromClause,
                QuerySourceScope.GetResult(_querySourceScopeParameter, fromClause));
        }
Exemple #10
0
        public override void VisitGroupJoinClause(
            [NotNull] GroupJoinClause groupJoinClause, [NotNull] QueryModel queryModel, int index)
        {
            Check.NotNull(groupJoinClause, "groupJoinClause");
            Check.NotNull(queryModel, "queryModel");

            var outerKeySelector
                = ReplaceClauseReferences(groupJoinClause.JoinClause.OuterKeySelector, groupJoinClause);

            var innerExpression
                = ReplaceClauseReferences(groupJoinClause.JoinClause.InnerSequence, groupJoinClause.JoinClause);

            var innerSequenceType
                = innerExpression.Type.GetSequenceType();

            var innerItemParameter
                = Expression.Parameter(innerSequenceType);

            var innerElementScoped
                = innerSequenceType.IsConstructedGenericType &&
                  innerSequenceType.GetGenericTypeDefinition() == typeof(QuerySourceScope <>);

            Type innerElementType;

            if (innerElementScoped)
            {
                innerElementType = innerSequenceType.GetTypeInfo().GenericTypeArguments[0];

                _querySourceMapping.AddMapping(
                    groupJoinClause.JoinClause,
                    QuerySourceScope.GetResult(innerItemParameter, groupJoinClause.JoinClause, innerElementType));
            }
            else
            {
                innerElementType = innerSequenceType;

                _querySourceMapping.AddMapping(groupJoinClause.JoinClause, innerItemParameter);
            }

            var innerKeySelector
                = ReplaceClauseReferences(groupJoinClause.JoinClause.InnerKeySelector, groupJoinClause);

            var innerItemsParameter
                = Expression.Parameter(innerExpression.Type);

            _expression
                = Expression.Call(
                      LinqOperatorProvider.GroupJoin.MakeGenericMethod(
                          typeof(QuerySourceScope),
                          innerSequenceType,
                          outerKeySelector.Type,
                          typeof(QuerySourceScope)),
                      _expression,
                      innerExpression,
                      Expression.Lambda(outerKeySelector, QuerySourceScopeParameter),
                      Expression.Lambda(innerKeySelector, innerItemParameter),
                      Expression.Lambda(
                          QuerySourceScope
                          .Create(
                              groupJoinClause,
                              innerElementScoped
                                    ? Expression.Call(
                                  LinqOperatorProvider.Select
                                  .MakeGenericMethod(
                                      innerSequenceType,
                                      innerElementType),
                                  innerItemsParameter,
                                  Expression.Lambda(
                                      QuerySourceScope.GetResult(
                                          innerItemParameter,
                                          groupJoinClause.JoinClause,
                                          innerElementType),
                                      innerItemParameter))
                                    : (Expression)innerItemsParameter,
                              QuerySourceScopeParameter),
                          QuerySourceScopeParameter, innerItemsParameter));

            var expressionTypeInfo = _expression.Type.GetTypeInfo();

            _querySourceMapping.AddMapping(
                groupJoinClause,
                QuerySourceScope
                .GetResult(
                    QuerySourceScopeParameter,
                    groupJoinClause,
                    expressionTypeInfo.GetGenericTypeDefinition()
                    .MakeGenericType(innerElementType)));

            if (expressionTypeInfo.GetGenericTypeDefinition() == typeof(IAsyncEnumerable <>))
            {
                queryModel.TransformExpressions(ReplaceClauseReferences);
            }
        }
Exemple #11
0
        public override void VisitJoinClause(
            [NotNull] JoinClause joinClause, [NotNull] QueryModel queryModel, int index)
        {
            Check.NotNull(joinClause, "joinClause");
            Check.NotNull(queryModel, "queryModel");

            var outerKeySelector
                = ReplaceClauseReferences(joinClause.OuterKeySelector, joinClause);

            var innerSequenceExpression
                = ReplaceClauseReferences(joinClause.InnerSequence, joinClause);

            var innerSequenceType
                = innerSequenceExpression.Type.GetSequenceType();

            var innerItemParameter
                = Expression.Parameter(innerSequenceType);

            var innerElementScoped
                = innerSequenceType.IsConstructedGenericType &&
                  innerSequenceType.GetGenericTypeDefinition() == typeof(QuerySourceScope <>);

            Type innerElementType;

            if (innerElementScoped)
            {
                innerElementType = innerSequenceType.GetTypeInfo().GenericTypeArguments[0];

                _querySourceMapping.AddMapping(
                    joinClause,
                    QuerySourceScope.GetResult(innerItemParameter, joinClause, innerElementType));
            }
            else
            {
                innerElementType = innerSequenceType;

                _querySourceMapping.AddMapping(joinClause, innerItemParameter);
            }

            var innerKeySelector
                = ReplaceClauseReferences(joinClause.InnerKeySelector, joinClause);

            _expression
                = Expression.Call(
                      LinqOperatorProvider.Join.MakeGenericMethod(
                          typeof(QuerySourceScope),
                          innerSequenceType,
                          outerKeySelector.Type,
                          typeof(QuerySourceScope)),
                      _expression,
                      innerSequenceExpression,
                      Expression.Lambda(outerKeySelector, QuerySourceScopeParameter),
                      Expression.Lambda(innerKeySelector, innerItemParameter),
                      Expression.Lambda(
                          QuerySourceScope
                          .Create(
                              joinClause,
                              innerElementScoped
                                    ? QuerySourceScope.GetResult(innerItemParameter, joinClause, innerElementType)
                                    : innerItemParameter,
                              QuerySourceScopeParameter),
                          QuerySourceScopeParameter, innerItemParameter));

            _querySourceMapping.ReplaceMapping(
                joinClause,
                QuerySourceScope.GetResult(QuerySourceScopeParameter, joinClause, innerElementType));
        }
 private TResult GetResult <TResult>(IQuerySource querySource)
 {
     return(_querySource == querySource
         ? ((TypedScope <TResult>) this)._result
         : _parentScope.GetResult <TResult>(querySource));
 }
 public virtual object GetResult([NotNull] IQuerySource querySource)
 => _querySource == querySource
         ? UntypedResult
         : _parentScope.GetResult(querySource);