Implementing StartsWith, EndsWith and Contains query extensions for NH

QueryOver.

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-08-20 11:08:44 -07:00
parent 32b3306399
commit ac256e9c06
3 changed files with 19 additions and 32 deletions
@@ -454,7 +454,7 @@ namespace Orchard.Tests.ContentManagement {
}
[Test]
public void IsStartingExtensionShouldBeUsed() {
public void StartsWithExtensionShouldBeUsed() {
_manager.Create<GammaPart>("gamma", init => { init.Record.Frap = "one"; });
_manager.Create<GammaPart>("gamma", init => { init.Record.Frap = "two"; });
_manager.Create<GammaPart>("gamma", init => { init.Record.Frap = "three"; });
@@ -462,7 +462,7 @@ namespace Orchard.Tests.ContentManagement {
_session.Flush();
var result = _manager.Query<GammaPart, GammaRecord>()
.Where(x => x.Frap.IsStartingWith("t"))
.Where(x => x.Frap.StartsWith("t"))
.List();
Assert.That(result.Count(), Is.EqualTo(2));
@@ -471,7 +471,7 @@ namespace Orchard.Tests.ContentManagement {
}
[Test]
public void IsEndingExtensionShouldBeUsed() {
public void EndsWithExtensionShouldBeUsed() {
_manager.Create<GammaPart>("gamma", init => { init.Record.Frap = "one"; });
_manager.Create<GammaPart>("gamma", init => { init.Record.Frap = "two"; });
_manager.Create<GammaPart>("gamma", init => { init.Record.Frap = "three"; });
@@ -479,7 +479,7 @@ namespace Orchard.Tests.ContentManagement {
_session.Flush();
var result = _manager.Query<GammaPart, GammaRecord>()
.Where(x => x.Frap.IsEndingWith("e"))
.Where(x => x.Frap.EndsWith("e"))
.List();
Assert.That(result.Count(), Is.EqualTo(2));
@@ -488,7 +488,7 @@ namespace Orchard.Tests.ContentManagement {
}
[Test]
public void IsContainingExtensionShouldBeUsed() {
public void ContainsExtensionShouldBeUsed() {
_manager.Create<GammaPart>("gamma", init => { init.Record.Frap = "one"; });
_manager.Create<GammaPart>("gamma", init => { init.Record.Frap = "two"; });
_manager.Create<GammaPart>("gamma", init => { init.Record.Frap = "three"; });
@@ -496,7 +496,7 @@ namespace Orchard.Tests.ContentManagement {
_session.Flush();
var result = _manager.Query<GammaPart, GammaRecord>()
.Where(x => x.Frap.IsContaining("o"))
.Where(x => x.Frap.Contains("o"))
.List();
Assert.That(result.Count(), Is.EqualTo(3));
@@ -504,7 +504,6 @@ namespace Orchard.Tests.ContentManagement {
Assert.That(result.Count(x => x.Get<GammaPart>().Record.Frap == "two"), Is.EqualTo(1));
Assert.That(result.Count(x => x.Get<GammaPart>().Record.Frap == "four"), Is.EqualTo(1));
}
}
}
@@ -10,42 +10,30 @@ using Orchard.ContentManagement.Records;
namespace Orchard.ContentManagement {
public static class RestrictionExtensions {
public static void RegisterExtensions() {
ExpressionProcessor.RegisterCustomMethodCall(() => RestrictionExtensions.IsStartingWith("", ""), RestrictionExtensions.ProcessIsStartingWith);
ExpressionProcessor.RegisterCustomMethodCall(() => RestrictionExtensions.IsEndingWith("", ""), RestrictionExtensions.ProcessIsEndingWith);
ExpressionProcessor.RegisterCustomMethodCall(() => RestrictionExtensions.IsContaining("", ""), RestrictionExtensions.ProcessIsContaining);
ExpressionProcessor.RegisterCustomMethodCall(() => "".StartsWith(""), RestrictionExtensions.ProcessStartsWith);
ExpressionProcessor.RegisterCustomMethodCall(() => "".EndsWith(""), RestrictionExtensions.ProcessEndsWith);
ExpressionProcessor.RegisterCustomMethodCall(() => "".Contains(""), RestrictionExtensions.ProcessContains);
}
public static bool IsStartingWith(this string item, string value) {
throw new NotImplementedException("Do not use this method directly");
}
public static bool IsEndingWith(this string item, string value) {
throw new NotImplementedException("Do not use this method directly");
}
public static bool IsContaining(this string item, string value) {
throw new NotImplementedException("Do not use this method directly");
}
public static ICriterion ProcessIsStartingWith(MethodCallExpression methodCallExpression) {
ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]);
string value = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
public static ICriterion ProcessStartsWith(MethodCallExpression methodCallExpression) {
ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Object);
string value = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[0]);
MatchMode matchMode = MatchMode.Start;
return projection.Create<ICriterion>(s => Restrictions.InsensitiveLike(s, value, matchMode), p => Restrictions.InsensitiveLike(p, value, matchMode));
}
public static ICriterion ProcessIsEndingWith(MethodCallExpression methodCallExpression) {
ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]);
string value = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
public static ICriterion ProcessEndsWith(MethodCallExpression methodCallExpression) {
ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Object);
string value = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[0]);
MatchMode matchMode = MatchMode.End;
return projection.Create<ICriterion>(s => Restrictions.InsensitiveLike(s, value, matchMode), p => Restrictions.InsensitiveLike(p, value, matchMode));
}
public static ICriterion ProcessIsContaining(MethodCallExpression methodCallExpression) {
ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]);
string value = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
public static ICriterion ProcessContains(MethodCallExpression methodCallExpression) {
ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Object);
string value = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[0]);
MatchMode matchMode = MatchMode.Anywhere;
return projection.Create<ICriterion>(s => Restrictions.InsensitiveLike(s, value, matchMode), p => Restrictions.InsensitiveLike(p, value, matchMode));
}