Example #1
0
        public void IsSimplifiableGroupAggregate_False_TooManySqlTables()
        {
            var sqlStatementBuilder = new SqlStatementBuilder(_simplifiableResolvedSqlStatement);

            sqlStatementBuilder.SqlTables.Add(SqlStatementModelObjectMother.CreateSqlTable());
            var sqlStatement = sqlStatementBuilder.GetSqlStatement();

            Assert.That(_groupAggregateSimplifier.IsSimplifiableGroupAggregate(sqlStatement), Is.False);
        }
        public void IsSimplifiableGroupAggregate_False_NoResolvedJoinedGroupingTableInfo()
        {
            var sqlStatementBuilder = new SqlStatementBuilder(_simplifiableResolvedSqlStatement);

            sqlStatementBuilder.SqlTables.Clear();
            sqlStatementBuilder.SqlTables.Add(SqlStatementModelObjectMother.CreateSqlTable(new ResolvedSimpleTableInfo(typeof(int), "table", "t0")));
            var sqlStatement = sqlStatementBuilder.GetSqlStatement();

            Assert.That(_groupAggregateSimplifier.IsSimplifiableGroupAggregate(sqlStatement), Is.False);
        }
        public void Visit_ReferenceToOtherTable()
        {
            var visitor = new GroupAggregateSimplifier.SimplifyingVisitor(_resolvedJoinedGroupingTable, _associatedGroupingSelectExpression.ElementExpression);

            var input = new SqlTableReferenceExpression(SqlStatementModelObjectMother.CreateSqlTable());

            visitor.Visit(input);

            Assert.That(visitor.CanBeTransferredToGroupingSource, Is.False);
        }
Example #4
0
        public void SetUp()
        {
            _stageMock = MockRepository.GenerateStrictMock <IMappingResolutionStage>();
            _mappingResolutionContext = new MappingResolutionContext();

            _visitor = new TestableSqlStatementResolver(_stageMock, _mappingResolutionContext);

            _unresolvedTableInfo         = SqlStatementModelObjectMother.CreateUnresolvedTableInfo(typeof(Cook));
            _sqlTable                    = SqlStatementModelObjectMother.CreateSqlTable(_unresolvedTableInfo);
            _fakeResolvedSimpleTableInfo = SqlStatementModelObjectMother.CreateResolvedTableInfo(typeof(Cook));
        }
        public void AddPreparedFromExpression_NoOrderings()
        {
            var sqlTable = SqlStatementModelObjectMother.CreateSqlTable();
            var preparedFromExpressionInfo = new FromExpressionInfo(
                sqlTable, new Ordering[0], new SqlTableReferenceExpression(sqlTable), null);

            _visitor.SqlStatementBuilder.Orderings.Clear();

            _visitor.AddPreparedFromExpression(preparedFromExpressionInfo);

            Assert.That(_visitor.SqlStatementBuilder.Orderings.Count, Is.EqualTo(0));
        }
        public void AddPreparedFromExpression_WhereConditionIsSet()
        {
            var sqlTable                   = SqlStatementModelObjectMother.CreateSqlTable();
            var whereCondition             = Expression.Constant(true);
            var preparedFromExpressionInfo = new FromExpressionInfo(
                sqlTable, new Ordering[0], new SqlTableReferenceExpression(sqlTable), whereCondition);

            _visitor.SqlStatementBuilder.WhereCondition = null;

            _visitor.AddPreparedFromExpression(preparedFromExpressionInfo);

            Assert.That(_visitor.SqlStatementBuilder.WhereCondition, Is.SameAs(whereCondition));
        }
Example #7
0
        public void UpdateEntityAndAddMapping()
        {
            var entity   = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression(typeof(Cook));
            var sqlTable = SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook));

            _context.AddSqlEntityMapping(entity, sqlTable);

            var result = (SqlEntityDefinitionExpression)_context.UpdateEntityAndAddMapping(entity, entity.Type, "newAlias", "newName");

            Assert.That(result.TableAlias, Is.EqualTo("newAlias"));
            Assert.That(result.Name, Is.EqualTo("newName"));
            Assert.That(_context.GetSqlTableForEntityExpression(result), Is.SameAs(sqlTable));
        }
        public void SetUp()
        {
            _resolverMock = MockRepository.GenerateMock <IMappingResolver>();
            _uniqueIdentifierGenerator = new UniqueIdentifierGenerator();

            _unresolvedTableInfo         = SqlStatementModelObjectMother.CreateUnresolvedTableInfo(typeof(Cook));
            _sqlTable                    = SqlStatementModelObjectMother.CreateSqlTable(_unresolvedTableInfo);
            _fakeResolvedSimpleTableInfo = SqlStatementModelObjectMother.CreateResolvedTableInfo(typeof(Cook));

            _stage = new DefaultMappingResolutionStage(_resolverMock, _uniqueIdentifierGenerator);

            _mappingResolutionContext = new MappingResolutionContext();
        }
Example #9
0
        public void RemoveNamesAndUpdateMapping_NameAroundEntity()
        {
            var entity   = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression(name: "X");
            var sqlTable = SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook));

            _context.AddSqlEntityMapping(entity, sqlTable);

            var result = _context.RemoveNamesAndUpdateMapping(new NamedExpression("X", entity));

            Assert.That(result, Is.TypeOf(entity.GetType()));
            Assert.That(((SqlEntityExpression)result).Name, Is.Null);
            Assert.That(_context.GetSqlTableForEntityExpression((SqlEntityExpression)result), Is.SameAs(sqlTable));
        }
        public void AddPreparedFromExpression_OrderingIsAdded()
        {
            var sqlTable = SqlStatementModelObjectMother.CreateSqlTable();
            var ordering = new Ordering(Expression.Constant("order"), OrderingDirection.Asc);
            var preparedFromExpressionInfo = new FromExpressionInfo(
                sqlTable, new[] { ordering }, new SqlTableReferenceExpression(sqlTable), null);

            _visitor.SqlStatementBuilder.Orderings.Clear();

            _visitor.AddPreparedFromExpression(preparedFromExpressionInfo);

            Assert.That(_visitor.SqlStatementBuilder.Orderings.Count, Is.EqualTo(1));
            Assert.That(_visitor.SqlStatementBuilder.Orderings[0], Is.SameAs(ordering));
        }
        public void AddPreparedFromExpression_NoWhereCondition()
        {
            var sqlTable = SqlStatementModelObjectMother.CreateSqlTable();
            var preparedFromExpressionInfo = new FromExpressionInfo(
                sqlTable, new Ordering[0], new SqlTableReferenceExpression(sqlTable), null);

            var originalWhereCondition = Expression.Constant(false);

            _visitor.SqlStatementBuilder.WhereCondition = originalWhereCondition;

            _visitor.AddPreparedFromExpression(preparedFromExpressionInfo);

            Assert.That(_visitor.SqlStatementBuilder.WhereCondition, Is.SameAs(originalWhereCondition));
        }
        public void VisitQuerySourceReferenceExpression_WithOrderings_AndDoNotExtractOrdingsPolicy()
        {
            var sqlTable = SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook));
            var sqlTableReferenceExpression = new SqlTableReferenceExpression(sqlTable);
            var selectProjection            = new NamedExpression("test", Expression.MakeMemberAccess(sqlTableReferenceExpression, typeof(Cook).GetProperty("Name")));
            var orderingExpression          = Expression.MakeMemberAccess(sqlTableReferenceExpression, typeof(Cook).GetProperty("ID"));
            var sqlStatement = new SqlStatementBuilder
            {
                DataInfo         = new StreamedSequenceInfo(typeof(DateTime[]), Expression.Constant(new DateTime(2000, 1, 1))),
                SelectProjection = selectProjection,
                SqlTables        = { sqlTable },
                Orderings        =
                { new Ordering(orderingExpression, OrderingDirection.Asc) }
            }.GetSqlStatement();
            var fakeWhereExpression = Expression.Constant(true);

            var innerSequenceExpression = new SqlSubStatementExpression(sqlStatement);

            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);

            _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(OrderingExtractionPolicy.DoNotExtractOrderings);

            visitor.VisitQuerySourceReference(querySourceReferenceExpression);

            _stageMock.VerifyAllExpectations();

            Assert.That(visitor.FromExpressionInfo != null); // inline condition because of ReSharper
            var fromExpressionInfo = (FromExpressionInfo)visitor.FromExpressionInfo;

            Assert.That(fromExpressionInfo.ExtractedOrderings, Is.Empty);
        }
Example #13
0
        public void ProcessNames_SqlEntityExpression()
        {
            var entityExpression = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression(typeof(Cook), "test2");
            var namedExpression  = new NamedExpression("test", entityExpression);

            var tableRegisteredForEntity = SqlStatementModelObjectMother.CreateSqlTable();

            _context.AddSqlEntityMapping(entityExpression, tableRegisteredForEntity);

            var result = _namedExpressionCombiner.ProcessNames(namedExpression);

            Assert.That(result, Is.Not.SameAs(namedExpression));
            Assert.That(result, Is.TypeOf(typeof(SqlEntityDefinitionExpression)));
            Assert.That(((SqlEntityDefinitionExpression)result).Name, Is.EqualTo("test_test2"));
            Assert.That(_context.GetSqlTableForEntityExpression((SqlEntityExpression)result), Is.SameAs(tableRegisteredForEntity));
        }
        public void HandleResultOperator_OrderingsWithoutTopInMainSqlStatement_ShouldBeRemoved()
        {
            _sqlStatementBuilder.Orderings.Add(SqlStatementModelObjectMother.CreateOrdering());
            var originalSqlTable = SqlStatementModelObjectMother.CreateSqlTable();

            _sqlStatementBuilder.SqlTables.Add(originalSqlTable);

            var stage = CreateDefaultSqlPreparationStage();

            var resultOperator = CreateValidResultOperator();

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

            Assert.That(_sqlStatementBuilder.Orderings, Is.Empty);
            Assert.That(_sqlStatementBuilder.SqlTables, Is.EqualTo(new[] { originalSqlTable }), "Query was not moved to a substatement.");
        }
        public void VisitQuerySourceReferenceExpression_WithNonNullWhereCondition()
        {
            var memberInfo = typeof(Cook).GetProperty("Assistants");
            var sqlTableReferenceExpression = new SqlTableReferenceExpression(SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook)));
            var innerSequenceExpression     =
                Expression.MakeMemberAccess(
                    sqlTableReferenceExpression, memberInfo);
            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);

            visitor.VisitQuerySourceReference(querySourceReferenceExpression);

            _stageMock.VerifyAllExpectations();

            Assert.That(visitor.FromExpressionInfo != null); // inline condition because of ReSharper
            var fromExpressionInfo = (FromExpressionInfo)visitor.FromExpressionInfo;

            Assert.That(fromExpressionInfo.WhereCondition, Is.AssignableTo(typeof(BinaryExpression)));
            Assert.That(fromExpressionInfo.WhereCondition.NodeType, Is.EqualTo(ExpressionType.AndAlso));
            Assert.That(((BinaryExpression)fromExpressionInfo.WhereCondition).Left, Is.TypeOf(typeof(JoinConditionExpression)));
            Assert.That(((JoinConditionExpression)((BinaryExpression)fromExpressionInfo.WhereCondition).Left).JoinedTable.JoinInfo, Is.TypeOf(typeof(UnresolvedCollectionJoinInfo)));
            Assert.That(
                ((UnresolvedCollectionJoinInfo)((JoinConditionExpression)((BinaryExpression)fromExpressionInfo.WhereCondition).Left).JoinedTable.JoinInfo).SourceExpression, Is.SameAs(sqlTableReferenceExpression));
            Assert.That(
                ((UnresolvedCollectionJoinInfo)((JoinConditionExpression)((BinaryExpression)fromExpressionInfo.WhereCondition).Left).JoinedTable.JoinInfo).MemberInfo, Is.SameAs(memberInfo));
            Assert.That(((JoinConditionExpression)((BinaryExpression)fromExpressionInfo.WhereCondition).Left).JoinedTable.JoinSemantics, Is.EqualTo(JoinSemantics.Inner));
            Assert.That(((BinaryExpression)fromExpressionInfo.WhereCondition).Right, Is.SameAs(fakeWhereExpression));
        }
        public void ResolveSubStatementReferenceExpression_CreatesSqlGroupingSelectExpressionWithNamedExpressions()
        {
            var expression = new SqlGroupingSelectExpression(
                Expression.Constant("key"), Expression.Constant("element"), new[] { Expression.Constant("aggregation") });
            var tableInfo = new ResolvedSubStatementTableInfo("q0", SqlStatementModelObjectMother.CreateSqlStatement_Resolved(typeof(Cook)));
            var sqlTable  = SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook));

            var exprectedResult = new SqlGroupingSelectExpression(
                new NamedExpression("key", Expression.Constant("key")),
                new NamedExpression("element", Expression.Constant("element")),
                new[] { new NamedExpression("a0", Expression.Constant("aggregation")) });

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

            SqlExpressionTreeComparer.CheckAreEqualTrees(result, exprectedResult);
            Assert.That(_context.GetReferencedGroupSource(((SqlGroupingSelectExpression)result)), Is.SameAs(sqlTable));
        }
        public void AddPreparedFromExpression_WhereConditionIsAdded()
        {
            var sqlTable                   = SqlStatementModelObjectMother.CreateSqlTable();
            var whereCondition             = Expression.Constant(true);
            var preparedFromExpressionInfo = new FromExpressionInfo(
                sqlTable, new Ordering[0], new SqlTableReferenceExpression(sqlTable), whereCondition);

            var originalWhereCondition = Expression.Constant(false);

            _visitor.SqlStatementBuilder.WhereCondition = originalWhereCondition;

            _visitor.AddPreparedFromExpression(preparedFromExpressionInfo);

            var expectedCombinedWhereCondition = Expression.AndAlso(originalWhereCondition, whereCondition);

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedCombinedWhereCondition, _visitor.SqlStatementBuilder.WhereCondition);
        }
        public void SimplifyIfPossible_WithNonSimplifiableProjection_ReturnsOriginalStatement()
        {
            Assert.That(_associatedGroupingSelectExpression.AggregationExpressions.Count, Is.EqualTo(0));
            var expression = new SqlSubStatementExpression(_simplifiableResolvedSqlStatement);

            _stageMock.Replay();

            var nonSimplifiableProjection = new AggregationExpression(
                typeof(int),
                new SqlTableReferenceExpression(SqlStatementModelObjectMother.CreateSqlTable()), AggregationModifier.Count);
            var result = _groupAggregateSimplifier.SimplifyIfPossible(expression, nonSimplifiableProjection);

            _stageMock.VerifyAllExpectations();

            var expected = new SqlSubStatementExpression(_simplifiableResolvedSqlStatement);

            SqlExpressionTreeComparer.CheckAreEqualTrees(expected, result);

            Assert.That(_associatedGroupingSelectExpression.AggregationExpressions.Count, Is.EqualTo(0));
        }
        public void VisitSelectClause_CreatesSelectProjection()
        {
            var preparedExpression = new SqlTableReferenceExpression(SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook)));

            _stageMock
            .Expect(
                mock =>
                mock.PrepareSelectExpression(
                    Arg <Expression> .Matches(e => e == _selectClause.Selector),
                    Arg <ISqlPreparationContext> .Matches(c => c != _context)))
            .Return(preparedExpression);
            _stageMock.Replay();

            _visitor.VisitSelectClause(_selectClause, _queryModel);

            _stageMock.VerifyAllExpectations();
            Assert.That(_visitor.SqlStatementBuilder.SelectProjection, Is.SameAs(preparedExpression));
            Assert.That(_visitor.SqlStatementBuilder.DataInfo, Is.TypeOf(typeof(StreamedSequenceInfo)));
            Assert.That(((StreamedSequenceInfo)_visitor.SqlStatementBuilder.DataInfo).DataType, Is.EqualTo(_selectClause.GetOutputDataInfo().DataType));
        }
Example #20
0
        public void ProcessNames_SqlGroupingSelectExpression()
        {
            var keyExpression            = new NamedExpression("key", Expression.Constant("key"));
            var elementExpression        = new NamedExpression("element", Expression.Constant("element"));
            var aggregationExpression    = new NamedExpression("a0", Expression.Constant("aggregation"));
            var groupingSelectExpression = new SqlGroupingSelectExpression(keyExpression, elementExpression, new[] { aggregationExpression });
            var expression = new NamedExpression("outer", groupingSelectExpression);
            var sqlTable   = SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook));

            _context.AddGroupReferenceMapping(groupingSelectExpression, sqlTable);

            var expectedResult = new SqlGroupingSelectExpression(
                new NamedExpression("outer_key", Expression.Constant("key")),
                new NamedExpression("outer_element", Expression.Constant("element")),
                new[] { new NamedExpression("outer_a0", Expression.Constant("aggregation")) });

            var result = _namedExpressionCombiner.ProcessNames(expression);

            SqlExpressionTreeComparer.CheckAreEqualTrees(result, expectedResult);
            Assert.That(_context.GetReferencedGroupSource(((SqlGroupingSelectExpression)result)), Is.SameAs(sqlTable));
        }
Example #21
0
        public void VisitAdditionalFromClause_AddsSqlTable_AndContextMapping()
        {
            var fakeSqlTableForMainFromClause = SqlStatementModelObjectMother.CreateSqlTable();

            _visitor.SqlStatementBuilder.SqlTables.Add(fakeSqlTableForMainFromClause);

            var constantExpression   = Expression.Constant(0);
            var additionalFromClause = new AdditionalFromClause("additional", typeof(int), constantExpression);

            _queryModel.BodyClauses.Add(additionalFromClause);

            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 == constantExpression),
                    Arg <ISqlPreparationContext> .Matches(c => c != _context),
                    Arg <Func <ITableInfo, SqlTable> > .Is.Anything,
                    Arg.Is(OrderingExtractionPolicy.ExtractOrderingsIntoProjection)))
            .Return(preparedFromExpressionInfo);

            _stageMock.Replay();

            _visitor.VisitAdditionalFromClause(additionalFromClause, _queryModel, 0);

            _stageMock.VerifyAllExpectations();

            Assert.That(_visitor.SqlStatementBuilder.SqlTables, Is.EqualTo(new[] { fakeSqlTableForMainFromClause, preparedSqlTable }));
            var contextMapping = _visitor.Context.GetExpressionMapping(new QuerySourceReferenceExpression(additionalFromClause));

            Assert.That(contextMapping, Is.Not.Null);
            Assert.That(((SqlTableReferenceExpression)contextMapping).SqlTable, Is.SameAs(preparedSqlTable));
        }
        public void ResolveEntityRefMemberExpression()
        {
            var kitchenCookMember         = typeof(Kitchen).GetProperty("Cook");
            var entityExpression          = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression(typeof(Kitchen));
            var entityRefMemberExpression = new SqlEntityRefMemberExpression(entityExpression, kitchenCookMember);
            var unresolvedJoinInfo        = SqlStatementModelObjectMother.CreateUnresolvedJoinInfo_KitchenCook();
            var fakeJoinInfo = SqlStatementModelObjectMother.CreateResolvedJoinInfo(typeof(Cook));

            _mappingResolutionContext.AddSqlEntityMapping(entityExpression, SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook)));

            var fakeEntityExpression = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression(typeof(Cook));

            _resolverMock
            .Expect(
                mock =>
                mock.ResolveJoinInfo(
                    Arg <UnresolvedJoinInfo> .Matches(joinInfo => joinInfo.MemberInfo == kitchenCookMember), Arg <UniqueIdentifierGenerator> .Is.Anything))
            .Return(fakeJoinInfo);

            _resolverMock
            .Expect(
                mock =>
                mock.ResolveSimpleTableInfo(
                    Arg <IResolvedTableInfo> .Is.Anything,
                    Arg <UniqueIdentifierGenerator> .Is.Anything))
            .Return(fakeEntityExpression);

            var result = _stage.ResolveEntityRefMemberExpression(entityRefMemberExpression, unresolvedJoinInfo, _mappingResolutionContext);

            _resolverMock.VerifyAllExpectations();

            Assert.That(result, Is.SameAs(fakeEntityExpression));
            var sqlTable = _mappingResolutionContext.GetSqlTableForEntityExpression(entityRefMemberExpression.OriginatingEntity);

            Assert.That(sqlTable.GetJoin(kitchenCookMember), Is.Not.Null);
            Assert.That(sqlTable.GetJoin(kitchenCookMember).JoinInfo, Is.SameAs(fakeJoinInfo));
        }
        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));
        }
        public void ResolveTableInfo_GroupReferenceTableInfo()
        {
            var sqlTable       = SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook));
            var groupingSelect = new SqlGroupingSelectExpression(
                Expression.MakeMemberAccess(new SqlTableReferenceExpression(sqlTable), typeof(Cook).GetProperty("Name")),
                Expression.MakeMemberAccess(new SqlTableReferenceExpression(sqlTable), typeof(Cook).GetProperty("ID")));
            var dataInfo = new StreamedSequenceInfo(
                typeof(IEnumerable <>).MakeGenericType(groupingSelect.Type),
                Expression.Constant(null, groupingSelect.Type));
            var whereCondition       = Expression.Constant(false);
            var groupByExpression    = groupingSelect.KeyExpression;
            var groupingSubStatement = new SqlStatementBuilder
            {
                DataInfo          = dataInfo,
                SelectProjection  = groupingSelect,
                SqlTables         = { sqlTable },
                WhereCondition    = whereCondition,
                GroupByExpression = groupByExpression
            }.GetSqlStatement();
            var groupSource = SqlStatementModelObjectMother.CreateSqlTable(new ResolvedSubStatementTableInfo("q0", groupingSubStatement));
            var tableInfo   = new UnresolvedGroupReferenceTableInfo(groupSource);

            var expectedKeyViaElement     = Expression.MakeMemberAccess(new SqlTableReferenceExpression(sqlTable), typeof(Cook).GetProperty("Name"));
            var expectedKeyViaGroupSource = Expression.MakeMemberAccess(new SqlTableReferenceExpression(groupSource), groupSource.ItemType.GetProperty("Key"));

            var expectedResultWhereCondition =
                Expression.OrElse(
                    Expression.AndAlso(new SqlIsNullExpression(expectedKeyViaElement), new SqlIsNullExpression(expectedKeyViaGroupSource)),
                    Expression.AndAlso(
                        Expression.AndAlso(new SqlIsNotNullExpression(expectedKeyViaElement), new SqlIsNotNullExpression(expectedKeyViaGroupSource)),
                        Expression.Equal(expectedKeyViaElement, expectedKeyViaGroupSource)));

            var fakeWhereCondition = Expression.Constant(false);

            _stageMock
            .Expect(mock => mock.ResolveWhereExpression(Arg <Expression> .Is.Anything, Arg.Is(_mappingResolutionContext)))
            .WhenCalled(mi => SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResultWhereCondition, (Expression)mi.Arguments[0]))
            .Return(fakeWhereCondition);
            _stageMock.Replay();

            var result = ResolvingTableInfoVisitor.ResolveTableInfo(tableInfo, _resolverMock, _generator, _stageMock, _mappingResolutionContext);

            _stageMock.VerifyAllExpectations();

            Assert.That(result, Is.TypeOf(typeof(ResolvedJoinedGroupingTableInfo)));

            var castResult = ((ResolvedJoinedGroupingTableInfo)result);

            var resultGroupingSelector = castResult.AssociatedGroupingSelectExpression;

            Assert.That(resultGroupingSelector, Is.SameAs(groupingSelect));

            Assert.That(castResult.GroupSourceTableAlias, Is.EqualTo("q0"));

            var resultSqlStatement = castResult.SqlStatement;

            Assert.That(resultSqlStatement.SqlTables, Is.EqualTo(groupingSubStatement.SqlTables));
            Assert.That(resultSqlStatement.Orderings, Is.Empty);
            Assert.That(resultSqlStatement.GroupByExpression, Is.Null);

            SqlExpressionTreeComparer.CheckAreEqualTrees(
                Expression.AndAlso(groupingSubStatement.WhereCondition, fakeWhereCondition),
                resultSqlStatement.WhereCondition);

            var expectedResultSelectProjection =
                Expression.MakeMemberAccess(new SqlTableReferenceExpression(resultSqlStatement.SqlTables[0]), typeof(Cook).GetProperty("ID"));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResultSelectProjection, resultSqlStatement.SelectProjection);

            Assert.That(resultSqlStatement.DataInfo, Is.TypeOf(typeof(StreamedSequenceInfo)));
            Assert.That(resultSqlStatement.DataInfo.DataType, Is.SameAs(typeof(IQueryable <int>)));

            var expectedItemExpression = resultSqlStatement.SelectProjection;

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedItemExpression, ((StreamedSequenceInfo)resultSqlStatement.DataInfo).ItemExpression);
        }
        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 Initialization_ThrowsWhenNoSequenceType()
        {
            var invalidGroupSource = SqlStatementModelObjectMother.CreateSqlTable(typeof(int));

            new UnresolvedGroupReferenceTableInfo(invalidGroupSource);
        }
Example #27
0
 public void SetUp()
 {
     _tableReferenceExpression = new SqlTableReferenceExpression(SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook)));
 }
 public void SetUp()
 {
     _referencedGroupSource = SqlStatementModelObjectMother.CreateSqlTable(typeof(IGrouping <int, string>));
     _tableInfo             = new UnresolvedGroupReferenceTableInfo(_referencedGroupSource);
 }
        public void VisitQuerySourceReferenceExpression_WithOrderings_AndExtractOrdingsPolicy()
        {
            var sqlTable = SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook));
            var sqlTableReferenceExpression = new SqlTableReferenceExpression(sqlTable);
            var selectProjection            = new NamedExpression("test", Expression.MakeMemberAccess(sqlTableReferenceExpression, typeof(Cook).GetProperty("Name")));
            var orderingExpression          = Expression.MakeMemberAccess(sqlTableReferenceExpression, typeof(Cook).GetProperty("ID"));
            var sqlStatement = new SqlStatementBuilder
            {
                DataInfo         = new StreamedSequenceInfo(typeof(DateTime[]), Expression.Constant(new DateTime(2000, 1, 1))),
                SelectProjection = selectProjection,
                SqlTables        = { sqlTable },
                Orderings        =
                { new Ordering(orderingExpression, OrderingDirection.Asc) }
            }.GetSqlStatement();
            var fakeSelectExpression = Expression.Constant(new KeyValuePair <string, int>("test", 5));
            var fakeWhereExpression  = Expression.Constant(true);

            var innerSequenceExpression = new SqlSubStatementExpression(sqlStatement);

            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);

            _stageMock
            .Expect(mock => mock.PrepareSelectExpression(Arg <Expression> .Is.Anything, Arg <ISqlPreparationContext> .Is.Anything))
            .Return(fakeSelectExpression);
            _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(OrderingExtractionPolicy.ExtractOrderingsIntoProjection);

            visitor.VisitQuerySourceReference(querySourceReferenceExpression);

            _stageMock.VerifyAllExpectations();

            Assert.That(visitor.FromExpressionInfo != null); // inline condition because of ReSharper
            var fromExpressionInfo = (FromExpressionInfo)visitor.FromExpressionInfo;

            Assert.That(fromExpressionInfo.ExtractedOrderings.Count, Is.EqualTo(1));
            Assert.That(fromExpressionInfo.ExtractedOrderings[0].Expression, Is.AssignableTo(typeof(MemberExpression)));
            Assert.That(((MemberExpression)fromExpressionInfo.ExtractedOrderings[0].Expression).Expression, Is.TypeOf(typeof(SqlTableReferenceExpression)));
            Assert.That(
                ((SqlTableReferenceExpression)((MemberExpression)fromExpressionInfo.ExtractedOrderings[0].Expression).Expression).SqlTable, Is.TypeOf(typeof(SqlTable)));
            Assert.That(
                ((SqlTable)((SqlTableReferenceExpression)((MemberExpression)fromExpressionInfo.ExtractedOrderings[0].Expression).Expression).SqlTable).TableInfo, Is.TypeOf(typeof(ResolvedSubStatementTableInfo)));
            var resolvedSubStatementtableInfo =
                (ResolvedSubStatementTableInfo)((SqlTable)((SqlTableReferenceExpression)((MemberExpression)fromExpressionInfo.ExtractedOrderings[0].Expression).Expression).SqlTable).TableInfo;

            Assert.That(resolvedSubStatementtableInfo.SqlStatement.SelectProjection, Is.SameAs(fakeSelectExpression));
            Assert.That(resolvedSubStatementtableInfo.SqlStatement.Orderings.Count, Is.EqualTo(0));
            Assert.That(((MemberExpression)fromExpressionInfo.ExtractedOrderings[0].Expression).Member, Is.EqualTo(typeof(KeyValuePair <,>).MakeGenericType(typeof(string), typeof(int)).GetProperty("Value")));
            Assert.That(fromExpressionInfo.ItemSelector, Is.AssignableTo(typeof(MemberExpression)));
            Assert.That(((MemberExpression)fromExpressionInfo.ItemSelector).Expression, Is.TypeOf(typeof(SqlTableReferenceExpression)));
            Assert.That(((SqlTableReferenceExpression)((MemberExpression)fromExpressionInfo.ItemSelector).Expression).SqlTable, Is.TypeOf(typeof(SqlTable)));
            Assert.That(
                ((SqlTable)((SqlTableReferenceExpression)((MemberExpression)fromExpressionInfo.ItemSelector).Expression).SqlTable).TableInfo, Is.TypeOf(typeof(ResolvedSubStatementTableInfo)));
            Assert.That(((MemberExpression)fromExpressionInfo.ItemSelector).Member, Is.EqualTo(typeof(KeyValuePair <,>).MakeGenericType(typeof(string), typeof(int)).GetProperty("Key")));
        }