AggregationExpression holds an aggregation modifier for a warapped expression.
상속: Remotion.Linq.Clauses.Expressions.ExtensionExpression
 public void SetUp ()
 {
   _wrappedExpression = Expression.Constant (1);
   _aggregationEpression = new AggregationExpression(typeof(int), _wrappedExpression, AggregationModifier.Max);
 }
    public Expression VisitAggregationExpression (AggregationExpression expression)
    {
      Expression newInnerExpression;
      if (expression.AggregationModifier == AggregationModifier.Count)
        newInnerExpression = ApplyValueContext (expression.Expression);
      else
        newInnerExpression = ApplySingleValueContext (expression.Expression);

      if (newInnerExpression != expression.Expression)
        return new AggregationExpression (expression.Type, newInnerExpression, expression.AggregationModifier);

      return expression;
    }
    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 SetUp ()
    {
      _dataInfo = new StreamedScalarValueInfo (typeof (int));

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

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

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

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

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

      _groupAggregateSimplifier = new GroupAggregateSimplifier (_stageMock, _context);
    }
    public void BuildSelectPart_WithAggregation ()
    {
      var aggregationExpression = new AggregationExpression (typeof(int), _entityExpression,AggregationModifier.Count);
      var sqlStatement = new SqlStatementBuilder { SelectProjection = aggregationExpression, DataInfo = new TestStreamedValueInfo (typeof (object)) }.GetSqlStatement ();

      _stageMock.Expect (
          mock => mock.GenerateTextForSelectExpression (_commandBuilder, aggregationExpression))
          .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("COUNT(*)"));

      _generator.BuildSelectPart (sqlStatement, _commandBuilder, false);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("SELECT COUNT(*)"));
      _stageMock.VerifyAllExpectations();
    }
    public void SimplifyIfPossible_SimplifiableStatement_AddsAggregationAndReturnsReference ()
    {
      Assert.That (_associatedGroupingSelectExpression.AggregationExpressions.Count, Is.EqualTo (0));

      var expression = new SqlSubStatementExpression (_simplifiableResolvedSqlStatement);

      var preparedResolvedAggregate = new AggregationExpression (
          typeof (int), 
          new NamedExpression ("element", Expression.Constant ("e")), 
          AggregationModifier.Count);
      _stageMock
          .Expect (mock => mock.ResolveAggregationExpression(Arg<Expression>.Is.Anything, Arg.Is (_context)))
          .Return (preparedResolvedAggregate)
          .WhenCalled (mi => {
            var expectedReplacedAggregate = new AggregationExpression (
                typeof (int),
                ((NamedExpression) _associatedGroupingSelectExpression.ElementExpression).Expression, 
                AggregationModifier.Count);
            SqlExpressionTreeComparer.CheckAreEqualTrees (expectedReplacedAggregate, (Expression) mi.Arguments[0]);
          });
      _stageMock.Replay();

      var result = _groupAggregateSimplifier.SimplifyIfPossible (expression, _simplifiableUnresolvedProjection);

      _stageMock.VerifyAllExpectations();

      Assert.That (_associatedGroupingSelectExpression.AggregationExpressions.Count, Is.EqualTo (1));
      Assert.That (
          ((NamedExpression) _associatedGroupingSelectExpression.AggregationExpressions[0]).Expression, 
          Is.SameAs (preparedResolvedAggregate));

      var expected = new SqlColumnDefinitionExpression (typeof (int), "q0", "a0", false);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
    }
    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 VisitAggregationExpression_Average ()
    {
      var columnExpression = new NamedExpression (null, new SqlColumnDefinitionExpression (typeof (string), "c", "Name", false));
      var expression = new AggregationExpression (typeof (double), columnExpression, AggregationModifier.Average);

      SqlGeneratingExpressionVisitor.GenerateSql (expression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("AVG([c].[Name])"));
    }
    public void VisitAggregationExpression_Count ()
    {
      var columnExpression = new SqlColumnDefinitionExpression (typeof (string), "c", "Name", false);
      var expression = new AggregationExpression (typeof (int), columnExpression, AggregationModifier.Count);

      SqlGeneratingExpressionVisitor.GenerateSql (expression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("COUNT(*)"));
    }
    public void VisitAggregationExpression_ComplexValue_ThrowsWithOtherAggregations ()
    {
      var entity = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression ();
      var aggregationExpression = new AggregationExpression (typeof (int), entity, AggregationModifier.Sum);

      Assert.That (() => _valueRequiredVisitor.VisitExpression (aggregationExpression), Throws.TypeOf<NotSupportedException>());
    }
    public void VisitAggregationExpression_ComplexValue_AcceptedByCount ()
    {
      var entity = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression();
      var aggregationExpression = new AggregationExpression (typeof (int), entity, AggregationModifier.Count);

      var result = _singleValueRequiredVisitor.VisitExpression (aggregationExpression);

      Assert.That (result, Is.SameAs (aggregationExpression));
    }
    public void VisitAggregationExpression_AppliesValueContextToInnerExpression ()
    {
      var aggregationExpression = new AggregationExpression (typeof (int), Expression.Constant (true), AggregationModifier.Count);

      var result = _singleValueRequiredVisitor.VisitExpression (aggregationExpression);

      var expected = new AggregationExpression (typeof (int), new SqlConvertedBooleanExpression (Expression.Constant (1)), AggregationModifier.Count);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
    }
    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 DifferentAggregationModifier ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var selectProjectionWithCountAggregation = new AggregationExpression (typeof (int), selectProjection, AggregationModifier.Count);

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjectionWithCountAggregation,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.False);
    }