forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoundGenerator.cs
41 lines (38 loc) · 1.45 KB
/
RoundGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Hql.Ast;
using NHibernate.Linq.Visitors;
using NHibernate.Util;
namespace NHibernate.Linq.Functions
{
internal class RoundGenerator : BaseHqlGeneratorForMethod
{
public RoundGenerator()
{
SupportedMethods = new[]
{
ReflectHelper.FastGetMethod(Math.Round, default(double)),
ReflectHelper.FastGetMethod(Math.Round, default(double), default(int)),
ReflectHelper.FastGetMethod(Math.Round, default(decimal)),
ReflectHelper.FastGetMethod(Math.Round, default(decimal), default(int)),
ReflectHelper.FastGetMethod(decimal.Round, default(decimal)),
ReflectHelper.FastGetMethod(decimal.Round, default(decimal), default(int)),
#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
ReflectHelper.FastGetMethod(MathF.Round, default(float)),
ReflectHelper.FastGetMethod(MathF.Round, default(float), default(int)),
#endif
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
var numberOfDecimals = arguments.Count == 2
? visitor.Visit(arguments[1]).AsExpression()
: treeBuilder.Constant(0);
return treeBuilder.TransparentCast(
treeBuilder.MethodCall("round", visitor.Visit(arguments[0]).AsExpression(), numberOfDecimals),
method.ReturnType);
}
}
}