Skip to content

Commit 8906825

Browse files
committed
Adjust RoundGenerator to consider Math.Round methods
1 parent 28dde43 commit 8906825

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

src/NHibernate/Linq/Functions/DecimalGenerator.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,4 @@ public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
103103
return treeBuilder.TransparentCast(treeBuilder.Negate(visitor.Visit(arguments[0]).AsExpression()), typeof(decimal));
104104
}
105105
}
106-
107-
public class DecimalRoundGenerator : BaseHqlGeneratorForMethod
108-
{
109-
public DecimalRoundGenerator()
110-
{
111-
SupportedMethods = new[]
112-
{
113-
ReflectHelper.GetMethodDefinition(() => decimal.Round(default(decimal))),
114-
ReflectHelper.GetMethodDefinition(() => decimal.Round(default(decimal), default(int))),
115-
};
116-
}
117-
118-
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
119-
{
120-
HqlExpression numberOfDecimals = (arguments.Count == 2) ? visitor.Visit(arguments[1]).AsExpression() : treeBuilder.Constant(0);
121-
return treeBuilder.TransparentCast(treeBuilder.MethodCall("round", visitor.Visit(arguments[0]).AsExpression(), numberOfDecimals), typeof(decimal));
122-
}
123-
}
124106
}

src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public DefaultLinqToHqlGeneratorsRegistry()
5858
this.Merge(new DecimalSubtractGenerator());
5959
this.Merge(new DecimalRemainderGenerator());
6060
this.Merge(new DecimalNegateGenerator());
61-
this.Merge(new DecimalRoundGenerator());
61+
this.Merge(new RoundGenerator());
6262
}
6363

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

src/NHibernate/Linq/Functions/MathGenerator.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ public MathGenerator()
4545
ReflectHelper.GetMethodDefinition(() => Math.Sign(default(short))),
4646
ReflectHelper.GetMethodDefinition(() => Math.Sign(default(sbyte))),
4747

48-
ReflectHelper.GetMethodDefinition(() => Math.Round(default(decimal))),
49-
ReflectHelper.GetMethodDefinition(() => Math.Round(default(decimal), default(int))),
50-
ReflectHelper.GetMethodDefinition(() => Math.Round(default(double))),
51-
ReflectHelper.GetMethodDefinition(() => Math.Round(default(double), default(int))),
5248
ReflectHelper.GetMethodDefinition(() => Math.Floor(default(decimal))),
5349
ReflectHelper.GetMethodDefinition(() => Math.Floor(default(double))),
5450
ReflectHelper.GetMethodDefinition(() => decimal.Floor(default(decimal))),
51+
5552
ReflectHelper.GetMethodDefinition(() => Math.Ceiling(default(decimal))),
5653
ReflectHelper.GetMethodDefinition(() => Math.Ceiling(default(double))),
5754
ReflectHelper.GetMethodDefinition(() => decimal.Ceiling(default(decimal))),
55+
5856
ReflectHelper.GetMethodDefinition(() => Math.Truncate(default(decimal))),
5957
ReflectHelper.GetMethodDefinition(() => Math.Truncate(default(double))),
6058

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 RoundGenerator : BaseHqlGeneratorForMethod
12+
{
13+
public RoundGenerator()
14+
{
15+
SupportedMethods = new[]
16+
{
17+
ReflectHelper.GetMethodDefinition(() => Math.Round(default(double))),
18+
ReflectHelper.GetMethodDefinition(() => Math.Round(default(double), default(int))),
19+
ReflectHelper.GetMethodDefinition(() => Math.Round(default(decimal))),
20+
ReflectHelper.GetMethodDefinition(() => Math.Round(default(decimal), default(int))),
21+
ReflectHelper.GetMethodDefinition(() => decimal.Round(default(decimal))),
22+
ReflectHelper.GetMethodDefinition(() => decimal.Round(default(decimal), default(int))),
23+
};
24+
}
25+
26+
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
27+
{
28+
var numberOfDecimals = arguments.Count == 2
29+
? visitor.Visit(arguments[1]).AsExpression()
30+
: treeBuilder.Constant(0);
31+
return treeBuilder.TransparentCast(
32+
treeBuilder.MethodCall("round", visitor.Visit(arguments[0]).AsExpression(), numberOfDecimals),
33+
method.ReturnType);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)