Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
        private SqlTable CreateSqlCompatibleSubStatementTable(
            SqlStatement originalStatement,
            Expression newSelectProjection,
            Func <ITableInfo, SqlTable> tableCreator)
        {
            // create a new statement equal to the original one, but with the tuple as its select projection
            var builder = new SqlStatementBuilder(originalStatement)
            {
                SelectProjection = newSelectProjection
            };

            builder.RecalculateDataInfo(originalStatement.SelectProjection);

            // clear orderings unless required for TopExpression
            if (originalStatement.TopExpression == null)
            {
                builder.Orderings.Clear();
            }

            var newSqlStatement = builder.GetSqlStatement();

            // put new statement into a sub-statement table
            var subStatementTableInfo = new ResolvedSubStatementTableInfo(_uniqueIdentifierGenerator.GetUniqueIdentifier("q"), newSqlStatement);

            return(tableCreator(subStatementTableInfo));
        }
        public void CreateSqlTableForSubStatement_WithOrderingsAndTopExpression_AndDoNotExtractOrderingsPolicy_ReturnsTableWithOrderings_WithOriginalProjection()
        {
            var builder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatementWithCook())
            {
                TopExpression = Expression.Constant("top"),
                Orderings     = { new Ordering(Expression.Constant("order1"), OrderingDirection.Asc) }
            };
            var statementWithOrderingsAndTopExpression = builder.GetSqlStatement();

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

            _stageMock.VerifyAllExpectations();

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

            var tableInfo = result.SqlTable.TableInfo;

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

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

            Assert.That(subStatement.Orderings, Is.Not.Empty);
            Assert.That(subStatement.SelectProjection, Is.SameAs(statementWithOrderingsAndTopExpression.SelectProjection));
        }
        public void CreateSqlTableForSubStatement_WithOrderings_ReturnsTableWithoutOrderings_WithNewProjection()
        {
            var fakeSelectProjection = Expression.Constant(new KeyValuePair <Cook, KeyValuePair <string, string> > ());

            _stageMock
            .Expect(mock => mock.PrepareSelectExpression(Arg <Expression> .Is.Anything, Arg.Is(_context)))
            .Return(fakeSelectProjection);
            _stageMock.Replay();

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

            _stageMock.VerifyAllExpectations();

            var tableInfo = ((SqlTable)result.SqlTable).TableInfo;

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

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

            Assert.That(subStatement.DataInfo, Is.TypeOf(typeof(StreamedSequenceInfo)));
            Assert.That(((StreamedSequenceInfo)subStatement.DataInfo).ItemExpression, Is.SameAs(subStatement.SelectProjection));
            Assert.That(((StreamedSequenceInfo)subStatement.DataInfo).DataType,
                        Is.SameAs(typeof(IQueryable <KeyValuePair <Cook, KeyValuePair <string, string> > >)));

            var expectedSubStatementBuilder = new SqlStatementBuilder(_statementWithOrderings)
            {
                SelectProjection = fakeSelectProjection,
                DataInfo         = subStatement.DataInfo
            };

            expectedSubStatementBuilder.Orderings.Clear();
            Assert.That(subStatement, Is.EqualTo(expectedSubStatementBuilder.GetSqlStatement()));

            Assert.That(result.WhereCondition, Is.Null);
        }
        public void CreateSqlTableForSubStatement_WithOrderings_WithTopExpression()
        {
            var builder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatementWithCook())
            {
                TopExpression = Expression.Constant("top"),
                Orderings     = { new Ordering(Expression.Constant("order1"), OrderingDirection.Asc) }
            };
            var statement            = builder.GetSqlStatement();
            var fakeSelectProjection = Expression.Constant(new KeyValuePair <Cook, string> ());

            _stageMock
            .Expect(mock => mock.PrepareSelectExpression(Arg <Expression> .Is.Anything, Arg <ISqlPreparationContext> .Matches(c => c == _context)))
            .Return(fakeSelectProjection);
            _stageMock.Replay();

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

            _stageMock.VerifyAllExpectations();

            var sqlTable = (SqlTable)result.SqlTable;

            Assert.That(sqlTable.TableInfo, Is.TypeOf(typeof(ResolvedSubStatementTableInfo)));
            Assert.That(((ResolvedSubStatementTableInfo)sqlTable.TableInfo).SqlStatement.Orderings.Count, Is.EqualTo(1));
            Assert.That(result.ExtractedOrderings.Count, Is.EqualTo(1));
        }
        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));
        }
Ejemplo n.º 7
0
        public void GetStatementAndResetBuilder()
        {
            var statementBuilder = new SqlStatementBuilder
            {
                DataInfo               = new TestStreamedValueInfo(typeof(Cook)),
                TopExpression          = ExpressionHelper.CreateExpression(),
                IsDistinctQuery        = true,
                SelectProjection       = new AggregationExpression(typeof(int), Expression.Constant(1), AggregationModifier.Min),
                SqlTables              = { new SqlTable(new ResolvedSimpleTableInfo(typeof(Cook), "CookTable", "c"), JoinSemantics.Inner) },
                WhereCondition         = Expression.Constant(true),
                RowNumberSelector      = Expression.Constant("selector"),
                CurrentRowNumberOffset = Expression.Constant(1),
                GroupByExpression      = Expression.Constant("group"),
                Orderings              = { new Ordering(Expression.Constant("order"), OrderingDirection.Desc) }
            };
            var sqlStatement = statementBuilder.GetSqlStatement();

            var result = statementBuilder.GetStatementAndResetBuilder();

            Assert.That(result, Is.Not.SameAs(sqlStatement));
            Assert.That(result, Is.EqualTo(sqlStatement));

            Assert.That(statementBuilder.DataInfo, Is.Null);
            Assert.That(statementBuilder.TopExpression, Is.Null);
            Assert.That(statementBuilder.IsDistinctQuery, Is.False);
            Assert.That(statementBuilder.SelectProjection, Is.Null);
            Assert.That(statementBuilder.SqlTables, Is.Empty);
            Assert.That(statementBuilder.Orderings, Is.Empty);
            Assert.That(statementBuilder.WhereCondition, Is.Null);
            Assert.That(statementBuilder.RowNumberSelector, Is.Null);
            Assert.That(statementBuilder.CurrentRowNumberOffset, Is.Null);
            Assert.That(statementBuilder.GroupByExpression, Is.Null);
        }
        public void VisitSqlSubStatementExpression_WithOrderingsAndNoTopExpression()
        {
            var builder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatementWithCook())
            {
                SelectProjection = new NamedExpression("test", Expression.Constant("test")),
                Orderings        = { new Ordering(Expression.Constant("order1"), OrderingDirection.Asc) }
            };
            var statement = builder.GetSqlStatement();
            var sqlSubStatementExpression = new SqlSubStatementExpression(statement);

            var stage = new DefaultSqlPreparationStage(_methodCallTransformerProvider, new ResultOperatorHandlerRegistry(), _generator);

            var result = SqlPreparationFromExpressionVisitor.AnalyzeFromExpression(
                sqlSubStatementExpression,
                stage,
                _generator,
                _methodCallTransformerProvider,
                _context,
                info => new SqlTable(info, JoinSemantics.Inner));

            var sqlTable = (SqlTable)result.SqlTable;

            Assert.That(sqlTable.TableInfo, Is.TypeOf(typeof(ResolvedSubStatementTableInfo)));
            Assert.That(((ResolvedSubStatementTableInfo)sqlTable.TableInfo).SqlStatement.Orderings.Count, Is.EqualTo(0));
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
0
        public void VisitSqlSubStatmentExpression_HasTopExpression_ReturnsNew()
        {
            var builder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatementWithCook());

            builder.Orderings.Add(new Ordering(Expression.Constant("order"), OrderingDirection.Asc));
            var sqlStatement = builder.GetSqlStatement();

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

            var subStatementExpression = new SqlSubStatementExpression(sqlStatement);

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

            Assert.That(result, Is.Not.SameAs(subStatementExpression));
            Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression)));
            Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.EqualTo(expectedStatement));
        }
Ejemplo n.º 11
0
        protected virtual SqlStatement ResolveSqlStatement(SqlStatement sqlStatement)
        {
            ArgumentUtility.CheckNotNull("sqlStatement", sqlStatement);

            foreach (var sqlTable in sqlStatement.SqlTables)
            {
                sqlTable.Accept(this);
            }

            var sqlStatementBuilder = new SqlStatementBuilder(sqlStatement);

            var previousSelectProjection = sqlStatementBuilder.SelectProjection;

            sqlStatementBuilder.SelectProjection = ResolveSelectProjection(sqlStatementBuilder.SelectProjection, sqlStatementBuilder);
            sqlStatementBuilder.RecalculateDataInfo(previousSelectProjection);

            if (sqlStatementBuilder.GroupByExpression != null)
            {
                sqlStatementBuilder.GroupByExpression = ResolveGroupByExpression(sqlStatementBuilder.GroupByExpression);
            }

            if (sqlStatementBuilder.WhereCondition != null)
            {
                sqlStatementBuilder.WhereCondition = ResolveWhereCondition(sqlStatementBuilder.WhereCondition);
            }

            if (sqlStatementBuilder.TopExpression != null)
            {
                sqlStatementBuilder.TopExpression = ResolveTopExpression(sqlStatementBuilder.TopExpression);
            }

            if (sqlStatementBuilder.Orderings.Count > 0)
            {
                for (int i = 0; i < sqlStatementBuilder.Orderings.Count; ++i)
                {
                    var resolvedOrderingExpression = _stage.ResolveOrderingExpression(sqlStatementBuilder.Orderings[i].Expression, _context);
                    if (resolvedOrderingExpression != sqlStatementBuilder.Orderings[i].Expression)
                    {
                        sqlStatementBuilder.Orderings[i] = new Ordering(resolvedOrderingExpression, sqlStatementBuilder.Orderings[i].OrderingDirection);
                    }
                }
            }

            for (int i = 0; i < sqlStatement.SetOperationCombinedStatements.Count; i++)
            {
                var combinedStatement    = sqlStatement.SetOperationCombinedStatements[i];
                var resolvedSqlStatement = _stage.ResolveSqlStatement(combinedStatement.SqlStatement, _context);
                if (!Equals(resolvedSqlStatement, combinedStatement.SqlStatement))
                {
                    sqlStatementBuilder.SetOperationCombinedStatements[i] = new SetOperationCombinedStatement(
                        resolvedSqlStatement,
                        combinedStatement.SetOperation);
                }
            }

            return(sqlStatementBuilder.GetSqlStatement());
        }
        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);
        }
Ejemplo n.º 13
0
        public void HandleResultOperator_GroupByAfterTopExpression()
        {
            var topExpression = Expression.Constant("top");

            _sqlStatementBuilder.TopExpression = topExpression;

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

            var originalStatement = _sqlStatementBuilder.GetSqlStatement();
            var originalDataInfo  = _sqlStatementBuilder.DataInfo;

            var fakeFromExpressionInfo = new FromExpressionInfo(
                new SqlTable(new ResolvedSubStatementTableInfo("sc", originalStatement), JoinSemantics.Inner), new Ordering[0], elementSelector, null);

            _stageMock
            .Expect(mock => mock.PrepareResultOperatorItemExpression(keySelector, _context))
            .Return(keySelector);
            _stageMock
            .Expect(mock => mock.PrepareResultOperatorItemExpression(elementSelector, _context))
            .Return(elementSelector);
            _stageMock
            .Expect(
                mock => mock.PrepareFromExpression(Arg <Expression> .Is.Anything, Arg.Is(_context), Arg <Func <ITableInfo, SqlTable> > .Is.Anything))
            .Return(fakeFromExpressionInfo)
            .WhenCalled(mi =>
            {
                var fromExpression = (Expression)mi.Arguments[0];
                CheckExpressionMovedIntoSubStatement(fromExpression, originalStatement);
            });

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

            _stageMock.VerifyAllExpectations();
            Assert.That(_sqlStatementBuilder.GroupByExpression, Is.SameAs(keySelector));
            Assert.That(_sqlStatementBuilder.SqlTables.Count, Is.EqualTo(1));
            Assert.That(_sqlStatementBuilder.SqlTables[0], Is.SameAs(fakeFromExpressionInfo.SqlTable));

            Assert.That(_sqlStatementBuilder.DataInfo, Is.Not.SameAs(originalDataInfo));
            Assert.That(
                _sqlStatementBuilder.DataInfo.DataType,
                Is.EqualTo(typeof(IQueryable <>).MakeGenericType(typeof(IGrouping <,>).MakeGenericType(typeof(string), typeof(string)))));
        }
        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 builder            = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement_Resolved(typeof(Cook)))
            {
                SelectProjection  = constantExpression,
                DataInfo          = new StreamedSequenceInfo(typeof(Restaurant[]), constantExpression),
                WhereCondition    = whereCondition,
                GroupByExpression = groupExpression,
                TopExpression     = topExpression,
            };

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

            _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(((SqlTable)sqlStatement.SqlTables[0]).TableInfo, _mappingResolutionContext))
            .Return(new ResolvedSimpleTableInfo(typeof(Cook), "CookTable", "c"));
            _stageMock.Replay();

            var resolveSqlStatement = _visitor.ResolveSqlStatement(sqlStatement);

            _stageMock.VerifyAllExpectations();
            Assert.That(resolveSqlStatement.DataInfo, Is.TypeOf(typeof(StreamedSequenceInfo)));
            Assert.That(((StreamedSequenceInfo)resolveSqlStatement.DataInfo).DataType, Is.EqualTo(typeof(IQueryable <>).MakeGenericType(typeof(Cook))));
            Assert.That(resolveSqlStatement.SelectProjection, Is.SameAs(fakeExpression));
            Assert.That(resolveSqlStatement.WhereCondition, Is.SameAs(fakeWhereCondition));
            Assert.That(resolveSqlStatement.TopExpression, Is.SameAs(fakeTopExpression));
            Assert.That(resolveSqlStatement.GroupByExpression, Is.SameAs(fakeGroupExpression));
            Assert.That(resolveSqlStatement.Orderings[0].Expression, Is.SameAs(fakeOrderExpression));
            Assert.That(sqlStatement.Orderings[0].Expression, Is.SameAs(ordering.Expression));
        }
Ejemplo n.º 15
0
        public void CreateExpression_WithSqlTables()
        {
            var sqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement());
            var sqlStatement        = sqlStatementBuilder.GetSqlStatement();

            var result = sqlStatement.CreateExpression();

            Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression)));
            Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.SameAs(sqlStatement));
        }
Ejemplo n.º 16
0
        public void GetSqlStatement_NoSelectProjection()
        {
            var statementBuilder = new SqlStatementBuilder();

            statementBuilder.DataInfo         = new TestStreamedValueInfo(typeof(int));
            statementBuilder.SelectProjection = null;
            Assert.That(
                () => statementBuilder.GetSqlStatement(),
                Throws.InvalidOperationException.With.Message.EqualTo("A SelectProjection must be set before the SqlStatement can be retrieved."));
        }
Ejemplo n.º 17
0
        public void GetSqlStatement_NoDataInfoSet()
        {
            var statementBuilder = new SqlStatementBuilder();

            statementBuilder.DataInfo         = null;
            statementBuilder.SelectProjection = ExpressionHelper.CreateExpression();
            Assert.That(
                () => statementBuilder.GetSqlStatement(),
                Throws.InvalidOperationException.With.Message.EqualTo("A DataInfo must be set before the SqlStatement can be retrieved."));
        }
Ejemplo n.º 18
0
        public virtual SqlStatement PrepareSqlStatement(QueryModel queryModel, ISqlPreparationContext context)
        {
            var builder = new SqlStatementBuilder
            {
                DataInfo         = new StreamedScalarValueInfo(typeof(string)),
                SelectProjection = Expression.Constant("Value added by preparation mixin")
            };

            return(builder.GetSqlStatement());
        }
        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);
        }
Ejemplo n.º 20
0
        public void CreateExpression_WithNoSqlTablesAndNoDistinctQueryAndNoAggregationModifier()
        {
            var sqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement());

            sqlStatementBuilder.SqlTables.Clear();
            var sqlStatement = sqlStatementBuilder.GetSqlStatement();

            var result = sqlStatement.CreateExpression();

            Assert.That(result, Is.SameAs(sqlStatement.SelectProjection));
        }
Ejemplo n.º 21
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, _generator, _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));
        }
Ejemplo n.º 22
0
 public static SqlStatement CreateMinimalSqlStatement(SqlStatementBuilder builder)
 {
     if (builder.SelectProjection == null)
     {
         builder.SelectProjection = ExpressionHelper.CreateExpression();
     }
     if (builder.DataInfo == null)
     {
         builder.DataInfo = new TestStreamedValueInfo(builder.SelectProjection.Type);
     }
     return(builder.GetSqlStatement());
 }
Ejemplo n.º 23
0
        public virtual Expression VisitSqlSubStatement(SqlSubStatementExpression expression)
        {
            ArgumentUtility.CheckNotNull("expression", expression);

            if (expression.SqlStatement.Orderings.Count > 0 && expression.SqlStatement.TopExpression == null)
            {
                var builder = new SqlStatementBuilder(expression.SqlStatement);
                builder.Orderings.Clear();
                return(new SqlSubStatementExpression(builder.GetSqlStatement()));
            }
            return(expression);
        }
Ejemplo n.º 24
0
        public void VisitSqlSubStatmentExpression_NoTopExpression_ReturnsSame()
        {
            var builder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatementWithCook());

            builder.Orderings.Clear();
            var sqlStatement = builder.GetSqlStatement();

            var subStatementExpression = new SqlSubStatementExpression(sqlStatement);

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

            Assert.That(result, Is.SameAs(subStatementExpression));
        }
Ejemplo n.º 25
0
        public void CreateExpression_IsDistinctQuery()
        {
            var sqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement());

            sqlStatementBuilder.SqlTables.Clear();
            sqlStatementBuilder.IsDistinctQuery = true;
            var sqlStatement = sqlStatementBuilder.GetSqlStatement();

            var result = sqlStatement.CreateExpression();

            Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression)));
            Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.SameAs(sqlStatement));
        }
Ejemplo n.º 26
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));
        }
Ejemplo n.º 27
0
        public virtual SqlStatement ResolveSqlStatement(SqlStatement sqlStatement, IMappingResolutionContext context)
        {
            Assert.That(sqlStatement.SelectProjection, Is.TypeOf(typeof(ConstantExpression)));
            Assert.That(((ConstantExpression)sqlStatement.SelectProjection).Value, Is.EqualTo("Value added by preparation mixin"));

            var builder = new SqlStatementBuilder
            {
                DataInfo         = new StreamedScalarValueInfo(typeof(string)),
                SelectProjection =
                    new SqlEntityDefinitionExpression(typeof(int), "c", "CookTable", e => e.GetColumn(typeof(int), "ID", false))
            };

            return(builder.GetSqlStatement());
        }
        public ITableInfo VisitUnresolvedGroupReferenceTableInfo(UnresolvedGroupReferenceTableInfo tableInfo)
        {
            var groupSourceSubStatementTableInfo = tableInfo.ReferencedGroupSource.GetResolvedTableInfo() as ResolvedSubStatementTableInfo;

            if (groupSourceSubStatementTableInfo == null)
            {
                var message = string.Format(
                    "This SQL generator only supports sequences in from expressions if they are members of an entity or if they come from a GroupBy operator. "
                    + "Sequence: '{0}'", tableInfo);
                throw new NotSupportedException(message);
            }

            var groupingSelectExpression = groupSourceSubStatementTableInfo.SqlStatement.SelectProjection as SqlGroupingSelectExpression;

            if (groupingSelectExpression == null)
            {
                throw new NotSupportedException(
                          "When a sequence retrieved by a subquery is used in a from expression, the subquery must end with a GroupBy operator.");
            }

            var elementSelectingStatementBuilder = new SqlStatementBuilder(groupSourceSubStatementTableInfo.SqlStatement)
            {
                GroupByExpression = null
            };

            var currentKeyExpression = Expression.MakeMemberAccess(
                new SqlTableReferenceExpression(tableInfo.ReferencedGroupSource),
                groupingSelectExpression.Type.GetProperty("Key"));

            var groupKeyJoinCondition = _stage.ResolveWhereExpression(
                Expression.OrElse(
                    Expression.AndAlso(new SqlIsNullExpression(groupingSelectExpression.KeyExpression), new SqlIsNullExpression(currentKeyExpression)),
                    Expression.AndAlso(
                        Expression.AndAlso(
                            new SqlIsNotNullExpression(groupingSelectExpression.KeyExpression),
                            new SqlIsNotNullExpression(currentKeyExpression)),
                        Expression.Equal(groupingSelectExpression.KeyExpression, currentKeyExpression))),
                _context);

            elementSelectingStatementBuilder.AddWhereCondition(groupKeyJoinCondition);

            elementSelectingStatementBuilder.SelectProjection = groupingSelectExpression.ElementExpression;
            elementSelectingStatementBuilder.RecalculateDataInfo(groupingSelectExpression);

            return(new ResolvedJoinedGroupingTableInfo(
                       _generator.GetUniqueIdentifier("q"),
                       elementSelectingStatementBuilder.GetSqlStatement(),
                       groupingSelectExpression,
                       groupSourceSubStatementTableInfo.TableAlias));
        }
Ejemplo n.º 29
0
        public void CreateExpression_HasAggregationModifier()
        {
            var sqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement());

            sqlStatementBuilder.SqlTables.Clear();
            var selectProjection = new AggregationExpression(typeof(double), sqlStatementBuilder.SelectProjection, AggregationModifier.Max);

            sqlStatementBuilder.SelectProjection = selectProjection;
            var sqlStatement = sqlStatementBuilder.GetSqlStatement();

            var result = sqlStatement.CreateExpression();

            Assert.That(result, Is.SameAs(selectProjection));
        }
Ejemplo n.º 30
0
        private Expression CheckAndSimplifyEntityWithinSubStatement(SqlSubStatementExpression sqlSubStatementExpression)
        {
            var newSelectProjection = ResolvePotentialEntity(sqlSubStatementExpression.SqlStatement.SelectProjection);

            if (newSelectProjection != sqlSubStatementExpression.SqlStatement.SelectProjection)
            {
                var newSubStatement = new SqlStatementBuilder(sqlSubStatementExpression.SqlStatement)
                {
                    SelectProjection = newSelectProjection
                };
                newSubStatement.RecalculateDataInfo(sqlSubStatementExpression.SqlStatement.SelectProjection);

                return(newSubStatement.GetSqlStatement().CreateExpression());
            }

            return(sqlSubStatementExpression);
        }