Skip to content

Commit ab66b2f

Browse files
committed
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 58e3cb2 commit ab66b2f

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 (ISession s = OpenSession())
11+
{
12+
using (ITransaction t = s.BeginTransaction())
13+
{
14+
var query = s.CreateQuery(@"
15+
SELECT Name FROM NHibernate.Test.NHSpecificTest.GH2620Enum.ClassWithString ROOT WHERE ROOT.Kind = :kind");
16+
query.SetParameter("kind", Kind.SomeKind);
17+
Assert.DoesNotThrow(() => query.List());
18+
}
19+
}
20+
}
21+
}
22+
}
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/Driver/MicrosoftDataSqlClientDriver.cs

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public MicrosoftDataSqlClientDriver()
7575
/// <inheritdoc />
7676
public virtual void AdjustParameterForValue(DbParameter parameter, SqlType sqlType, object value)
7777
{
78+
if (value is Enum)
79+
value = Enum.GetName(value.GetType(), value);
80+
7881
if (value is string stringVal)
7982
switch (parameter.DbType)
8083
{

src/NHibernate/Driver/SqlClientDriver.cs

+3
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ public override bool SupportsMultipleQueries
320320

321321
public virtual void AdjustParameterForValue(DbParameter parameter, SqlType sqlType, object value)
322322
{
323+
if (value is Enum)
324+
value = Enum.GetName(value.GetType(), value);
325+
323326
if (value is string stringVal)
324327
{
325328
switch (parameter.DbType)

src/NHibernate/Type/AbstractStringType.cs

+2-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;
@@ -64,7 +64,7 @@ public override void Set(DbCommand cmd, object value, int index, ISessionImpleme
6464
// set the parameter value before the size check, since ODBC changes the size automatically
6565
parameter.Value = value;
6666

67-
if (parameter.Size > 0 && ((string)value).Length > parameter.Size)
67+
if (parameter.Size > 0 && value != null && Convert.ToString(value).Length > parameter.Size)
6868
throw new HibernateException("The length of the string value exceeds the length configured in the mapping/parameter.");
6969
}
7070

0 commit comments

Comments
 (0)