From: <ric...@us...> - 2009-11-19 12:31:29
|
Revision: 4837 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4837&view=rev Author: ricbrown Date: 2009-11-19 12:31:20 +0000 (Thu, 19 Nov 2009) Log Message: ----------- Added more Restrictions overloads for lambda expressions. Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Criterion/Restrictions.cs trunk/nhibernate/src/NHibernate/NHibernate.csproj trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaRestrictionBuilder.cs Added: trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaRestrictionBuilder.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaRestrictionBuilder.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaRestrictionBuilder.cs 2009-11-19 12:31:20 UTC (rev 4837) @@ -0,0 +1,123 @@ + +using System; +using System.Collections.Generic; +using System.Linq.Expressions; + +using NHibernate.Impl; +using NHibernate.SqlCommand; + +namespace NHibernate.Criterion.Lambda +{ + + public class LambdaRestrictionBuilder + { + public class LambdaBetweenBuilder + { + private string propertyName; + private object lo; + + public LambdaBetweenBuilder(string propertyName, object lo) + { + this.propertyName = propertyName; + this.lo = lo; + } + + public AbstractCriterion And(object hi) + { + return Restrictions.Between(propertyName, lo, hi); + } + } + + private string propertyName; + + /// <summary> + /// Constructed with property name + /// </summary> + public LambdaRestrictionBuilder(string propertyName) + { + this.propertyName = propertyName; + } + + /// <summary> + /// Apply a "between" constraint to the named property + /// </summary> + public LambdaBetweenBuilder IsBetween(object lo) + { + return new LambdaBetweenBuilder(propertyName, lo); + } + + /// <summary> + /// A case-insensitive "like", similar to Postgres "ilike" operator + /// </summary> + public AbstractCriterion IsInsensitiveLike(object value) + { + return Restrictions.InsensitiveLike(propertyName, value); + } + + /// <summary> + /// A case-insensitive "like", similar to Postgres "ilike" operator + /// </summary> + public AbstractCriterion IsInsensitiveLike(string value, MatchMode matchMode) + { + return Restrictions.InsensitiveLike(propertyName, value, matchMode); + } + + /// <summary> + /// Apply an "is empty" constraint to the named property + /// </summary> + public AbstractEmptinessExpression IsEmpty + { + get { return Restrictions.IsEmpty(propertyName); } + } + + /// <summary> + /// Apply a "not is empty" constraint to the named property + /// </summary> + public AbstractEmptinessExpression IsNotEmpty + { + get { return Restrictions.IsNotEmpty(propertyName); } + } + + /// <summary> + /// Apply an "is null" constraint to the named property + /// </summary> + public AbstractCriterion IsNull + { + get { return Restrictions.IsNull(propertyName); } + } + + /// <summary> + /// Apply an "not is null" constraint to the named property + /// </summary> + public AbstractCriterion IsNotNull + { + get { return Restrictions.IsNotNull(propertyName); } + } + + /// <summary> + /// Apply a "like" constraint to the named property + /// </summary> + public SimpleExpression IsLike(object value) + { + return Restrictions.Like(propertyName, value); + } + + /// <summary> + /// Apply a "like" constraint to the named property + /// </summary> + public SimpleExpression IsLike(string value, MatchMode matchMode) + { + return Restrictions.Like(propertyName, value, matchMode); + } + + /// <summary> + /// Apply a "like" constraint to the named property + /// </summary> + public AbstractCriterion IsLike(string value, MatchMode matchMode, char? escapeChar) + { + return Restrictions.Like(propertyName, value, matchMode, escapeChar); + } + + } + +} Modified: trunk/nhibernate/src/NHibernate/Criterion/Restrictions.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/Restrictions.cs 2009-11-19 11:15:09 UTC (rev 4836) +++ trunk/nhibernate/src/NHibernate/Criterion/Restrictions.cs 2009-11-19 12:31:20 UTC (rev 4837) @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq.Expressions; +using NHibernate.Criterion.Lambda; using NHibernate.Impl; namespace NHibernate.Criterion @@ -758,5 +759,27 @@ return criterion; } + /// <summary> + /// Build an ICriterion for the given property + /// </summary> + /// <param name="expression">lambda expression identifying property</param> + /// <returns>returns LambdaRestrictionBuilder</returns> + public static LambdaRestrictionBuilder WhereProperty<T>(Expression<Func<T, object>> expression) + { + string property = ExpressionProcessor.FindMemberExpression(expression.Body); + return new LambdaRestrictionBuilder(property); + } + + /// <summary> + /// Build an ICriterion for the given property + /// </summary> + /// <param name="expression">lambda expression identifying property</param> + /// <returns>returns LambdaRestrictionBuilder</returns> + public static LambdaRestrictionBuilder WhereProperty(Expression<Func<object>> expression) + { + string property = ExpressionProcessor.FindMemberExpression(expression.Body); + return new LambdaRestrictionBuilder(property); + } + } } Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj =================================================================== --- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2009-11-19 11:15:09 UTC (rev 4836) +++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2009-11-19 12:31:20 UTC (rev 4837) @@ -505,6 +505,7 @@ <Compile Include="Context\WcfOperationSessionContext.cs" /> <Compile Include="Criterion\GroupedProjection.cs" /> <Compile Include="Criterion\IPropertyProjection.cs" /> + <Compile Include="Criterion\Lambda\LambdaRestrictionBuilder.cs" /> <Compile Include="Criterion\Lambda\LambdaSubqueryBuilder.cs" /> <Compile Include="Criterion\Lambda\QueryOverFetchBuilder.cs" /> <Compile Include="Criterion\Lambda\QueryOverJoinBuilder.cs" /> Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2009-11-19 11:15:09 UTC (rev 4836) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2009-11-19 12:31:20 UTC (rev 4837) @@ -29,6 +29,41 @@ AssertCriteriaAreEqual(expected, actual); } + [Test] + public void SqlFunctions() + { + ICriteria expected = + CreateTestCriteria(typeof(Person), "personAlias") + .Add(Restrictions.Between("Age", 18, 65)) + .Add(Restrictions.Between("personAlias.Age", 18, 65)) + .Add(Restrictions.InsensitiveLike("Name", "test")) + .Add(Restrictions.InsensitiveLike("Name", "tEsT", MatchMode.Anywhere)) + .Add(Restrictions.IsEmpty("Children")) + .Add(Restrictions.IsNotEmpty("Children")) + .Add(Restrictions.IsNotNull("Name")) + .Add(Restrictions.IsNull("Name")) + .Add(Restrictions.Like("Name", "%test%")) + .Add(Restrictions.Like("Name", "test", MatchMode.Anywhere)) + .Add(Restrictions.Like("Name", "test", MatchMode.Anywhere, '?')); + + Person personAlias = null; + var actual = + CreateTestQueryOver<Person>(() => personAlias) + .Where(Restrictions.WhereProperty<Person>(p => p.Age).IsBetween(18).And(65)) + .And(Restrictions.WhereProperty(() => personAlias.Age).IsBetween(18).And(65)) + .And(Restrictions.WhereProperty<Person>(p => p.Name).IsInsensitiveLike("test")) + .And(Restrictions.WhereProperty<Person>(p => p.Name).IsInsensitiveLike("tEsT", MatchMode.Anywhere)) + .And(Restrictions.WhereProperty<Person>(p => p.Children).IsEmpty) + .And(Restrictions.WhereProperty<Person>(p => p.Children).IsNotEmpty) + .And(Restrictions.WhereProperty<Person>(p => p.Name).IsNotNull) + .And(Restrictions.WhereProperty<Person>(p => p.Name).IsNull) + .And(Restrictions.WhereProperty<Person>(p => p.Name).IsLike("%test%")) + .And(Restrictions.WhereProperty<Person>(p => p.Name).IsLike("test", MatchMode.Anywhere)) + .And(Restrictions.WhereProperty<Person>(p => p.Name).IsLike("test", MatchMode.Anywhere, '?')); + + 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. |