Skip to content

Commit 8cadb51

Browse files
committed
Added remaining Restrictions overloads for lambda expressions.
SVN: trunk@4838
1 parent 70629f8 commit 8cadb51

File tree

7 files changed

+178
-18
lines changed

7 files changed

+178
-18
lines changed

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

+56-15
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,31 @@ public void ArbitraryCriterion()
1818
ICriteria expected =
1919
CreateTestCriteria(typeof(Person), "personAlias")
2020
.Add(Restrictions.Lt("Age", 65))
21-
.Add(Restrictions.Ge("personAlias.Age", 18));
21+
.Add(Restrictions.Ge("personAlias.Age", 18))
22+
.Add(Restrictions.Not(Restrictions.Ge("Age", 65)))
23+
.Add(Restrictions.Not(Restrictions.Lt("personAlias.Age", 18)));
2224

2325
Person personAlias = null;
2426
var actual =
2527
CreateTestQueryOver<Person>(() => personAlias)
2628
.Where(Restrictions.Where<Person>(p => p.Age < 65))
27-
.And(Restrictions.Where(() => personAlias.Age >= 18));
29+
.And(Restrictions.Where(() => personAlias.Age >= 18))
30+
.And(Restrictions.WhereNot<Person>(p => p.Age >= 65))
31+
.And(Restrictions.WhereNot(() => personAlias.Age < 18));
2832

2933
AssertCriteriaAreEqual(expected, actual);
3034
}
3135

3236
[Test]
33-
public void SqlFunctions()
37+
public void SqlOperators()
3438
{
3539
ICriteria expected =
3640
CreateTestCriteria(typeof(Person), "personAlias")
3741
.Add(Restrictions.Between("Age", 18, 65))
3842
.Add(Restrictions.Between("personAlias.Age", 18, 65))
43+
.Add(Restrictions.In("Name", new string[] { "name1", "name2", "name3" }))
44+
.Add(Restrictions.In("Name", new ArrayList() { "name1", "name2", "name3" }))
45+
.Add(Restrictions.InG<int>("Age", new int[] { 1, 2, 3 }))
3946
.Add(Restrictions.InsensitiveLike("Name", "test"))
4047
.Add(Restrictions.InsensitiveLike("Name", "tEsT", MatchMode.Anywhere))
4148
.Add(Restrictions.IsEmpty("Children"))
@@ -44,22 +51,56 @@ public void SqlFunctions()
4451
.Add(Restrictions.IsNull("Name"))
4552
.Add(Restrictions.Like("Name", "%test%"))
4653
.Add(Restrictions.Like("Name", "test", MatchMode.Anywhere))
47-
.Add(Restrictions.Like("Name", "test", MatchMode.Anywhere, '?'));
54+
.Add(Restrictions.Like("Name", "test", MatchMode.Anywhere, '?'))
55+
.Add(Restrictions.NaturalId()
56+
.Set("Name", "my name")
57+
.Set("personAlias.Age", 18));
4858

4959
Person personAlias = null;
5060
var actual =
5161
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, '?'));
62+
.Where(Restrictions.On<Person>(p => p.Age).IsBetween(18).And(65))
63+
.And(Restrictions.On(() => personAlias.Age).IsBetween(18).And(65))
64+
.And(Restrictions.On<Person>(p => p.Name).IsIn(new string[] { "name1", "name2", "name3" }))
65+
.And(Restrictions.On<Person>(p => p.Name).IsIn(new ArrayList() { "name1", "name2", "name3" }))
66+
.And(Restrictions.On<Person>(p => p.Age).IsInG<int>(new int[] { 1, 2, 3 }))
67+
.And(Restrictions.On<Person>(p => p.Name).IsInsensitiveLike("test"))
68+
.And(Restrictions.On<Person>(p => p.Name).IsInsensitiveLike("tEsT", MatchMode.Anywhere))
69+
.And(Restrictions.On<Person>(p => p.Children).IsEmpty)
70+
.And(Restrictions.On<Person>(p => p.Children).IsNotEmpty)
71+
.And(Restrictions.On<Person>(p => p.Name).IsNotNull)
72+
.And(Restrictions.On<Person>(p => p.Name).IsNull)
73+
.And(Restrictions.On<Person>(p => p.Name).IsLike("%test%"))
74+
.And(Restrictions.On<Person>(p => p.Name).IsLike("test", MatchMode.Anywhere))
75+
.And(Restrictions.On<Person>(p => p.Name).IsLike("test", MatchMode.Anywhere, '?'))
76+
.And(Restrictions.NaturalId()
77+
.Set<Person>(p => p.Name).Is("my name")
78+
.Set(() => personAlias.Age).Is(18));
79+
80+
AssertCriteriaAreEqual(expected, actual);
81+
}
82+
83+
[Test]
84+
public void Junction()
85+
{
86+
ICriteria expected =
87+
CreateTestCriteria(typeof(Person), "personAlias")
88+
.Add(Restrictions.Conjunction()
89+
.Add(Restrictions.Eq("Name", "test"))
90+
.Add(Restrictions.Eq("personAlias.Name", "test")))
91+
.Add(Restrictions.Disjunction()
92+
.Add(Restrictions.Eq("Name", "test"))
93+
.Add(Restrictions.Eq("personAlias.Name", "test")));
94+
95+
Person personAlias = null;
96+
var actual =
97+
CreateTestQueryOver<Person>(() => personAlias)
98+
.Where(Restrictions.Conjunction()
99+
.Add<Person>(p => p.Name == "test")
100+
.Add(() => personAlias.Name == "test"))
101+
.And(Restrictions.Disjunction()
102+
.Add<Person>(p => p.Name == "test")
103+
.Add(() => personAlias.Name == "test"));
63104

64105
AssertCriteriaAreEqual(expected, actual);
65106
}

src/NHibernate/Criterion/Junction.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq.Expressions;
35
using NHibernate.Engine;
6+
using NHibernate.Impl;
47
using NHibernate.SqlCommand;
58
using NHibernate.Util;
6-
using System.Collections.Generic;
79

810
namespace NHibernate.Criterion
911
{
@@ -30,6 +32,28 @@ public Junction Add(ICriterion criterion)
3032
return this;
3133
}
3234

35+
/// <summary>
36+
/// Adds an <see cref="ICriterion"/> to the list of <see cref="ICriterion"/>s
37+
/// to junction together.
38+
/// </summary>
39+
public Junction Add<T>(Expression<Func<T, bool>> expression)
40+
{
41+
ICriterion criterion = ExpressionProcessor.ProcessExpression<T>(expression);
42+
criteria.Add(criterion);
43+
return this;
44+
}
45+
46+
/// <summary>
47+
/// Adds an <see cref="ICriterion"/> to the list of <see cref="ICriterion"/>s
48+
/// to junction together.
49+
/// </summary>
50+
public Junction Add(Expression<Func<bool>> expression)
51+
{
52+
ICriterion criterion = ExpressionProcessor.ProcessExpression(expression);
53+
criteria.Add(criterion);
54+
return this;
55+
}
56+
3357
/// <summary>
3458
/// Get the Sql operator to put between multiple <see cref="ICriterion"/>s.
3559
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Linq.Expressions;
6+
7+
using NHibernate.Impl;
8+
using NHibernate.SqlCommand;
9+
10+
namespace NHibernate.Criterion.Lambda
11+
{
12+
13+
public class LambdaNaturalIdentifierBuilder
14+
{
15+
private NaturalIdentifier naturalIdentifier;
16+
private string propertyName;
17+
18+
public LambdaNaturalIdentifierBuilder(NaturalIdentifier naturalIdentifier, string propertyName)
19+
{
20+
this.naturalIdentifier = naturalIdentifier;
21+
this.propertyName = propertyName;
22+
}
23+
24+
public NaturalIdentifier Is(object value)
25+
{
26+
return naturalIdentifier.Set(propertyName, value);
27+
}
28+
29+
}
30+
31+
}

src/NHibernate/Criterion/Lambda/LambdaRestrictionBuilder.cs

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using System;
3+
using System.Collections;
34
using System.Collections.Generic;
45
using System.Linq.Expressions;
56

@@ -46,6 +47,30 @@ public LambdaBetweenBuilder IsBetween(object lo)
4647
return new LambdaBetweenBuilder(propertyName, lo);
4748
}
4849

50+
/// <summary>
51+
/// Apply an "in" constraint to the named property
52+
/// </summary>
53+
public AbstractCriterion IsIn(ICollection values)
54+
{
55+
return Restrictions.In(propertyName, values);
56+
}
57+
58+
/// <summary>
59+
/// Apply an "in" constraint to the named property
60+
/// </summary>
61+
public AbstractCriterion IsIn(object[] values)
62+
{
63+
return Restrictions.In(propertyName, values);
64+
}
65+
66+
/// <summary>
67+
/// Apply an "in" constraint to the named property
68+
/// </summary>
69+
public AbstractCriterion IsInG<T>(ICollection<T> values)
70+
{
71+
return Restrictions.InG(propertyName, values);
72+
}
73+
4974
/// <summary>
5075
/// A case-insensitive "like", similar to Postgres "ilike" operator
5176
/// </summary>

src/NHibernate/Criterion/NaturalIdentifier.cs

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using NHibernate.Criterion.Lambda;
35
using NHibernate.Engine;
6+
using NHibernate.Impl;
47
using NHibernate.SqlCommand;
58

69
namespace NHibernate.Criterion
@@ -31,5 +34,17 @@ public NaturalIdentifier Set(string property, object value)
3134
conjunction.Add(Restrictions.Eq(property, value));
3235
return this;
3336
}
37+
38+
public LambdaNaturalIdentifierBuilder Set<T>(Expression<Func<T, object>> expression)
39+
{
40+
string property = ExpressionProcessor.FindMemberExpression(expression.Body);
41+
return new LambdaNaturalIdentifierBuilder(this, property);
42+
}
43+
44+
public LambdaNaturalIdentifierBuilder Set(Expression<Func<object>> expression)
45+
{
46+
string property = ExpressionProcessor.FindMemberExpression(expression.Body);
47+
return new LambdaNaturalIdentifierBuilder(this, property);
48+
}
3449
}
3550
}

src/NHibernate/Criterion/Restrictions.cs

+25-2
Original file line numberDiff line numberDiff line change
@@ -759,12 +759,35 @@ public static ICriterion Where(Expression<Func<bool>> expression)
759759
return criterion;
760760
}
761761

762+
/// <summary>
763+
/// Create an ICriterion for the negation of the supplied LambdaExpression
764+
/// </summary>
765+
/// <typeparam name="T">generic type</typeparam>
766+
/// <param name="expression">lambda expression</param>
767+
/// <returns>return NHibernate.Criterion.ICriterion</returns>
768+
public static ICriterion WhereNot<T>(Expression<Func<T, bool>> expression)
769+
{
770+
ICriterion criterion = ExpressionProcessor.ProcessExpression<T>(expression);
771+
return Restrictions.Not(criterion);
772+
}
773+
774+
/// <summary>
775+
/// Create an ICriterion for the negation of the supplied LambdaExpression
776+
/// </summary>
777+
/// <param name="expression">lambda expression</param>
778+
/// <returns>return NHibernate.Criterion.ICriterion</returns>
779+
public static ICriterion WhereNot(Expression<Func<bool>> expression)
780+
{
781+
ICriterion criterion = ExpressionProcessor.ProcessExpression(expression);
782+
return Restrictions.Not(criterion);
783+
}
784+
762785
/// <summary>
763786
/// Build an ICriterion for the given property
764787
/// </summary>
765788
/// <param name="expression">lambda expression identifying property</param>
766789
/// <returns>returns LambdaRestrictionBuilder</returns>
767-
public static LambdaRestrictionBuilder WhereProperty<T>(Expression<Func<T, object>> expression)
790+
public static LambdaRestrictionBuilder On<T>(Expression<Func<T, object>> expression)
768791
{
769792
string property = ExpressionProcessor.FindMemberExpression(expression.Body);
770793
return new LambdaRestrictionBuilder(property);
@@ -775,7 +798,7 @@ public static LambdaRestrictionBuilder WhereProperty<T>(Expression<Func<T, objec
775798
/// </summary>
776799
/// <param name="expression">lambda expression identifying property</param>
777800
/// <returns>returns LambdaRestrictionBuilder</returns>
778-
public static LambdaRestrictionBuilder WhereProperty(Expression<Func<object>> expression)
801+
public static LambdaRestrictionBuilder On(Expression<Func<object>> expression)
779802
{
780803
string property = ExpressionProcessor.FindMemberExpression(expression.Body);
781804
return new LambdaRestrictionBuilder(property);

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\LambdaNaturalIdentifierBuilder.cs" />
508509
<Compile Include="Criterion\Lambda\LambdaRestrictionBuilder.cs" />
509510
<Compile Include="Criterion\Lambda\LambdaSubqueryBuilder.cs" />
510511
<Compile Include="Criterion\Lambda\QueryOverFetchBuilder.cs" />

0 commit comments

Comments
 (0)