|
From: <pa...@us...> - 2011-03-23 18:41:34
|
Revision: 5513
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5513&view=rev
Author: patearl
Date: 2011-03-23 18:41:28 +0000 (Wed, 23 Mar 2011)
Log Message:
-----------
Support ToString in Linq expressions (NH-2563).
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs
trunk/nhibernate/src/NHibernate/Linq/Functions/StringGenerator.cs
trunk/nhibernate/src/NHibernate.Test/Linq/FunctionTests.cs
Modified: trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs 2011-03-23 18:29:04 UTC (rev 5512)
+++ trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs 2011-03-23 18:41:28 UTC (rev 5513)
@@ -18,6 +18,7 @@
RegisterGenerator(new DictionaryContainsKeyRuntimeHqlGenerator());
RegisterGenerator(new GenericDictionaryItemRuntimeHqlGenerator());
RegisterGenerator(new GenericDictionaryContainsKeyRuntimeHqlGenerator());
+ RegisterGenerator(new ToStringRuntimeMethodHqlGenerator());
this.Merge(new StartsWithGenerator());
this.Merge(new EndsWithGenerator());
Modified: trunk/nhibernate/src/NHibernate/Linq/Functions/StringGenerator.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Linq/Functions/StringGenerator.cs 2011-03-23 18:29:04 UTC (rev 5512)
+++ trunk/nhibernate/src/NHibernate/Linq/Functions/StringGenerator.cs 2011-03-23 18:41:28 UTC (rev 5513)
@@ -1,3 +1,5 @@
+using System;
+using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
@@ -193,4 +195,31 @@
}
}
+ public class ToStringRuntimeMethodHqlGenerator : IRuntimeMethodHqlGenerator
+ {
+ private readonly ToStringHqlGeneratorForMethod generator = new ToStringHqlGeneratorForMethod();
+
+ public bool SupportsMethod(MethodInfo method)
+ {
+ return method != null && method.Name == "ToString" && method.GetBaseDefinition().DeclaringType == typeof(object);
+ }
+
+ public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
+ {
+ return generator;
+ }
+ }
+
+ public class ToStringHqlGeneratorForMethod : IHqlGeneratorForMethod
+ {
+ public IEnumerable<MethodInfo> SupportedMethods
+ {
+ get { throw new NotSupportedException(); }
+ }
+
+ public HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
+ {
+ return treeBuilder.MethodCall("str", visitor.Visit(targetObject).AsExpression());
+ }
+ }
}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate.Test/Linq/FunctionTests.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Linq/FunctionTests.cs 2011-03-23 18:29:04 UTC (rev 5512)
+++ trunk/nhibernate/src/NHibernate.Test/Linq/FunctionTests.cs 2011-03-23 18:41:28 UTC (rev 5513)
@@ -90,5 +90,25 @@
ObjectDumper.Write(query);
}
- }
+
+ [Test]
+ public void ToStringFunction()
+ {
+ var query = from ol in db.OrderLines
+ where ol.Quantity.ToString() == "4"
+ select ol;
+
+ Assert.AreEqual(55, query.Count());
+ }
+
+ [Test]
+ public void ToStringWithContains()
+ {
+ var query = from ol in db.OrderLines
+ where ol.Quantity.ToString().Contains("5")
+ select ol;
+
+ Assert.AreEqual(498, query.Count());
+ }
+ }
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|