Skip to content

Commit 70629f8

Browse files
committed
Added more Restrictions overloads for lambda expressions.
SVN: trunk@4837
1 parent baae36a commit 70629f8

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs

+35
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,41 @@ public void ArbitraryCriterion()
2929
AssertCriteriaAreEqual(expected, actual);
3030
}
3131

32+
[Test]
33+
public void SqlFunctions()
34+
{
35+
ICriteria expected =
36+
CreateTestCriteria(typeof(Person), "personAlias")
37+
.Add(Restrictions.Between("Age", 18, 65))
38+
.Add(Restrictions.Between("personAlias.Age", 18, 65))
39+
.Add(Restrictions.InsensitiveLike("Name", "test"))
40+
.Add(Restrictions.InsensitiveLike("Name", "tEsT", MatchMode.Anywhere))
41+
.Add(Restrictions.IsEmpty("Children"))
42+
.Add(Restrictions.IsNotEmpty("Children"))
43+
.Add(Restrictions.IsNotNull("Name"))
44+
.Add(Restrictions.IsNull("Name"))
45+
.Add(Restrictions.Like("Name", "%test%"))
46+
.Add(Restrictions.Like("Name", "test", MatchMode.Anywhere))
47+
.Add(Restrictions.Like("Name", "test", MatchMode.Anywhere, '?'));
48+
49+
Person personAlias = null;
50+
var actual =
51+
CreateTestQueryOver<Person>(() => personAlias)
52+
.Where(Restrictions.WhereProperty<Person>(p => p.Age).IsBetween(18).And(65))
53+
.And(Restrictions.WhereProperty(() => personAlias.Age).IsBetween(18).And(65))
54+
.And(Restrictions.WhereProperty<Person>(p => p.Name).IsInsensitiveLike("test"))
55+
.And(Restrictions.WhereProperty<Person>(p => p.Name).IsInsensitiveLike("tEsT", MatchMode.Anywhere))
56+
.And(Restrictions.WhereProperty<Person>(p => p.Children).IsEmpty)
57+
.And(Restrictions.WhereProperty<Person>(p => p.Children).IsNotEmpty)
58+
.And(Restrictions.WhereProperty<Person>(p => p.Name).IsNotNull)
59+
.And(Restrictions.WhereProperty<Person>(p => p.Name).IsNull)
60+
.And(Restrictions.WhereProperty<Person>(p => p.Name).IsLike("%test%"))
61+
.And(Restrictions.WhereProperty<Person>(p => p.Name).IsLike("test", MatchMode.Anywhere))
62+
.And(Restrictions.WhereProperty<Person>(p => p.Name).IsLike("test", MatchMode.Anywhere, '?'));
63+
64+
AssertCriteriaAreEqual(expected, actual);
65+
}
66+
3267
}
3368

3469
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq.Expressions;
5+
6+
using NHibernate.Impl;
7+
using NHibernate.SqlCommand;
8+
9+
namespace NHibernate.Criterion.Lambda
10+
{
11+
12+
public class LambdaRestrictionBuilder
13+
{
14+
public class LambdaBetweenBuilder
15+
{
16+
private string propertyName;
17+
private object lo;
18+
19+
public LambdaBetweenBuilder(string propertyName, object lo)
20+
{
21+
this.propertyName = propertyName;
22+
this.lo = lo;
23+
}
24+
25+
public AbstractCriterion And(object hi)
26+
{
27+
return Restrictions.Between(propertyName, lo, hi);
28+
}
29+
}
30+
31+
private string propertyName;
32+
33+
/// <summary>
34+
/// Constructed with property name
35+
/// </summary>
36+
public LambdaRestrictionBuilder(string propertyName)
37+
{
38+
this.propertyName = propertyName;
39+
}
40+
41+
/// <summary>
42+
/// Apply a "between" constraint to the named property
43+
/// </summary>
44+
public LambdaBetweenBuilder IsBetween(object lo)
45+
{
46+
return new LambdaBetweenBuilder(propertyName, lo);
47+
}
48+
49+
/// <summary>
50+
/// A case-insensitive "like", similar to Postgres "ilike" operator
51+
/// </summary>
52+
public AbstractCriterion IsInsensitiveLike(object value)
53+
{
54+
return Restrictions.InsensitiveLike(propertyName, value);
55+
}
56+
57+
/// <summary>
58+
/// A case-insensitive "like", similar to Postgres "ilike" operator
59+
/// </summary>
60+
public AbstractCriterion IsInsensitiveLike(string value, MatchMode matchMode)
61+
{
62+
return Restrictions.InsensitiveLike(propertyName, value, matchMode);
63+
}
64+
65+
/// <summary>
66+
/// Apply an "is empty" constraint to the named property
67+
/// </summary>
68+
public AbstractEmptinessExpression IsEmpty
69+
{
70+
get { return Restrictions.IsEmpty(propertyName); }
71+
}
72+
73+
/// <summary>
74+
/// Apply a "not is empty" constraint to the named property
75+
/// </summary>
76+
public AbstractEmptinessExpression IsNotEmpty
77+
{
78+
get { return Restrictions.IsNotEmpty(propertyName); }
79+
}
80+
81+
/// <summary>
82+
/// Apply an "is null" constraint to the named property
83+
/// </summary>
84+
public AbstractCriterion IsNull
85+
{
86+
get { return Restrictions.IsNull(propertyName); }
87+
}
88+
89+
/// <summary>
90+
/// Apply an "not is null" constraint to the named property
91+
/// </summary>
92+
public AbstractCriterion IsNotNull
93+
{
94+
get { return Restrictions.IsNotNull(propertyName); }
95+
}
96+
97+
/// <summary>
98+
/// Apply a "like" constraint to the named property
99+
/// </summary>
100+
public SimpleExpression IsLike(object value)
101+
{
102+
return Restrictions.Like(propertyName, value);
103+
}
104+
105+
/// <summary>
106+
/// Apply a "like" constraint to the named property
107+
/// </summary>
108+
public SimpleExpression IsLike(string value, MatchMode matchMode)
109+
{
110+
return Restrictions.Like(propertyName, value, matchMode);
111+
}
112+
113+
/// <summary>
114+
/// Apply a "like" constraint to the named property
115+
/// </summary>
116+
public AbstractCriterion IsLike(string value, MatchMode matchMode, char? escapeChar)
117+
{
118+
return Restrictions.Like(propertyName, value, matchMode, escapeChar);
119+
}
120+
121+
}
122+
123+
}

src/NHibernate/Criterion/Restrictions.cs

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq.Expressions;
5+
using NHibernate.Criterion.Lambda;
56
using NHibernate.Impl;
67

78
namespace NHibernate.Criterion
@@ -758,5 +759,27 @@ public static ICriterion Where(Expression<Func<bool>> expression)
758759
return criterion;
759760
}
760761

762+
/// <summary>
763+
/// Build an ICriterion for the given property
764+
/// </summary>
765+
/// <param name="expression">lambda expression identifying property</param>
766+
/// <returns>returns LambdaRestrictionBuilder</returns>
767+
public static LambdaRestrictionBuilder WhereProperty<T>(Expression<Func<T, object>> expression)
768+
{
769+
string property = ExpressionProcessor.FindMemberExpression(expression.Body);
770+
return new LambdaRestrictionBuilder(property);
771+
}
772+
773+
/// <summary>
774+
/// Build an ICriterion for the given property
775+
/// </summary>
776+
/// <param name="expression">lambda expression identifying property</param>
777+
/// <returns>returns LambdaRestrictionBuilder</returns>
778+
public static LambdaRestrictionBuilder WhereProperty(Expression<Func<object>> expression)
779+
{
780+
string property = ExpressionProcessor.FindMemberExpression(expression.Body);
781+
return new LambdaRestrictionBuilder(property);
782+
}
783+
761784
}
762785
}

src/NHibernate/NHibernate.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@
505505
<Compile Include="Context\WcfOperationSessionContext.cs" />
506506
<Compile Include="Criterion\GroupedProjection.cs" />
507507
<Compile Include="Criterion\IPropertyProjection.cs" />
508+
<Compile Include="Criterion\Lambda\LambdaRestrictionBuilder.cs" />
508509
<Compile Include="Criterion\Lambda\LambdaSubqueryBuilder.cs" />
509510
<Compile Include="Criterion\Lambda\QueryOverFetchBuilder.cs" />
510511
<Compile Include="Criterion\Lambda\QueryOverJoinBuilder.cs" />

0 commit comments

Comments
 (0)