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 all 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
88 changes: 88 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH3426/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.NH3426
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : 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 const 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 async Task SelectGuidToStringAsync()
{
using (var session = OpenSession())
{
var list = await (session.Query<Entity>()
.Select(x => new { Id = x.Id.ToString() })
.ToListAsync());

Assert.AreEqual(id.ToUpper(), list[0].Id.ToUpper());
}
}

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

Assert.That(list, Has.Count.EqualTo(1));
}
}
}
}
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; }
}
}
76 changes: 76 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3426/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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 const 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());
}
}

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

Assert.That(list, Has.Count.EqualTo(1));
}
}
}
}
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
4 changes: 3 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,7 @@ public class MySQL55Dialect : MySQL5Dialect
public MySQL55Dialect()
{
RegisterColumnType(DbType.Guid, "CHAR(36)");
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "?1"));
}
}
}
}
6 changes: 5 additions & 1 deletion 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 @@ -11,9 +12,12 @@ public MySQL5Dialect()
// My SQL supports precision up to 65, but .Net is limited to 28-29.
RegisterColumnType(DbType.Decimal, 29, "DECIMAL($p, $s)");
RegisterColumnType(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)))"));
}

protected override void RegisterCastTypes() {
protected override void RegisterCastTypes()
{
base.RegisterCastTypes();
// MySql 5 also supports DECIMAL as a cast type target
// http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html
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
2 changes: 2 additions & 0 deletions src/NHibernate/Dialect/PostgreSQLDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public PostgreSQLDialect()

// Register the date function, since when used in LINQ select clauses, NH must know the data type.
RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "cast(?1 as date)"));

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

RegisterKeywords();
}
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 @@ -94,6 +94,8 @@ protected virtual void RegisterFunctions()

// NH-3787: SQLite requires the cast in SQL too for not defaulting to string.
RegisterFunction("transparentcast", new CastFunction());

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

#region private static readonly string[] DialectKeywords = { ... }
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));
RegisterFunction("trim", new AnsiTrimEmulationFunction("str_replace"));
RegisterFunction("upper", new StandardSQLFunction("upper"));
Expand Down
7 changes: 7 additions & 0 deletions src/NHibernate/Linq/Functions/StringGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,20 @@ 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