1
+ using System ;
1
2
using System . Collections ;
2
3
using NHibernate . Dialect ;
3
4
using NHibernate . Dialect . Function ;
@@ -17,7 +18,7 @@ public class HQLFunctions : TestCase
17
18
static HQLFunctions ( )
18
19
{
19
20
notSupportedStandardFunction . Add ( "locate" ,
20
- new System . Type [ ] { typeof ( MsSql2000Dialect ) , typeof ( MsSql2005Dialect ) , typeof ( FirebirdDialect ) } ) ;
21
+ new System . Type [ ] { typeof ( MsSql2000Dialect ) , typeof ( MsSql2005Dialect ) , typeof ( FirebirdDialect ) , typeof ( PostgreSQLDialect ) } ) ;
21
22
notSupportedStandardFunction . Add ( "bit_length" ,
22
23
new System . Type [ ] { typeof ( MsSql2000Dialect ) , typeof ( MsSql2005Dialect ) } ) ;
23
24
notSupportedStandardFunction . Add ( "extract" ,
@@ -222,6 +223,36 @@ public void SubString()
222
223
Animal result = ( Animal ) s . CreateQuery ( hql ) . UniqueResult ( ) ;
223
224
Assert . AreEqual ( "abcdef" , result . Description ) ;
224
225
226
+ hql = "from Animal a where substring(a.Description, 2, 3) = ?" ;
227
+ result = ( Animal ) s . CreateQuery ( hql )
228
+ . SetParameter ( 0 , "bcd" )
229
+ . UniqueResult ( ) ;
230
+ Assert . AreEqual ( "abcdef" , result . Description ) ;
231
+
232
+
233
+ hql = "from Animal a where substring(a.Description, 2, ?) = 'bcd'" ;
234
+ result = ( Animal ) s . CreateQuery ( hql )
235
+ . SetParameter ( 0 , 3 )
236
+ . UniqueResult ( ) ;
237
+ Assert . AreEqual ( "abcdef" , result . Description ) ;
238
+
239
+
240
+ hql = "from Animal a where substring(a.Description, ?, ?) = ?" ;
241
+ result = ( Animal ) s . CreateQuery ( hql )
242
+ . SetParameter ( 0 , 2 )
243
+ . SetParameter ( 1 , 3 )
244
+ . SetParameter ( 2 , "bcd" )
245
+ . UniqueResult ( ) ;
246
+ Assert . AreEqual ( "abcdef" , result . Description ) ;
247
+
248
+ hql = "select substring(a.Description, ?, ?) from Animal a" ;
249
+ IList results = s . CreateQuery ( hql )
250
+ . SetParameter ( 0 , 2 )
251
+ . SetParameter ( 1 , 3 )
252
+ . List ( ) ;
253
+ Assert . AreEqual ( 1 , results . Count ) ;
254
+ Assert . AreEqual ( "bcd" , results [ 0 ] ) ;
255
+
225
256
if ( twoArgSubstringSupported )
226
257
{
227
258
hql = "from Animal a where substring(a.Description, 4) = 'def'" ;
@@ -560,6 +591,13 @@ public void Cast()
560
591
result = ( Animal ) s . CreateQuery ( hql ) . UniqueResult ( ) ;
561
592
Assert . AreEqual ( "abcdef" , result . Description ) ;
562
593
594
+ // Rendered in WHERE using a property and named param
595
+ hql = "from Animal a where cast(:aParam+a.BodyWeight as Double)>0" ;
596
+ result = ( Animal ) s . CreateQuery ( hql )
597
+ . SetDouble ( "aParam" , 2D )
598
+ . UniqueResult ( ) ;
599
+ Assert . AreEqual ( "abcdef" , result . Description ) ;
600
+
563
601
// Rendered in WHERE using a property and nested functions
564
602
hql = "from Animal a where cast(cast(cast(a.BodyWeight as string) as double) as int) = 1" ;
565
603
result = ( Animal ) s . CreateQuery ( hql ) . UniqueResult ( ) ;
@@ -595,6 +633,30 @@ public void Cast()
595
633
Assert . AreEqual ( 1 , l . Count ) ;
596
634
Assert . AreEqual ( 129 , l [ 0 ] ) ;
597
635
636
+ // Rendered in HAVING using a property and named param (NOT SUPPORTED)
637
+ try
638
+ {
639
+ hql = "select cast(:aParam+a.BodyWeight as int) from Animal a group by cast(:aParam+a.BodyWeight as int) having cast(:aParam+a.BodyWeight as int)>0" ;
640
+ l = s . CreateQuery ( hql ) . SetInt32 ( "aParam" , 10 ) . List ( ) ;
641
+ Assert . AreEqual ( 1 , l . Count ) ;
642
+ Assert . AreEqual ( 11 , l [ 0 ] ) ;
643
+ }
644
+ catch ( QueryException ex )
645
+ {
646
+ if ( ! ( ex . InnerException is NotSupportedException ) )
647
+ throw ;
648
+ }
649
+ catch ( ADOException ex )
650
+ {
651
+ // This test raises an exception in SQL Server because named
652
+ // parameters internally are always positional (@p0, @p1, etc.)
653
+ // and named differently hence they mismatch between GROUP BY and HAVING clauses.
654
+ if ( ! ex . InnerException . Message . Equals (
655
+ "Column 'Animal.BodyWeight' is invalid in the HAVING clause " +
656
+ "because it is not contained in either an aggregate function or the GROUP BY clause." ) )
657
+ throw ;
658
+ }
659
+
598
660
// Rendered in HAVING using a property and nested functions
599
661
string castExpr = "cast(cast(cast(a.BodyWeight as string) as double) as int)" ;
600
662
hql = string . Format ( "select {0} from Animal a group by {0} having {0} = 1" , castExpr ) ;
0 commit comments