Skip to content

Commit f1e54fc

Browse files
authored
Fix nullable Guid ToString is not translated correctly on some dialects (#2046)
1 parent a6e7a44 commit f1e54fc

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH3426/Fixture.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,18 @@ public async Task CompareStringColumnWithGuidToStringAsync()
9797
Assert.That(list, Has.Count.EqualTo(0));
9898
}
9999
}
100+
101+
[Test]
102+
public async Task CompareStringColumnWithNullableGuidToStringAsync()
103+
{
104+
using (var session = OpenSession())
105+
{
106+
var list = await (session.Query<Entity>()
107+
.Where(x => ((Guid?) x.Id).ToString() == x.Id.ToString())
108+
.ToListAsync());
109+
110+
Assert.That(list, Has.Count.EqualTo(1));
111+
}
112+
}
100113
}
101114
}

src/NHibernate.Test/NHSpecificTest/NH3426/Fixture.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,18 @@ public void CompareStringColumnWithGuidToString()
8585
Assert.That(list, Has.Count.EqualTo(0));
8686
}
8787
}
88+
89+
[Test]
90+
public void CompareStringColumnWithNullableGuidToString()
91+
{
92+
using (var session = OpenSession())
93+
{
94+
var list = session.Query<Entity>()
95+
.Where(x => ((Guid?) x.Id).ToString() == x.Id.ToString())
96+
.ToList();
97+
98+
Assert.That(list, Has.Count.EqualTo(1));
99+
}
100+
}
88101
}
89102
}

src/NHibernate/Linq/Functions/StringGenerator.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,21 +317,15 @@ public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
317317

318318
public class ToStringHqlGeneratorForMethod : IHqlGeneratorForMethod
319319
{
320-
private static readonly System.Type _guidType = typeof(Guid);
321-
322-
public IEnumerable<MethodInfo> SupportedMethods
323-
{
324-
get { throw new NotSupportedException(); }
325-
}
320+
public IEnumerable<MethodInfo> SupportedMethods => throw new NotSupportedException();
326321

327322
public HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
328323
{
329-
if (targetObject.Type == _guidType)
330-
{
331-
return treeBuilder.MethodCall("strguid", visitor.Visit(targetObject).AsExpression());
332-
}
324+
var methodName = targetObject.Type == typeof(Guid) || targetObject.Type == typeof(Guid?)
325+
? "strguid"
326+
: "str";
333327

334-
return treeBuilder.MethodCall("str", visitor.Visit(targetObject).AsExpression());
328+
return treeBuilder.MethodCall(methodName, visitor.Visit(targetObject).AsExpression());
335329
}
336330
}
337331
}

0 commit comments

Comments
 (0)