Skip to content

Fix Guid.ToString() #1856

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 26 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3426/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace NHibernate.Test.NHSpecificTest.NH3426
{
public class Entity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
}
63 changes: 63 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3426/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3426
{
[TestFixture]
public class Fixture : TestCaseMappingByCode
{

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id);
rc.Property(x => x.Name);
});
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

private static readonly string _id = "9FF2D288-56E6-F349-9CFC-48902132D65B";

protected override void OnSetUp()
{
using (var session = OpenSession())
{
session.Save(new Entity { Id = Guid.Parse(_id), Name = "Name 1" });

session.Flush();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
{
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}
}

[Test]
public void SelectGuidToString()
{
using (var session = OpenSession())
{
var list = session.Query<Entity>()
.Select(x => new { Id = x.Id.ToString() })
.ToList();

Assert.AreEqual(_id.ToUpper(), list[0].Id.ToUpper());
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="5.12.1" />
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="6.3.0" />
<PackageReference Include="Npgsql" Version="3.2.4.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Dialect/Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ protected Dialect()
RegisterFunction("bnot", new Function.BitwiseNativeOperation("~", true));

RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as char)"));
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "?1"));

// register hibernate types for default use in scalar sqlquery type auto detection
RegisterHibernateType(DbType.Int64, NHibernateUtil.Int64.Name);
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Dialect/FirebirdDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ private void OverrideStandardHQLFunctions()
RegisterFunction("upper", new StandardSafeSQLFunction("upper", NHibernateUtil.String, 1));
RegisterFunction("mod", new StandardSafeSQLFunction("mod", NHibernateUtil.Double, 2));
RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as VARCHAR(255))"));
RegisterFunction("strguid", new StandardSQLFunction("uuid_to_char", NHibernateUtil.String));
RegisterFunction("sysdate", new CastedFunction("today", NHibernateUtil.Date));
RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "cast(?1 as date)"));
// Bitwise operations
Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/Dialect/MySQL55Dialect.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using System.Data;
using NHibernate.Dialect.Function;

namespace NHibernate.Dialect
{
Expand All @@ -10,4 +11,4 @@ public MySQL55Dialect()
RegisterColumnType(DbType.Guid, "CHAR(36)");
}
}
}
}
3 changes: 3 additions & 0 deletions src/NHibernate/Dialect/MySQL5Dialect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using NHibernate.Dialect.Function;
using NHibernate.SqlCommand;

namespace NHibernate.Dialect
Expand All @@ -22,6 +23,8 @@ protected override void RegisterCastTypes() {
RegisterCastType(DbType.Double, "DECIMAL(19,5)");
RegisterCastType(DbType.Single, "DECIMAL(19,5)");
RegisterCastType(DbType.Guid, "BINARY(16)");

RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "concat(hex(reverse(substr(?1, 1, 4))), '-', hex(reverse(substring(?1, 5, 2))), '-', hex(reverse(substr(?1, 7, 2))), '-', hex(substr(?1, 9, 2)), '-', hex(substr(?1, 11)))"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So works both under Linux and Windows. But endianness is processor dependent. I guess both run under x86/amd64 systems. Anyone able of testing this with a PowerPC maybe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not have problems with endianness because on these 2 systems Guids are stored as .ToByteArray() and retrieved by new Guid(byte[]) on a client side. As far as I know modern .NET Fx and .NET Core do not run on big-endian platforms. (Mono does, but we have much bigger problems there)

What we, potentially, can do is to add a runtime check into a function generator.

We also need to have a test which checks a server generated Guids.

}

//Reference 5.x
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Dialect/Oracle8iDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ protected virtual void RegisterFunctions()
RegisterFunction("next_day", new StandardSQLFunction("next_day", NHibernateUtil.Date));

RegisterFunction("str", new StandardSQLFunction("to_char", NHibernateUtil.String));
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "substr(rawtohex(?1), 7, 2) || substr(rawtohex(?1), 5, 2) || substr(rawtohex(?1), 3, 2) || substr(rawtohex(?1), 1, 2) || '-' || substr(rawtohex(?1), 11, 2) || substr(rawtohex(?1), 9, 2) || '-' || substr(rawtohex(?1), 15, 2) || substr(rawtohex(?1), 13, 2) || '-' || substr(rawtohex(?1), 17, 4) || '-' || substr(rawtohex(?1), 21) "));

RegisterFunction("iif", new SQLFunctionTemplate(null, "case when ?1 then ?2 else ?3 end"));

Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/Dialect/PostgreSQL82Dialect.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Data;
using NHibernate.Dialect.Function;

namespace NHibernate.Dialect
{
Expand Down Expand Up @@ -28,4 +29,4 @@ public override string GetDropSequenceString(string sequenceName)
return string.Concat("drop sequence if exists ", sequenceName);
}
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate/Dialect/SybaseASE15Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public SybaseASE15Dialect()
RegisterFunction("sqrt", new StandardSQLFunction("sqrt", NHibernateUtil.Double));
RegisterFunction("square", new StandardSQLFunction("square"));
RegisterFunction("str", new StandardSQLFunction("str", NHibernateUtil.String));
RegisterFunction("strguid", new StandardSQLFunction("str", NHibernateUtil.String));
RegisterFunction("tan", new StandardSQLFunction("tan", NHibernateUtil.Double));
// TODO RegisterFunction("trim", new SQLFunctionTemplate(NHibernateUtil.String, "ltrim(rtrim(?1))"));
RegisterFunction("upper", new StandardSQLFunction("upper"));
Expand Down
6 changes: 6 additions & 0 deletions src/NHibernate/Linq/Functions/StringGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,19 @@ 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 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());
}

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