Skip to content

Commit

Permalink
Fix enums used as parameter for string columns (#2622)
Browse files Browse the repository at this point in the history
Special treatment of enums mapped on string columns because all reasons
for checking string lengths or streamlining the length of parameters of
queries on string columns to the length of the column for SQL Server
are also valid for enums because they are sometimes by convention used
to model the possible values of a string column

* Fixes #2621
  • Loading branch information
csharper2010 authored Oct 31, 2022
1 parent 10393df commit e5ad951
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
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);
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

0 comments on commit e5ad951

Please sign in to comment.