Skip to content

Commit 766a403

Browse files
committed
Fix odbc tests
1 parent 97e4ebd commit 766a403

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

src/NHibernate/Driver/OdbcDriver.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,18 @@ private void SetVariableLengthParameterSize(DbParameter dbParam, SqlType sqlType
7878
{
7979
switch (dbParam.DbType)
8080
{
81-
case DbType.AnsiString:
8281
case DbType.AnsiStringFixedLength:
83-
case DbType.String:
8482
case DbType.StringFixedLength:
83+
// For types that are using one character (CharType, AnsiCharType, TrueFalseType, YesNoType and EnumCharType),
84+
// we have to specify the length otherwise sql function like charindex won't work as expected.
85+
if (sqlType.Length == 1)
86+
{
87+
dbParam.Size = sqlType.Length;
88+
}
89+
90+
break;
91+
case DbType.AnsiString:
92+
case DbType.String:
8593
// NH-4083: do not limit to column length if above 2000. Setting size may trigger conversion from
8694
// nvarchar to ntext when size is superior or equal to 2000, causing some queries to fail:
8795
// https://stackoverflow.com/q/8569844/1178314

src/NHibernate/Driver/SqlClientDriver.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
161161
{
162162
case DbType.AnsiString:
163163
case DbType.AnsiStringFixedLength:
164-
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForAnsiClob : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
164+
dbParam.Size = IsAnsiText(dbParam, sqlType)
165+
? MsSql2000Dialect.MaxSizeForAnsiClob
166+
: IsChar(dbParam, sqlType) ? sqlType.Length : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
165167
break;
166168
case DbType.Binary:
167169
dbParam.Size = IsBlob(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForBlob : MsSql2000Dialect.MaxSizeForLengthLimitedBinary;
@@ -174,7 +176,9 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
174176
break;
175177
case DbType.String:
176178
case DbType.StringFixedLength:
177-
dbParam.Size = IsText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForClob : MsSql2000Dialect.MaxSizeForLengthLimitedString;
179+
dbParam.Size = IsText(dbParam, sqlType)
180+
? MsSql2000Dialect.MaxSizeForClob
181+
: IsChar(dbParam, sqlType) ? sqlType.Length : MsSql2000Dialect.MaxSizeForLengthLimitedString;
178182
break;
179183
case DbType.DateTime2:
180184
dbParam.Size = MsSql2000Dialect.MaxDateTime2;
@@ -283,6 +287,17 @@ protected static bool IsBlob(DbParameter dbParam, SqlType sqlType)
283287
return (sqlType is BinaryBlobSqlType) || ((DbType.Binary == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedBinary));
284288
}
285289

290+
/// <summary>
291+
/// Interprets if a parameter is a character (for the purposes of setting its default size)
292+
/// </summary>
293+
/// <param name="dbParam">The parameter</param>
294+
/// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param>
295+
/// <returns>True, if the parameter should be interpreted as a character, otherwise False</returns>
296+
protected static bool IsChar(DbParameter dbParam, SqlType sqlType)
297+
{
298+
return (DbType.StringFixedLength == dbParam.DbType || DbType.StringFixedLength == dbParam.DbType) &&
299+
sqlType.LengthDefined && sqlType.Length == 1;
300+
}
286301
public override IResultSetsCommand GetResultSetsCommand(ISessionImplementor session)
287302
{
288303
return new BasicResultSetsCommand(session);

src/NHibernate/Driver/SqlServerCeDriver.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
7676
{
7777
base.InitializeParameter(dbParam, name, AdjustSqlType(sqlType));
7878

79+
// For types that are using one character (CharType, AnsiCharType, TrueFalseType, YesNoType and EnumCharType),
80+
// we have to specify the length otherwise sql function like charindex won't work as expected.
81+
if (sqlType.LengthDefined && sqlType.Length == 1)
82+
{
83+
dbParam.Size = sqlType.Length;
84+
}
85+
7986
AdjustDbParamTypeForLargeObjects(dbParam, sqlType);
8087
}
8188

src/NHibernate/Type/AbstractCharType.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ public override System.Type ReturnedClass
4848
public override void Set(DbCommand cmd, object value, int index, ISessionImplementor session)
4949
{
5050
cmd.Parameters[index].Value = Convert.ToChar(value);
51-
// Some databases (e.g. Sql Server) require the length for char, otherwise functions like IndexOf('a')
52-
// won't work as expected.
53-
cmd.Parameters[index].Size = SqlType.Length;
5451
}
5552

5653
public override string ObjectToSQLString(object value, Dialect.Dialect dialect)

0 commit comments

Comments
 (0)