Skip to content

Commit cb2dad6

Browse files
committed
Adjust DateTime parameters
1 parent cea4f90 commit cb2dad6

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/NHibernate.Test/TypesTest/AbstractDateTimeTypeFixture.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ protected DateTimeKind GetTypeKind()
548548
}
549549
}
550550

551-
public class ClientDriverWithParamsStats : IDriver
551+
public class ClientDriverWithParamsStats : IDriver, IParameterAdjuster
552552
{
553553
private readonly Dictionary<SqlType, int> _usedSqlTypes = new Dictionary<SqlType, int>();
554554
private readonly Dictionary<DbType, int> _usedDbTypes = new Dictionary<DbType, int>();
@@ -679,5 +679,11 @@ void IDriver.AdjustCommand(DbCommand command)
679679
DateTime IDriver.MinDate => _driverImplementation.MinDate;
680680

681681
#endregion
682+
683+
public void AdjustParameterForValue(DbParameter parameter, SqlType sqlType, object value)
684+
{
685+
var adjustingDriver = _driverImplementation as IParameterAdjuster;
686+
adjustingDriver?.AdjustParameterForValue(parameter, sqlType, value);
687+
}
682688
}
683689
}

src/NHibernate/Driver/NpgsqlDriver.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System;
12
using System.Data;
23
using System.Data.Common;
34
using NHibernate.AdoNet;
5+
using NHibernate.SqlTypes;
46

57
namespace NHibernate.Driver
68
{
@@ -25,7 +27,7 @@ namespace NHibernate.Driver
2527
/// <a href="http://pgfoundry.org/projects/npgsql">http://pgfoundry.org/projects/npgsql</a>.
2628
/// </p>
2729
/// </remarks>
28-
public class NpgsqlDriver : ReflectionBasedDriver, IEmbeddedBatcherFactoryProvider
30+
public class NpgsqlDriver : ReflectionBasedDriver, IEmbeddedBatcherFactoryProvider, IParameterAdjuster
2931
{
3032
/// <summary>
3133
/// Initializes a new instance of the <see cref="NpgsqlDriver"/> class.
@@ -74,10 +76,19 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
7476

7577
// Prior to v3, Npgsql was expecting DateTime for time.
7678
// https://github.com/npgsql/npgsql/issues/347
77-
public override bool RequiresTimeSpanForTime => (DriverVersion?.Major ?? 3) >= 3;
79+
public override bool RequiresTimeSpanForTime => DriverVersionMajor >= 3;
80+
81+
private int DriverVersionMajor => DriverVersion?.Major ?? 3;
7882

7983
public override bool HasDelayedDistributedTransactionCompletion => true;
8084

8185
System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass => typeof(GenericBatchingBatcherFactory);
86+
public void AdjustParameterForValue(DbParameter parameter, SqlType sqlType, object value)
87+
{
88+
if (DriverVersionMajor >= 6 && sqlType.DbType == DbType.DateTime && value is DateTime time && time.Kind != DateTimeKind.Utc)
89+
{
90+
parameter.DbType = DbType.DateTime2;
91+
}
92+
}
8293
}
8394
}

src/NHibernate/Type/AbstractDateTimeType.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Data.Common;
55
using System.Globalization;
6+
using NHibernate.Driver;
67
using NHibernate.Engine;
78
using NHibernate.SqlTypes;
89

@@ -90,7 +91,13 @@ public override void Set(DbCommand st, object value, int index, ISessionImplemen
9091
// hour with daylight shift to be always wrongly converted. So better just fail.
9192
if (Kind != DateTimeKind.Unspecified && dateValue.Kind != Kind)
9293
throw new ArgumentException($"{Name} expect date kind {Kind} but it is {dateValue.Kind}", nameof(value));
93-
st.Parameters[index].Value = AdjustDateTime(dateValue);
94+
95+
var parameter = st.Parameters[index];
96+
97+
//Allow the driver to adjust the parameter for the value
98+
var adjustedValue = AdjustDateTime(dateValue);
99+
session.Factory.ConnectionProvider.Driver.AdjustParameterForValue(parameter, SqlType, adjustedValue);
100+
parameter.Value = adjustedValue;
94101
}
95102

96103
#region IVersionType Members

0 commit comments

Comments
 (0)