|
From: <ric...@us...> - 2009-11-19 14:46:25
|
Revision: 4838
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4838&view=rev
Author: ricbrown
Date: 2009-11-19 14:46:12 +0000 (Thu, 19 Nov 2009)
Log Message:
-----------
Added remaining Restrictions overloads for lambda expressions.
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Criterion/Junction.cs
trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaRestrictionBuilder.cs
trunk/nhibernate/src/NHibernate/Criterion/NaturalIdentifier.cs
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/LambdaNaturalIdentifierBuilder.cs
Modified: trunk/nhibernate/src/NHibernate/Criterion/Junction.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/Junction.cs 2009-11-19 12:31:20 UTC (rev 4837)
+++ trunk/nhibernate/src/NHibernate/Criterion/Junction.cs 2009-11-19 14:46:12 UTC (rev 4838)
@@ -1,9 +1,11 @@
using System;
using System.Collections;
+using System.Collections.Generic;
+using System.Linq.Expressions;
using NHibernate.Engine;
+using NHibernate.Impl;
using NHibernate.SqlCommand;
using NHibernate.Util;
-using System.Collections.Generic;
namespace NHibernate.Criterion
{
@@ -31,6 +33,28 @@
}
/// <summary>
+ /// Adds an <see cref="ICriterion"/> to the list of <see cref="ICriterion"/>s
+ /// to junction together.
+ /// </summary>
+ public Junction Add<T>(Expression<Func<T, bool>> expression)
+ {
+ ICriterion criterion = ExpressionProcessor.ProcessExpression<T>(expression);
+ criteria.Add(criterion);
+ return this;
+ }
+
+ /// <summary>
+ /// Adds an <see cref="ICriterion"/> to the list of <see cref="ICriterion"/>s
+ /// to junction together.
+ /// </summary>
+ public Junction Add(Expression<Func<bool>> expression)
+ {
+ ICriterion criterion = ExpressionProcessor.ProcessExpression(expression);
+ criteria.Add(criterion);
+ return this;
+ }
+
+ /// <summary>
/// Get the Sql operator to put between multiple <see cref="ICriterion"/>s.
/// </summary>
protected abstract String Op { get; }
Added: trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaNaturalIdentifierBuilder.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaNaturalIdentifierBuilder.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaNaturalIdentifierBuilder.cs 2009-11-19 14:46:12 UTC (rev 4838)
@@ -0,0 +1,31 @@
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq.Expressions;
+
+using NHibernate.Impl;
+using NHibernate.SqlCommand;
+
+namespace NHibernate.Criterion.Lambda
+{
+
+ public class LambdaNaturalIdentifierBuilder
+ {
+ private NaturalIdentifier naturalIdentifier;
+ private string propertyName;
+
+ public LambdaNaturalIdentifierBuilder(NaturalIdentifier naturalIdentifier, string propertyName)
+ {
+ this.naturalIdentifier = naturalIdentifier;
+ this.propertyName = propertyName;
+ }
+
+ public NaturalIdentifier Is(object value)
+ {
+ return naturalIdentifier.Set(propertyName, value);
+ }
+
+ }
+
+}
Modified: trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaRestrictionBuilder.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaRestrictionBuilder.cs 2009-11-19 12:31:20 UTC (rev 4837)
+++ trunk/nhibernate/src/NHibernate/Criterion/Lambda/LambdaRestrictionBuilder.cs 2009-11-19 14:46:12 UTC (rev 4838)
@@ -1,5 +1,6 @@
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
@@ -47,6 +48,30 @@
}
/// <summary>
+ /// Apply an "in" constraint to the named property
+ /// </summary>
+ public AbstractCriterion IsIn(ICollection values)
+ {
+ return Restrictions.In(propertyName, values);
+ }
+
+ /// <summary>
+ /// Apply an "in" constraint to the named property
+ /// </summary>
+ public AbstractCriterion IsIn(object[] values)
+ {
+ return Restrictions.In(propertyName, values);
+ }
+
+ /// <summary>
+ /// Apply an "in" constraint to the named property
+ /// </summary>
+ public AbstractCriterion IsInG<T>(ICollection<T> values)
+ {
+ return Restrictions.InG(propertyName, values);
+ }
+
+ /// <summary>
/// A case-insensitive "like", similar to Postgres "ilike" operator
/// </summary>
public AbstractCriterion IsInsensitiveLike(object value)
Modified: trunk/nhibernate/src/NHibernate/Criterion/NaturalIdentifier.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/NaturalIdentifier.cs 2009-11-19 12:31:20 UTC (rev 4837)
+++ trunk/nhibernate/src/NHibernate/Criterion/NaturalIdentifier.cs 2009-11-19 14:46:12 UTC (rev 4838)
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
+using System.Linq.Expressions;
+using NHibernate.Criterion.Lambda;
using NHibernate.Engine;
+using NHibernate.Impl;
using NHibernate.SqlCommand;
namespace NHibernate.Criterion
@@ -31,5 +34,17 @@
conjunction.Add(Restrictions.Eq(property, value));
return this;
}
+
+ public LambdaNaturalIdentifierBuilder Set<T>(Expression<Func<T, object>> expression)
+ {
+ string property = ExpressionProcessor.FindMemberExpression(expression.Body);
+ return new LambdaNaturalIdentifierBuilder(this, property);
+ }
+
+ public LambdaNaturalIdentifierBuilder Set(Expression<Func<object>> expression)
+ {
+ string property = ExpressionProcessor.FindMemberExpression(expression.Body);
+ return new LambdaNaturalIdentifierBuilder(this, property);
+ }
}
}
Modified: trunk/nhibernate/src/NHibernate/Criterion/Restrictions.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/Restrictions.cs 2009-11-19 12:31:20 UTC (rev 4837)
+++ trunk/nhibernate/src/NHibernate/Criterion/Restrictions.cs 2009-11-19 14:46:12 UTC (rev 4838)
@@ -760,11 +760,34 @@
}
/// <summary>
+ /// Create an ICriterion for the negation of the supplied LambdaExpression
+ /// </summary>
+ /// <typeparam name="T">generic type</typeparam>
+ /// <param name="expression">lambda expression</param>
+ /// <returns>return NHibernate.Criterion.ICriterion</returns>
+ public static ICriterion WhereNot<T>(Expression<Func<T, bool>> expression)
+ {
+ ICriterion criterion = ExpressionProcessor.ProcessExpression<T>(expression);
+ return Restrictions.Not(criterion);
+ }
+
+ /// <summary>
+ /// Create an ICriterion for the negation of the supplied LambdaExpression
+ /// </summary>
+ /// <param name="expression">lambda expression</param>
+ /// <returns>return NHibernate.Criterion.ICriterion</returns>
+ public static ICriterion WhereNot(Expression<Func<bool>> expression)
+ {
+ ICriterion criterion = ExpressionProcessor.ProcessExpression(expression);
+ return Restrictions.Not(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)
+ public static LambdaRestrictionBuilder On<T>(Expression<Func<T, object>> expression)
{
string property = ExpressionProcessor.FindMemberExpression(expression.Body);
return new LambdaRestrictionBuilder(property);
@@ -775,7 +798,7 @@
/// </summary>
/// <param name="expression">lambda expression identifying property</param>
/// <returns>returns LambdaRestrictionBuilder</returns>
- public static LambdaRestrictionBuilder WhereProperty(Expression<Func<object>> expression)
+ public static LambdaRestrictionBuilder On(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 12:31:20 UTC (rev 4837)
+++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2009-11-19 14:46:12 UTC (rev 4838)
@@ -505,6 +505,7 @@
<Compile Include="Context\WcfOperationSessionContext.cs" />
<Compile Include="Criterion\GroupedProjection.cs" />
<Compile Include="Criterion\IPropertyProjection.cs" />
+ <Compile Include="Criterion\Lambda\LambdaNaturalIdentifierBuilder.cs" />
<Compile Include="Criterion\Lambda\LambdaRestrictionBuilder.cs" />
<Compile Include="Criterion\Lambda\LambdaSubqueryBuilder.cs" />
<Compile Include="Criterion\Lambda\QueryOverFetchBuilder.cs" />
Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2009-11-19 12:31:20 UTC (rev 4837)
+++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/RestrictionsFixture.cs 2009-11-19 14:46:12 UTC (rev 4838)
@@ -18,24 +18,31 @@
ICriteria expected =
CreateTestCriteria(typeof(Person), "personAlias")
.Add(Restrictions.Lt("Age", 65))
- .Add(Restrictions.Ge("personAlias.Age", 18));
+ .Add(Restrictions.Ge("personAlias.Age", 18))
+ .Add(Restrictions.Not(Restrictions.Ge("Age", 65)))
+ .Add(Restrictions.Not(Restrictions.Lt("personAlias.Age", 18)));
Person personAlias = null;
var actual =
CreateTestQueryOver<Person>(() => personAlias)
.Where(Restrictions.Where<Person>(p => p.Age < 65))
- .And(Restrictions.Where(() => personAlias.Age >= 18));
+ .And(Restrictions.Where(() => personAlias.Age >= 18))
+ .And(Restrictions.WhereNot<Person>(p => p.Age >= 65))
+ .And(Restrictions.WhereNot(() => personAlias.Age < 18));
AssertCriteriaAreEqual(expected, actual);
}
[Test]
- public void SqlFunctions()
+ public void SqlOperators()
{
ICriteria expected =
CreateTestCriteria(typeof(Person), "personAlias")
.Add(Restrictions.Between("Age", 18, 65))
.Add(Restrictions.Between("personAlias.Age", 18, 65))
+ .Add(Restrictions.In("Name", new string[] { "name1", "name2", "name3" }))
+ .Add(Restrictions.In("Name", new ArrayList() { "name1", "name2", "name3" }))
+ .Add(Restrictions.InG<int>("Age", new int[] { 1, 2, 3 }))
.Add(Restrictions.InsensitiveLike("Name", "test"))
.Add(Restrictions.InsensitiveLike("Name", "tEsT", MatchMode.Anywhere))
.Add(Restrictions.IsEmpty("Children"))
@@ -44,26 +51,60 @@
.Add(Restrictions.IsNull("Name"))
.Add(Restrictions.Like("Name", "%test%"))
.Add(Restrictions.Like("Name", "test", MatchMode.Anywhere))
- .Add(Restrictions.Like("Name", "test", MatchMode.Anywhere, '?'));
+ .Add(Restrictions.Like("Name", "test", MatchMode.Anywhere, '?'))
+ .Add(Restrictions.NaturalId()
+ .Set("Name", "my name")
+ .Set("personAlias.Age", 18));
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, '?'));
+ .Where(Restrictions.On<Person>(p => p.Age).IsBetween(18).And(65))
+ .And(Restrictions.On(() => personAlias.Age).IsBetween(18).And(65))
+ .And(Restrictions.On<Person>(p => p.Name).IsIn(new string[] { "name1", "name2", "name3" }))
+ .And(Restrictions.On<Person>(p => p.Name).IsIn(new ArrayList() { "name1", "name2", "name3" }))
+ .And(Restrictions.On<Person>(p => p.Age).IsInG<int>(new int[] { 1, 2, 3 }))
+ .And(Restrictions.On<Person>(p => p.Name).IsInsensitiveLike("test"))
+ .And(Restrictions.On<Person>(p => p.Name).IsInsensitiveLike("tEsT", MatchMode.Anywhere))
+ .And(Restrictions.On<Person>(p => p.Children).IsEmpty)
+ .And(Restrictions.On<Person>(p => p.Children).IsNotEmpty)
+ .And(Restrictions.On<Person>(p => p.Name).IsNotNull)
+ .And(Restrictions.On<Person>(p => p.Name).IsNull)
+ .And(Restrictions.On<Person>(p => p.Name).IsLike("%test%"))
+ .And(Restrictions.On<Person>(p => p.Name).IsLike("test", MatchMode.Anywhere))
+ .And(Restrictions.On<Person>(p => p.Name).IsLike("test", MatchMode.Anywhere, '?'))
+ .And(Restrictions.NaturalId()
+ .Set<Person>(p => p.Name).Is("my name")
+ .Set(() => personAlias.Age).Is(18));
AssertCriteriaAreEqual(expected, actual);
}
+ [Test]
+ public void Junction()
+ {
+ ICriteria expected =
+ CreateTestCriteria(typeof(Person), "personAlias")
+ .Add(Restrictions.Conjunction()
+ .Add(Restrictions.Eq("Name", "test"))
+ .Add(Restrictions.Eq("personAlias.Name", "test")))
+ .Add(Restrictions.Disjunction()
+ .Add(Restrictions.Eq("Name", "test"))
+ .Add(Restrictions.Eq("personAlias.Name", "test")));
+
+ Person personAlias = null;
+ var actual =
+ CreateTestQueryOver<Person>(() => personAlias)
+ .Where(Restrictions.Conjunction()
+ .Add<Person>(p => p.Name == "test")
+ .Add(() => personAlias.Name == "test"))
+ .And(Restrictions.Disjunction()
+ .Add<Person>(p => p.Name == "test")
+ .Add(() => personAlias.Name == "test"));
+
+ 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.
|