From: <ric...@us...> - 2011-06-04 14:14:45
|
Revision: 5905 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5905&view=rev Author: ricbrown Date: 2011-06-04 14:14:38 +0000 (Sat, 04 Jun 2011) Log Message: ----------- NH-2683: Added QueryOver functions to property comparisons Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.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 2011-06-04 05:53:30 UTC (rev 5904) +++ trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs 2011-06-04 14:14:38 UTC (rev 5905) @@ -32,7 +32,7 @@ { private readonly static IDictionary<ExpressionType, Func<IProjection, object, ICriterion>> _simpleExpressionCreators = null; - private readonly static IDictionary<ExpressionType, Func<string, string, ICriterion>> _propertyExpressionCreators = null; + private readonly static IDictionary<ExpressionType, Func<IProjection, IProjection, ICriterion>> _propertyExpressionCreators = null; private readonly static IDictionary<LambdaSubqueryType, IDictionary<ExpressionType, Func<string, DetachedCriteria, AbstractCriterion>>> _subqueryExpressionCreatorTypes = null; private readonly static IDictionary<string, Func<MethodCallExpression, ICriterion>> _customMethodCallProcessors = null; private readonly static IDictionary<string, Func<MethodCallExpression, IProjection>> _customProjectionProcessors = null; @@ -47,7 +47,7 @@ _simpleExpressionCreators[ExpressionType.LessThan] = Lt; _simpleExpressionCreators[ExpressionType.LessThanOrEqual] = Le; - _propertyExpressionCreators = new Dictionary<ExpressionType, Func<string, string, ICriterion>>(); + _propertyExpressionCreators = new Dictionary<ExpressionType, Func<IProjection, IProjection, ICriterion>>(); _propertyExpressionCreators[ExpressionType.Equal] = Restrictions.EqProperty; _propertyExpressionCreators[ExpressionType.NotEqual] = Restrictions.NotEqProperty; _propertyExpressionCreators[ExpressionType.GreaterThan] = Restrictions.GtProperty; @@ -465,13 +465,13 @@ private static ICriterion ProcessMemberExpression(Expression left, Expression right, ExpressionType nodeType) { - string leftProperty = FindMemberExpression(left); - string rightProperty = FindMemberExpression(right); + IProjection leftProperty = FindMemberProjection(left); + IProjection rightProperty = FindMemberProjection(right); if (!_propertyExpressionCreators.ContainsKey(nodeType)) throw new Exception("Unhandled property expression type: " + nodeType); - Func<string, string, ICriterion> propertyExpressionCreator = _propertyExpressionCreators[nodeType]; + Func<IProjection, IProjection, ICriterion> propertyExpressionCreator = _propertyExpressionCreators[nodeType]; ICriterion criterion = propertyExpressionCreator(leftProperty, rightProperty); return criterion; } Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2011-06-04 05:53:30 UTC (rev 5904) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2011-06-04 14:14:38 UTC (rev 5905) @@ -664,6 +664,31 @@ } [Test] + public void FunctionsProperty() + { + using (ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + s.Save(new Person() { Name = "p1", BirthDate = new DateTime(2009, 08, 07) }); + s.Save(new Person() { Name = "p2", BirthDate = new DateTime(2008, 07, 07) }); + s.Save(new Person() { Name = "p3", BirthDate = new DateTime(2007, 06, 07) }); + + t.Commit(); + } + + using (ISession s = OpenSession()) + { + var persons = + s.QueryOver<Person>() + .Where(p => p.BirthDate.MonthPart() == p.BirthDate.DayPart()) + .List(); + + persons.Count.Should().Be(1); + persons[0].Name.Should().Be("p2"); + } + } + + [Test] public void FunctionsOrder() { using (ISession s = OpenSession()) Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs 2011-06-04 05:53:30 UTC (rev 5904) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs 2011-06-04 14:14:38 UTC (rev 5905) @@ -61,12 +61,12 @@ .Add(Restrictions.Ge(Projections.Property("Name"), "test name")) .Add(Restrictions.Lt(Projections.Property("Name"), "test name")) .Add(Restrictions.Le(Projections.Property("Name"), "test name")) - .Add(Restrictions.EqProperty("Name", "Name")) - .Add(Restrictions.Not(Restrictions.EqProperty("Name", "Name"))) - .Add(Restrictions.GtProperty("Name", "Name")) - .Add(Restrictions.GeProperty("Name", "Name")) - .Add(Restrictions.LtProperty("Name", "Name")) - .Add(Restrictions.LeProperty("Name", "Name")); + .Add(Restrictions.EqProperty(Projections.Property("Name"), Projections.Property("Name"))) + .Add(Restrictions.Not(Restrictions.EqProperty(Projections.Property("Name"), Projections.Property("Name")))) + .Add(Restrictions.GtProperty(Projections.Property("Name"), Projections.Property("Name"))) + .Add(Restrictions.GeProperty(Projections.Property("Name"), Projections.Property("Name"))) + .Add(Restrictions.LtProperty(Projections.Property("Name"), Projections.Property("Name"))) + .Add(Restrictions.LeProperty(Projections.Property("Name"), Projections.Property("Name"))); IQueryOver<Person> actual = CreateTestQueryOver<Person>() @@ -91,12 +91,12 @@ { ICriteria expected = CreateTestCriteria(typeof(Person)) - .Add(Restrictions.EqProperty("Age", "Height")) - .Add(Restrictions.NotEqProperty("Age", "Height")) - .Add(Restrictions.GtProperty("Age", "Height")) - .Add(Restrictions.GeProperty("Age", "Height")) - .Add(Restrictions.LtProperty("Age", "Height")) - .Add(Restrictions.LeProperty("Age", "Height")); + .Add(Restrictions.EqProperty(Projections.Property("Age"), Projections.Property("Height"))) + .Add(Restrictions.NotEqProperty(Projections.Property("Age"), Projections.Property("Height"))) + .Add(Restrictions.GtProperty(Projections.Property("Age"), Projections.Property("Height"))) + .Add(Restrictions.GeProperty(Projections.Property("Age"), Projections.Property("Height"))) + .Add(Restrictions.LtProperty(Projections.Property("Age"), Projections.Property("Height"))) + .Add(Restrictions.LeProperty(Projections.Property("Age"), Projections.Property("Height"))); IQueryOver<Person> actual = CreateTestQueryOver<Person>() Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2011-06-04 05:53:30 UTC (rev 5904) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2011-06-04 14:14:38 UTC (rev 5905) @@ -305,6 +305,22 @@ AssertCriteriaAreEqual(expected, actual); } + [Test] + public void FunctionExtensionsProperty() + { + ICriteria expected = + CreateTestCriteria(typeof(Person)) + .Add(Restrictions.EqProperty( + Projections.SqlFunction("month", NHibernateUtil.Int32, Projections.Property("BirthDate")), + Projections.SqlFunction("day", NHibernateUtil.Int32, Projections.Property("BirthDate")))); + + IQueryOver<Person> actual = + CreateTestQueryOver<Person>() + .Where(p => p.BirthDate.MonthPart() == p.BirthDate.DayPart()); + + 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. |