Example #1
0
        public void EnsureNoSetOperations_WithSetOperations()
        {
            _statementBuilder.SetOperationCombinedStatements.Add(SqlStatementModelObjectMother.CreateSetOperationCombinedStatement());

            var originalStatement      = _statementBuilder.GetSqlStatement();
            var fakeFromExpressionInfo = CreateFakeFromExpressionInfo(new Ordering[0]);

            _stageMock
            .Expect(
                mock => mock.PrepareFromExpression(
                    Arg <Expression> .Is.Anything,
                    Arg <ISqlPreparationContext> .Is.Anything,
                    Arg <Func <ITableInfo, SqlTable> > .Is.Anything,
                    Arg.Is(OrderingExtractionPolicy.ExtractOrderingsIntoProjection)))
            .Return(fakeFromExpressionInfo);
            _stageMock.Replay();

            _handler.EnsureNoSetOperations(_statementBuilder, _generator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();
            Assert.That(_statementBuilder.GetSqlStatement(), Is.Not.EqualTo(originalStatement));
            Assert.That(_statementBuilder.SetOperationCombinedStatements, Is.Empty);

            var sqlTable = _statementBuilder.SqlTables[0];

            Assert.That(((ResolvedSubStatementTableInfo)sqlTable.TableInfo).SqlStatement, Is.EqualTo(originalStatement));
            Assert.That(sqlTable.JoinSemantics, Is.EqualTo(JoinSemantics.Inner));
        }
Example #2
0
        public void Build_WithSetOperationCombinedStatement()
        {
            var setOperationCombinedStatement = SqlStatementModelObjectMother.CreateSetOperationCombinedStatement();
            var sqlStatement = SqlStatementModelObjectMother.CreateMinimalSqlStatement(new SqlStatementBuilder
            {
                SqlTables = { _sqlTable },
                SetOperationCombinedStatements = { setOperationCombinedStatement }
            });

            _stageMock.Expect(
                mock => mock.GenerateTextForSelectExpression(_commandBuilder, sqlStatement.SelectProjection))
            .WhenCalled(mi => ((SqlCommandBuilder)mi.Arguments[0]).Append("[t].[ID],[t].[Name],[t].[City]"));
            _stageMock.Expect(mock => mock.GenerateTextForFromTable(_commandBuilder, sqlStatement.SqlTables[0], true))
            .WhenCalled(mi => ((SqlCommandBuilder)mi.Arguments[0]).Append("[Table] AS [t]"));
            _stageMock.Expect(mock => mock.GenerateTextForSqlStatement(_commandBuilder, sqlStatement.SetOperationCombinedStatements[0].SqlStatement))
            .WhenCalled(mi => ((SqlCommandBuilder)mi.Arguments[0]).Append("SELECT FOO FROM BAR"));
            _stageMock.Replay();

            _generator.Build(sqlStatement, _commandBuilder, false);

            _commandBuilder.SetInMemoryProjectionBody(Expression.Constant(0));
            var result = _commandBuilder.GetCommand();

            Assert.That(result.CommandText, Is.EqualTo("SELECT [t].[ID],[t].[Name],[t].[City] FROM [Table] AS [t] UNION (SELECT FOO FROM BAR)"));
            _stageMock.VerifyAllExpectations();
        }
        public void HandleResultOperator_AfterSetOperation_CreatesSubStatement()
        {
            _sqlStatementBuilder.SetOperationCombinedStatements.Add(SqlStatementModelObjectMother.CreateSetOperationCombinedStatement());

            var resultOperator = new MaxResultOperator();

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

            AssertStatementWasMovedToSubStatement(_sqlStatementBuilder);
        }
Example #4
0
        public void HandleResultOperator_TakeAfterSetOperation()
        {
            _sqlStatementBuilder.SetOperationCombinedStatements.Add(SqlStatementModelObjectMother.CreateSetOperationCombinedStatement());

            var takeExpression = Expression.Constant(2);
            var resultOperator = new TakeResultOperator(takeExpression);

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

            AssertStatementWasMovedToSubStatement(_sqlStatementBuilder);
        }
        public void HandleResultOperator_SkipAfterSetOperations_MovesStatementToSubStatement()
        {
            _sqlStatementBuilder.SetOperationCombinedStatements.Add(SqlStatementModelObjectMother.CreateSetOperationCombinedStatement());

            var resultOperator = new SkipResultOperator(Expression.Constant(0));

            var stage = CreateDefaultSqlPreparationStage();

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

            AssertStatementWasMovedToSubStatement(GetSubStatement(_sqlStatementBuilder.SqlTables[0]));
        }
Example #6
0
        public void ResolveSqlStatement_WithNoChanges_ShouldLeaveAllObjectsTheSame()
        {
            var constantExpression            = Expression.Constant(new Restaurant());
            var whereCondition                = Expression.Constant(true);
            var topExpression                 = Expression.Constant("top");
            var groupExpression               = Expression.Constant("group");
            var ordering                      = new Ordering(Expression.Constant("ordering"), OrderingDirection.Desc);
            var setOperationCombinedStatement = SqlStatementModelObjectMother.CreateSetOperationCombinedStatement();
            var builder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement_Resolved(typeof(Cook)))
            {
                SelectProjection  = constantExpression,
                DataInfo          = new StreamedSequenceInfo(typeof(Restaurant[]), constantExpression),
                WhereCondition    = whereCondition,
                GroupByExpression = groupExpression,
                TopExpression     = topExpression,
                SetOperationCombinedStatements = { setOperationCombinedStatement }
            };

            builder.Orderings.Add(ordering);
            var sqlStatement = builder.GetSqlStatement();

            _stageMock
            .Expect(mock => mock.ResolveSelectExpression(Arg.Is(constantExpression), Arg <SqlStatementBuilder> .Is.Anything, Arg.Is(_mappingResolutionContext)))
            .Return(constantExpression);
            _stageMock
            .Expect(mock => mock.ResolveWhereExpression(whereCondition, _mappingResolutionContext))
            .Return(whereCondition);
            _stageMock
            .Expect(mock => mock.ResolveGroupByExpression(groupExpression, _mappingResolutionContext))
            .Return(groupExpression);
            _stageMock
            .Expect(mock => mock.ResolveTopExpression(topExpression, _mappingResolutionContext))
            .Return(topExpression);
            _stageMock
            .Expect(mock => mock.ResolveOrderingExpression(ordering.Expression, _mappingResolutionContext))
            .Return(ordering.Expression);
            _stageMock
            .Expect(mock => mock.ResolveTableInfo(sqlStatement.SqlTables[0].TableInfo, _mappingResolutionContext))
            .Return((IResolvedTableInfo)sqlStatement.SqlTables[0].TableInfo);
            _stageMock
            .Expect(mock => mock.ResolveSqlStatement(setOperationCombinedStatement.SqlStatement, _mappingResolutionContext))
            .Return(setOperationCombinedStatement.SqlStatement);

            _stageMock.Replay();

            var resolvedSqlStatement = _visitor.ResolveSqlStatement(sqlStatement);

            _stageMock.VerifyAllExpectations();

            Assert.That(resolvedSqlStatement, Is.EqualTo(sqlStatement));
        }
        public void HandleResultOperator_GroupByAfterSetOperations()
        {
            _sqlStatementBuilder.SetOperationCombinedStatements.Add(SqlStatementModelObjectMother.CreateSetOperationCombinedStatement());

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

            var stage = CreateDefaultSqlPreparationStage();

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

            AssertStatementWasMovedToSubStatement(_sqlStatementBuilder);
        }
        public void HandleResultOperator_AllAfterSetOperation()
        {
            _sqlStatementBuilder.SetOperationCombinedStatements.Add(SqlStatementModelObjectMother.CreateSetOperationCombinedStatement());

            var stage = CreateDefaultSqlPreparationStage();

            var predicate      = Expression.Constant(true);
            var resultOperator = new AllResultOperator(predicate);

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

            var innerStatementOfExistsExpression = GetInnerStatementAfterExistsTransformation(_sqlStatementBuilder);

            AssertStatementWasMovedToSubStatement(innerStatementOfExistsExpression);
        }
Example #9
0
        public void BuildSelectPart_OuterSelect_WithSetOperationsAggregation()
        {
            var sqlStatement = SqlStatementModelObjectMother.CreateMinimalSqlStatement(new SqlStatementBuilder
            {
                SetOperationCombinedStatements = { SqlStatementModelObjectMother.CreateSetOperationCombinedStatement() }
            });

            _stageMock.Expect(
                mock => mock.GenerateTextForOuterSelectExpression(
                    _commandBuilder,
                    sqlStatement.SelectProjection,
                    SetOperationsMode.StatementIsSetCombined)
                );

            _generator.BuildSelectPart(sqlStatement, _commandBuilder, true);

            _stageMock.VerifyAllExpectations();
        }
Example #10
0
        public void ResolveSqlStatement()
        {
            var constantExpression            = Expression.Constant(new Restaurant());
            var whereCondition                = Expression.Constant(true);
            var topExpression                 = Expression.Constant("top");
            var groupExpression               = Expression.Constant("group");
            var ordering                      = new Ordering(Expression.Constant("ordering"), OrderingDirection.Desc);
            var setOperationCombinedStatement = SqlStatementModelObjectMother.CreateSetOperationCombinedStatement();
            var builder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement_Resolved(typeof(Cook)))
            {
                SelectProjection  = constantExpression,
                DataInfo          = new StreamedSequenceInfo(typeof(Restaurant[]), constantExpression),
                WhereCondition    = whereCondition,
                GroupByExpression = groupExpression,
                TopExpression     = topExpression,
                SetOperationCombinedStatements = { setOperationCombinedStatement }
            };

            builder.Orderings.Add(ordering);
            var sqlStatement        = builder.GetSqlStatement();
            var fakeExpression      = Expression.Constant(new Cook());
            var fakeWhereCondition  = Expression.Constant(true);
            var fakeGroupExpression = Expression.Constant("group");
            var fakeTopExpression   = Expression.Constant("top");
            var fakeOrderExpression = Expression.Constant("order");
            var fakeSqlStatement    = SqlStatementModelObjectMother.CreateSqlStatement();

            Assert.That(fakeSqlStatement, Is.Not.EqualTo(setOperationCombinedStatement.SqlStatement), "This is important for the test below.");

            _stageMock
            .Expect(mock => mock.ResolveSelectExpression(Arg.Is(constantExpression), Arg <SqlStatementBuilder> .Is.Anything, Arg.Is(_mappingResolutionContext)))
            .Return(fakeExpression);
            _stageMock
            .Expect(mock => mock.ResolveWhereExpression(whereCondition, _mappingResolutionContext))
            .Return(fakeWhereCondition);
            _stageMock
            .Expect(mock => mock.ResolveGroupByExpression(groupExpression, _mappingResolutionContext))
            .Return(fakeGroupExpression);
            _stageMock
            .Expect(mock => mock.ResolveTopExpression(topExpression, _mappingResolutionContext))
            .Return(fakeTopExpression);
            _stageMock
            .Expect(mock => mock.ResolveOrderingExpression(ordering.Expression, _mappingResolutionContext))
            .Return(fakeOrderExpression);
            _stageMock
            .Expect(mock => mock.ResolveTableInfo(sqlStatement.SqlTables[0].TableInfo, _mappingResolutionContext))
            .Return(new ResolvedSimpleTableInfo(typeof(Cook), "CookTable", "c"));
            _stageMock
            .Expect(mock => mock.ResolveSqlStatement(setOperationCombinedStatement.SqlStatement, _mappingResolutionContext))
            .Return(fakeSqlStatement);

            _stageMock.Replay();

            var resolvedSqlStatement = _visitor.ResolveSqlStatement(sqlStatement);

            _stageMock.VerifyAllExpectations();
            Assert.That(resolvedSqlStatement.DataInfo, Is.TypeOf(typeof(StreamedSequenceInfo)));
            Assert.That(((StreamedSequenceInfo)resolvedSqlStatement.DataInfo).DataType, Is.EqualTo(typeof(IQueryable <>).MakeGenericType(typeof(Cook))));
            Assert.That(resolvedSqlStatement.SelectProjection, Is.SameAs(fakeExpression));
            Assert.That(resolvedSqlStatement.WhereCondition, Is.SameAs(fakeWhereCondition));
            Assert.That(resolvedSqlStatement.TopExpression, Is.SameAs(fakeTopExpression));
            Assert.That(resolvedSqlStatement.GroupByExpression, Is.SameAs(fakeGroupExpression));
            Assert.That(resolvedSqlStatement.Orderings[0].Expression, Is.SameAs(fakeOrderExpression));
            Assert.That(sqlStatement.Orderings[0].Expression, Is.SameAs(ordering.Expression));
            Assert.That(resolvedSqlStatement.SetOperationCombinedStatements.Single().SqlStatement, Is.SameAs(fakeSqlStatement));
        }