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 5 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; }
}
}
68 changes: 68 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3426/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is SQLiteDialect;
}

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(list[0].Id.ToUpper(), _id.ToUpper());
}
}
}
}
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 SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as VARCHAR(255))"));
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
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 StandardSQLFunction("to_char", NHibernateUtil.String));

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

Expand Down
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