Skip to content

Commit 99ec9a0

Browse files
committed
Support of parametrs in HQLFunctions (by Ricardo Stuven)
SVN: trunk@3701
1 parent cd6c6b2 commit 99ec9a0

26 files changed

+401
-253
lines changed

src/NHibernate.Test/HQLFunctionTest/HQLFunctions.cs

+63-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using NHibernate.Dialect;
34
using NHibernate.Dialect.Function;
@@ -17,7 +18,7 @@ public class HQLFunctions : TestCase
1718
static HQLFunctions()
1819
{
1920
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) });
2122
notSupportedStandardFunction.Add("bit_length",
2223
new System.Type[] { typeof(MsSql2000Dialect), typeof(MsSql2005Dialect) });
2324
notSupportedStandardFunction.Add("extract",
@@ -222,6 +223,36 @@ public void SubString()
222223
Animal result = (Animal) s.CreateQuery(hql).UniqueResult();
223224
Assert.AreEqual("abcdef", result.Description);
224225

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+
225256
if (twoArgSubstringSupported)
226257
{
227258
hql = "from Animal a where substring(a.Description, 4) = 'def'";
@@ -560,6 +591,13 @@ public void Cast()
560591
result = (Animal)s.CreateQuery(hql).UniqueResult();
561592
Assert.AreEqual("abcdef", result.Description);
562593

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+
563601
// Rendered in WHERE using a property and nested functions
564602
hql = "from Animal a where cast(cast(cast(a.BodyWeight as string) as double) as int) = 1";
565603
result = (Animal)s.CreateQuery(hql).UniqueResult();
@@ -595,6 +633,30 @@ public void Cast()
595633
Assert.AreEqual(1, l.Count);
596634
Assert.AreEqual(129, l[0]);
597635

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+
598660
// Rendered in HAVING using a property and nested functions
599661
string castExpr = "cast(cast(cast(a.BodyWeight as string) as double) as int)";
600662
hql = string.Format("select {0} from Animal a group by {0} having {0} = 1", castExpr);

src/NHibernate.Test/HQLFunctionTest/SQLFunctionTemplateTest.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ public void Simple()
1616
Assert.IsTrue(ft.HasArguments);
1717
IList args = new ArrayList();
1818
args.Add("'abcd <'");
19-
Assert.AreEqual("ltrim( 'abcd <' )", ft.Render(args, factoryImpl));
19+
Assert.AreEqual("ltrim( 'abcd <' )", ft.Render(args, factoryImpl).ToString());
2020

2121
ft = new SQLFunctionTemplate(NHibernateUtil.String, "ltrim( Az?ab )");
2222
Assert.IsFalse(ft.HasArguments);
23-
Assert.AreEqual("ltrim( Az?ab )", ft.Render(args, factoryImpl));
23+
Assert.AreEqual("ltrim( Az?ab )", ft.Render(args, factoryImpl).ToString());
2424

2525
ft = new SQLFunctionTemplate(NHibernateUtil.String, "function( ?1 )? 5:6");
2626
Assert.IsTrue(ft.HasArguments);
27-
Assert.AreEqual("function( 'abcd <' )? 5:6", ft.Render(args, factoryImpl));
27+
Assert.AreEqual("function( 'abcd <' )? 5:6", ft.Render(args, factoryImpl).ToString());
2828

2929
ft = new SQLFunctionTemplate(NHibernateUtil.String, "????????1?");
3030
Assert.IsTrue(ft.HasArguments);
31-
Assert.AreEqual("???????'abcd <'?", ft.Render(args, factoryImpl));
31+
Assert.AreEqual("???????'abcd <'?", ft.Render(args, factoryImpl).ToString());
3232
}
3333

3434
[Test]
@@ -44,14 +44,14 @@ public void RepetedParams()
4444
args.Add("'param2 ab '");
4545
Assert.AreEqual(
4646
"replace( replace( rtrim( replace( replace( 'param1 ', ' ', '${space}$' ), 'param2 ab ', ' ' ) ), ' ', 'param2 ab ' ), '${space}$', ' ' )",
47-
ft.Render(args, factoryImpl));
47+
ft.Render(args, factoryImpl).ToString());
4848

4949
args.Clear();
5050
ft = new SQLFunctionTemplate(NHibernateUtil.String, "?1 ?3 ?2 ?3 ?1");
5151
args.Add(1);
5252
args.Add(2);
5353
args.Add(3);
54-
Assert.AreEqual("1 3 2 3 1", ft.Render(args, factoryImpl));
54+
Assert.AreEqual("1 3 2 3 1", ft.Render(args, factoryImpl).ToString());
5555
}
5656

5757
//[Test] not required
@@ -68,7 +68,7 @@ public void NoStringArguments()
6868
DateTime.Today.ToString(DateTimeFormatInfo.InvariantInfo),
6969
(125.6D).ToString(NumberFormatInfo.InvariantInfo),
7070
(0910.123456m).ToString(NumberFormatInfo.InvariantInfo));
71-
Assert.AreEqual(expected, ft.Render(args, factoryImpl));
71+
Assert.AreEqual(expected, ft.Render(args, factoryImpl).ToString());
7272
}
7373

7474
[Test]
@@ -79,21 +79,21 @@ public void ArgsDiffParams()
7979

8080
// No Args; 2 params
8181
ft = new SQLFunctionTemplate(NHibernateUtil.String, "func(?1,?2)");
82-
Assert.AreEqual("func(,)", ft.Render(args, factoryImpl));
82+
Assert.AreEqual("func(,)", ft.Render(args, factoryImpl).ToString());
8383

8484
// Args<params
8585
args.Clear();
8686
ft = new SQLFunctionTemplate(NHibernateUtil.String, "func(?1,?2)");
8787
args.Add(1);
88-
Assert.AreEqual("func(1,)", ft.Render(args, factoryImpl));
88+
Assert.AreEqual("func(1,)", ft.Render(args, factoryImpl).ToString());
8989

9090
// Args>params
9191
args.Clear();
9292
ft = new SQLFunctionTemplate(NHibernateUtil.String, "func(?1,?3)");
9393
args.Add(1);
9494
args.Add(2);
9595
args.Add(3);
96-
Assert.AreEqual("func(1,3)", ft.Render(args, factoryImpl));
96+
Assert.AreEqual("func(1,3)", ft.Render(args, factoryImpl).ToString());
9797
}
9898
}
99-
}
99+
}

0 commit comments

Comments
 (0)