Skip to content

Fix enums used as parameter for string columns #2622

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 5 commits into from
Oct 31, 2022
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
20 changes: 20 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2621Enum/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2621Enum
{
public class Fixture : BugTestCase
{
[Test]
public void TestOk()
{
using (var s = OpenSession())
using (s.BeginTransaction())
{
var query = s.CreateQuery(
@"SELECT Name FROM NHibernate.Test.NHSpecificTest.GH2621Enum.ClassWithString ROOT WHERE ROOT.Kind = :kind");
query.SetParameter("kind", Kind.SomeKind);
Copy link
Member

@hazzik hazzik Dec 9, 2020

Choose a reason for hiding this comment

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

Why not using one of the following methods?

/* 1 */ query.SetParameter("kind", Kind.SomeKind, NHibernateUtil.Enum(typeof(Kind)));
/* 2 */ query.SetEnum("kind", Kind.SomeKind);
/* 3 */ query.SetParameter("kind", Kind.SomeKind.ToString());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it's an existing code base where I cannot check all calls to SetParameter.

I would say that the change is a very specific one that does not hurt.

Assert.DoesNotThrow(() => query.List());
}
}
}
}
14 changes: 14 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2621Enum/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernate.Test.NHSpecificTest.GH2621Enum"
assembly="NHibernate.Test"
>
<class name="ClassWithString" >
<id name="Id">
<generator class="increment"/>
</id>
<property name="Name"/>
<property name="Kind"/>
</class>

</hibernate-mapping>
15 changes: 15 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2621Enum/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NHibernate.Test.NHSpecificTest.GH2621Enum
{
public abstract class ClassWithString
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Kind { get; set; }
}

public enum Kind
{
SomeKind,
SomeOtherKind
}
}
5 changes: 4 additions & 1 deletion src/NHibernate/Type/AbstractStringType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Globalization;
Expand Down Expand Up @@ -58,6 +58,9 @@ public override void Set(DbCommand cmd, object value, int index, ISessionImpleme
{
var parameter = cmd.Parameters[index];

if (value is Enum)
value = value.ToString();

//Allow the driver to adjust the parameter for the value
session.Factory.ConnectionProvider.Driver.AdjustParameterForValue(parameter, SqlType, value);

Expand Down