Exemple #1
0
        public void VisitSubqueryExpressionTest_IsCountQuery()
        {
            var mainFromClause          = ExpressionHelper.CreateMainFromClause <Cook>();
            var querModel               = ExpressionHelper.CreateQueryModel(mainFromClause);
            var expression              = new SubQueryExpression(querModel);
            var sqlStatement            = SqlStatementModelObjectMother.CreateSqlStatement();
            var fakeSqlStatementBuilder = new SqlStatementBuilder(sqlStatement)
            {
                SelectProjection =
                    new AggregationExpression(typeof(int), sqlStatement.SelectProjection, AggregationModifier.Count)
            };

            var fakeSqlStatement = fakeSqlStatementBuilder.GetSqlStatement();

            _stageMock
            .Expect(mock => mock.PrepareSqlStatement(querModel, _context))
            .Return(fakeSqlStatement);

            var result = SqlPreparationExpressionVisitor.TranslateExpression(expression, _context, _stageMock, _methodCallTransformerProvider);

            Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression)));
            Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.SameAs(fakeSqlStatement));
            Assert.That(
                ((AggregationExpression)((SqlSubStatementExpression)result).SqlStatement.SelectProjection).AggregationModifier,
                Is.EqualTo(AggregationModifier.Count));
        }
        public void VisitSqlSubStatementExpression_WithChanges()
        {
            var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement(Expression.Constant(0));
            var expression   = new SqlSubStatementExpression(sqlStatement);

            var fakeResolvedStatement = SqlStatementModelObjectMother.CreateSqlStatement(Expression.Constant(1));

            _stageMock
            .Expect(mock => mock.ResolveSqlStatement(sqlStatement, _mappingResolutionContext))
            .Return(fakeResolvedStatement);
            var fakeSimplifiedExpression = Expression.Constant(0);

            _groupAggregateSimplifierMock
            .Expect(
                mock => mock.SimplifyIfPossible(
                    Arg <SqlSubStatementExpression> .Matches(e => ReferenceEquals(e.SqlStatement, fakeResolvedStatement)),
                    Arg.Is(expression.SqlStatement.SelectProjection)))
            .Return(fakeSimplifiedExpression);

            var result = _visitor.Visit(expression);

            _stageMock.VerifyAllExpectations();
            _groupAggregateSimplifierMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(fakeSimplifiedExpression));
        }
Exemple #3
0
        public void VisitSubqueryExpressionTest_RevisitsResult()
        {
            var mainFromClause          = ExpressionHelper.CreateMainFromClause <Cook>();
            var querModel               = ExpressionHelper.CreateQueryModel(mainFromClause);
            var expression              = new SubQueryExpression(querModel);
            var fakeSqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement())
            {
                TopExpression = null
            };

            fakeSqlStatementBuilder.Orderings.Add(new Ordering(Expression.Constant("order"), OrderingDirection.Asc));
            fakeSqlStatementBuilder.SqlTables.Add(SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook)));
            var fakeSqlStatement = fakeSqlStatementBuilder.GetSqlStatement();

            fakeSqlStatementBuilder.Orderings.Clear();
            var expectedStatement = fakeSqlStatementBuilder.GetSqlStatement();

            _stageMock
            .Expect(mock => mock.PrepareSqlStatement(querModel, _context))
            .Return(fakeSqlStatement);

            var result = SqlPreparationExpressionVisitor.TranslateExpression(expression, _context, _stageMock, _methodCallTransformerProvider);

            Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression)));
            Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.EqualTo(expectedStatement));
        }
        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));
        }
Exemple #5
0
        public void VisitMemberExpression_WithInnerSqlSubStatementExpression()
        {
            var selectProjection = Expression.Constant(new Cook());
            var fakeStatement    = SqlStatementModelObjectMother.CreateSqlStatement(
                new NamedExpression("test", selectProjection), new SqlTable(new ResolvedSimpleTableInfo(typeof(Cook), "CookTable", "c"), JoinSemantics.Left));
            var memberInfo = typeof(Cook).GetProperty("Name");
            var queryModel = ExpressionHelper.CreateQueryModel <Cook>();

            queryModel.ResultOperators.Add(new FirstResultOperator(false));
            var expression       = new SubQueryExpression(queryModel);
            var memberExpression = Expression.MakeMemberAccess(expression, memberInfo);

            _stageMock
            .Expect(mock => mock.PrepareSqlStatement(queryModel, _context))
            .Return(fakeStatement);
            _stageMock.Replay();

            var result = SqlPreparationExpressionVisitor.TranslateExpression(memberExpression, _context, _stageMock, _methodCallTransformerProvider);

            _stageMock.VerifyAllExpectations();

            Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression)));
            var expectedSelectProjection = new NamedExpression("test", Expression.MakeMemberAccess(selectProjection, memberInfo));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedSelectProjection, ((SqlSubStatementExpression)result).SqlStatement.SelectProjection);
            Assert.That(((SqlSubStatementExpression)result).SqlStatement.DataInfo, Is.TypeOf(typeof(StreamedSequenceInfo)));
            Assert.That(((SqlSubStatementExpression)result).SqlStatement.DataInfo.DataType, Is.EqualTo(typeof(IQueryable <>).MakeGenericType(typeof(string))));
        }
        public void ResolveTableInfo_GroupReferenceTableInfo_NoGorupingSubStatement()
        {
            var subStatement = SqlStatementModelObjectMother.CreateSqlStatement(Expression.Constant(new int[0]));
            var groupSource  = SqlStatementModelObjectMother.CreateSqlTable(new ResolvedSubStatementTableInfo("q0", subStatement));
            var tableInfo    = new UnresolvedGroupReferenceTableInfo(groupSource);

            ResolvingTableInfoVisitor.ResolveTableInfo(tableInfo, _resolverMock, _generator, _stageMock, _mappingResolutionContext);
        }
        public void ResolvePotentialEntity_Substatement_WithoutEntity_ReturnsSameExpression()
        {
            var subStatement           = SqlStatementModelObjectMother.CreateSqlStatement(Expression.Constant(0));
            var subStatementExpression = new SqlSubStatementExpression(subStatement);

            var result = _entityIdentityResolver.ResolvePotentialEntity(subStatementExpression);

            Assert.That(result, Is.SameAs(subStatementExpression));
        }
Exemple #8
0
        private SqlStatementBuilder CreateSqlStatementBuilder(Type sequenceItemType)
        {
            var selectProjection = ExpressionHelper.CreateExpression(sequenceItemType);

            return(new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement())
            {
                DataInfo = new StreamedSequenceInfo(sequenceItemType.MakeArrayType(), selectProjection),
                SelectProjection = selectProjection
            });
        }
Exemple #9
0
        public void GenerateTextForSqlStatement()
        {
            var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement(
                _entityExpression, new[] { new SqlTable(new ResolvedSimpleTableInfo(typeof(int), "Table", "t"), JoinSemantics.Inner) });

            var stage = new DefaultSqlGenerationStage();

            stage.GenerateTextForSqlStatement(_commandBuilder, sqlStatement);

            Assert.That(_commandBuilder.GetCommandText(), Is.EqualTo("SELECT [t].[ID],[t].[Name],[t].[City] FROM [Table] AS [t]"));
        }
 public void SetUp()
 {
     _stageMock           = MockRepository.GenerateMock <ISqlPreparationStage> ();
     _generator           = new UniqueIdentifierGenerator();
     _handler             = new CastResultOperatorHandler();
     _sqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement())
     {
         DataInfo = new StreamedSequenceInfo(typeof(Cook[]), Expression.Constant(new Cook()))
     };
     _context = SqlStatementModelObjectMother.CreateSqlPreparationContext();
 }
 public void SetUp()
 {
     _generator           = new UniqueIdentifierGenerator();
     _stage               = new DefaultSqlPreparationStage(new CompoundMethodCallTransformerProvider(), new ResultOperatorHandlerRegistry(), _generator);
     _handler             = new SumResultOperatorHandler();
     _sqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement())
     {
         DataInfo = new StreamedSequenceInfo(typeof(int[]), Expression.Constant(5))
     };
     _context = SqlStatementModelObjectMother.CreateSqlPreparationContext();
 }
        public void ResolvePotentialEntity_Substatement_WithEntity_TrivialProjection_ResolvesToIdentityDirectly()
        {
            // No SQL tables => the substatement can be completely eliminated.
            var subStatement           = SqlStatementModelObjectMother.CreateSqlStatement(_entityExpression);
            var subStatementExpression = new SqlSubStatementExpression(subStatement);

            var result = _entityIdentityResolver.ResolvePotentialEntity(subStatementExpression);

            var expectedSelectProjection = _entityExpression.GetIdentityExpression();

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedSelectProjection, result);
        }
Exemple #13
0
        public override void SetUp()
        {
            base.SetUp();

            _stage               = CreateDefaultSqlPreparationStage();
            _handler             = new TakeResultOperatorHandler();
            _sqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement())
            {
                DataInfo = new StreamedSequenceInfo(typeof(Cook[]), Expression.Constant(new Cook()))
            };
            _context = SqlStatementModelObjectMother.CreateSqlPreparationContext();
        }
        public override void SetUp()
        {
            base.SetUp();

            _stage               = CreateDefaultSqlPreparationStage();
            _handler             = new TestableAggregationResultOperatorHandler();
            _sqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement())
            {
                DataInfo         = new StreamedSequenceInfo(typeof(int[]), Expression.Constant(5)),
                SelectProjection = new NamedExpression(null, Expression.Constant(0))
            };
            _context = SqlStatementModelObjectMother.CreateSqlPreparationContext();
        }
        public void ResolvePotentialEntity_Substatement_StripsConversions()
        {
            var subStatement           = SqlStatementModelObjectMother.CreateSqlStatement(Expression.Convert(_entityExpression, typeof(object)), SqlStatementModelObjectMother.CreateSqlTable());
            var subStatementExpression = new SqlSubStatementExpression(subStatement);

            var result = _entityIdentityResolver.ResolvePotentialEntity(Expression.Convert(subStatementExpression, typeof(object)));

            Assert.That(result, Is.TypeOf <SqlSubStatementExpression> ());
            var expectedSelectProjection = _entityExpression.GetIdentityExpression();

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedSelectProjection, ((SqlSubStatementExpression)result).SqlStatement.SelectProjection);
            Assert.That(((SqlSubStatementExpression)result).SqlStatement.DataInfo.DataType, Is.SameAs(typeof(IQueryable <int>)));
        }
        public void ResolveSubStatementReferenceExpression_CopiesExpression_ForUnknownExpression()
        {
            var referencedExpression = Expression.Constant(0);
            var sqlStatement         = SqlStatementModelObjectMother.CreateSqlStatement(referencedExpression);

            var tableInfo = new ResolvedSubStatementTableInfo("q0", sqlStatement);
            var sqlTable  = new SqlTable(tableInfo, JoinSemantics.Inner);

            var result = SubStatementReferenceResolver.ResolveSubStatementReferenceExpression(
                referencedExpression, tableInfo, sqlTable, _context);

            Assert.That(result, Is.SameAs(referencedExpression));
        }
Exemple #17
0
        public void BuildSelectPart_WithSelect()
        {
            var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement();

            _stageMock.Expect(
                mock => mock.GenerateTextForSelectExpression(_commandBuilder, sqlStatement.SelectProjection))
            .WhenCalled(mi => ((SqlCommandBuilder)mi.Arguments[0]).Append("[t].[ID],[t].[Name],[t].[City]"));
            _stageMock.Replay();

            _generator.BuildSelectPart(sqlStatement, _commandBuilder, false);

            Assert.That(_commandBuilder.GetCommandText(), Is.EqualTo("SELECT [t].[ID],[t].[Name],[t].[City]"));
            _stageMock.VerifyAllExpectations();
        }
        public void ResolveSubStatementReferenceExpression_CreatesColumnExpression_ForNamedExpression()
        {
            var referencedExpression = new NamedExpression("test", Expression.Constant(0));
            var sqlStatement         = SqlStatementModelObjectMother.CreateSqlStatement(referencedExpression);

            var tableInfo = new ResolvedSubStatementTableInfo("q0", sqlStatement);
            var sqlTable  = new SqlTable(tableInfo, JoinSemantics.Inner);

            var result = SubStatementReferenceResolver.ResolveSubStatementReferenceExpression(
                referencedExpression, tableInfo, sqlTable, _context);

            var expectedResult = new SqlColumnDefinitionExpression(typeof(int), "q0", "test", false);

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result);
        }
Exemple #19
0
        public void BuildSelectPart_OuterSelect_NoSetOperationsAggregation()
        {
            var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement();

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

            _generator.BuildSelectPart(sqlStatement, _commandBuilder, true);

            _stageMock.VerifyAllExpectations();
        }
        public void ResolveSubStatementReferenceExpression_RegistersEntityTable_WithinUnknownExpression()
        {
            var referencedExpression = Expression.Convert(SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression(typeof(Cook)), typeof(Chef));
            var sqlStatement         = SqlStatementModelObjectMother.CreateSqlStatement(referencedExpression);

            var tableInfo = new ResolvedSubStatementTableInfo("q0", sqlStatement);
            var sqlTable  = new SqlTable(tableInfo, JoinSemantics.Inner);

            var result = SubStatementReferenceResolver.ResolveSubStatementReferenceExpression(
                referencedExpression, tableInfo, sqlTable, _context);

            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.Convert));
            var entityReference = (SqlEntityExpression)((UnaryExpression)result).Operand;

            Assert.That(_context.GetSqlTableForEntityExpression(entityReference), Is.SameAs(sqlTable));
        }
Exemple #21
0
        public void BuildSetOperationCombinedStatementsPart_UnionAll()
        {
            var setOperationCombinedStatement = new SetOperationCombinedStatement(SqlStatementModelObjectMother.CreateSqlStatement(), SetOperation.UnionAll);
            var sqlStatement = SqlStatementModelObjectMother.CreateMinimalSqlStatement(
                new SqlStatementBuilder
            {
                SetOperationCombinedStatements = { setOperationCombinedStatement }
            });

            _stageMock.Expect(mock => mock.GenerateTextForSqlStatement(_commandBuilder, setOperationCombinedStatement.SqlStatement))
            .WhenCalled(mi => ((SqlCommandBuilder)mi.Arguments[0]).Append("statement"));
            _stageMock.Replay();

            _generator.BuildSetOperationCombinedStatementsPart(sqlStatement, _commandBuilder);

            Assert.That(_commandBuilder.GetCommandText(), Is.EqualTo(" UNION ALL (statement)"));
            _stageMock.VerifyAllExpectations();
        }
        public void VisitSqlSubStatementExpression_NoChanges()
        {
            var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement();
            var expression   = new SqlSubStatementExpression(sqlStatement);

            _stageMock
            .Expect(mock => mock.ResolveSqlStatement(sqlStatement, _mappingResolutionContext))
            .Return(sqlStatement);
            _groupAggregateSimplifierMock
            .Expect(mock => mock.SimplifyIfPossible(expression, expression.SqlStatement.SelectProjection))
            .Return(expression);

            var result = _visitor.Visit(expression);

            _stageMock.VerifyAllExpectations();
            _groupAggregateSimplifierMock.VerifyAllExpectations();
            Assert.That(result, Is.SameAs(expression));
        }
        public void ResolveSubStatementReferenceExpression_CreatesNewExpressionWithReferences_ForNewExpressions_WithoutMembers()
        {
            var newExpression = Expression.New(
                TypeForNewExpression.GetConstructor(typeof(int)),
                new[] { new NamedExpression("const", Expression.Constant(0)) });

            var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement(newExpression);

            var tableInfo = new ResolvedSubStatementTableInfo("q0", sqlStatement);
            var sqlTable  = new SqlTable(tableInfo, JoinSemantics.Inner);

            var result = SubStatementReferenceResolver.ResolveSubStatementReferenceExpression(newExpression, tableInfo, sqlTable, _context);

            var expectedResult = Expression.New(
                TypeForNewExpression.GetConstructor(typeof(int)),
                new Expression[] { new NamedExpression("m0", new SqlColumnDefinitionExpression(typeof(int), "q0", "const", false)) });

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result);
        }
Exemple #24
0
        public void VisitSubqueryExpressionTest_WithSqlTables()
        {
            var mainFromClause          = ExpressionHelper.CreateMainFromClause <Cook>();
            var querModel               = ExpressionHelper.CreateQueryModel(mainFromClause);
            var expression              = new SubQueryExpression(querModel);
            var fakeSqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement());

            fakeSqlStatementBuilder.SqlTables.Add(_sqlTable);
            var fakeSqlStatement = fakeSqlStatementBuilder.GetSqlStatement();

            _stageMock
            .Expect(mock => mock.PrepareSqlStatement(querModel, _context))
            .Return(fakeSqlStatement);

            var result = SqlPreparationExpressionVisitor.TranslateExpression(expression, _context, _stageMock, _methodCallTransformerProvider);

            Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression)));
            Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.SameAs(fakeSqlStatement));
        }
Exemple #25
0
        public void SetUp()
        {
            _dataInfo = new StreamedScalarValueInfo(typeof(int));

            _resolvedElementExpressionReference = new SqlColumnDefinitionExpression(typeof(string), "q0", "element", false);
            _resolvedSelectProjection           = new NamedExpression(
                null,
                new AggregationExpression(typeof(int), _resolvedElementExpressionReference, AggregationModifier.Min));

            _associatedGroupingSelectExpression = new SqlGroupingSelectExpression(
                new NamedExpression("key", Expression.Constant("k")),
                new NamedExpression("element", Expression.Constant("e")));

            _resolvedJoinedGroupingSubStatement = SqlStatementModelObjectMother.CreateSqlStatement(_associatedGroupingSelectExpression);
            _resolvedJoinedGroupingTable        = new SqlTable(
                new ResolvedJoinedGroupingTableInfo(
                    "q1",
                    _resolvedJoinedGroupingSubStatement,
                    _associatedGroupingSelectExpression,
                    "q0"), JoinSemantics.Inner);

            _simplifiableResolvedSqlStatement = new SqlStatement(
                _dataInfo,
                _resolvedSelectProjection,
                new[] { _resolvedJoinedGroupingTable },
                null,
                null,
                new Ordering[0],
                null,
                false,
                Expression.Constant(0),
                Expression.Constant(0));
            _simplifiableUnresolvedProjection = new AggregationExpression(
                typeof(int),
                new SqlTableReferenceExpression(_resolvedJoinedGroupingTable),
                AggregationModifier.Count);

            _stageMock = MockRepository.GenerateStrictMock <IMappingResolutionStage> ();
            _context   = new MappingResolutionContext();

            _groupAggregateSimplifier = new GroupAggregateSimplifier(_stageMock, _context);
        }
Exemple #26
0
        public void VisitSqlSubStatementExpression_LeavesSqlSubStatementExpression_ForStreamedSequenceInfo()
        {
            var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement(Expression.Constant(new Cook()));
            var expression   = new SqlSubStatementExpression(sqlStatement);

            _stageMock
            .Expect(mock => mock.ResolveSqlStatement(sqlStatement, _mappingResolutionContext))
            .Return(sqlStatement);
            _groupAggregateSimplifier
            .Expect(mock => mock.SimplifyIfPossible(expression, sqlStatement.SelectProjection))
            .Return(expression);

            var visitor = CreateVisitor(new SqlStatementBuilder(sqlStatement));
            var result  = visitor.VisitExpression(expression);

            _stageMock.VerifyAllExpectations();
            _groupAggregateSimplifier.VerifyAllExpectations();
            Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression)));
            Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.SameAs(sqlStatement));
        }
        public void ResolveSqlStatement()
        {
            var sqlTable = SqlStatementModelObjectMother.CreateSqlTable_WithUnresolvedTableInfo(typeof(Cook));
            var tableReferenceExpression = new SqlTableReferenceExpression(new SqlTable(_fakeResolvedSimpleTableInfo, JoinSemantics.Inner));
            var sqlStatement             = SqlStatementModelObjectMother.CreateSqlStatement(tableReferenceExpression, new[] { sqlTable });
            var fakeEntityExpression     = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression(typeof(Cook));

            _resolverMock
            .Expect(mock => mock.ResolveTableInfo((UnresolvedTableInfo)((SqlTable)sqlStatement.SqlTables[0]).TableInfo, _uniqueIdentifierGenerator))
            .Return(_fakeResolvedSimpleTableInfo);
            _resolverMock
            .Expect(mock => mock.ResolveSimpleTableInfo(tableReferenceExpression.SqlTable.GetResolvedTableInfo(), _uniqueIdentifierGenerator))
            .Return(fakeEntityExpression);
            _resolverMock.Replay();

            var newSqlStatment = _stage.ResolveSqlStatement(sqlStatement, _mappingResolutionContext);

            _resolverMock.VerifyAllExpectations();
            Assert.That(((SqlTable)newSqlStatment.SqlTables[0]).TableInfo, Is.SameAs(_fakeResolvedSimpleTableInfo));
            Assert.That(newSqlStatment.SelectProjection, Is.SameAs(fakeEntityExpression));
        }
Exemple #28
0
        public void GenerateTextForOuterSqlStatement()
        {
            var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement(
                _entityExpression, new[] { new SqlTable(new ResolvedSimpleTableInfo(typeof(int), "Table", "t"), JoinSemantics.Inner) });

            var stage = new DefaultSqlGenerationStage();

            stage.GenerateTextForOuterSqlStatement(_commandBuilder, sqlStatement);

            Assert.That(_commandBuilder.GetCommandText(), Is.EqualTo("SELECT [t].[ID],[t].[Name],[t].[City] FROM [Table] AS [t]"));

            var inMemoryProjection = _commandBuilder.GetInMemoryProjectionBody();

            Assert.That(inMemoryProjection, Is.AssignableTo(typeof(MethodCallExpression)));

            var methodCallExpression = (MethodCallExpression)inMemoryProjection;

            Assert.That(methodCallExpression.Method, Is.EqualTo((typeof(IDatabaseResultRow).GetMethod("GetEntity").MakeGenericMethod(sqlStatement.SelectProjection.Type))));
            Assert.That(methodCallExpression.Arguments.Count, Is.EqualTo(1));
            Assert.That(((ColumnID[])((ConstantExpression)methodCallExpression.Arguments[0]).Value)[0].ColumnName, Is.EqualTo("ID"));
            Assert.That(((ColumnID[])((ConstantExpression)methodCallExpression.Arguments[0]).Value)[1].ColumnName, Is.EqualTo("Name"));
            Assert.That(((ColumnID[])((ConstantExpression)methodCallExpression.Arguments[0]).Value)[2].ColumnName, Is.EqualTo("City"));
        }
Exemple #29
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));
        }
        private static UnionResultOperator CreateValidResultOperator()
        {
            var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement(Expression.Constant(0));

            return(new UnionResultOperator("x", typeof(int), new SqlSubStatementExpression(sqlStatement)));
        }