Skip to content

Commit

Permalink
Fix of NH-1034 (parameters in HQL functions)
Browse files Browse the repository at this point in the history
SVN: branches/2.0.x@3730
  • Loading branch information
fabiomaulo committed Aug 23, 2008
1 parent 763f6d3 commit eca2881
Show file tree
Hide file tree
Showing 26 changed files with 404 additions and 256 deletions.
69 changes: 66 additions & 3 deletions src/NHibernate.Test/HQLFunctionTest/HQLFunctions.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using NHibernate.Dialect;
using NHibernate.Dialect.Function;
Expand All @@ -17,7 +18,7 @@ public class HQLFunctions : TestCase
static HQLFunctions()
{
notSupportedStandardFunction.Add("locate",
new System.Type[] { typeof(MsSql2000Dialect), typeof(MsSql2005Dialect), typeof(FirebirdDialect) });
new System.Type[] { typeof(MsSql2000Dialect), typeof(MsSql2005Dialect), typeof(FirebirdDialect), typeof(PostgreSQLDialect) });
notSupportedStandardFunction.Add("bit_length",
new System.Type[] { typeof(MsSql2000Dialect), typeof(MsSql2005Dialect) });
notSupportedStandardFunction.Add("extract",
Expand Down Expand Up @@ -222,6 +223,36 @@ public void SubString()
Animal result = (Animal) s.CreateQuery(hql).UniqueResult();
Assert.AreEqual("abcdef", result.Description);

hql = "from Animal a where substring(a.Description, 2, 3) = ?";
result = (Animal)s.CreateQuery(hql)
.SetParameter(0, "bcd")
.UniqueResult();
Assert.AreEqual("abcdef", result.Description);


hql = "from Animal a where substring(a.Description, 2, ?) = 'bcd'";
result = (Animal)s.CreateQuery(hql)
.SetParameter(0, 3)
.UniqueResult();
Assert.AreEqual("abcdef", result.Description);


hql = "from Animal a where substring(a.Description, ?, ?) = ?";
result = (Animal)s.CreateQuery(hql)
.SetParameter(0, 2)
.SetParameter(1, 3)
.SetParameter(2, "bcd")
.UniqueResult();
Assert.AreEqual("abcdef", result.Description);

hql = "select substring(a.Description, ?, ?) from Animal a";
IList results = s.CreateQuery(hql)
.SetParameter(0, 2)
.SetParameter(1, 3)
.List();
Assert.AreEqual(1, results.Count);
Assert.AreEqual("bcd", results[0]);

if (twoArgSubstringSupported)
{
hql = "from Animal a where substring(a.Description, 4) = 'def'";
Expand Down Expand Up @@ -304,6 +335,7 @@ public void Trim()

Animal a = new Animal(" abc", 20);
s.Save(a);
s.Flush();
hql = "from Animal a where trim(both from a.Description) = 'abc'";
lresult = s.CreateQuery(hql).List();
Assert.AreEqual(2, lresult.Count);
Expand Down Expand Up @@ -536,7 +568,7 @@ public void Cast()
Assert.AreEqual(1.3f, l[0]);

// Rendered in SELECT using a property in an operation with costant
hql = "select cast(7+123-5*a.BodyWeight as double) from Animal a";
hql = "select cast(7+123-5*a.BodyWeight as Double) from Animal a";
l = s.CreateQuery(hql).List();
Assert.AreEqual(1, l.Count);
Assert.AreEqual(7f + 123f - 5f * 1.3f, l[0]);
Expand All @@ -559,6 +591,13 @@ public void Cast()
result = (Animal)s.CreateQuery(hql).UniqueResult();
Assert.AreEqual("abcdef", result.Description);

// Rendered in WHERE using a property and named param
hql = "from Animal a where cast(:aParam+a.BodyWeight as Double)>0";
result = (Animal)s.CreateQuery(hql)
.SetDouble("aParam", 2D)
.UniqueResult();
Assert.AreEqual("abcdef", result.Description);

// Rendered in WHERE using a property and nested functions
hql = "from Animal a where cast(cast(cast(a.BodyWeight as string) as double) as int) = 1";
result = (Animal)s.CreateQuery(hql).UniqueResult();
Expand Down Expand Up @@ -594,6 +633,30 @@ public void Cast()
Assert.AreEqual(1, l.Count);
Assert.AreEqual(129, l[0]);

// Rendered in HAVING using a property and named param (NOT SUPPORTED)
try
{
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";
l = s.CreateQuery(hql).SetInt32("aParam", 10).List();
Assert.AreEqual(1, l.Count);
Assert.AreEqual(11, l[0]);
}
catch (QueryException ex)
{
if (!(ex.InnerException is NotSupportedException))
throw;
}
catch (ADOException ex)
{
// This test raises an exception in SQL Server because named
// parameters internally are always positional (@p0, @p1, etc.)
// and named differently hence they mismatch between GROUP BY and HAVING clauses.
if (!ex.InnerException.Message.Equals(
"Column 'Animal.BodyWeight' is invalid in the HAVING clause " +
"because it is not contained in either an aggregate function or the GROUP BY clause."))
throw;
}

// Rendered in HAVING using a property and nested functions
string castExpr = "cast(cast(cast(a.BodyWeight as string) as double) as int)";
hql = string.Format("select {0} from Animal a group by {0} having {0} = 1", castExpr);
Expand Down Expand Up @@ -788,4 +851,4 @@ public void ParameterLikeArgument()
}
}
}
}
}
22 changes: 11 additions & 11 deletions src/NHibernate.Test/HQLFunctionTest/SQLFunctionTemplateTest.cs
Expand Up @@ -16,19 +16,19 @@ public void Simple()
Assert.IsTrue(ft.HasArguments);
IList args = new ArrayList();
args.Add("'abcd <'");
Assert.AreEqual("ltrim( 'abcd <' )", ft.Render(args, factoryImpl));
Assert.AreEqual("ltrim( 'abcd <' )", ft.Render(args, factoryImpl).ToString());

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

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

ft = new SQLFunctionTemplate(NHibernateUtil.String, "????????1?");
Assert.IsTrue(ft.HasArguments);
Assert.AreEqual("???????'abcd <'?", ft.Render(args, factoryImpl));
Assert.AreEqual("???????'abcd <'?", ft.Render(args, factoryImpl).ToString());
}

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

args.Clear();
ft = new SQLFunctionTemplate(NHibernateUtil.String, "?1 ?3 ?2 ?3 ?1");
args.Add(1);
args.Add(2);
args.Add(3);
Assert.AreEqual("1 3 2 3 1", ft.Render(args, factoryImpl));
Assert.AreEqual("1 3 2 3 1", ft.Render(args, factoryImpl).ToString());
}

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

[Test]
Expand All @@ -79,21 +79,21 @@ public void ArgsDiffParams()

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

// Args<params
args.Clear();
ft = new SQLFunctionTemplate(NHibernateUtil.String, "func(?1,?2)");
args.Add(1);
Assert.AreEqual("func(1,)", ft.Render(args, factoryImpl));
Assert.AreEqual("func(1,)", ft.Render(args, factoryImpl).ToString());

// Args>params
args.Clear();
ft = new SQLFunctionTemplate(NHibernateUtil.String, "func(?1,?3)");
args.Add(1);
args.Add(2);
args.Add(3);
Assert.AreEqual("func(1,3)", ft.Render(args, factoryImpl));
Assert.AreEqual("func(1,3)", ft.Render(args, factoryImpl).ToString());
}
}
}
}

0 comments on commit eca2881

Please sign in to comment.