Skip to content

Commit 7c1594a

Browse files
committed
Add support for Decimal.Truncate
In Firebird the function is called 'trunc' MySQL requires the number of decimals to be specified
1 parent a533078 commit 7c1594a

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

src/NHibernate.Test/NHSpecificTest/GH0831/FixtureByCode.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ public void CanHandleSubtract()
203203
});
204204
}
205205

206+
[Test]
207+
public void CanHandleTruncate()
208+
{
209+
AssumeFunctionSupported("truncate");
210+
211+
Assert.Multiple(() =>
212+
{
213+
CanFilter(e => decimal.Truncate(e.EntityValue) > 1m);
214+
CanSelect(e => decimal.Truncate(e.EntityValue));
215+
});
216+
}
217+
206218
private void CanFilter(Expression<Func<Entity, bool>> predicate)
207219
{
208220
using (ISession session = OpenSession())

src/NHibernate/Dialect/FirebirdDialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ private void RegisterMathematicalFunctions()
464464
RegisterFunction("rand", new NoArgSQLFunction("rand", NHibernateUtil.Double));
465465
RegisterFunction("sign", new StandardSQLFunction("sign", NHibernateUtil.Int32));
466466
RegisterFunction("sqtr", new StandardSQLFunction("sqtr", NHibernateUtil.Double));
467-
RegisterFunction("truncate", new StandardSQLFunction("truncate"));
467+
RegisterFunction("truncate", new StandardSQLFunction("trunc"));
468468
RegisterFunction("floor", new StandardSQLFunction("floor"));
469469
RegisterFunction("round", new StandardSQLFunction("round"));
470470
}

src/NHibernate/Dialect/MySQLDialect.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ protected virtual void RegisterFunctions()
261261
RegisterFunction("ceiling", new StandardSQLFunction("ceiling"));
262262
RegisterFunction("floor", new StandardSQLFunction("floor"));
263263
RegisterFunction("round", new StandardSQLFunction("round"));
264-
RegisterFunction("truncate", new StandardSQLFunction("truncate"));
265-
264+
RegisterFunction("truncate", new SQLFunctionTemplate(null, "truncate(?1, 0)"));
265+
266266
RegisterFunction("rand", new NoArgSQLFunction("rand", NHibernateUtil.Double));
267267

268268
RegisterFunction("power", new StandardSQLFunction("power", NHibernateUtil.Double));

src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public DefaultLinqToHqlGeneratorsRegistry()
5959
this.Merge(new DecimalRemainderGenerator());
6060
this.Merge(new DecimalNegateGenerator());
6161
this.Merge(new RoundGenerator());
62+
this.Merge(new TruncateGenerator());
6263
}
6364

6465
protected bool GetRuntimeMethodGenerator(MethodInfo method, out IHqlGeneratorForMethod methodGenerator)

src/NHibernate/Linq/Functions/MathGenerator.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ public MathGenerator()
5353
ReflectHelper.GetMethodDefinition(() => Math.Ceiling(default(double))),
5454
ReflectHelper.GetMethodDefinition(() => decimal.Ceiling(default(decimal))),
5555

56-
ReflectHelper.GetMethodDefinition(() => Math.Truncate(default(decimal))),
57-
ReflectHelper.GetMethodDefinition(() => Math.Truncate(default(double))),
58-
5956
ReflectHelper.GetMethodDefinition(() => Math.Pow(default(double), default(double))),
6057
};
6158
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using System.Linq.Expressions;
4+
using System.Reflection;
5+
using NHibernate.Hql.Ast;
6+
using NHibernate.Linq.Visitors;
7+
using NHibernate.Util;
8+
9+
namespace NHibernate.Linq.Functions
10+
{
11+
internal class TruncateGenerator : BaseHqlGeneratorForMethod
12+
{
13+
public TruncateGenerator()
14+
{
15+
SupportedMethods = new[]
16+
{
17+
ReflectHelper.GetMethodDefinition(() => Math.Truncate(default(decimal))),
18+
ReflectHelper.GetMethodDefinition(() => Math.Truncate(default(double))),
19+
ReflectHelper.GetMethodDefinition(() => decimal.Truncate(default(decimal)))
20+
};
21+
}
22+
23+
public override HqlTreeNode BuildHql(MethodInfo method, Expression expression, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
24+
{
25+
return treeBuilder.MethodCall("truncate", visitor.Visit(arguments[0]).AsExpression());
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)