1
+ using System ;
1
2
using System . Collections . ObjectModel ;
2
3
using System . Linq ;
3
4
using System . Linq . Expressions ;
4
5
using System . Reflection ;
5
6
using System . Text . RegularExpressions ;
6
- using NHibernate . Cfg ;
7
7
using NHibernate . DomainModel . Northwind . Entities ;
8
8
using NHibernate . Hql . Ast ;
9
9
using NHibernate . Linq . Functions ;
@@ -23,6 +23,11 @@ public static bool IsLike(this string source, string pattern)
23
23
24
24
return Regex . IsMatch ( source , pattern ) ;
25
25
}
26
+
27
+ public static TimeSpan GetTime ( this DateTime dateTime )
28
+ {
29
+ return dateTime . TimeOfDay ;
30
+ }
26
31
}
27
32
28
33
public class MyLinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
@@ -32,6 +37,20 @@ public MyLinqToHqlGeneratorsRegistry():base()
32
37
RegisterGenerator ( ReflectHelper . GetMethodDefinition ( ( ) => MyLinqExtensions . IsLike ( null , null ) ) ,
33
38
new IsLikeGenerator ( ) ) ;
34
39
RegisterGenerator ( ReflectHelper . GetMethodDefinition ( ( ) => new object ( ) . Equals ( null ) ) , new ObjectEqualsGenerator ( ) ) ;
40
+ RegisterGenerator ( ReflectHelper . GetMethodDefinition ( ( ) => MyLinqExtensions . GetTime ( default ( DateTime ) ) ) , new GetTimeGenerator ( ) ) ;
41
+ }
42
+ }
43
+
44
+ public class GetTimeGenerator : BaseHqlGeneratorForMethod
45
+ {
46
+ public GetTimeGenerator ( )
47
+ {
48
+ SupportedMethods = new [ ] { ReflectHelper . GetMethodDefinition ( ( ) => MyLinqExtensions . GetTime ( default ( DateTime ) ) ) } ;
49
+ }
50
+
51
+ public override HqlTreeNode BuildHql ( MethodInfo method , Expression targetObject , ReadOnlyCollection < Expression > arguments , HqlTreeBuilder treeBuilder , IHqlExpressionVisitor visitor )
52
+ {
53
+ return treeBuilder . MethodCall ( "cast" , visitor . Visit ( arguments [ 0 ] ) . AsExpression ( ) , treeBuilder . Ident ( NHibernateUtil . TimeAsTimeSpan . Name ) ) ;
35
54
}
36
55
}
37
56
@@ -81,6 +100,14 @@ public void CanUseObjectEquals()
81
100
Assert . That ( users . All ( c => c . NullableEnum1 == EnumStoredAsString . Medium ) , Is . True ) ;
82
101
}
83
102
103
+ [ Test ( Description = "GH-2963" ) ]
104
+ public void CanUseComparisonWithExtensionOnMappedProperty ( )
105
+ {
106
+ var time = DateTime . UtcNow . GetTime ( ) ;
107
+ //using(new SqlLogSpy())
108
+ db . Users . Where ( u => u . RegisteredAt . GetTime ( ) > time ) . Select ( u => u . Id ) . ToList ( ) ;
109
+ }
110
+
84
111
[ Test ]
85
112
public void CanUseMyCustomExtension ( )
86
113
{
0 commit comments