Represents a SQL "a IN b" expression.
Inheritance: Remotion.Linq.Clauses.Expressions.ExtensionExpression
    public void SetUp ()
    {
      _leftExpression = Expression.Constant (1);
      _rightExpression = Expression.Constant (2);

      _expression = new SqlInExpression (_leftExpression, _rightExpression);
    }
    public void HandleResultOperator ()
    {
      var itemExpression = Expression.Constant (new Cook ());
      var resultOperator = new ContainsResultOperator (itemExpression);
      var sqlStatement = _sqlStatementBuilder.GetSqlStatement ();

      var preparedExpression = Expression.Constant (new Cook (), typeof (Cook));
      _stageMock.Expect (mock => mock.PrepareResultOperatorItemExpression (itemExpression, _context)).Return (preparedExpression);
      _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)));
      
      var expectedExpression = new SqlInExpression (preparedExpression, new SqlSubStatementExpression (sqlStatement));
      
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, _sqlStatementBuilder.SelectProjection);
    }
    public SqlInExpression ResolvePotentialEntityComparison (SqlInExpression inExpression)
    {
      ArgumentUtility.CheckNotNull ("inExpression", inExpression);

      var newLeft = ResolvePotentialEntity (inExpression.LeftExpression);
      var newRight = ResolvePotentialEntity (inExpression.RightExpression);

      if (newLeft != inExpression.LeftExpression || newRight != inExpression.RightExpression)
        return new SqlInExpression (newLeft, newRight);

      return inExpression;
    }
    public Expression VisitSqlInExpression (SqlInExpression expression)
    {
      var baseVisitedExpression = (SqlInExpression) VisitExtensionExpression (expression);

      var expressionWithSimplifiedEntities = _entityIdentityResolver.ResolvePotentialEntityComparison (baseVisitedExpression);

      if (expressionWithSimplifiedEntities != baseVisitedExpression)
        return VisitExpression (expressionWithSimplifiedEntities);

      return baseVisitedExpression;
    }
 public Expression VisitSqlInExpression (SqlInExpression expression)
 {
   try
   {
     return VisitChildrenWithGivenSemantics (expression, SqlExpressionContext.SingleValueRequired);
   }
   catch (NotSupportedException ex)
   {
     var message = string.Format (
         "The SQL 'IN' operator (originally probably a call to a 'Contains' method) requires a single value, so the following expression cannot "
         + "be translated to SQL: '{0}'.",
         FormattingExpressionTreeVisitor.Format (expression));
     throw new NotSupportedException (message, ex);
   }
 }
    public void VisitSqlInExpression_RevisitingTerminatesAfterInnerChanges_WithoutOuterChanges  ()
    {
      var left = Expression.Constant (0);
      var right = Expression.Constant (1);
      var inExpression = new SqlInExpression (left, right);

      var fakeResolvedLeft = new SqlLiteralExpression (2);
      _resolverMock.Expect (mock => mock.ResolveConstantExpression (left)).Return (fakeResolvedLeft);
      var fakeResolvedRight = new SqlLiteralExpression (3);
      _resolverMock.Expect (mock => mock.ResolveConstantExpression (right)).Return (fakeResolvedRight);

      _entityIdentityResolverMock
          .Expect (mock => mock.ResolvePotentialEntityComparison (Arg<SqlInExpression>.Is.Anything))
          .Return (null)
          .WhenCalled (mi => mi.ReturnValue = mi.Arguments[0]);

      // No revisiting

      var result = _visitor.VisitExpression (inExpression);

      _resolverMock.VerifyAllExpectations ();
      _entityIdentityResolverMock.VerifyAllExpectations ();

      Assert.That (result, Is.TypeOf<SqlInExpression> ());
      Assert.That (((SqlInExpression) result).LeftExpression, Is.SameAs (fakeResolvedLeft));
      Assert.That (((SqlInExpression) result).RightExpression, Is.SameAs (fakeResolvedRight));
    }
    public void VisitSqlInExpression ()
    {
      var left = Expression.Constant (0);
      var right = Expression.Constant (1);
      var inExpression = new SqlInExpression (left, right);

      var fakeResolvedLeft = new SqlLiteralExpression (2);
      _resolverMock.Expect (mock => mock.ResolveConstantExpression (left)).Return (fakeResolvedLeft);
      var fakeResolvedRight = new SqlLiteralExpression (3);
      _resolverMock.Expect (mock => mock.ResolveConstantExpression (right)).Return (fakeResolvedRight);

      var fakeResolvedInExpression = new SqlInExpression (Expression.Constant (4), Expression.Constant (5));
      _entityIdentityResolverMock
          .Expect (
              mock => mock.ResolvePotentialEntityComparison (
                  Arg<SqlInExpression>.Matches (e => e.LeftExpression == fakeResolvedLeft && e.RightExpression == fakeResolvedRight)))
          .Return (fakeResolvedInExpression);
      
      // Result is revisited
      _resolverMock
          .Expect (mock => mock.ResolveConstantExpression ((ConstantExpression) fakeResolvedInExpression.LeftExpression))
          .Return (fakeResolvedInExpression.LeftExpression);
      _resolverMock
          .Expect (mock => mock.ResolveConstantExpression ((ConstantExpression) fakeResolvedInExpression.RightExpression))
          .Return (fakeResolvedInExpression.RightExpression);
      _entityIdentityResolverMock
          .Expect (mock => mock.ResolvePotentialEntityComparison (fakeResolvedInExpression))
          .Return (fakeResolvedInExpression);

      var result = _visitor.VisitExpression (inExpression);

      _resolverMock.VerifyAllExpectations ();
      _entityIdentityResolverMock.VerifyAllExpectations ();

      Assert.That (result, Is.SameAs (fakeResolvedInExpression));
    }
    public void ResolvePotentialEntityComparison_SqlInExpression_NonEntities_ReturnsSameExpression ()
    {
      var sqlInExpression = new SqlInExpression (Expression.Constant (0), Expression.Constant (0));

      var result = _entityIdentityResolver.ResolvePotentialEntityComparison (sqlInExpression);

      Assert.That (result, Is.SameAs (sqlInExpression));
    }
    public void ResolvePotentialEntityComparison_SqlInExpression_EntitiesAreResolvedToIdentity_RightOnly ()
    {
      var sqlInExpression = new SqlInExpression (Expression.Constant (null, typeof (Cook)), _entityExpression);

      var result = _entityIdentityResolver.ResolvePotentialEntityComparison (sqlInExpression);

      var expected = new SqlInExpression (Expression.Constant (null, typeof (Cook)), _entityExpression.GetIdentityExpression ());
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
    }
    public void ResolvePotentialEntityComparison_SqlInExpression_ResolvesEntitiesToIdentity ()
    {
      var sqlInExpression = new SqlInExpression (_entityExpression, _entityConstantExpression);

      var result = _entityIdentityResolver.ResolvePotentialEntityComparison (sqlInExpression);

      var expected = new SqlInExpression (_entityExpression.GetIdentityExpression (), _entityConstantExpression.IdentityExpression);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
    }
    public void VisitSqlInExpression ()
    {
      var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatementWithCook();
      var sqlSubStatementExpression = new SqlSubStatementExpression (sqlStatement);
      var sqlInExpression = new SqlInExpression (Expression.Constant (1), sqlSubStatementExpression);

      _stageMock
          .Expect (
              mock =>
              mock.GenerateTextForSqlStatement (
                  Arg.Is (_commandBuilder), Arg<SqlStatement>.Is.Anything))
          .WhenCalled (mi => ((SqlCommandBuilder) mi.Arguments[0]).Append ("test"));
      _stageMock.Replay();

      SqlGeneratingExpressionVisitor.GenerateSql (
          sqlInExpression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("@1 IN (test)"));
      _stageMock.VerifyAllExpectations();
    }
    public void SqlInExpression_WithInvalidChildren ()
    {
      var newExpression = CreateNewExpression();
      var expression = new SqlInExpression (newExpression, Expression.Constant (new[] { 1, 2, 3 }));

      Assert.That (
          () => _predicateRequiredVisitor.VisitSqlInExpression (expression),
          Throws.TypeOf<NotSupportedException> ().With.Message.EqualTo (
              "The SQL 'IN' operator (originally probably a call to a 'Contains' method) requires a single value, so the following expression "
              + "cannot be translated to SQL: 'new TypeForNewExpression(0) IN value(System.Int32[])'."));
    }
    public void SqlInExpression ()
    {
      var expression = new SqlInExpression (Expression.Constant (true), Expression.Constant (true));
      var expectedResult = new SqlInExpression (new SqlConvertedBooleanExpression (Expression.Constant (1)), new SqlConvertedBooleanExpression (Expression.Constant (1)));

      var result = _predicateRequiredVisitor.VisitSqlInExpression (expression);

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }