Skip to content

Commit

Permalink
Fix incorrect parameter length for char types in MicrosoftDataSqlClie…
Browse files Browse the repository at this point in the history
…ntDriver (nhibernate#3360)

* Run tests for MicrosoftDataSqlClientDriver
* Fix character parameter lengths for MicrosoftDataSqlClientDriver
  • Loading branch information
hazzik authored Jul 16, 2023
1 parent 5e1ff9f commit 04c4d52
Showing 7 changed files with 49 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/NetCoreTests.yml
Original file line number Diff line number Diff line change
@@ -12,6 +12,10 @@ jobs:
CONNECTION_STRING: "Server=localhost;initial catalog=nhibernate;User Id=sa;Password=P@ssw0rd;packet size=4096;"
DB_INIT: |
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P@ssw0rd" -e "MSSQL_PID=Express" -p 1433:1433 -d --name sqlexpress mcr.microsoft.com/mssql/server:2019-latest;
- DB: SqlServer2008-MicrosoftDataSqlClientDriver
CONNECTION_STRING: "Server=localhost;initial catalog=nhibernate;User Id=sa;Password=P@ssw0rd;packet size=4096;"
DB_INIT: |
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P@ssw0rd" -e "MSSQL_PID=Express" -p 1433:1433 -d --name sqlexpress mcr.microsoft.com/mssql/server:2019-latest;
- DB: PostgreSQL
CONNECTION_STRING: "Host=localhost;Username=nhibernate;Password=nhibernate;Database=nhibernate;Enlist=true;"
DB_INIT: |
4 changes: 4 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ environment:
matrix:
- DB: SqlServer2008
CONNECTION_STRING: Server=(local)\SQL2017;User ID=sa;Password=Password12!;initial catalog=nhibernate;
- DB: SqlServer2008-MicrosoftDataSqlClientDriver
CONNECTION_STRING: Server=(local)\SQL2017;User ID=sa;Password=Password12!;initial catalog=nhibernate;
- DB: PostgreSQL
CONNECTION_STRING: Host=localhost;Port=5432;Username=postgres;Password=Password12!;Database=nhibernate;Enlist=true;
- DB: Firebird
@@ -73,7 +75,9 @@ before_test:
Start-Service 'postgresql-x64-10'
}
'SqlServer2008' { Start-Service 'MSSQL$SQL2017' }
'SqlServer2008-MicrosoftDataSqlClientDriver' { Start-Service 'MSSQL$SQL2017' }
'SqlServer2012' { Start-Service 'MSSQL$SQL2017' }
'SqlServer2012-MicrosoftDataSqlClientDriver' { Start-Service 'MSSQL$SQL2017' }
'SQLite' { }
}
test_script:
13 changes: 13 additions & 0 deletions psake.ps1
Original file line number Diff line number Diff line change
@@ -63,9 +63,22 @@ Task Set-Configuration {
};
'SqlServer2008' = @{
'connection.connection_string' = 'Server=(local)\SQL2017;User ID=sa;Password=Password12!;initial catalog=nhibernate;'
'connection.driver_class' = 'NHibernate.Driver.Sql2008ClientDriver';
'dialect' = 'NHibernate.Dialect.MsSql2008Dialect'
};
'SqlServer2012' = @{
'connection.connection_string' = 'Server=(local)\SQL2017;User ID=sa;Password=Password12!;initial catalog=nhibernate;';
'connection.driver_class' = 'NHibernate.Driver.Sql2008ClientDriver';
'dialect' = 'NHibernate.Dialect.MsSql2012Dialect'
};
'SqlServer2008-MicrosoftDataSqlClientDriver' = @{
'connection.connection_string' = 'Server=(local)\SQL2017;User ID=sa;Password=Password12!;initial catalog=nhibernate;'
'connection.driver_class' = 'NHibernate.Driver.MicrosoftDataSqlClientDriver';
'dialect' = 'NHibernate.Dialect.MsSql2008Dialect'
};
'SqlServer2012-MicrosoftDataSqlClientDriver' = @{
'connection.connection_string' = 'Server=(local)\SQL2017;User ID=sa;Password=Password12!;initial catalog=nhibernate;';
'connection.driver_class' = 'NHibernate.Driver.MicrosoftDataSqlClientDriver';
'dialect' = 'NHibernate.Dialect.MsSql2012Dialect'
};
'Oracle' = @{
1 change: 1 addition & 0 deletions src/NHibernate.TestDatabaseSetup/TestDatabaseSetup.cs
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ public class DatabaseSetup
{
{"NHibernate.Driver.SqlClientDriver", SetupSqlServer},
{"NHibernate.Driver.Sql2008ClientDriver", SetupSqlServer},
{"NHibernate.Driver.MicrosoftDataSqlClientDriver", SetupSqlServer},
{"NHibernate.Driver.FirebirdClientDriver", SetupFirebird},
{"NHibernate.Driver.NpgsqlDriver", SetupNpgsql},
{"NHibernate.Driver.OracleDataClientDriver", SetupOracle},
16 changes: 14 additions & 2 deletions src/NHibernate/Driver/MicrosoftDataSqlClientDriver.cs
Original file line number Diff line number Diff line change
@@ -112,7 +112,7 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
case DbType.AnsiStringFixedLength:
dbParam.Size = IsAnsiText(dbParam, sqlType)
? MsSql2000Dialect.MaxSizeForAnsiClob
: MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
: IsChar(dbParam, sqlType) ? sqlType.Length : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
break;
case DbType.Binary:
dbParam.Size = IsBlob(dbParam, sqlType)
@@ -130,7 +130,7 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
case DbType.StringFixedLength:
dbParam.Size = IsText(dbParam, sqlType)
? MsSql2000Dialect.MaxSizeForClob
: MsSql2000Dialect.MaxSizeForLengthLimitedString;
: IsChar(dbParam, sqlType) ? sqlType.Length : MsSql2000Dialect.MaxSizeForLengthLimitedString;
break;
case DbType.DateTime2:
dbParam.Size = MsSql2000Dialect.MaxDateTime2;
@@ -199,6 +199,18 @@ protected static bool IsBlob(DbParameter dbParam, SqlType sqlType)
return sqlType is BinaryBlobSqlType || DbType.Binary == dbParam.DbType && sqlType.LengthDefined &&
sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedBinary;
}

/// <summary>
/// Interprets if a parameter is a character (for the purposes of setting its default size)
/// </summary>
/// <param name="dbParam">The parameter</param>
/// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param>
/// <returns>True, if the parameter should be interpreted as a character, otherwise False</returns>
protected static bool IsChar(DbParameter dbParam, SqlType sqlType)
{
return sqlType.LengthDefined && sqlType.Length == 1 &&
(dbParam.DbType == DbType.StringFixedLength || dbParam.DbType == DbType.AnsiStringFixedLength);
}

/// <inheritdoc />
public override IResultSetsCommand GetResultSetsCommand(ISessionImplementor session)
4 changes: 2 additions & 2 deletions src/NHibernate/Driver/SqlClientDriver.cs
Original file line number Diff line number Diff line change
@@ -295,8 +295,8 @@ protected static bool IsBlob(DbParameter dbParam, SqlType sqlType)
/// <returns>True, if the parameter should be interpreted as a character, otherwise False</returns>
protected static bool IsChar(DbParameter dbParam, SqlType sqlType)
{
return (DbType.StringFixedLength == dbParam.DbType || DbType.AnsiStringFixedLength == dbParam.DbType) &&
sqlType.LengthDefined && sqlType.Length == 1;
return sqlType.LengthDefined && sqlType.Length == 1 &&
(dbParam.DbType == DbType.StringFixedLength || dbParam.DbType == DbType.AnsiStringFixedLength);
}

public override IResultSetsCommand GetResultSetsCommand(ISessionImplementor session)
11 changes: 11 additions & 0 deletions teamcity.build
Original file line number Diff line number Diff line change
@@ -67,11 +67,22 @@
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.Sql2008ClientDriver" />
</target>

<target name="setup-teamcity-sqlServer-MicrosoftDataSqlClientDriver">
<property name="db-service" value="MSSQL$SQLEXPRESS" />
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.MicrosoftDataSqlClientDriver" />
</target>

<target name="setup-teamcity-sqlServer2012">
<property name="db-service" value="MSSQL$SQLEXPRESS" />
<property name="nhibernate.dialect" value="NHibernate.Dialect.MsSql2012Dialect" />
</target>

<target name="setup-teamcity-sqlServer2012-MicrosoftDataSqlClientDriver">
<property name="db-service" value="MSSQL$SQLEXPRESS" />
<property name="nhibernate.dialect" value="NHibernate.Dialect.MsSql2012Dialect" />
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.MicrosoftDataSqlClientDriver" />
</target>

<target name="setup-teamcity-sqlServerCe32">
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.SqlServerCeDriver" />
<property name="nhibernate.dialect" value="NHibernate.Dialect.MsSqlCe40Dialect" />

0 comments on commit 04c4d52

Please sign in to comment.