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 17 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; }
}
}
64 changes: 64 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3426/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Dialect;
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());
}
}
}
}
4 changes: 2 additions & 2 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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 Expand Up @@ -97,4 +97,4 @@
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>
</Project>
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, "cast(?1 as char)"));

// 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
1 change: 1 addition & 0 deletions src/NHibernate/Dialect/MsSql2000Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ protected virtual void RegisterFunctions()
// Casting to CHAR (without specified length) truncates to 30 characters.
// A longer version would be safer, but 50 is enough to prevent errors when casting uniqueidentifer to a string representation (NH-2858)
RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as nvarchar(50))"));
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as nvarchar(50))"));

RegisterFunction("substring", new EmulatedLengthSubstringFunction());

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Dialect/MsSqlCeDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ protected virtual void RegisterFunctions()
{
RegisterFunction("substring", new EmulatedLengthSubstringFunction());
RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as nvarchar)"));
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as nvarchar)"));

RegisterFunction("current_timestamp", new NoArgSQLFunction("getdate", NHibernateUtil.DateTime, true));
RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.DateTime, "dateadd(dd, 0, datediff(dd, 0, ?1))"));
Expand Down
5 changes: 4 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 @@ -8,6 +9,8 @@ public class MySQL55Dialect : MySQL5Dialect
public MySQL55Dialect()
{
RegisterColumnType(DbType.Guid, "CHAR(36)");

RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "?1"));
}
}
}
}
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
2 changes: 2 additions & 0 deletions src/NHibernate/Dialect/MySQLDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public MySQLDialect()
//special:
RegisterColumnType(DbType.Guid, "VARCHAR(40)");

RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "?1"));

RegisterKeywords();

RegisterCastTypes();
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 @@ -300,6 +300,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
4 changes: 3 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 All @@ -16,6 +17,7 @@ public class PostgreSQL82Dialect : PostgreSQL81Dialect
public PostgreSQL82Dialect()
{
RegisterColumnType(DbType.Guid, "uuid");
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as varchar(36))"));
}

public override bool SupportsIfExistsBeforeTableName
Expand All @@ -28,4 +30,4 @@ public override string GetDropSequenceString(string sequenceName)
return string.Concat("drop sequence if exists ", sequenceName);
}
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate/Dialect/PostgreSQLDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public PostgreSQLDialect()
// Override standard HQL function
RegisterFunction("current_timestamp", new NoArgSQLFunction("now", NHibernateUtil.DateTime, true));
RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as varchar)"));
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as nvarchar)"));
RegisterFunction("locate", new PositionSubstringFunction());
RegisterFunction("iif", new SQLFunctionTemplate(null, "case when ?1 then ?2 else ?3 end"));
RegisterFunction("replace", new StandardSQLFunction("replace", NHibernateUtil.String));
Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/Dialect/SQLiteDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ protected virtual void RegisterFunctions()
// 'YYYY-MM-DD 00:00:00' format for the date function.
RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "datetime(date(?1))"));

RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "?1"));

RegisterFunction("substring", new StandardSQLFunction("substr", NHibernateUtil.String));
RegisterFunction("left", new SQLFunctionTemplate(NHibernateUtil.String, "substr(?1,1,?2)"));
RegisterFunction("trim", new AnsiTrimEmulationFunction());
Expand Down
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
1 change: 1 addition & 0 deletions src/NHibernate/Dialect/SybaseSQLAnywhere10Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ protected virtual void RegisterStringFunctions()
RegisterFunction("soundex", new StandardSQLFunction("soundex", NHibernateUtil.Int32));
RegisterFunction("space", new StandardSQLFunction("space", NHibernateUtil.String));
RegisterFunction("str", new VarArgsSQLFunction(NHibernateUtil.String, "str(", ",", ")"));
RegisterFunction("strguid", new VarArgsSQLFunction(NHibernateUtil.String, "str(", "," ,")"));
RegisterFunction("string", new VarArgsSQLFunction(NHibernateUtil.String, "string(", ",", ")"));
RegisterFunction("strtouuid", new StandardSQLFunction("strtouuid"));
RegisterFunction("stuff", new StandardSQLFunction("stuff", NHibernateUtil.String));
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