From: <ric...@us...> - 2011-05-15 13:04:09
|
Revision: 5817 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5817&view=rev Author: ricbrown Date: 2011-05-15 13:04:02 +0000 (Sun, 15 May 2011) Log Message: ----------- NH-2683: Started with single extension function .Year() Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Criterion/ProjectionsExtensions.cs trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Model.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs Modified: trunk/nhibernate/src/NHibernate/Criterion/ProjectionsExtensions.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/ProjectionsExtensions.cs 2011-05-15 13:00:22 UTC (rev 5816) +++ trunk/nhibernate/src/NHibernate/Criterion/ProjectionsExtensions.cs 2011-05-15 13:04:02 UTC (rev 5817) @@ -19,5 +19,20 @@ string aliasContainer = ExpressionProcessor.FindPropertyExpression(alias.Body); return Projections.Alias(projection, aliasContainer); } + + /// <summary> + /// Project SQL function year() + /// Note: throws an exception outside of a QueryOver expression + /// </summary> + public static int Year(this DateTime dateTimeProperty) + { + throw new Exception("Not to be used directly - use inside QueryOver expression"); + } + + internal static IProjection ProcessYear(MethodCallExpression methodCallExpression) + { + IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]); + return Projections.SqlFunction("year", NHibernateUtil.DateTime, property); + } } } Modified: trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs 2011-05-15 13:00:22 UTC (rev 5816) +++ trunk/nhibernate/src/NHibernate/Impl/ExpressionProcessor.cs 2011-05-15 13:04:02 UTC (rev 5817) @@ -31,14 +31,15 @@ public static class ExpressionProcessor { - private readonly static IDictionary<ExpressionType, Func<string, object, ICriterion>> _simpleExpressionCreators = null; + 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<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; static ExpressionProcessor() { - _simpleExpressionCreators = new Dictionary<ExpressionType, Func<string, object, ICriterion>>(); + _simpleExpressionCreators = new Dictionary<ExpressionType, Func<IProjection, object, ICriterion>>(); _simpleExpressionCreators[ExpressionType.Equal] = Eq; _simpleExpressionCreators[ExpressionType.NotEqual] = Ne; _simpleExpressionCreators[ExpressionType.GreaterThan] = Gt; @@ -86,36 +87,39 @@ RegisterCustomMethodCall(() => RestrictionExtensions.IsIn(null, new object[0]), RestrictionExtensions.ProcessIsInArray); RegisterCustomMethodCall(() => RestrictionExtensions.IsIn(null, new List<object>()), RestrictionExtensions.ProcessIsInCollection); RegisterCustomMethodCall(() => RestrictionExtensions.IsBetween(null, null).And(null), RestrictionExtensions.ProcessIsBetween); + + _customProjectionProcessors = new Dictionary<string, Func<MethodCallExpression, IProjection>>(); + RegisterCustomProjection(() => ProjectionsExtensions.Year(default(DateTime)), ProjectionsExtensions.ProcessYear); } - private static ICriterion Eq(string propertyName, object value) + private static ICriterion Eq(IProjection propertyName, object value) { return Restrictions.Eq(propertyName, value); } - private static ICriterion Ne(string propertyName, object value) + private static ICriterion Ne(IProjection propertyName, object value) { return NHibernate.Criterion.Restrictions.Not( NHibernate.Criterion.Restrictions.Eq(propertyName, value)); } - private static ICriterion Gt(string propertyName, object value) + private static ICriterion Gt(IProjection propertyName, object value) { return NHibernate.Criterion.Restrictions.Gt(propertyName, value); } - private static ICriterion Ge(string propertyName, object value) + private static ICriterion Ge(IProjection propertyName, object value) { return NHibernate.Criterion.Restrictions.Ge(propertyName, value); } - private static ICriterion Lt(string propertyName, object value) + private static ICriterion Lt(IProjection propertyName, object value) { return NHibernate.Criterion.Restrictions.Lt(propertyName, value); } - private static ICriterion Le(string propertyName, object value) + private static ICriterion Le(IProjection propertyName, object value) { return NHibernate.Criterion.Restrictions.Le(propertyName, value); } @@ -131,6 +135,23 @@ } /// <summary> + /// Retrieves the projection for the expression + /// </summary> + public static IProjection FindMemberProjection(Expression expression) + { + if (expression is MethodCallExpression) + { + MethodCallExpression methodCallExpression = (MethodCallExpression)expression; + + string signature = Signature(methodCallExpression.Method); + if (_customProjectionProcessors.ContainsKey(signature)) + return _customProjectionProcessors[signature](methodCallExpression); + } + + return Projections.Property(FindMemberExpression(expression)); + } + + /// <summary> /// Retrieves the name of the property from a member expression /// </summary> /// <param name="expression">An expression tree that can contain either a member, or a conversion from a member. @@ -245,7 +266,8 @@ if (expression is MethodCallExpression) { - return typeof(System.Type); + var methodCallExpression = (MethodCallExpression)expression; + return methodCallExpression.Method.ReturnType; } throw new Exception("Could not determine member type from " + expression.ToString()); @@ -320,7 +342,7 @@ private static ICriterion ProcessSimpleExpression(Expression left, Expression right, ExpressionType nodeType) { - string property = FindMemberExpression(left); + IProjection property = FindMemberProjection(left); System.Type propertyType = FindMemberType(left); object value = FindValue(right); @@ -332,7 +354,7 @@ if (!_simpleExpressionCreators.ContainsKey(nodeType)) throw new Exception("Unhandled simple expression type: " + nodeType); - Func<string, object, ICriterion> simpleExpressionCreator = _simpleExpressionCreators[nodeType]; + Func<IProjection, object, ICriterion> simpleExpressionCreator = _simpleExpressionCreators[nodeType]; ICriterion criterion = simpleExpressionCreator(property, value); return criterion; } @@ -347,7 +369,7 @@ return ProcessSimpleExpression(methodCall.Arguments[0], methodCall.Arguments[1], be.NodeType); } - private static ICriterion ProcessSimpleNullExpression(string property, ExpressionType expressionType) + private static ICriterion ProcessSimpleNullExpression(IProjection property, ExpressionType expressionType) { if (expressionType == ExpressionType.Equal) return Restrictions.IsNull(property); @@ -615,6 +637,18 @@ _customMethodCallProcessors.Add(signature, functionProcessor); } + /// <summary> + /// Register a custom projection for use in a QueryOver expression + /// </summary> + /// <param name="function">Lambda expression demonstrating call of custom method</param> + /// <param name="functionProcessor">function to convert MethodCallExpression to IProjection</param> + public static void RegisterCustomProjection<T>(Expression<Func<T>> function, Func<MethodCallExpression, IProjection> functionProcessor) + { + MethodCallExpression functionExpression = (MethodCallExpression)function.Body; + string signature = Signature(functionExpression.Method); + _customProjectionProcessors.Add(signature, functionProcessor); + } + } } Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Model.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Model.cs 2011-05-15 13:00:22 UTC (rev 5816) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Model.cs 2011-05-15 13:04:02 UTC (rev 5817) @@ -31,6 +31,7 @@ public virtual Person Father { get; set; } public virtual bool IsParent { get; set; } public virtual char Blood { get; set; } + public virtual DateTime BirthDate { get; set; } public virtual int? NullableAge { get; set; } public virtual PersonGender? NullableGender { get; set; } Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs 2011-05-15 13:00:22 UTC (rev 5816) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/QueryOverFixture.cs 2011-05-15 13:04:02 UTC (rev 5817) @@ -21,13 +21,13 @@ { ICriteria expected = CreateTestCriteria(typeof(Person)) - .Add(Restrictions.Eq("Name", "test name")) - .Add(Restrictions.Not(Restrictions.Eq("Name", "not test name"))) - .Add(Restrictions.Gt("Age", 10)) - .Add(Restrictions.Ge("Age", 11)) - .Add(Restrictions.Lt("Age", 50)) - .Add(Restrictions.Le("Age", 49)) - .Add(Restrictions.Eq("class", typeof(Person))) + .Add(Restrictions.Eq(Projections.Property("Name"), "test name")) + .Add(Restrictions.Not(Restrictions.Eq(Projections.Property("Name"), "not test name"))) + .Add(Restrictions.Gt(Projections.Property("Age"), 10)) + .Add(Restrictions.Ge(Projections.Property("Age"), 11)) + .Add(Restrictions.Lt(Projections.Property("Age"), 50)) + .Add(Restrictions.Le(Projections.Property("Age"), 49)) + .Add(Restrictions.Eq(Projections.Property("class"), typeof(Person))) .Add(Restrictions.Eq("class", typeof(Person).FullName)); IQueryOver<Person> actual = @@ -55,12 +55,12 @@ { ICriteria expected = CreateTestCriteria(typeof(Person)) - .Add(Restrictions.Eq("Name", "test name")) - .Add(Restrictions.Not(Restrictions.Eq("Name", "test name"))) - .Add(Restrictions.Gt("Name", "test name")) - .Add(Restrictions.Ge("Name", "test name")) - .Add(Restrictions.Lt("Name", "test name")) - .Add(Restrictions.Le("Name", "test name")) + .Add(Restrictions.Eq(Projections.Property("Name"), "test name")) + .Add(Restrictions.Not(Restrictions.Eq(Projections.Property("Name"), "test name"))) + .Add(Restrictions.Gt(Projections.Property("Name"), "test name")) + .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")) @@ -115,8 +115,8 @@ { ICriteria expected = CreateTestCriteria(typeof(Person)) - .Add(Restrictions.Eq("Blood", 'A')) - .Add(Restrictions.Not(Restrictions.Eq("Blood", 'B'))); + .Add(Restrictions.Eq(Projections.Property("Blood"), 'A')) + .Add(Restrictions.Not(Restrictions.Eq(Projections.Property("Blood"), 'B'))); IQueryOver<Person> actual = CreateTestQueryOver<Person>() @@ -132,9 +132,9 @@ ICriteria expected = CreateTestCriteria(typeof(Person)) .Add(Restrictions.And( - Restrictions.Eq("Name", "test name"), + Restrictions.Eq(Projections.Property("Name"), "test name"), Restrictions.Or( - Restrictions.Gt("Age", 21), + Restrictions.Gt(Projections.Property("Age"), 21), Restrictions.Eq("HasCar", true)))); IQueryOver<Person> actual = @@ -150,7 +150,7 @@ ICriteria expected = CreateTestCriteria(typeof(Person), "personAlias") .Add(Restrictions.Or( - Restrictions.Not(Restrictions.Eq("Name", "test name")), + Restrictions.Not(Restrictions.Eq(Projections.Property("Name"), "test name")), Restrictions.Not(Restrictions.Like("personAlias.Name", "%test%")))); Person personAlias = null; @@ -166,8 +166,8 @@ { ICriteria expected = CreateTestCriteria(typeof(Person), "personAlias") - .Add(Restrictions.Not(Restrictions.Eq("Name", "test name"))) - .Add(Restrictions.Not(Restrictions.Eq("personAlias.Name", "test name"))); + .Add(Restrictions.Not(Restrictions.Eq(Projections.Property("Name"), "test name"))) + .Add(Restrictions.Not(Restrictions.Eq(Projections.Property("personAlias.Name"), "test name"))); Person personAlias = null; IQueryOver<Person> actual = @@ -224,13 +224,13 @@ { ICriteria expected = CreateTestCriteria(typeof(Person), "personAlias") - .Add(Restrictions.Eq("personAlias.Name", "test name")) - .Add(Restrictions.Not(Restrictions.Eq("personAlias.Name", "not test name"))) - .Add(Restrictions.Gt("personAlias.Age", 10)) - .Add(Restrictions.Ge("personAlias.Age", 11)) - .Add(Restrictions.Lt("personAlias.Age", 50)) - .Add(Restrictions.Le("personAlias.Age", 49)) - .Add(Restrictions.Eq("personAlias.class", typeof(Person))) + .Add(Restrictions.Eq(Projections.Property("personAlias.Name"), "test name")) + .Add(Restrictions.Not(Restrictions.Eq(Projections.Property("personAlias.Name"), "not test name"))) + .Add(Restrictions.Gt(Projections.Property("personAlias.Age"), 10)) + .Add(Restrictions.Ge(Projections.Property("personAlias.Age"), 11)) + .Add(Restrictions.Lt(Projections.Property("personAlias.Age"), 50)) + .Add(Restrictions.Le(Projections.Property("personAlias.Age"), 49)) + .Add(Restrictions.Eq(Projections.Property("personAlias.class"), typeof(Person))) .Add(Restrictions.Eq("personAlias.class", typeof(Person).FullName)); Person personAlias = null; @@ -254,7 +254,7 @@ ICriteria expected = CreateTestCriteria(typeof(Person)) .CreateCriteria("Father") - .Add(Expression.Eq("Name", "test name")); + .Add(Expression.Eq(Projections.Property("Name"), "test name")); IQueryOver<Person> actual = CreateTestQueryOver<Person>() @@ -270,7 +270,7 @@ ICriteria expected = CreateTestCriteria(typeof(Person), "personAlias") .CreateCriteria("personAlias.Father") - .Add(Expression.Eq("Name", "test name")); + .Add(Expression.Eq(Projections.Property("Name"), "test name")); Person personAlias = null; IQueryOver<Person> actual = @@ -287,7 +287,7 @@ ICriteria expected = CreateTestCriteria(typeof(Person)) .CreateCriteria("Children") - .Add(Expression.Eq("Nickname", "test name")); + .Add(Expression.Eq(Projections.Property("Nickname"), "test name")); IQueryOver<Person> actual = CreateTestQueryOver<Person>() @@ -303,7 +303,7 @@ ICriteria expected = CreateTestCriteria(typeof(Person), "personAlias") .CreateCriteria("personAlias.Children", JoinType.InnerJoin) - .Add(Expression.Eq("Nickname", "test name")); + .Add(Expression.Eq(Projections.Property("Nickname"), "test name")); Person personAlias = null; IQueryOver<Person> actual = @@ -416,17 +416,17 @@ { ICriteria expected = CreateTestCriteria(typeof(Person)) - .CreateCriteria("PersonList", "alias1", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "many func t,bool")) - .CreateCriteria("PersonList", "alias2", JoinType.LeftOuterJoin, Restrictions.Eq("alias1.Name", "many func bool")) + .CreateCriteria("PersonList", "alias1", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "many func t,bool")) + .CreateCriteria("PersonList", "alias2", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias1.Name"), "many func bool")) .CreateCriteria("PersonList", "alias3", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "many private")) - .CreateCriteria("Father", "alias4", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "one func t,bool")) - .CreateCriteria("Father", "alias5", JoinType.LeftOuterJoin, Restrictions.Eq("alias4.Name", "one func bool")) + .CreateCriteria("Father", "alias4", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "one func t,bool")) + .CreateCriteria("Father", "alias5", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias4.Name"), "one func bool")) .CreateCriteria("Father", "alias6", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "one private")) - .CreateCriteria("alias1.PersonList", "alias7", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a many func t,bool")) - .CreateCriteria("alias2.PersonList", "alias8", JoinType.LeftOuterJoin, Restrictions.Eq("alias1.Name", "a many func bool")) + .CreateCriteria("alias1.PersonList", "alias7", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "a many func t,bool")) + .CreateCriteria("alias2.PersonList", "alias8", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias1.Name"), "a many func bool")) .CreateCriteria("alias3.PersonList", "alias9", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a many private")) - .CreateCriteria("alias4.Father", "alias10", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a one func t,bool")) - .CreateCriteria("alias5.Father", "alias11", JoinType.LeftOuterJoin, Restrictions.Eq("alias4.Name", "a one func bool")) + .CreateCriteria("alias4.Father", "alias10", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "a one func t,bool")) + .CreateCriteria("alias5.Father", "alias11", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias4.Name"), "a one func bool")) .CreateCriteria("alias6.Father", "alias12", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a one private")); Person alias1 = null; @@ -448,13 +448,13 @@ .Left.JoinQueryOver(p => p.PersonList, () => alias3, Restrictions.Eq("Name", "many private")) .Left.JoinQueryOver(p => p.Father, () => alias4, p => p.Name == "one func t,bool") .Left.JoinQueryOver(p => p.Father, () => alias5, () => alias4.Name == "one func bool") - .Left.JoinQueryOver(p => p.Father, () => alias6, p => p.Name == "one private") + .Left.JoinQueryOver(p => p.Father, () => alias6, Restrictions.Eq("Name", "one private")) .Left.JoinQueryOver(() => alias1.PersonList, () => alias7, p => p.Name == "a many func t,bool") .Left.JoinQueryOver(() => alias2.PersonList, () => alias8, () => alias1.Name == "a many func bool") .Left.JoinQueryOver(() => alias3.PersonList, () => alias9, Restrictions.Eq("Name", "a many private")) .Left.JoinQueryOver(() => alias4.Father, () => alias10, p => p.Name == "a one func t,bool") .Left.JoinQueryOver(() => alias5.Father, () => alias11, () => alias4.Name == "a one func bool") - .Left.JoinQueryOver(() => alias6.Father, () => alias12, p => p.Name == "a one private"); + .Left.JoinQueryOver(() => alias6.Father, () => alias12, Restrictions.Eq("Name", "a one private")); AssertCriteriaAreEqual(expected, actual); } @@ -464,17 +464,17 @@ { DetachedCriteria expected = DetachedCriteria.For<Person>() - .CreateCriteria("PersonList", "alias1", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "many func t,bool")) - .CreateCriteria("PersonList", "alias2", JoinType.LeftOuterJoin, Restrictions.Eq("alias1.Name", "many func bool")) + .CreateCriteria("PersonList", "alias1", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "many func t,bool")) + .CreateCriteria("PersonList", "alias2", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias1.Name"), "many func bool")) .CreateCriteria("PersonList", "alias3", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "many private")) - .CreateCriteria("Father", "alias4", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "one func t,bool")) - .CreateCriteria("Father", "alias5", JoinType.LeftOuterJoin, Restrictions.Eq("alias4.Name", "one func bool")) + .CreateCriteria("Father", "alias4", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "one func t,bool")) + .CreateCriteria("Father", "alias5", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias4.Name"), "one func bool")) .CreateCriteria("Father", "alias6", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "one private")) - .CreateCriteria("alias1.PersonList", "alias7", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a many func t,bool")) - .CreateCriteria("alias2.PersonList", "alias8", JoinType.LeftOuterJoin, Restrictions.Eq("alias1.Name", "a many func bool")) + .CreateCriteria("alias1.PersonList", "alias7", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "a many func t,bool")) + .CreateCriteria("alias2.PersonList", "alias8", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias1.Name"), "a many func bool")) .CreateCriteria("alias3.PersonList", "alias9", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a many private")) - .CreateCriteria("alias4.Father", "alias10", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a one func t,bool")) - .CreateCriteria("alias5.Father", "alias11", JoinType.LeftOuterJoin, Restrictions.Eq("alias4.Name", "a one func bool")) + .CreateCriteria("alias4.Father", "alias10", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "a one func t,bool")) + .CreateCriteria("alias5.Father", "alias11", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias4.Name"), "a one func bool")) .CreateCriteria("alias6.Father", "alias12", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a one private")); Person alias1 = null; @@ -496,13 +496,13 @@ .Left.JoinQueryOver(p => p.PersonList, () => alias3, Restrictions.Eq("Name", "many private")) .Left.JoinQueryOver(p => p.Father, () => alias4, p => p.Name == "one func t,bool") .Left.JoinQueryOver(p => p.Father, () => alias5, () => alias4.Name == "one func bool") - .Left.JoinQueryOver(p => p.Father, () => alias6, p => p.Name == "one private") + .Left.JoinQueryOver(p => p.Father, () => alias6, Restrictions.Eq("Name", "one private")) .Left.JoinQueryOver(() => alias1.PersonList, () => alias7, p => p.Name == "a many func t,bool") .Left.JoinQueryOver(() => alias2.PersonList, () => alias8, () => alias1.Name == "a many func bool") .Left.JoinQueryOver(() => alias3.PersonList, () => alias9, Restrictions.Eq("Name", "a many private")) .Left.JoinQueryOver(() => alias4.Father, () => alias10, p => p.Name == "a one func t,bool") .Left.JoinQueryOver(() => alias5.Father, () => alias11, () => alias4.Name == "a one func bool") - .Left.JoinQueryOver(() => alias6.Father, () => alias12, p => p.Name == "a one private"); + .Left.JoinQueryOver(() => alias6.Father, () => alias12, Restrictions.Eq("Name", "a one private")); AssertCriteriaAreEqual(expected, actual); } @@ -512,17 +512,17 @@ { ICriteria expected = CreateTestCriteria(typeof(Person)) - .CreateAlias("PersonList", "alias1", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "many func t,bool")) - .CreateAlias("PersonList", "alias2", JoinType.LeftOuterJoin, Restrictions.Eq("alias1.Name", "many func bool")) + .CreateAlias("PersonList", "alias1", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "many func t,bool")) + .CreateAlias("PersonList", "alias2", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias1.Name"), "many func bool")) .CreateAlias("PersonList", "alias3", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "many private")) - .CreateAlias("Father", "alias4", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "one func t,bool")) - .CreateAlias("Father", "alias5", JoinType.LeftOuterJoin, Restrictions.Eq("alias4.Name", "one func bool")) + .CreateAlias("Father", "alias4", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "one func t,bool")) + .CreateAlias("Father", "alias5", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias4.Name"), "one func bool")) .CreateAlias("Father", "alias6", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "one private")) - .CreateAlias("alias1.PersonList", "alias7", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a many func t,bool")) - .CreateAlias("alias2.PersonList", "alias8", JoinType.LeftOuterJoin, Restrictions.Eq("alias1.Name", "a many func bool")) + .CreateAlias("alias1.PersonList", "alias7", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "a many func t,bool")) + .CreateAlias("alias2.PersonList", "alias8", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias1.Name"), "a many func bool")) .CreateAlias("alias3.PersonList", "alias9", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a many private")) - .CreateAlias("alias4.Father", "alias10", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a one func t,bool")) - .CreateAlias("alias5.Father", "alias11", JoinType.LeftOuterJoin, Restrictions.Eq("alias4.Name", "a one func bool")) + .CreateAlias("alias4.Father", "alias10", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "a one func t,bool")) + .CreateAlias("alias5.Father", "alias11", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias4.Name"), "a one func bool")) .CreateAlias("alias6.Father", "alias12", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a one private")); Person alias1 = null; @@ -544,13 +544,13 @@ .Left.JoinAlias(p => p.PersonList, () => alias3, Restrictions.Eq("Name", "many private")) .Left.JoinAlias(p => p.Father, () => alias4, p => p.Name == "one func t,bool") .Left.JoinAlias(p => p.Father, () => alias5, () => alias4.Name == "one func bool") - .Left.JoinAlias(p => p.Father, () => alias6, p => p.Name == "one private") + .Left.JoinAlias(p => p.Father, () => alias6, Restrictions.Eq("Name", "one private")) .Left.JoinAlias(() => alias1.PersonList, () => alias7, p => p.Name == "a many func t,bool") .Left.JoinAlias(() => alias2.PersonList, () => alias8, () => alias1.Name == "a many func bool") .Left.JoinAlias(() => alias3.PersonList, () => alias9, Restrictions.Eq("Name", "a many private")) .Left.JoinAlias(() => alias4.Father, () => alias10, p => p.Name == "a one func t,bool") .Left.JoinAlias(() => alias5.Father, () => alias11, () => alias4.Name == "a one func bool") - .Left.JoinAlias(() => alias6.Father, () => alias12, p => p.Name == "a one private"); + .Left.JoinAlias(() => alias6.Father, () => alias12, Restrictions.Eq("Name", "a one private")); AssertCriteriaAreEqual(expected, actual); } @@ -560,17 +560,17 @@ { DetachedCriteria expected = DetachedCriteria.For<Person>() - .CreateAlias("PersonList", "alias1", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "many func t,bool")) - .CreateAlias("PersonList", "alias2", JoinType.LeftOuterJoin, Restrictions.Eq("alias1.Name", "many func bool")) + .CreateAlias("PersonList", "alias1", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "many func t,bool")) + .CreateAlias("PersonList", "alias2", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias1.Name"), "many func bool")) .CreateAlias("PersonList", "alias3", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "many private")) - .CreateAlias("Father", "alias4", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "one func t,bool")) - .CreateAlias("Father", "alias5", JoinType.LeftOuterJoin, Restrictions.Eq("alias4.Name", "one func bool")) + .CreateAlias("Father", "alias4", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "one func t,bool")) + .CreateAlias("Father", "alias5", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias4.Name"), "one func bool")) .CreateAlias("Father", "alias6", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "one private")) - .CreateAlias("alias1.PersonList", "alias7", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a many func t,bool")) - .CreateAlias("alias2.PersonList", "alias8", JoinType.LeftOuterJoin, Restrictions.Eq("alias1.Name", "a many func bool")) + .CreateAlias("alias1.PersonList", "alias7", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "a many func t,bool")) + .CreateAlias("alias2.PersonList", "alias8", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias1.Name"), "a many func bool")) .CreateAlias("alias3.PersonList", "alias9", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a many private")) - .CreateAlias("alias4.Father", "alias10", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a one func t,bool")) - .CreateAlias("alias5.Father", "alias11", JoinType.LeftOuterJoin, Restrictions.Eq("alias4.Name", "a one func bool")) + .CreateAlias("alias4.Father", "alias10", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("Name"), "a one func t,bool")) + .CreateAlias("alias5.Father", "alias11", JoinType.LeftOuterJoin, Restrictions.Eq(Projections.Property("alias4.Name"), "a one func bool")) .CreateAlias("alias6.Father", "alias12", JoinType.LeftOuterJoin, Restrictions.Eq("Name", "a one private")); Person alias1 = null; @@ -592,13 +592,13 @@ .Left.JoinAlias(p => p.PersonList, () => alias3, Restrictions.Eq("Name", "many private")) .Left.JoinAlias(p => p.Father, () => alias4, p => p.Name == "one func t,bool") .Left.JoinAlias(p => p.Father, () => alias5, () => alias4.Name == "one func bool") - .Left.JoinAlias(p => p.Father, () => alias6, p => p.Name == "one private") + .Left.JoinAlias(p => p.Father, () => alias6, Restrictions.Eq("Name", "one private")) .Left.JoinAlias(() => alias1.PersonList, () => alias7, p => p.Name == "a many func t,bool") .Left.JoinAlias(() => alias2.PersonList, () => alias8, () => alias1.Name == "a many func bool") .Left.JoinAlias(() => alias3.PersonList, () => alias9, Restrictions.Eq("Name", "a many private")) .Left.JoinAlias(() => alias4.Father, () => alias10, p => p.Name == "a one func t,bool") .Left.JoinAlias(() => alias5.Father, () => alias11, () => alias4.Name == "a one func bool") - .Left.JoinAlias(() => alias6.Father, () => alias12, p => p.Name == "a one private"); + .Left.JoinAlias(() => alias6.Father, () => alias12, Restrictions.Eq("Name", "a one private")); AssertCriteriaAreEqual(expected, actual); } @@ -781,7 +781,7 @@ { DetachedCriteria expected = DetachedCriteria.For<Person>("personAlias") - .Add(Restrictions.Eq("personAlias.Name", "test name")); + .Add(Restrictions.Eq(Projections.Property("personAlias.Name"), "test name")); Person personAlias = null; QueryOver<Person> actual = Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2011-05-15 13:00:22 UTC (rev 5816) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2011-05-15 13:04:02 UTC (rev 5817) @@ -17,10 +17,10 @@ { ICriteria expected = CreateTestCriteria(typeof(Person), "personAlias") - .Add(Restrictions.Lt("Age", 65)) - .Add(Restrictions.Ge("personAlias.Age", 18)) - .Add(Restrictions.Not(Restrictions.Ge("Age", 65))) - .Add(Restrictions.Not(Restrictions.Lt("personAlias.Age", 18))); + .Add(Restrictions.Lt(Projections.Property("Age"), 65)) + .Add(Restrictions.Ge(Projections.Property("personAlias.Age"), 18)) + .Add(Restrictions.Not(Restrictions.Ge(Projections.Property("Age"), 65))) + .Add(Restrictions.Not(Restrictions.Lt(Projections.Property("personAlias.Age"), 18))); Person personAlias = null; var actual = @@ -92,11 +92,11 @@ ICriteria expected = CreateTestCriteria(typeof(Person), "personAlias") .Add(Restrictions.Conjunction() - .Add(Restrictions.Eq("Name", "test")) - .Add(Restrictions.Eq("personAlias.Name", "test"))) + .Add(Restrictions.Eq(Projections.Property("Name"), "test")) + .Add(Restrictions.Eq(Projections.Property("personAlias.Name"), "test"))) .Add(Restrictions.Disjunction() - .Add(Restrictions.Eq("Name", "test")) - .Add(Restrictions.Eq("personAlias.Name", "test"))); + .Add(Restrictions.Eq(Projections.Property("Name"), "test")) + .Add(Restrictions.Eq(Projections.Property("personAlias.Name"), "test"))); Person personAlias = null; var actual = @@ -182,23 +182,23 @@ { 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")); + .Add(Restrictions.IsNull(Projections.Property("Name"))) + .Add(Restrictions.IsNull(Projections.Property("Name"))) + .Add(Restrictions.IsNull(Projections.Property("Name"))) + .Add(Restrictions.IsNull(Projections.Property("Father"))) + .Add(Restrictions.IsNull(Projections.Property("Father"))) + .Add(Restrictions.IsNull(Projections.Property("NullableGender"))) + .Add(Restrictions.IsNull(Projections.Property("NullableAge"))) + .Add(Restrictions.IsNull(Projections.Property("NullableIsParent"))) + .Add(Restrictions.Not(Restrictions.IsNull(Projections.Property("Name")))) + .Add(Restrictions.Not(Restrictions.IsNull(Projections.Property("Name")))) + .Add(Restrictions.Not(Restrictions.IsNull(Projections.Property("Name")))) + .Add(Restrictions.Not(Restrictions.IsNull(Projections.Property("Father")))) + .Add(Restrictions.Not(Restrictions.IsNull(Projections.Property("Father")))) + .Add(Restrictions.Not(Restrictions.IsNull(Projections.Property("NullableGender")))) + .Add(Restrictions.Not(Restrictions.IsNull(Projections.Property("NullableAge")))) + .Add(Restrictions.Not(Restrictions.IsNull(Projections.Property("NullableIsParent")))) + .Add(Restrictions.IsNull(Projections.Property("personAlias.Name"))); Person personAlias = null; CustomPerson nullPerson = null; @@ -255,6 +255,20 @@ AssertCriteriaAreEqual(expected, actual); } + [Test] + public void FunctionExtensions() + { + ICriteria expected = + CreateTestCriteria(typeof(Person)) + .Add(Restrictions.Eq(Projections.SqlFunction("year", NHibernateUtil.DateTime, Projections.Property("BirthDate")), 1970)); + + IQueryOver<Person> actual = + CreateTestQueryOver<Person>() + .Where(p => p.BirthDate.Year() == 1970); + + 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. |