|
From: <ric...@us...> - 2010-04-05 07:15:49
|
Revision: 4969
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4969&view=rev
Author: ricbrown
Date: 2010-04-05 07:15:43 +0000 (Mon, 05 Apr 2010)
Log Message:
-----------
Implement NH-2152 (QueryOver equality to null)
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs
trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/ExpressionProcessorFixture.cs
trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs
Modified: trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs 2010-04-05 07:14:54 UTC (rev 4968)
+++ trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs 2010-04-05 07:15:43 UTC (rev 4969)
@@ -292,6 +292,9 @@
object value = valueExpression.DynamicInvoke();
value = ConvertType(value, propertyType);
+ if (value == null)
+ return ProcessSimpleNullExpression(property, be.NodeType);
+
if (!_simpleExpressionCreators.ContainsKey(be.NodeType))
throw new Exception("Unhandled simple expression type: " + be.NodeType);
@@ -300,6 +303,18 @@
return criterion;
}
+ private static ICriterion ProcessSimpleNullExpression(string property, ExpressionType expressionType)
+ {
+ if (expressionType == ExpressionType.Equal)
+ return Restrictions.IsNull(property);
+
+ if (expressionType == ExpressionType.NotEqual)
+ return Restrictions.Not(
+ Restrictions.IsNull(property));
+
+ throw new Exception("Cannot supply null value to operator " + expressionType);
+ }
+
private static ICriterion ProcessMemberExpression(BinaryExpression be)
{
string leftProperty = FindMemberExpression(be.Left);
Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/ExpressionProcessorFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/ExpressionProcessorFixture.cs 2010-04-05 07:14:54 UTC (rev 4968)
+++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/ExpressionProcessorFixture.cs 2010-04-05 07:15:43 UTC (rev 4969)
@@ -61,8 +61,7 @@
{
Person person = new Person() { Name = null };
ICriterion criterion = ExpressionProcessor.ProcessExpression<Person>(p => p.Name == person.Name);
- SimpleExpression simpleExpression = (SimpleExpression)criterion;
- Assert.AreEqual(null, simpleExpression.Value);
+ Assert.That(criterion, Is.InstanceOfType<NullExpression>());
}
[Test]
Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2010-04-05 07:14:54 UTC (rev 4968)
+++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2010-04-05 07:15:43 UTC (rev 4969)
@@ -171,6 +171,56 @@
AssertCriteriaAreEqual(expected, actual);
}
+ [Test]
+ public void NullRestriction()
+ {
+ ICriteria expected =
+ CreateTestCriteria(typeof(Person), "personAlias")
+ .Add(Restrictions.IsNull("Name"))
+ .Add(Restrictions.IsNull("Name"))
+ .Add(Restrictions.IsNull("Name"))
+ .Add(Restrictions.IsNull("Father"))
+ .Add(Restrictions.IsNull("Father"))
+ .Add(Restrictions.IsNull("NullableGender"))
+ .Add(Restrictions.IsNull("NullableAge"))
+ .Add(Restrictions.IsNull("NullableIsParent"))
+ .Add(Restrictions.Not(Restrictions.IsNull("Name")))
+ .Add(Restrictions.Not(Restrictions.IsNull("Name")))
+ .Add(Restrictions.Not(Restrictions.IsNull("Name")))
+ .Add(Restrictions.Not(Restrictions.IsNull("Father")))
+ .Add(Restrictions.Not(Restrictions.IsNull("Father")))
+ .Add(Restrictions.Not(Restrictions.IsNull("NullableGender")))
+ .Add(Restrictions.Not(Restrictions.IsNull("NullableAge")))
+ .Add(Restrictions.Not(Restrictions.IsNull("NullableIsParent")))
+ .Add(Restrictions.IsNull("personAlias.Name"));
+
+ Person personAlias = null;
+ CustomPerson nullPerson = null;
+ Person.StaticName = null;
+ Person emptyPerson = new Person() { Name = null };
+ var actual =
+ CreateTestQueryOver<Person>(() => personAlias)
+ .Where(p => p.Name == null)
+ .Where(p => p.Name == Person.StaticName)
+ .Where(p => p.Name == emptyPerson.Name)
+ .Where(p => p.Father == null)
+ .Where(p => p.Father == nullPerson)
+ .Where(p => p.NullableGender == null)
+ .Where(p => p.NullableAge == null)
+ .Where(p => p.NullableIsParent == null)
+ .Where(p => p.Name != null)
+ .Where(p => p.Name != Person.StaticName)
+ .Where(p => p.Name != emptyPerson.Name)
+ .Where(p => p.Father != null)
+ .Where(p => p.Father != nullPerson)
+ .Where(p => p.NullableGender != null)
+ .Where(p => p.NullableAge != null)
+ .Where(p => p.NullableIsParent != null)
+ .Where(() => personAlias.Name == null);
+
+ AssertCriteriaAreEqual(expected, actual);
+ }
+
}
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|