Skip to content

Commit 4093211

Browse files
wvopthazzik
authored andcommittedNov 25, 2020
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
1 parent bbe7fc9 commit 4093211

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
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

+5-2
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,13 +58,16 @@ 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 = Enum.GetName(value.GetType(), value);
63+
6164
//Allow the driver to adjust the parameter for the value
6265
session.Factory.ConnectionProvider.Driver.AdjustParameterForValue(parameter, SqlType, value);
6366

6467
// set the parameter value before the size check, since ODBC changes the size automatically
6568
parameter.Value = value;
6669

67-
if (parameter.Size > 0 && ((string)value).Length > parameter.Size)
70+
if (parameter.Size > 0 && value != null && ((string) value).Length > parameter.Size)
6871
throw new HibernateException("The length of the string value exceeds the length configured in the mapping/parameter.");
6972
}
7073

0 commit comments

Comments
 (0)