Skip to content

Commit 578a89b

Browse files
committed
NH-3096 - add HQL generator for Nullable`1.GetValueOrDefault()
1 parent 5979442 commit 578a89b

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.Linq.ByMethod
5+
{
6+
[TestFixture]
7+
public class GetValueOrDefaultTests : LinqTestCase
8+
{
9+
[Test]
10+
public void CoalesceInWhere()
11+
{
12+
var orders = db.Orders
13+
.Where(x => (x.Freight ?? 100) > 0)
14+
.ToList();
15+
16+
Assert.AreEqual(830, orders.Count);
17+
}
18+
19+
[Test]
20+
public void GetValueOrDefaultInWhere()
21+
{
22+
var orders = db.Orders
23+
.Where(x => x.Freight.GetValueOrDefault(100) > 0)
24+
.ToList();
25+
26+
Assert.AreEqual(830, orders.Count);
27+
}
28+
}
29+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@
502502
<Compile Include="Linq\ByMethod\CastTests.cs" />
503503
<Compile Include="Linq\ByMethod\GroupByHavingTests.cs" />
504504
<Compile Include="Linq\ByMethod\GroupByTests.cs" />
505+
<Compile Include="Linq\ByMethod\GetValueOrDefaultTests.cs" />
505506
<Compile Include="Linq\CasingTest.cs" />
506507
<Compile Include="Linq\CharComparisonTests.cs" />
507508
<Compile Include="Linq\CollectionAssert.cs" />

src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public DefaultLinqToHqlGeneratorsRegistry()
2020
RegisterGenerator(new GenericDictionaryContainsKeyRuntimeHqlGenerator());
2121
RegisterGenerator(new ToStringRuntimeMethodHqlGenerator());
2222
RegisterGenerator(new LikeGenerator());
23+
RegisterGenerator(new GetValueOrDefaultGenerator());
2324

2425
RegisterGenerator(new CompareGenerator());
2526
this.Merge(new CompareGenerator());
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq.Expressions;
5+
using System.Reflection;
6+
using NHibernate.Hql.Ast;
7+
using NHibernate.Linq.Visitors;
8+
using NHibernate.Util;
9+
10+
namespace NHibernate.Linq.Functions
11+
{
12+
internal class GetValueOrDefaultGenerator : IHqlGeneratorForMethod, IRuntimeMethodHqlGenerator
13+
{
14+
public bool SupportsMethod(MethodInfo method)
15+
{
16+
return method != null && String.Equals(method.Name, "GetValueOrDefault", StringComparison.OrdinalIgnoreCase) && method.IsMethodOf(typeof (Nullable<>));
17+
}
18+
19+
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
20+
{
21+
return this;
22+
}
23+
24+
public IEnumerable<MethodInfo> SupportedMethods
25+
{
26+
get { throw new NotSupportedException("This is an runtime method generator"); }
27+
}
28+
29+
public HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
30+
{
31+
return treeBuilder.Coalesce(visitor.Visit(targetObject).AsExpression(), visitor.Visit(arguments[0]).AsExpression());
32+
}
33+
}
34+
}

src/NHibernate/NHibernate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@
292292
<Compile Include="Linq\ExpressionTransformers\RemoveRedundantCast.cs" />
293293
<Compile Include="Linq\ExpressionTransformers\SimplifyCompareTransformer.cs" />
294294
<Compile Include="Linq\Functions\CompareGenerator.cs" />
295+
<Compile Include="Linq\Functions\GetValueOrDefaultGenerator.cs" />
295296
<Compile Include="Linq\Functions\MathGenerator.cs" />
296297
<Compile Include="Linq\Functions\DictionaryGenerator.cs" />
297298
<Compile Include="Linq\Functions\EqualsGenerator.cs" />

0 commit comments

Comments
 (0)