Exemplo n.º 1
0
        public void Transform_ChildrenTransformedBeforeParent()
        {
            var inputChildExpression       = CreateSimpleExpression(0);
            var transformedChildExpression = CreateSimpleExpression(1);

            var inputParentExpression       = Expression.UnaryPlus(inputChildExpression);
            var transformedParentExpression = CreateSimpleExpression(2);

            var providerMock = MockRepository.GenerateStrictMock <IExpressionTranformationProvider> ();

            providerMock
            .Expect(mock => mock.GetTransformations(inputChildExpression))
            .Return(new ExpressionTransformation[] { expr => transformedChildExpression });
            providerMock
            .Expect(mock => mock.GetTransformations(transformedChildExpression))
            .Return(new ExpressionTransformation[0]);
            providerMock
            .Expect(mock => mock.GetTransformations(
                        Arg <Expression> .Matches(expr => expr is UnaryExpression && ((UnaryExpression)expr).Operand == transformedChildExpression)))
            .Return(new ExpressionTransformation[] { expr => transformedParentExpression });
            providerMock
            .Expect(mock => mock.GetTransformations(transformedParentExpression))
            .Return(new ExpressionTransformation[0]);
            providerMock.Replay();

            var result = TransformingExpressionTreeVisitor.Transform(inputParentExpression, providerMock);

            providerMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(transformedParentExpression));
        }
Exemplo n.º 2
0
        private static void AssertLambda(LambdaExpression lambda, string expected)
        {
            var expression = lambda.Body;

            expression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);
            expression = TransformingExpressionTreeVisitor.Transform(expression, _transformationProvider);
            var result = SDataExpressionBuilderVisitor.BuildExpression(expression);

            Assert.That(result, Is.EqualTo(expected));
        }
        public static string GetN1QlSelectNewExpression(NewExpression expression, N1QlQueryGenerationContext queryGenerationContext)
        {
            // Ensure that any date/time expressions are properly converted to Unix milliseconds as needed
            expression = (NewExpression)TransformingExpressionTreeVisitor.Transform(expression,
                                                                                    ExpressionTransformers.DateTimeTransformationRegistry.Default);

            var visitor = new N1QlExpressionTreeVisitor(queryGenerationContext);

            visitor.VisitSelectNewExpression(expression);
            return(visitor.GetN1QlExpression());
        }
Exemplo n.º 4
0
        public void Transform_NullExpression()
        {
            var providerMock = MockRepository.GenerateStrictMock <IExpressionTranformationProvider> ();

            providerMock.Replay();

            var result = TransformingExpressionTreeVisitor.Transform(null, providerMock);

            providerMock.VerifyAllExpectations();
            Assert.That(result, Is.Null);
        }
Exemplo n.º 5
0
        private string GetN1QlExpression(Expression expression)
        {
            if (_groupingStatus == GroupingStatus.AfterGroupSubquery)
            {
                // SELECT, HAVING, and ORDER BY clauses must be remapped to refer directly to the extents in the grouping subquery
                // rather than refering to the output of the grouping subquery

                expression = TransformingExpressionTreeVisitor.Transform(expression, _groupingExpressionTransformerRegistry);
            }

            return(N1QlExpressionTreeVisitor.GetN1QlExpression(expression, _queryGenerationContext));
        }
Exemplo n.º 6
0
        public void Transform_NoMatchingTransformation()
        {
            var inputExpression = CreateSimpleExpression(0);

            var providerMock = MockRepository.GenerateStrictMock <IExpressionTranformationProvider> ();

            providerMock
            .Expect(mock => mock.GetTransformations(inputExpression))
            .Return(new ExpressionTransformation[0]);
            providerMock.Replay();

            var result = TransformingExpressionTreeVisitor.Transform(inputExpression, providerMock);

            providerMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(inputExpression));
        }
Exemplo n.º 7
0
        public void Transform_TwoMatchingTransformations_FirstTransformationChangesExpression()
        {
            var inputExpression       = CreateSimpleExpression(0);
            var transformedExpression = CreateSimpleExpression(0);

            var providerMock = MockRepository.GenerateStrictMock <IExpressionTranformationProvider> ();

            providerMock
            .Expect(mock => mock.GetTransformations(inputExpression))
            .Return(new ExpressionTransformation[] {
                expr => transformedExpression,
                expr => { throw new InvalidOperationException("Must not be called."); }
            });
            providerMock
            .Expect(mock => mock.GetTransformations(transformedExpression))
            .Return(new ExpressionTransformation[0]);
            providerMock.Replay();

            var result = TransformingExpressionTreeVisitor.Transform(inputExpression, providerMock);

            providerMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(transformedExpression));
        }
 public Expression Process(Expression expressionTree)
 {
     return(TransformingExpressionTreeVisitor.Transform(expressionTree, _provider));
 }
        public Expression Process(Expression expressionTree)
        {
            ArgumentUtility.CheckNotNull("expressionTree", expressionTree);

            return(TransformingExpressionTreeVisitor.Transform(expressionTree, _provider));
        }
Exemplo n.º 10
0
        private string GetSelectParameters(SelectClause selectClause, QueryModel queryModel)
        {
            string expression;

            if (selectClause.Selector.GetType() == typeof(QuerySourceReferenceExpression))
            {
                if (_queryPartsAggregator.AggregateFunction == null)
                {
                    expression = GetN1QlExpression(selectClause.Selector);

                    if (_queryPartsAggregator.QueryType != N1QlQueryType.Array)
                    {
                        expression = string.Concat(expression, ".*");
                    }
                }
                else
                {
                    // for aggregates, just use "*" (i.e. AggregateFunction = "COUNT", expression = "*" results in COUNT(*)"

                    expression = "*";
                }
            }
            else if (selectClause.Selector.NodeType == ExpressionType.New)
            {
                if (_queryPartsAggregator.QueryType != N1QlQueryType.Array)
                {
                    var selector = selectClause.Selector as NewExpression;

                    if (_groupingStatus == GroupingStatus.AfterGroupSubquery)
                    {
                        // SELECT clauses must be remapped to refer directly to the extents in the grouping subquery
                        // rather than refering to the output of the grouping subquery

                        selector = (NewExpression)TransformingExpressionTreeVisitor.Transform(selector, _groupingExpressionTransformerRegistry);
                    }

                    expression =
                        N1QlExpressionTreeVisitor.GetN1QlSelectNewExpression(selector, _queryGenerationContext);
                }
                else
                {
                    expression = GetN1QlExpression(selectClause.Selector);
                }
            }
            else
            {
                expression = GetN1QlExpression(selectClause.Selector);

                if ((_queryPartsAggregator.QueryType == N1QlQueryType.Subquery) || (_queryPartsAggregator.QueryType == N1QlQueryType.Array))
                {
                    // For LINQ, this subquery is expected to return a list of the specific property being selected
                    // But N1QL will always return a list of objects with a single property
                    // So we need to use an ARRAY statement to convert the list

                    _queryPartsAggregator.ArrayPropertyExtractionPart = N1QlHelpers.EscapeIdentifier("result");

                    expression += " as " + _queryPartsAggregator.ArrayPropertyExtractionPart;
                }
            }

            return(expression);
        }
        private string GetSelectParameters(SelectClause selectClause, QueryModel queryModel)
        {
            string expression;

            if (selectClause.Selector.GetType() == typeof(QuerySourceReferenceExpression))
            {
                if (_queryPartsAggregator.AggregateFunction == null)
                {
                    expression = GetN1QlExpression(selectClause.Selector);

                    if (_queryPartsAggregator.QueryType != N1QlQueryType.Array)
                    {
                        expression = string.Concat(expression, ".*");
                    }
                }
                else
                {
                    // for aggregates, just use "*" (i.e. AggregateFunction = "COUNT", expression = "*" results in COUNT(*)"

                    ResultExtractionRequired = true;
                    _queryPartsAggregator.PropertyExtractionPart = N1QlHelpers.EscapeIdentifier("result");
                    expression = "*";
                }
            }
            else if (selectClause.Selector.NodeType == ExpressionType.New)
            {
                if (_queryPartsAggregator.QueryType != N1QlQueryType.Array)
                {
                    var selector = selectClause.Selector as NewExpression;

                    if (_groupingStatus == GroupingStatus.AfterGroupSubquery)
                    {
                        // SELECT clauses must be remapped to refer directly to the extents in the grouping subquery
                        // rather than refering to the output of the grouping subquery

                        selector = (NewExpression)TransformingExpressionTreeVisitor.Transform(selector, _groupingExpressionTransformerRegistry);
                    }

                    expression =
                        N1QlExpressionTreeVisitor.GetN1QlSelectNewExpression(selector, _queryGenerationContext);
                }
                else
                {
                    expression = GetN1QlExpression(selectClause.Selector);
                }
            }
            else
            {
                expression = GetN1QlExpression(selectClause.Selector);

                if (_queryPartsAggregator.QueryType == N1QlQueryType.Subquery)
                {
                    // For LINQ, this subquery is expected to return a list of the specific property being selected
                    // But N1QL will always return a list of objects with a single property
                    // So we need to use an ARRAY statement to convert the list

                    _queryPartsAggregator.PropertyExtractionPart = N1QlHelpers.EscapeIdentifier("result");

                    expression += " as " + _queryPartsAggregator.PropertyExtractionPart;
                }
                else
                {
                    // This is a select expression on the main query that doesn't use a "new" clause, and isn't against an extent
                    // So it will return an array of objects with properties, while LINQ is expecting an array of values
                    // Since we can't extract the properties from the object in N1QL like we can with subqueries
                    // We need to indicate to the BucketQueryExecutor that it will need to extract the properties

                    _queryPartsAggregator.PropertyExtractionPart = N1QlHelpers.EscapeIdentifier("result");
                    ResultExtractionRequired = true;
                }
            }

            return(expression);
        }