Holds an Expression that originally had bool type, but was converted to int because SQL doesn't know a boolean data type.
상속: Remotion.Linq.Clauses.Expressions.ExtensionExpression
 public void Initialization_NullableBoolean ()
 {
   var innerExpression = Expression.Constant (1, typeof (int?));
   var convertedExpression = new SqlConvertedBooleanExpression (innerExpression);
   Assert.That (convertedExpression.Type, Is.SameAs (typeof (bool?)));
   Assert.That (convertedExpression.Expression, Is.SameAs (innerExpression));
 }
    public void ApplyContext ()
    {
      var valueExpression = Expression.Constant (0);
      var predicateExpression = Expression.Constant (true);

      var convertedValue = SqlContextExpressionVisitor.ApplySqlExpressionContext (
          new SqlConvertedBooleanExpression (valueExpression), SqlExpressionContext.PredicateRequired, _stageMock, _mappingResolutionContext);
      var convertedPredicate = SqlContextExpressionVisitor.ApplySqlExpressionContext (
          predicateExpression, SqlExpressionContext.SingleValueRequired, _stageMock, _mappingResolutionContext);

      var expectedConvertedValue = Expression.Equal (valueExpression, new SqlLiteralExpression (1));
      var expectedConvertedPredicate = new SqlConvertedBooleanExpression (Expression.Constant (1));

      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedConvertedValue, convertedValue);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedConvertedPredicate, convertedPredicate);
    }
    public virtual Expression VisitSqlConvertedBooleanExpression (SqlConvertedBooleanExpression expression)
    {
      ArgumentUtility.CheckNotNull ("expression", expression);

      var namedExpression = expression.Expression as NamedExpression;
      if (namedExpression != null)
      {
        // Since ADO.NET returns bit columns as actual boolean values (not as integer values), we need to convert the NamedExpression back to be
        // of type bool/bool? instead of int/int?.
        var conversionToBool = GetBitConversionExpression (
            sourceType: namedExpression.Type, targetType: expression.Type, convertedExpression: namedExpression.Expression);

        var newNamedExpression = new NamedExpression (namedExpression.Name, conversionToBool);
        return VisitExpression (newNamedExpression);
      }
      else
      {
        VisitExpression (expression.Expression);
        return expression;
      }
    }
    public void VisitSqlConvertedBooleanExpression_WithAnInnerNamedExpression_NullableBool_EmitsBitConversion_AndUsesNullableBoolInProjection ()
    {
      var namedNullableIntExpression = new NamedExpression ("test", Expression.Constant (0, typeof (int?)));
      var sqlConvertedBooleanExpression = new SqlConvertedBooleanExpression (namedNullableIntExpression);

      _visitor.VisitSqlConvertedBooleanExpression (sqlConvertedBooleanExpression);

      var expectedProjection = GetExpectedProjectionForNamedExpression (_commandBuilder.InMemoryProjectionRowParameter, "test", 0, typeof (bool?));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedProjection, _commandBuilder.GetInMemoryProjectionBody());

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("CONVERT(BIT, @1) AS [test]"));
    }
    public void VisitNamedExpression_MovesNameIntoSqlConvertedBooleanExpression ()
    {
      var namedExpression = new NamedExpression ("Name", Expression.Constant (true));

      var result = _valueRequiredVisitor.VisitExpression (namedExpression);

      var expected = new SqlConvertedBooleanExpression (new NamedExpression ("Name", Expression.Constant (1)));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
    }
    public void VisitSqlConvertedBooleanExpression_WithAnInnerNamedExpression_ReturnsAdaptedNamedExpressionOfCorrectType ()
    {
      var sqlConvertedBooleanExpression = new SqlConvertedBooleanExpression (_namedIntExpression);

      var result = _visitor.VisitSqlConvertedBooleanExpression (sqlConvertedBooleanExpression);

      var expectedResult = new NamedExpression (_namedIntExpression.Name, new SqlConvertExpression (typeof (bool), _namedIntExpression.Expression));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public Expression VisitSqlConvertedBooleanExpression (SqlConvertedBooleanExpression expression)
    {
      ArgumentUtility.CheckNotNull ("expression", expression);

      var newInner = ApplyValueContext (expression.Expression);
      Assertion.DebugAssert (
          newInner == expression.Expression,
          "There is currently no visit method that would change an int-typed expression with ValueRequired.");

      // This condition cannot be true at the moment because there currently is no int-typed expression that would be changed by ValueRequired.
      //if (newInner != expression.Expression)
      //  return new ConvertedBooleanExpression (newInner);

      return expression;
    }
    public void VisitSqlCaseExpression_AppliesValueContext ()
    {
      var case1 = new SqlCaseExpression.CaseWhenPair (Expression.Constant (true), Expression.Constant (true));
      var case2 = new SqlCaseExpression.CaseWhenPair (Expression.Constant (false), Expression.Constant (false));
      var elseCase = Expression.Constant (true);
      var expression = new SqlCaseExpression (typeof (bool), new[] { case1, case2 }, elseCase);

      var result = _valueRequiredVisitor.VisitSqlCaseExpression (expression);

      var expectedCase1 = new SqlCaseExpression.CaseWhenPair (
          Expression.Equal (Expression.Constant (1), new SqlLiteralExpression (1)), new SqlConvertedBooleanExpression (Expression.Constant (1)));
      var expectedCase2 = new SqlCaseExpression.CaseWhenPair (
          Expression.Equal (Expression.Constant (0), new SqlLiteralExpression (1)), new SqlConvertedBooleanExpression (Expression.Constant (0)));
      var expectedElseCase = new SqlConvertedBooleanExpression (Expression.Constant (1));
      var expectedExpression = new SqlCaseExpression (typeof (bool), new[] { expectedCase1, expectedCase2 }, expectedElseCase);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
    }
    public void VisitSqlConvertedBooleanExpression_WithADifferentExpression_VisitsInnerExpressionAndReturnsInputExpression ()
    {
      var sqlConvertedBooleanExpression = new SqlConvertedBooleanExpression (new SqlLiteralExpression (0));

      var result = _visitor.VisitSqlConvertedBooleanExpression (sqlConvertedBooleanExpression);

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("0"));
      Assert.That (result, Is.SameAs (sqlConvertedBooleanExpression));
    }
    public void ResolveWhereExpression ()
    {
      var expression = Expression.Constant (true);
      var fakeResult = new SqlConvertedBooleanExpression (Expression.Constant (0));

      _resolverMock
          .Expect (mock => mock.ResolveConstantExpression (expression))
          .Return (fakeResult);
      _resolverMock
          .Expect (mock => mock.ResolveConstantExpression ((ConstantExpression) fakeResult.Expression))
          .Return (fakeResult.Expression);
      _resolverMock.Replay();

      var result = _stage.ResolveWhereExpression (expression, _mappingResolutionContext);

      _resolverMock.VerifyAllExpectations();

      Assert.That (((BinaryExpression) result).Left, Is.SameAs (fakeResult.Expression));
      Assert.That (((SqlLiteralExpression) ((BinaryExpression) result).Right).Value, Is.EqualTo (1));
    }
    public void VisitSqlColumnExpression_BoolColumn_ConvertedToIntColumn_IsPrimaryColumn_ValueRequired ()
    {
      var column = new SqlColumnDefinitionExpression (typeof (bool), "x", "y", true);

      var result = _valueRequiredVisitor.VisitSqlColumnExpression (column);

      var expectedExpression = new SqlConvertedBooleanExpression (new SqlColumnDefinitionExpression (typeof (int), "x", "y", true));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
    }
    public void VisitSqlColumnExpression_BoolColumn_ConvertedToIntColumn_PredicateRequired ()
    {
      var nullableColumn = new SqlColumnDefinitionExpression (typeof (bool?), "x", "y", false);
      var column = new SqlColumnDefinitionExpression (typeof (bool), "x", "y", false);

      var result = _predicateRequiredVisitor.VisitSqlColumnExpression (column);
      var nullableResult = _predicateRequiredVisitor.VisitSqlColumnExpression (nullableColumn);

      var expectedExpression = new SqlConvertedBooleanExpression (new SqlColumnDefinitionExpression (typeof (int), "x", "y", false));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
      var expectedNullableExpression = new SqlConvertedBooleanExpression (new SqlColumnDefinitionExpression (typeof (int?), "x", "y", false));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedNullableExpression, nullableResult);
    }
    public void VisitSqlConvertedBooleanExpression_InnerUnchanged ()
    {
      var converted = new SqlConvertedBooleanExpression (Expression.Constant (0));

      var result = _valueRequiredVisitor.VisitSqlConvertedBooleanExpression (converted);

      Assert.That (result, Is.SameAs (converted));
    }
    public void VisitConstantExpression_BoolConstants_PredicateRequired ()
    {
      var constantTrue = Expression.Constant (true);
      var constantFalse = Expression.Constant (false);
      var constantNullableTrue = Expression.Constant (true, typeof (bool?));
      var constantNullableFalse = Expression.Constant (false, typeof (bool?));
      var constantNullableNull = Expression.Constant (null, typeof (bool?));

      var resultTrue = _predicateRequiredVisitor.VisitConstantExpression (constantTrue);
      var resultFalse = _predicateRequiredVisitor.VisitConstantExpression (constantFalse);
      var resultNullableTrue = _predicateRequiredVisitor.VisitConstantExpression (constantNullableTrue);
      var resultNullableFalse = _predicateRequiredVisitor.VisitConstantExpression (constantNullableFalse);
      var resultNullableNull = _predicateRequiredVisitor.VisitConstantExpression (constantNullableNull);

      var expectedExpressionTrue = new SqlConvertedBooleanExpression (Expression.Constant (1));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpressionTrue, resultTrue);
      var expectedExpressionFalse = new SqlConvertedBooleanExpression (Expression.Constant (0));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpressionFalse, resultFalse);
      var expectedExpressionNullableTrue = new SqlConvertedBooleanExpression (Expression.Constant (1, typeof (int?)));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpressionNullableTrue, resultNullableTrue);
      var expectedExpressionNullableFalse = new SqlConvertedBooleanExpression (Expression.Constant (0, typeof (int?)));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpressionNullableFalse, resultNullableFalse);
      var expectedExpressionNullableNull = new SqlConvertedBooleanExpression (Expression.Constant (null, typeof (int?)));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpressionNullableNull, resultNullableNull);
    }
    public void VisitExpression_ConvertedInt_ToPredicate ()
    {
      var expression = new SqlConvertedBooleanExpression (new CustomExpression (typeof (int)));
      var nullableExpression = new SqlConvertedBooleanExpression (new CustomExpression (typeof (int?)));

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.PredicateRequired, _stageMock, _mappingResolutionContext);
      var result = visitor.VisitExpression (expression);
      var resultNullable = visitor.VisitExpression (nullableExpression);

      var expected = Expression.Equal (expression.Expression, new SqlLiteralExpression (1));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
      var expectedNullable = Expression.Equal (nullableExpression.Expression, new SqlLiteralExpression (1, true), true, null);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedNullable, resultNullable);
    }
    public void VisitExpression_ValueSemantics_LeavesExistingSqlConvertedBooleanExpression ()
    {
      var convertedBooleanExpression = new SqlConvertedBooleanExpression (Expression.Constant (1));

      var result = _valueRequiredVisitor.VisitExpression (convertedBooleanExpression);

      Assert.That (result, Is.SameAs (convertedBooleanExpression));
    }
    public void VisitExpression_SingleValueRequired_ConvertsBool_ToSingleValue ()
    {
      var expression = new CustomExpression (typeof (bool));
      var nullableExpression = new CustomExpression (typeof (bool?));

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.SingleValueRequired, _stageMock, _mappingResolutionContext);
      var result = visitor.VisitExpression (expression);
      var resultNullable = visitor.VisitExpression (nullableExpression);

      var expected = new SqlConvertedBooleanExpression (GetNonNullablePredicateAsValueExpression (expression));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
      var expectedNullable = new SqlConvertedBooleanExpression (GetNullablePredicateAsValueExpression (nullableExpression));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedNullable, resultNullable);
    }
    public void VisitSqlConvertedBooleanExpression_WithAnInnerNamedExpression_NullableBool_ReturnsAdaptedNamedExpressionOfCorrectType ()
    {
      var namedNullableIntExpression = new NamedExpression ("test", Expression.Constant (0, typeof (int?)));
      var sqlConvertedBooleanExpression = new SqlConvertedBooleanExpression (namedNullableIntExpression);

      var result = _visitor.VisitSqlConvertedBooleanExpression (sqlConvertedBooleanExpression);

      var expectedResult = new NamedExpression (
          namedNullableIntExpression.Name, new SqlConvertExpression (typeof (bool?), namedNullableIntExpression.Expression));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void VisitBinaryExpression_BinaryBoolExpression_Coalesce ()
    {
      // nullableBool ?? bool is converted not as ConvertedBool (nullableInt) ?? ConvertedBool (int), but as ConvertedBool (nullableInt ?? int).
      var expression = Expression.Coalesce (Expression.Constant (false, typeof (bool?)), Expression.Constant (true));
      var nullableExpression = Expression.Coalesce (Expression.Constant (false, typeof (bool?)), Expression.Constant (true, typeof (bool?)));

      var result = _valueRequiredVisitor.VisitBinaryExpression (expression);
      var nullableResult = _valueRequiredVisitor.VisitBinaryExpression (nullableExpression);
      var resultForPredicateSemantics = _predicateRequiredVisitor.VisitBinaryExpression (expression);

      var expectedExpression = new SqlConvertedBooleanExpression (Expression.Coalesce (
          Expression.Constant (0, typeof (int?)),
          Expression.Constant (1)));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
      var expectedNullableExpression = new SqlConvertedBooleanExpression (Expression.Coalesce (
          Expression.Constant (0, typeof (int?)),
          Expression.Constant (1, typeof (int?))));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedNullableExpression, nullableResult);
      var expectedResultForPredicateSemantics = new SqlConvertedBooleanExpression (Expression.Coalesce (
          Expression.Constant (0, typeof (int?)),
          Expression.Constant (1)));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResultForPredicateSemantics, resultForPredicateSemantics);
    }
    public void VisitSqlConvertedBooleanExpression_WithAnInnerNamedExpression_OmitsSqlConversion_ForColumns ()
    {
      var namedIntColumnExpression = new NamedExpression ("col", SqlStatementModelObjectMother.CreateSqlColumn (typeof (int)));
      var sqlConvertedBooleanExpression = new SqlConvertedBooleanExpression (namedIntColumnExpression);

       Assert.That (_visitor.ColumnPosition, Is.EqualTo (0));

      _visitor.VisitSqlConvertedBooleanExpression (sqlConvertedBooleanExpression);

      Assert.That (_visitor.ColumnPosition, Is.EqualTo (1));

      var expectedProjection = GetExpectedProjectionForNamedExpression (_commandBuilder.InMemoryProjectionRowParameter, "col", 0, typeof (bool));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedProjection, _commandBuilder.GetInMemoryProjectionBody());

      Assert.That (_commandBuilder.GetCommandText(), Is.EqualTo ("[t0].[column] AS [col]"));
    }
    public void Reduce_Nullable ()
    {
      var innerExpression = Expression.Constant (1, typeof (int?));
      var convertedExpression = new SqlConvertedBooleanExpression (innerExpression);

      var result = convertedExpression.Reduce ();

      var expectedResult = Expression.Convert (
          innerExpression, typeof (bool?), typeof (BooleanUtility).GetMethod ("ConvertNullableIntToNullableBool", new[] { typeof (int?) }));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void VisitSqlConvertedBooleanExpression ()
    {
      var expression = new SqlConvertedBooleanExpression (Expression.Constant (1));

      SqlGeneratingExpressionVisitor.GenerateSql (expression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText (), Is.EqualTo ("@1"));
      Assert.That (_commandBuilder.GetCommandParameters ()[0].Value, Is.EqualTo (1));
    }
    public void VisitBinaryExpression_BinaryBoolExpression_Coalesce_OptimizationForCoalesceToFalse ()
    {
      // With predicate semantics, nullableBool ?? false is optimized to be the same as Convert (nullableBool, typeof (bool)) because SQL handles 
      // NULL in a falsey way in predicates, and the generated SQL is nicer.
      // With value semantics, this is not optimized.

      var expression = Expression.Coalesce (Expression.Constant (true, typeof (bool?)), Expression.Constant (false));

      var resultForValueSemantics = _valueRequiredVisitor.VisitBinaryExpression (expression);
      var resultForPredicateSemantics = _predicateRequiredVisitor.VisitBinaryExpression (expression);

      var expectedExpressionForValueSemantics = new SqlConvertedBooleanExpression (Expression.Coalesce (
          Expression.Constant (1, typeof (int?)),
          Expression.Constant (0)));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpressionForValueSemantics, resultForValueSemantics);
      var expectedResultForPredicateSemantics =
          Expression.Convert (
              Expression.Equal (Expression.Constant (1, typeof (int?)), new SqlLiteralExpression (1, true), true, null),
              typeof (bool));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResultForPredicateSemantics, resultForPredicateSemantics);
    }
    public void ResolveSubStatementReferenceExpression_ResolvesChildren_ForUnknownExpression ()
    {
      var referencedExpression = new SqlConvertedBooleanExpression (new NamedExpression ("test", Expression.Constant (0)));
      var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement (referencedExpression);

      var tableInfo = new ResolvedSubStatementTableInfo ("q0", sqlStatement);
      var sqlTable = new SqlTable (tableInfo, JoinSemantics.Inner);

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

      var expectedResult = new SqlConvertedBooleanExpression (new SqlColumnDefinitionExpression (typeof (int), "q0", "test", false));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
    public void VisitUnaryExpression_SqlConvertedBooleanExpression ()
    {
      var convertToBoolExpression = Expression.Convert (Expression.Constant (true, typeof (bool?)), typeof (bool));
      var convertToNullableBoolExpression = Expression.Convert (Expression.Constant (true), typeof (bool?));

      var result = _valueRequiredVisitor.VisitUnaryExpression (convertToBoolExpression);
      var resultNullable = _valueRequiredVisitor.VisitUnaryExpression (convertToNullableBoolExpression);

      var expectedExpression = new SqlConvertedBooleanExpression (Expression.Convert (Expression.Constant (1, typeof (int?)), typeof (int)));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
      var expectedNullableExpression = new SqlConvertedBooleanExpression (Expression.Convert (Expression.Constant (1), typeof (int?)));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedNullableExpression, resultNullable);
    }
    public void Reduce_NonNullable ()
    {
      var innerExpression = Expression.Constant (1);
      var convertedExpression = new SqlConvertedBooleanExpression (innerExpression);

      var result = convertedExpression.Reduce();

      var expectedResult = Expression.Convert (innerExpression, typeof (bool), typeof (Convert).GetMethod ("ToBoolean", new[] { typeof (int) }));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedResult, result);
    }
 public void SetUp ()
 {
   _innerExpression = Expression.Constant (1);
   _sqlConvertedBooleanExpression = new SqlConvertedBooleanExpression (_innerExpression);
 }