Skip to content

Commit e5ad951

Browse files
authored
Fix enums used as parameter for string columns (#2622)
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
1 parent 10393df commit e5ad951

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2621Enum
4+
{
5+
public class Fixture : BugTestCase
6+
{
7+
[Test]
8+
public void TestOk()
9+
{
10+
using (var s = OpenSession())
11+
using (s.BeginTransaction())
12+
{
13+
var query = s.CreateQuery(
14+
@"SELECT Name FROM NHibernate.Test.NHSpecificTest.GH2621Enum.ClassWithString ROOT WHERE ROOT.Kind = :kind");
15+
query.SetParameter("kind", Kind.SomeKind);
16+
Assert.DoesNotThrow(() => query.List());
17+
}
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
namespace="NHibernate.Test.NHSpecificTest.GH2621Enum"
4+
assembly="NHibernate.Test"
5+
>
6+
<class name="ClassWithString" >
7+
<id name="Id">
8+
<generator class="increment"/>
9+
</id>
10+
<property name="Name"/>
11+
<property name="Kind"/>
12+
</class>
13+
14+
</hibernate-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH2621Enum
2+
{
3+
public abstract class ClassWithString
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
public virtual string Kind { get; set; }
8+
}
9+
10+
public enum Kind
11+
{
12+
SomeKind,
13+
SomeOtherKind
14+
}
15+
}

src/NHibernate/Type/AbstractStringType.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Data.Common;
44
using System.Globalization;
@@ -58,6 +58,9 @@ public override void Set(DbCommand cmd, object value, int index, ISessionImpleme
5858
{
5959
var parameter = cmd.Parameters[index];
6060

61+
if (value is Enum)
62+
value = value.ToString();
63+
6164
//Allow the driver to adjust the parameter for the value
6265
session.Factory.ConnectionProvider.Driver.AdjustParameterForValue(parameter, SqlType, value);
6366

0 commit comments

Comments
 (0)