Example #1
0
        protected override Expression VisitConditional(ConditionalExpression expression)
        {
            ArgumentUtility.CheckNotNull("expression", expression);

            return(SqlCaseExpression.CreateIfThenElse(
                       expression.Type, Visit(expression.Test), Visit(expression.IfTrue), Visit(expression.IfFalse)));
        }
        public Expression ResolveIDPropertyViaForeignKey(SqlEntityExpression originatingEntity, RelationEndPointDefinition foreignKeyEndPoint)
        {
            ArgumentUtility.CheckNotNull("originatingEntity", originatingEntity);
            ArgumentUtility.CheckNotNull("foreignKeyEndPoint", foreignKeyEndPoint);

            var foreignKeyStorageProperty = _rdbmsPersistenceModelProvider.GetStoragePropertyDefinition(foreignKeyEndPoint.PropertyDefinition);

            var fullObjectIDStoragePropertyDefinition = foreignKeyStorageProperty as ObjectIDStoragePropertyDefinition;

            if (fullObjectIDStoragePropertyDefinition != null)
            {
                var classIDExpression = ResolveStorageProperty(originatingEntity, fullObjectIDStoragePropertyDefinition.ClassIDProperty);
                var valueExpression   = ResolveStorageProperty(originatingEntity, fullObjectIDStoragePropertyDefinition.ValueProperty);
                return(CreateCompoundObjectIDExpression(classIDExpression, valueExpression));
            }

            var objectIDWithoutClassIDStoragePropertyDefinition = foreignKeyStorageProperty as ObjectIDWithoutClassIDStoragePropertyDefinition;

            if (objectIDWithoutClassIDStoragePropertyDefinition != null)
            {
                // If the foreign key has no ClassID because the related object's class is always the same one, we still need to provide a full ObjectID,
                // including ClassID; otherwise, access to the ClassID property wouldn't work later on. (I.e., where o.Customer.ID.ClassID == ...)
                // We'll therefore use a literal as the ClassID. However, if the value property is null, we must also make the ClassID null.
                var valueExpression   = ResolveStorageProperty(originatingEntity, objectIDWithoutClassIDStoragePropertyDefinition.ValueProperty);
                var classIDExpression = SqlCaseExpression.CreateIfThenElse(
                    typeof(string),
                    new SqlIsNotNullExpression(valueExpression),
                    new SqlLiteralExpression(objectIDWithoutClassIDStoragePropertyDefinition.ClassDefinition.ID),
                    Expression.Constant(null, typeof(string)));
                return(CreateCompoundObjectIDExpression(classIDExpression, valueExpression));
            }

            return(null);
        }
        public void CreateIfThenElse()
        {
            var result = SqlCaseExpression.CreateIfThenElse(typeof(int), _predicate1, _value1, _value2);

            Assert.That(result.Cases, Has.Count.EqualTo(1));
            Assert.That(result.Cases[0].When, Is.SameAs(_predicate1));
            Assert.That(result.Cases[0].Then, Is.SameAs(_value1));
            Assert.That(result.ElseCase, Is.SameAs(_value2));
        }
 private Expression CreateValueExpressionForPredicate(Expression predicate)
 {
     // If the predicate is nullable, we have three cases (true, false, null). Otherweise, we just have two cases.
     if (predicate.Type == typeof(bool?))
     {
         return(SqlCaseExpression.CreateIfThenElseNull(typeof(int?), predicate, new SqlLiteralExpression(1), new SqlLiteralExpression(0)));
     }
     else
     {
         return(SqlCaseExpression.CreateIfThenElse(typeof(int), predicate, new SqlLiteralExpression(1), new SqlLiteralExpression(0)));
     }
 }
Example #5
0
        public void VisitBinaryExpression_WithConditionalExpressionInBinaryExpression()
        {
            var leftExpression    = Expression.Constant("Name");
            var testPredicate     = Expression.Constant(true);
            var ifTrueExpression  = Expression.Constant("true");
            var ifFalseExpression = Expression.Constant("false");
            var rightExpression   = Expression.Condition(testPredicate, ifTrueExpression, ifFalseExpression);
            var binaryExpression  = Expression.Equal(leftExpression, rightExpression);

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

            var expectedResult = Expression.Equal(
                leftExpression, SqlCaseExpression.CreateIfThenElse(typeof(string), testPredicate, ifTrueExpression, ifFalseExpression));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result);
        }
Example #6
0
        public void VisitMemberExpression_WithInnerCoalesceExpression_RevisitsResult()
        {
            var left  = Expression.Constant("left");
            var right = Expression.Constant("right");
            var coalesceExpression = Expression.Coalesce(left, right);
            var memberExpression   = Expression.Property(coalesceExpression, "Length");

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

            var expectedResult = SqlCaseExpression.CreateIfThenElse(
                typeof(int),
                new SqlIsNotNullExpression(coalesceExpression.Left),
                new SqlLengthExpression(coalesceExpression.Left),
                new SqlLengthExpression(coalesceExpression.Right));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result);
        }
Example #7
0
        public void ResolveIDPropertyViaForeignKey_WithObjectIDWithoutClassID_ResolvesToCompoundWithFixedConditionalClassID()
        {
            var propertyDefinition      = CreatePropertyDefinitionAndAssociateWithClass(_classDefinition, "Customer", "Customer");
            var objectIDStorageProperty =
                new ObjectIDWithoutClassIDStoragePropertyDefinition(
                    SimpleStoragePropertyDefinitionObjectMother.CreateGuidStorageProperty("CustomerID"),
                    GetTypeDefinition(typeof(Customer)));

            var foreignKeyEndPointDefinition = new RelationEndPointDefinition(propertyDefinition, false);

            _rdbmsPersistenceModelProviderStub
            .Stub(stub => stub.GetStoragePropertyDefinition(foreignKeyEndPointDefinition.PropertyDefinition))
            .Return(objectIDStorageProperty);

            var originatingEntity = CreateEntityDefinition(typeof(Order), "o");

            var result = _storageSpecificExpressionResolver.ResolveIDPropertyViaForeignKey(originatingEntity, foreignKeyEndPointDefinition);

            var expectedValueColumn = originatingEntity.GetColumn(typeof(Guid), "CustomerID", false);
            var expected            = Expression.New(
                MemberInfoFromExpressionUtility.GetConstructor(() => new ObjectID("classID", "value")),
                new Expression[]
            {
                new NamedExpression(
                    "ClassID",
                    SqlCaseExpression.CreateIfThenElse(
                        typeof(string),
                        new SqlIsNotNullExpression(expectedValueColumn),
                        new SqlLiteralExpression("Customer"),
                        Expression.Constant(null, typeof(string)))),
                new NamedExpression("Value", Expression.Convert(expectedValueColumn, typeof(object)))
            },
                new[] { typeof(ObjectID).GetProperty("ClassID"), typeof(ObjectID).GetProperty("Value") });

            SqlExpressionTreeComparer.CheckAreEqualTrees(expected, result);
        }