GetSqlStatement() public method

public GetSqlStatement ( ) : SqlStatement
return SqlStatement
    public void SetUp ()
    {
      _stageMock = MockRepository.GenerateStrictMock<ISqlPreparationStage>();
      _context = SqlStatementModelObjectMother.CreateSqlPreparationContext ();
      _generator = new UniqueIdentifierGenerator();
      _factory = new SqlPreparationSubStatementTableFactory (_stageMock, _context, _generator);

      var builderForStatementWithOrderings = new SqlStatementBuilder (SqlStatementModelObjectMother.CreateSqlStatementWithCook ())
      {
        Orderings = {
            new Ordering (Expression.Constant ("order1"), OrderingDirection.Desc),
            new Ordering (Expression.Constant ("order2"), OrderingDirection.Asc),
        }
      };
      _statementWithOrderings = builderForStatementWithOrderings.GetSqlStatement ();
    }
    public SqlStatement VisitSqlStatement (SqlStatement sqlStatement, SqlExpressionContext expressionContext)
    {
      ArgumentUtility.CheckNotNull ("sqlStatement", sqlStatement);

      if (expressionContext == SqlExpressionContext.PredicateRequired)
        throw new InvalidOperationException ("A SqlStatement cannot be used as a predicate.");

      var statementBuilder = new SqlStatementBuilder (sqlStatement);

      var newSelectProjection = _stage.ApplyContext (sqlStatement.SelectProjection, expressionContext, _mappingResolutionContext);
      statementBuilder.SelectProjection = newSelectProjection;
      statementBuilder.RecalculateDataInfo (sqlStatement.SelectProjection);

      var newSqlStatement = statementBuilder.GetSqlStatement();
      return newSqlStatement.Equals (sqlStatement) ? sqlStatement : newSqlStatement;
    }
        private SqlStatement ConvertValueStatementToSequenceStatement()
        {
            var newDataInfo = new StreamedSequenceInfo(typeof(IEnumerable <>).MakeGenericType(SqlStatement.DataInfo.DataType), SqlStatement.SelectProjection);

            var adjustedStatementBuilder = new SqlStatementBuilder(SqlStatement)
            {
                DataInfo = newDataInfo
            };

            if (SqlStatement.DataInfo is StreamedSingleValueInfo && SqlStatement.SqlTables.Count != 0)
            {
                // A sub-statement might use a different TopExpression than 1 (or none at all) in order to provoke a SQL error when more than one item is
                // returned. When we convert the statement to a sequence statement, however, we must ensure that the exact "only 1 value is returned"
                // semantics is ensured because we can't provoke a SQL error (but instead would return strange result sets).
                adjustedStatementBuilder.TopExpression = new SqlLiteralExpression(1);
            }

            return(adjustedStatementBuilder.GetSqlStatement());
        }
    public void VisitSqlStatement_ExpressionsAndStreamedSequenceDataTypeChanged ()
    {
      var builder = new SqlStatementBuilder (SqlStatementModelObjectMother.CreateSqlStatementWithCook());
      builder.DataInfo = new StreamedSequenceInfo (typeof (IQueryable<>).MakeGenericType (builder.SelectProjection.Type), builder.SelectProjection);
      var sqlStatement = builder.GetSqlStatement();

      var fakeResult = Expression.Constant ("test");
      
      _stageMock
          .Expect (mock => mock.ApplyContext (sqlStatement.SelectProjection, SqlExpressionContext.ValueRequired, _mappingResolutionContext))
          .Return (fakeResult);
     _stageMock.Replay();

      var result = SqlContextSelectionAdjuster.ApplyContext (sqlStatement, SqlExpressionContext.ValueRequired, _stageMock, _mappingResolutionContext);

      _stageMock.VerifyAllExpectations();
      Assert.That (result, Is.Not.SameAs (sqlStatement));
      Assert.That (result.SelectProjection, Is.SameAs (fakeResult));
      Assert.That (result.DataInfo, Is.TypeOf (typeof (StreamedSequenceInfo)));
      Assert.That (((StreamedSequenceInfo) result.DataInfo).ResultItemType, Is.EqualTo (typeof (string)));
      Assert.That (result.DataInfo.DataType, Is.EqualTo (typeof (IQueryable<>).MakeGenericType (fakeResult.Type)));
    }
    public void GetSqlStatement ()
    {
      var statementBuilder = new SqlStatementBuilder ();
      statementBuilder.DataInfo = new TestStreamedValueInfo (typeof (int));

      var constantExpression = Expression.Constant ("test");
      statementBuilder.SelectProjection = constantExpression;
      var sqlTable = SqlStatementModelObjectMother.CreateSqlTable();
      statementBuilder.SqlTables.Add (sqlTable);

      var result = statementBuilder.GetSqlStatement();

      Assert.That (result.SelectProjection, Is.SameAs (constantExpression));
      Assert.That (result.SqlTables.Count, Is.EqualTo (1));
      Assert.That (result.SqlTables[0], Is.SameAs (sqlTable));
    }
    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));
    }
    public void GetSqlStatement_CheckProperties ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (Cook));
      var topExpression = ExpressionHelper.CreateExpression();
      var isDistinctQuery = BooleanObjectMother.GetRandomBoolean();
      var selectProjection = new AggregationExpression (typeof (int), Expression.Constant (1), AggregationModifier.Min);
      var whereCondition = Expression.Constant (true);
      var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);
      var ordering = new Ordering (Expression.Constant ("order"), OrderingDirection.Desc);
      var rowNumberSelector = Expression.Constant ("selector");
      var currentRowNumberOffset = Expression.Constant (1);
      var groupExpression = Expression.Constant ("group");

      var statementBuilder = new SqlStatementBuilder
                             {
                                 DataInfo = dataInfo,
                                 TopExpression = topExpression,
                                 IsDistinctQuery = isDistinctQuery,
                                 SelectProjection = selectProjection,
                                 SqlTables = { sqlTable },
                                 WhereCondition = whereCondition,
                                 RowNumberSelector = rowNumberSelector,
                                 CurrentRowNumberOffset = currentRowNumberOffset,
                                 GroupByExpression = groupExpression,
                                 Orderings = { ordering }
                             };

      var sqlStatement = statementBuilder.GetSqlStatement();

      Assert.That (sqlStatement.DataInfo, Is.SameAs (dataInfo));
      Assert.That (sqlStatement.TopExpression, Is.SameAs (topExpression));
      Assert.That (sqlStatement.IsDistinctQuery, Is.EqualTo (isDistinctQuery));
      Assert.That (sqlStatement.SelectProjection, Is.SameAs (selectProjection));
      Assert.That (sqlStatement.SqlTables[0], Is.SameAs (sqlTable));
      Assert.That (sqlStatement.Orderings[0], Is.SameAs (ordering));
      Assert.That (sqlStatement.WhereCondition, Is.SameAs (whereCondition));
      Assert.That (sqlStatement.RowNumberSelector, Is.SameAs (rowNumberSelector));
      Assert.That (sqlStatement.CurrentRowNumberOffset, Is.SameAs (currentRowNumberOffset));
      Assert.That (sqlStatement.GroupByExpression, Is.SameAs (groupExpression));
    }
    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 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));
    }
 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."));
 }
    private SqlStatement ConvertValueStatementToSequenceStatement ()
    {
      var newDataInfo = new StreamedSequenceInfo (typeof (IEnumerable<>).MakeGenericType (SqlStatement.DataInfo.DataType), SqlStatement.SelectProjection);

      var adjustedStatementBuilder = new SqlStatementBuilder (SqlStatement) { DataInfo = newDataInfo };
      if (SqlStatement.DataInfo is StreamedSingleValueInfo && SqlStatement.SqlTables.Count!=0)
      {
        // A sub-statement might use a different TopExpression than 1 (or none at all) in order to provoke a SQL error when more than one item is 
        // returned. When we convert the statement to a sequence statement, however, we must ensure that the exact "only 1 value is returned" 
        // semantics is ensured because we can't provoke a SQL error (but instead would return strange result sets).
        adjustedStatementBuilder.TopExpression = new SqlLiteralExpression (1);
      }

      return adjustedStatementBuilder.GetSqlStatement();
    }
Ejemplo n.º 12
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));
    }
    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);
    }
    protected override Expression VisitMemberExpression (MemberExpression expression)
    {
      ArgumentUtility.CheckNotNull ("expression", expression);

      var newInnerExpression = VisitExpression (expression.Expression);

      var innerExpressionAsSqlCaseExpression = newInnerExpression as SqlCaseExpression;
      if (innerExpressionAsSqlCaseExpression != null)
      {
        var originalCases = innerExpressionAsSqlCaseExpression.Cases;
        var originalElseCase = innerExpressionAsSqlCaseExpression.ElseCase;
        var newCases = originalCases.Select (c => new SqlCaseExpression.CaseWhenPair (c.When, Expression.MakeMemberAccess (c.Then, expression.Member)));
        var newElseCase = originalElseCase != null ? Expression.MakeMemberAccess (originalElseCase, expression.Member) : null;
        // If there is no else case, ensure that the resulting type is nullable
        var caseExpressionType =
            newElseCase == null && expression.Type.IsValueType && Nullable.GetUnderlyingType (expression.Type) == null
                ? typeof (Nullable<>).MakeGenericType (expression.Type)
                : expression.Type;
        var newSqlCaseExpression = new SqlCaseExpression (caseExpressionType, newCases, newElseCase);
        return VisitExpression (newSqlCaseExpression);
      }

      if (newInnerExpression.NodeType == ExpressionType.Coalesce)
      {
        var innerExpressionAsBinaryExpression = (BinaryExpression) newInnerExpression;
        var newConditionalExpression = Expression.Condition (
            new SqlIsNotNullExpression (innerExpressionAsBinaryExpression.Left), 
            Expression.MakeMemberAccess (innerExpressionAsBinaryExpression.Left, expression.Member), 
            Expression.MakeMemberAccess (innerExpressionAsBinaryExpression.Right, expression.Member));
        return VisitExpression (newConditionalExpression);
      }

      var innerExpressionAsSqlSubStatementExpression = newInnerExpression as SqlSubStatementExpression;
      if (innerExpressionAsSqlSubStatementExpression != null)
      {
        var sqlStatementBuilder = new SqlStatementBuilder (innerExpressionAsSqlSubStatementExpression.SqlStatement);
        var namedExpression = (NamedExpression) sqlStatementBuilder.SelectProjection;
        sqlStatementBuilder.SelectProjection = new NamedExpression (
            namedExpression.Name, VisitExpression (Expression.MakeMemberAccess (namedExpression.Expression, expression.Member)));
        sqlStatementBuilder.RecalculateDataInfo (innerExpressionAsSqlSubStatementExpression.SqlStatement.SelectProjection);
        return new SqlSubStatementExpression (sqlStatementBuilder.GetSqlStatement());
      }

      var memberAsPropertyInfo = expression.Member as PropertyInfo;
      if (memberAsPropertyInfo != null)
      {
        var methodInfo = memberAsPropertyInfo.GetGetMethod();
        if (methodInfo != null)
        {
          var methodCallExpression = Expression.Call (expression.Expression, methodInfo);
          var tranformer = _methodCallTransformerProvider.GetTransformer(methodCallExpression);
          if (tranformer != null)
          {
            var tranformedExpression = tranformer.Transform (methodCallExpression);
            return VisitExpression (tranformedExpression);
          }
        }
      }
      return base.VisitMemberExpression (expression);
    }
    public void VisitSqlStatement_EverthingIsCopiedIfSelectionProjectionHasChanged ()
    {
      var selectProjection = Expression.Constant("select");
      var whereCondition = Expression.Constant(true);
      var topExpression = Expression.Constant("top");
      var dataInfo = new StreamedSequenceInfo(typeof(Cook[]), Expression.Constant(new Cook()));
      var builder = new SqlStatementBuilder () 
      {  
        SelectProjection = selectProjection,
        WhereCondition = whereCondition,
        TopExpression = topExpression,
        IsDistinctQuery = true,
        DataInfo = dataInfo
      };
      var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);
      builder.SqlTables.Add (sqlTable);
      var ordering = new Ordering (Expression.Constant ("order"),OrderingDirection.Asc);
      builder.Orderings.Add (ordering);
      var sqlStatement = builder.GetSqlStatement();
      var fakeResult = Expression.Constant ("fake");

      _stageMock
          .Expect (mock => mock.ApplyContext (sqlStatement.SelectProjection, SqlExpressionContext.ValueRequired, _mappingResolutionContext))
          .Return (fakeResult);
      _stageMock.Replay ();

      var result = SqlContextSelectionAdjuster.ApplyContext (sqlStatement, SqlExpressionContext.ValueRequired, _stageMock, _mappingResolutionContext);

      _stageMock.VerifyAllExpectations();
      Assert.That (result.SelectProjection, Is.SameAs (fakeResult));
      Assert.That (result.DataInfo, Is.SameAs (dataInfo));
      Assert.That (result.WhereCondition, Is.SameAs (whereCondition));
      Assert.That (result.TopExpression, Is.SameAs (topExpression));
      Assert.That (result.SqlTables[0], Is.SameAs (sqlTable));
      Assert.That (result.Orderings[0], Is.SameAs (ordering));
      Assert.That (result.IsDistinctQuery, Is.True);
    }
    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));
   }
    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);
    }
Ejemplo n.º 18
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));
    }
    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 virtual Expression VisitSqlSubStatementExpression (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;
    }
    public void VisitSqlStatement_CopiesIsDistinctQueryFlag ()
    {
      var builder = new SqlStatementBuilder (SqlStatementModelObjectMother.CreateSqlStatementWithCook()) { IsDistinctQuery = true };
      var sqlStatement = builder.GetSqlStatement();

      _stageMock
          .Expect (mock => mock.ApplyContext (sqlStatement.SelectProjection, SqlExpressionContext.ValueRequired, _mappingResolutionContext))
          .Return (sqlStatement.SelectProjection);
      _stageMock.Replay();

      var result = SqlContextSelectionAdjuster.ApplyContext (sqlStatement, SqlExpressionContext.ValueRequired, _stageMock, _mappingResolutionContext);

      _stageMock.VerifyAllExpectations();
      Assert.That (result.IsDistinctQuery, Is.True);
    }
    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 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 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."));
 }
    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;
    }
Ejemplo n.º 27
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));
    }