Skip to content

Fix nullable Guid ToString is not translated correctly on some dialects #2046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH3426/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,18 @@ public async Task CompareStringColumnWithGuidToStringAsync()
Assert.That(list, Has.Count.EqualTo(0));
}
}

[Test]
public async Task CompareStringColumnWithNullableGuidToStringAsync()
{
using (var session = OpenSession())
{
var list = await (session.Query<Entity>()
.Where(x => ((Guid?) x.Id).ToString() == x.Id.ToString())
.ToListAsync());

Assert.That(list, Has.Count.EqualTo(1));
}
}
}
}
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3426/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,18 @@ public void CompareStringColumnWithGuidToString()
Assert.That(list, Has.Count.EqualTo(0));
}
}

[Test]
public void CompareStringColumnWithNullableGuidToString()
{
using (var session = OpenSession())
{
var list = session.Query<Entity>()
.Where(x => ((Guid?) x.Id).ToString() == x.Id.ToString())
.ToList();

Assert.That(list, Has.Count.EqualTo(1));
}
}
}
}
16 changes: 5 additions & 11 deletions src/NHibernate/Linq/Functions/StringGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,21 +317,15 @@ public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)

public class ToStringHqlGeneratorForMethod : IHqlGeneratorForMethod
{
private static readonly System.Type _guidType = typeof(Guid);

public IEnumerable<MethodInfo> SupportedMethods
{
get { throw new NotSupportedException(); }
}
public IEnumerable<MethodInfo> SupportedMethods => throw new NotSupportedException();

public HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
if (targetObject.Type == _guidType)
{
return treeBuilder.MethodCall("strguid", visitor.Visit(targetObject).AsExpression());
}
var methodName = targetObject.Type == typeof(Guid) || targetObject.Type == typeof(Guid?)
? "strguid"
: "str";

return treeBuilder.MethodCall("str", visitor.Visit(targetObject).AsExpression());
return treeBuilder.MethodCall(methodName, visitor.Visit(targetObject).AsExpression());
}
}
}