Skip to content

Commit 35637c6

Browse files
committed
Let Npgsql 6 decide how to handle DateTime parameters
1 parent 457d767 commit 35637c6

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<PackageReference Include="NUnit" Version="3.13.2" />
6464
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
6565
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="6.6.0" />
66-
<PackageReference Include="Npgsql" Version="5.0.11" />
66+
<PackageReference Include="Npgsql" Version="6.0.4" />
6767
</ItemGroup>
6868
<ItemGroup Condition="$(NhNetFx)">
6969
<Reference Include="System.Configuration" />

src/NHibernate/Driver/NpgsqlDriver.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,33 @@ public override IResultSetsCommand GetResultSetsCommand(Engine.ISessionImplement
6565

6666
protected override void InitializeParameter(DbParameter dbParam, string name, SqlTypes.SqlType sqlType)
6767
{
68-
base.InitializeParameter(dbParam, name, sqlType);
68+
if (sqlType == null)
69+
throw new QueryException($"No type assigned to parameter '{name}'");
6970

70-
// Since the .NET currency type has 4 decimal places, we use a decimal type in PostgreSQL instead of its native 2 decimal currency type.
71+
dbParam.ParameterName = FormatNameForParameter(name);
7172
if (sqlType.DbType == DbType.Currency)
73+
{
74+
// Since the .NET currency type has 4 decimal places, we use a decimal type in PostgreSQL instead of its native 2 decimal currency type.
7275
dbParam.DbType = DbType.Decimal;
76+
}
77+
else if (DriverVersionMajor < 6 || sqlType.DbType != DbType.DateTime)
78+
{
79+
dbParam.DbType = sqlType.DbType;
80+
}
81+
else
82+
{
83+
// Let Npgsql 6 driver to decide parameter type
84+
}
7385
}
7486

7587
// Prior to v3, Npgsql was expecting DateTime for time.
7688
// https://github.com/npgsql/npgsql/issues/347
77-
public override bool RequiresTimeSpanForTime => (DriverVersion?.Major ?? 3) >= 3;
89+
public override bool RequiresTimeSpanForTime => DriverVersionMajor >= 3;
7890

7991
public override bool HasDelayedDistributedTransactionCompletion => true;
8092

8193
System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass => typeof(GenericBatchingBatcherFactory);
94+
95+
private int DriverVersionMajor => DriverVersion?.Major ?? 3;
8296
}
8397
}

0 commit comments

Comments
 (0)