public void HandleResultOperator()
        {
            var keySelector     = new SqlColumnDefinitionExpression(typeof(string), "c", "Name", false);
            var elementSelector = Expression.Constant("elementSelector");
            var resultOperator  = new GroupResultOperator("itemName", keySelector, elementSelector);

            _stageMock
            .Expect(mock => mock.PrepareResultOperatorItemExpression(keySelector, _context))
            .Return(keySelector);
            _stageMock
            .Expect(mock => mock.PrepareResultOperatorItemExpression(elementSelector, _context))
            .Return(elementSelector);
            _stageMock.Replay();

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, UniqueIdentifierGenerator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();
            Assert.That(_sqlStatementBuilder.GroupByExpression, Is.SameAs(keySelector));

            var expectedSelectProjection = new SqlGroupingSelectExpression(
                new NamedExpression("key", keySelector),
                new NamedExpression("element", elementSelector));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedSelectProjection, _sqlStatementBuilder.SelectProjection);

            Assert.That(
                _sqlStatementBuilder.DataInfo.DataType,
                Is.EqualTo(typeof(IQueryable <>).MakeGenericType(typeof(IGrouping <,>).MakeGenericType(typeof(string), typeof(string)))));
        }
        public void HandleResultOperator_CreatesAndPreparesSubStatementWithNewProjection()
        {
            var resultOperator = new SkipResultOperator(Expression.Constant(0));

            _sqlStatementBuilder.Orderings.Add(_ordering);

            var expectedNewProjection = Expression.New(
                _tupleCtor,
                new Expression[] { _selectProjection, new SqlRowNumberExpression(new[] { _ordering }) },
                new MemberInfo[] { GetTupleMethod("get_Key"), GetTupleMethod("get_Value") });

            var fakePreparedProjection = GetFakePreparedProjection();

            _stageMock
            .Expect(mock => mock.PrepareSelectExpression(Arg <Expression> .Is.Anything, Arg.Is(_context)))
            .WhenCalled(mi => SqlExpressionTreeComparer.CheckAreEqualTrees(expectedNewProjection, (Expression)mi.Arguments[0]))
            .Return(fakePreparedProjection);
            _stageMock.Replay();

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, UniqueIdentifierGenerator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();

            Assert.That(_sqlStatementBuilder.SqlTables.Count, Is.EqualTo(1));
            var sqlTable = _sqlStatementBuilder.SqlTables[0];

            Assert.That(sqlTable.TableInfo, Is.TypeOf(typeof(ResolvedSubStatementTableInfo)));
            Assert.That(sqlTable.JoinSemantics, Is.EqualTo(JoinSemantics.Inner));

            var subStatementTableInfo = ((ResolvedSubStatementTableInfo)sqlTable.TableInfo);

            Assert.That(subStatementTableInfo.SqlStatement.SelectProjection, Is.SameAs(fakePreparedProjection));
            Assert.That(subStatementTableInfo.TableAlias, Is.EqualTo("q0"));
        }
        public void CreateSqlTableForSubStatement_WithoutOrderings()
        {
            var statementWithoutOrderings = SqlStatementModelObjectMother.CreateSqlStatementWithCook();

            var result = _factory.CreateSqlTableForStatement(
                statementWithoutOrderings,
                info => new SqlTable(info, JoinSemantics.Inner),
                OrderingExtractionPolicy.ExtractOrderingsIntoProjection);

            _stageMock.VerifyAllExpectations();

            var tableInfo = result.SqlTable.TableInfo;

            Assert.That(tableInfo, Is.TypeOf(typeof(ResolvedSubStatementTableInfo)));

            var subStatement = ((ResolvedSubStatementTableInfo)tableInfo).SqlStatement;

            Assert.That(subStatement, Is.SameAs(statementWithoutOrderings));

            Assert.That(result.WhereCondition, Is.Null);
            Assert.That(result.ExtractedOrderings, Is.Empty);

            var expectedItemSelector = new SqlTableReferenceExpression(result.SqlTable);

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedItemSelector, result.ItemSelector);
        }
        public void HandleResultOperator()
        {
            var resultOperator            = new UnionResultOperator("x", typeof(int), ExpressionHelper.CreateExpression(typeof(int[])));
            var preparedSource2Statement  = SqlStatementModelObjectMother.CreateSqlStatement();
            var preparedSource2Expression = new SqlSubStatementExpression(preparedSource2Statement);

            _stageMock
            .Expect(mock => mock.PrepareResultOperatorItemExpression(resultOperator.Source2, _context))
            .Return(preparedSource2Expression);

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, UniqueIdentifierGenerator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();

            // Main functionality
            Assert.That(_sqlStatementBuilder.SetOperationCombinedStatements, Has.Count.EqualTo(1));
            Assert.That(_sqlStatementBuilder.SetOperationCombinedStatements[0].SetOperation, Is.EqualTo(SetOperation.Union));
            Assert.That(_sqlStatementBuilder.SetOperationCombinedStatements[0].SqlStatement, Is.SameAs(preparedSource2Statement));

            // Data info
            Assert.That(_sqlStatementBuilder.DataInfo, Is.TypeOf(typeof(StreamedSequenceInfo)));
            Assert.That(((StreamedSequenceInfo)_sqlStatementBuilder.DataInfo).DataType, Is.EqualTo(typeof(IQueryable <>).MakeGenericType(typeof(int))));
            Assert.That(
                ((StreamedSequenceInfo)_sqlStatementBuilder.DataInfo).ItemExpression,
                Is.EqualTo(new QuerySourceReferenceExpression(resultOperator)));

            // Everyone referencing the result operator should reference the (outer) select projection instead.
            Assert.That(
                _context.GetExpressionMapping(new QuerySourceReferenceExpression(resultOperator)),
                Is.SameAs(_sqlStatementBuilder.SelectProjection));
        }
Beispiel #5
0
        public void CreateSqlQuery()
        {
            var fakePreparationResult = CreateSqlStatement();

            _preparationStageMock
            .Expect(mock => mock.PrepareSqlStatement(_queryModel, null))
            .Return(fakePreparationResult);
            var fakeResolutionResult = CreateSqlStatement();

            _resolutionStageMock
            .Expect(mock => mock.ResolveSqlStatement(Arg.Is(fakePreparationResult), Arg <MappingResolutionContext> .Is.TypeOf))
            .Return(fakeResolutionResult);
            _generationStageMock
            .Expect(mock => mock.GenerateTextForOuterSqlStatement(Arg <SqlCommandBuilder> .Is.TypeOf, Arg.Is(fakeResolutionResult)))
            .WhenCalled(mi =>
            {
                var sqlCommandBuilder = ((SqlCommandBuilder)mi.Arguments[0]);
                sqlCommandBuilder.Append("TestTest");
                sqlCommandBuilder.SetInMemoryProjectionBody(Expression.Constant(null));
            });

            var result = _sqlQueryGenerator.CreateSqlQuery(_queryModel);

            _preparationStageMock.VerifyAllExpectations();
            _resolutionStageMock.VerifyAllExpectations();
            _generationStageMock.VerifyAllExpectations();

            Assert.That(result.SqlCommand.CommandText, Is.EqualTo("TestTest"));
        }
        public void HandleResultOperator()
        {
            var resultOperator = new AnyResultOperator();
            var sqlStatement   = _sqlStatementBuilder.GetSqlStatement();

            var fakePreparedSelectProjection = Expression.Constant(false);

            _stageMock
            .Expect(mock => mock.PrepareSelectExpression(Arg <Expression> .Matches(e => e is SqlExistsExpression), Arg.Is(_context)))
            .WhenCalled(
                mi =>
            {
                var selectProjection = (Expression)mi.Arguments[0];
                Assert.That(selectProjection, Is.TypeOf(typeof(SqlExistsExpression)));

                var expectedExistsExpression = new SqlExistsExpression(new SqlSubStatementExpression(sqlStatement));
                SqlExpressionTreeComparer.CheckAreEqualTrees(expectedExistsExpression, selectProjection);
            })
            .Return(fakePreparedSelectProjection);

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, _generator, _stageMock, _context);

            Assert.That(_sqlStatementBuilder.DataInfo, Is.TypeOf(typeof(StreamedScalarValueInfo)));
            Assert.That(((StreamedScalarValueInfo)_sqlStatementBuilder.DataInfo).DataType, Is.EqualTo(typeof(Boolean)));

            _stageMock.VerifyAllExpectations();

            Assert.That(_sqlStatementBuilder.SelectProjection, Is.SameAs(fakePreparedSelectProjection));
        }
Beispiel #7
0
        public void MoveCurrentStatementToSqlTable()
        {
            var originalStatement = _statementBuilder.GetSqlStatement();

            var fakeFromExpressionInfo = CreateFakeFromExpressionInfo(new Ordering[0]);
            Func <ITableInfo, SqlTable> tableGenerator = info => new SqlTable(info, JoinSemantics.Inner);

            var someOrderingExtractionPolicy = Some.Item(
                OrderingExtractionPolicy.DoNotExtractOrderings,
                OrderingExtractionPolicy.ExtractOrderingsIntoProjection);

            _stageMock
            .Expect(mock => mock.PrepareFromExpression(
                        Arg <SqlSubStatementExpression> .Is.TypeOf,
                        Arg.Is(_context),
                        Arg.Is(tableGenerator),
                        Arg.Is(someOrderingExtractionPolicy)))
            .Return(fakeFromExpressionInfo)
            .WhenCalled(mi =>
            {
                var sqlStatement = ((SqlSubStatementExpression)mi.Arguments[0]).SqlStatement;
                SqlExpressionTreeComparer.CheckAreEqualTrees(new NamedExpression(null, originalStatement.SelectProjection), sqlStatement.SelectProjection);

                Assert.That(sqlStatement.DataInfo, Is.SameAs(originalStatement.DataInfo));
                Assert.That(sqlStatement.WhereCondition, Is.SameAs(originalStatement.WhereCondition));
            });
            _stageMock.Replay();

            _handler.MoveCurrentStatementToSqlTable(_statementBuilder, _context, tableGenerator, _stageMock, someOrderingExtractionPolicy);

            _stageMock.VerifyAllExpectations();

            Assert.That(_statementBuilder.DataInfo, Is.SameAs(originalStatement.DataInfo));
            Assert.That(_statementBuilder.SqlTables[0], Is.SameAs(fakeFromExpressionInfo.SqlTable));
            Assert.That(_statementBuilder.SelectProjection, Is.SameAs(fakeFromExpressionInfo.ItemSelector));

            var mappedItemExpression = _context.GetExpressionMapping(((StreamedSequenceInfo)originalStatement.DataInfo).ItemExpression);

            Assert.That(mappedItemExpression, Is.Not.Null);
            Assert.That(mappedItemExpression, Is.SameAs(fakeFromExpressionInfo.ItemSelector));
        }
        public void VisitQuerySourceReferenceExpression()
        {
            var innerSequenceExpression = Expression.Constant(new[] { new Cook() });
            var joinClause = new JoinClause(
                "x",
                typeof(Cook[]),
                innerSequenceExpression,
                Expression.Constant(new Cook()),
                Expression.Constant(new Cook()));
            var groupJoinClause = new GroupJoinClause("g", typeof(Cook[]), joinClause);
            var querySourceReferenceExpression = new QuerySourceReferenceExpression(groupJoinClause);
            var fakeWhereExpression            = Expression.Constant(true);

            _stageMock
            .Expect(mock => mock.PrepareWhereExpression(Arg <Expression> .Matches(e => e is BinaryExpression), Arg.Is(_context)))
            .WhenCalled(
                mi =>
                SqlExpressionTreeComparer.CheckAreEqualTrees(
                    Expression.Equal(groupJoinClause.JoinClause.OuterKeySelector, groupJoinClause.JoinClause.InnerKeySelector),
                    (Expression)mi.Arguments[0]))
            .Return(fakeWhereExpression);
            _stageMock.Replay();

            var visitor = CreateTestableVisitor(_someOrderingExtractionPolicy);
            var result  = visitor.VisitQuerySourceReference(querySourceReferenceExpression);

            _stageMock.VerifyAllExpectations();

            Debug.Assert(visitor.FromExpressionInfo != null, "_visitor.FromExpressionInfo != null");
            var fromExpressionInfo = (FromExpressionInfo)visitor.FromExpressionInfo;

            SqlExpressionTreeComparer.CheckAreEqualTrees(
                new SqlTableReferenceExpression(fromExpressionInfo.SqlTable),
                _context.GetExpressionMapping(new QuerySourceReferenceExpression(groupJoinClause.JoinClause)));
            SqlExpressionTreeComparer.CheckAreEqualTrees(fromExpressionInfo.ItemSelector, result);
            Assert.That(((UnresolvedTableInfo)fromExpressionInfo.SqlTable.TableInfo).ItemType, Is.EqualTo(typeof(Cook)));
            Assert.That(fromExpressionInfo.WhereCondition, Is.SameAs(fakeWhereExpression));
            Assert.That(fromExpressionInfo.ExtractedOrderings.Count, Is.EqualTo(0));
        }
        public void HandleResultOperator()
        {
            var predicate         = Expression.Constant(true);
            var preparedPredicate = Expression.Constant(false);
            var resultOperator    = new AllResultOperator(predicate);
            var sqlStatement      = _sqlStatementBuilder.GetSqlStatement();

            var fakePreparedSelectProjection = Expression.Constant(false);

            _stageMock
            .Expect(mock => mock.PrepareWhereExpression(
                        Arg <Expression> .Matches(e => e.NodeType == ExpressionType.Not && (((UnaryExpression)e).Operand == predicate)),
                        Arg <ISqlPreparationContext> .Matches(c => c == _context)))
            .Return(preparedPredicate);
            _stageMock
            .Expect(mock => mock.PrepareSelectExpression(Arg <Expression> .Matches(e => e.NodeType == ExpressionType.Not), Arg.Is(_context)))
            .WhenCalled(
                mi =>
            {
                var selectProjection     = (Expression)mi.Arguments[0];
                var expectedSubStatement = new SqlStatementBuilder(sqlStatement)
                {
                    WhereCondition = preparedPredicate
                }.GetSqlStatement();
                var expectedExistsExpression = new SqlExistsExpression(new SqlSubStatementExpression(expectedSubStatement));
                var expectedExpression       = Expression.Not(expectedExistsExpression);

                SqlExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, selectProjection);
            })
            .Return(fakePreparedSelectProjection);
            _stageMock.Replay();

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, UniqueIdentifierGenerator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();

            Assert.That(_sqlStatementBuilder.DataInfo, Is.TypeOf(typeof(StreamedScalarValueInfo)));
            Assert.That(((StreamedScalarValueInfo)_sqlStatementBuilder.DataInfo).DataType, Is.EqualTo(typeof(Boolean)));

            Assert.That(_sqlStatementBuilder.SelectProjection, Is.SameAs(fakePreparedSelectProjection));
        }
        public void HandleResultOperator()
        {
            var itemExpression = Expression.Constant(new Cook());
            var resultOperator = new ContainsResultOperator(itemExpression);
            var sqlStatement   = _sqlStatementBuilder.GetSqlStatement();

            var preparedExpression = Expression.Constant(new Cook(), typeof(Cook));

            _stageMock.Expect(mock => mock.PrepareResultOperatorItemExpression(itemExpression, _context)).Return(preparedExpression);
            _stageMock.Replay();

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, _generator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();

            Assert.That(_sqlStatementBuilder.DataInfo, Is.TypeOf(typeof(StreamedScalarValueInfo)));
            Assert.That(((StreamedScalarValueInfo)_sqlStatementBuilder.DataInfo).DataType, Is.EqualTo(typeof(Boolean)));

            var expectedExpression = new SqlInExpression(preparedExpression, new SqlSubStatementExpression(sqlStatement));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, _sqlStatementBuilder.SelectProjection);
        }
        public void VisitMainFromClause_CreatesFromExpression()
        {
            var preparedSqlTable           = SqlStatementModelObjectMother.CreateSqlTable();
            var preparedFromExpressionInfo = new FromExpressionInfo(
                preparedSqlTable, new Ordering[] { }, new SqlTableReferenceExpression(preparedSqlTable), null);

            _stageMock
            .Expect(
                mock => mock.PrepareFromExpression(
                    Arg <Expression> .Matches(e => e == _mainFromClause.FromExpression),
                    Arg <ISqlPreparationContext> .Matches(c => c != _context),
                    Arg <Func <ITableInfo, SqlTable> > .Is.Anything))
            .Return(preparedFromExpressionInfo);

            _stageMock.Replay();

            _visitor.VisitMainFromClause(_mainFromClause, _queryModel);
            _stageMock.VerifyAllExpectations();

            Assert.That(_visitor.SqlStatementBuilder.SqlTables, Is.EqualTo(new[] { preparedSqlTable }));
            Assert.That(
                ((SqlTableReferenceExpression)_visitor.Context.GetExpressionMapping(new QuerySourceReferenceExpression(_mainFromClause))).SqlTable,
                Is.SameAs(preparedSqlTable));
        }