Skip to content

Commit 1a89b1e

Browse files
NH-3787 - Fix decimal truncation in some Linq queries
1 parent a3bb8e6 commit 1a89b1e

File tree

7 files changed

+35
-3
lines changed

7 files changed

+35
-3
lines changed

src/NHibernate.Test/NHSpecificTest/NH3787/TestFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void TestLinqProjection()
7070
}
7171
}
7272

73-
[Test, Ignore("Not fixed yet.")]
73+
[Test]
7474
public void TestLinqQueryOnExpression()
7575
{
7676
using (var s = OpenSession())

src/NHibernate/Dialect/Dialect.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ protected Dialect()
9898
RegisterFunction("upper", new StandardSQLFunction("upper"));
9999
RegisterFunction("lower", new StandardSQLFunction("lower"));
100100
RegisterFunction("cast", new CastFunction());
101+
RegisterFunction("transparentcast", new TransparentCastFunction());
101102
RegisterFunction("extract", new AnsiExtractFunction());
102103
RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "||", ")"));
103104

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace NHibernate.Dialect.Function
4+
{
5+
/// <summary>
6+
/// A HQL only cast for helping HQL knowing the type. Does not generates any actual cast in code.
7+
/// </summary>
8+
[Serializable]
9+
public class TransparentCastFunction : CastFunction
10+
{
11+
protected override bool CastingIsRequired(string sqlType)
12+
{
13+
return false;
14+
}
15+
}
16+
}

src/NHibernate/Hql/Ast/ANTLR/SessionFactoryHelperExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public IType FindFunctionReturnType(String functionName, IASTNode first)
7777

7878
if (first != null)
7979
{
80-
if (functionName == "cast")
80+
if (sqlFunction is CastFunction)
8181
{
8282
argumentType = TypeFactory.HeuristicType(first.NextSibling.Text);
8383
}

src/NHibernate/Hql/Ast/HqlTreeBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ public HqlCast Cast(HqlExpression expression, System.Type type)
301301
return new HqlCast(_factory, expression, type);
302302
}
303303

304+
public HqlTransparentCast TransparentCast(HqlExpression expression, System.Type type)
305+
{
306+
return new HqlTransparentCast(_factory, expression, type);
307+
}
308+
304309
public HqlBitwiseNot BitwiseNot()
305310
{
306311
return new HqlBitwiseNot(_factory);

src/NHibernate/Hql/Ast/HqlTreeNode.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,16 @@ public HqlCast(IASTFactory factory, HqlExpression expression, System.Type type)
701701
}
702702
}
703703

704+
public class HqlTransparentCast : HqlExpression
705+
{
706+
public HqlTransparentCast(IASTFactory factory, HqlExpression expression, System.Type type)
707+
: base(HqlSqlWalker.METHOD_CALL, "method", factory)
708+
{
709+
AddChild(new HqlIdent(factory, "transparentcast"));
710+
AddChild(new HqlExpressionList(factory, expression, new HqlIdent(factory, type)));
711+
}
712+
}
713+
704714
public class HqlCoalesce : HqlExpression
705715
{
706716
public HqlCoalesce(IASTFactory factory, HqlExpression lhs, HqlExpression rhs)

src/NHibernate/Linq/Visitors/HqlGeneratorExpressionVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ protected HqlTreeNode VisitConditionalExpression(ConditionalExpression expressio
540540

541541
return (expression.Type == typeof (bool) || expression.Type == (typeof (bool?)))
542542
? @case
543-
: _hqlTreeBuilder.Cast(@case, expression.Type);
543+
: _hqlTreeBuilder.TransparentCast(@case, expression.Type);
544544
}
545545

546546
protected HqlTreeNode VisitSubQueryExpression(SubQueryExpression expression)

0 commit comments

Comments
 (0)