Skip to content

Commit 67f96e7

Browse files
NH-3919 - Allow reverting to DateTime.
1 parent 2e24875 commit 67f96e7

File tree

4 files changed

+87
-11
lines changed

4 files changed

+87
-11
lines changed

src/NHibernate.Test/DialectTest/MsSql2008DialectFixture.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
using System.Reflection;
1+
using System;
2+
using System.Reflection;
23
using NHibernate.Cfg;
34
using NHibernate.Dialect;
45
using NHibernate.Engine;
56
using NHibernate.SqlTypes;
67
using NHibernate.Type;
78
using NUnit.Framework;
9+
using Environment = NHibernate.Cfg.Environment;
810

911
namespace NHibernate.Test.DialectTest
1012
{
1113
[TestFixture]
1214
public class MsSql2008DialectFixture
1315
{
1416
[Test]
15-
public void CheckSql2005DateTimeTypes()
17+
public void CheckPreSql2008DateTimeTypes()
1618
{
1719
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
1820
cfg.SetProperty(Environment.Dialect, typeof(MsSql2005Dialect).FullName);
@@ -45,6 +47,25 @@ public void CheckSql2008DateTimeTypes()
4547
#pragma warning restore 618
4648
}
4749

50+
[Test]
51+
[Obsolete]
52+
public void CheckKeepDateTime()
53+
{
54+
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
55+
cfg.SetProperty(Environment.Dialect, typeof(MsSql2008Dialect).FullName);
56+
cfg.SetProperty(Environment.SqlTypesKeepDateTime, "true");
57+
var mapping = GetMapping(cfg);
58+
AssertSqlType(NHibernateUtil.DateTime, SqlTypeFactory.DateTime, mapping);
59+
AssertSqlType(NHibernateUtil.Timestamp, SqlTypeFactory.DateTime, mapping);
60+
AssertSqlType(NHibernateUtil.TimestampUtc, SqlTypeFactory.DateTime, mapping);
61+
AssertSqlType(NHibernateUtil.DbTimestamp, SqlTypeFactory.DateTime, mapping);
62+
AssertSqlType(NHibernateUtil.LocalDateTime, SqlTypeFactory.DateTime, mapping);
63+
AssertSqlType(NHibernateUtil.UtcDateTime, SqlTypeFactory.DateTime, mapping);
64+
#pragma warning disable 618 // DateTime2 is obsolete
65+
AssertSqlType(NHibernateUtil.DateTime2, SqlTypeFactory.DateTime2, mapping);
66+
#pragma warning restore 618
67+
}
68+
4869
private static readonly FieldInfo _mappingField =
4970
typeof(Configuration).GetField("mapping", BindingFlags.Instance | BindingFlags.NonPublic);
5071

src/NHibernate/Cfg/Environment.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ public static string Version
208208
/// </summary>
209209
public const string OdbcDateTimeScale = "odbc.explicit_datetime_scale";
210210

211+
// Obsolete Since v5.0
212+
/// <summary>
213+
/// Disable switching built-in NHibernate date-time types from DbType.DateTime to DbType.DateTime2
214+
/// for dialects supporting datetime2.
215+
/// </summary>
216+
[Obsolete("Migrate SQL datetime type to datetime2 instead.")]
217+
public const string SqlTypesKeepDateTime = "sql_types.keep_datetime";
218+
211219
/// <summary>
212220
/// <para>Oracle has a dual Unicode support model.</para>
213221
/// <para>Either the whole database use an Unicode encoding, and then all string types

src/NHibernate/Dialect/MsSql2008Dialect.cs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Data;
3-
using NHibernate.Cfg;
44
using NHibernate.Dialect.Function;
55
using NHibernate.Driver;
66
using NHibernate.SqlTypes;
77
using NHibernate.Type;
88
using NHibernate.Util;
9+
using Environment = NHibernate.Cfg.Environment;
910

1011
namespace NHibernate.Dialect
1112
{
1213
public class MsSql2008Dialect : MsSql2005Dialect
1314
{
15+
private static readonly IInternalLogger _log = LoggerProvider.LoggerFor(typeof(MsSql2008Dialect));
16+
17+
// Since v5.0
18+
[Obsolete("Migrate SQL datetime type to datetime2 instead.")]
19+
protected bool KeepDateTime { get; private set; }
20+
21+
public override void Configure(IDictionary<string, string> settings)
22+
{
23+
base.Configure(settings);
24+
25+
#pragma warning disable 618 // KeepDateTime & SqlTypesKeepDateTime are obsolete
26+
KeepDateTime = PropertiesHelper.GetBoolean(Environment.SqlTypesKeepDateTime, settings, false);
27+
if (KeepDateTime)
28+
{
29+
_log.WarnFormat(
30+
"Using obsolete parameter {0} for reverting date time types back to DbType.DateTime instead of DateTime2",
31+
Environment.SqlTypesKeepDateTime);
32+
// Re-register functions, they depend on this setting.
33+
RegisterFunctions();
34+
}
35+
#pragma warning restore 618
36+
}
37+
1438
protected override void RegisterDateTimeTypeMappings()
1539
{
1640
base.RegisterDateTimeTypeMappings();
@@ -23,8 +47,18 @@ protected override void RegisterDateTimeTypeMappings()
2347
protected override void RegisterFunctions()
2448
{
2549
base.RegisterFunctions();
26-
RegisterFunction("current_timestamp", new NoArgSQLFunction("sysdatetime", NHibernateUtil.DateTime, true));
27-
RegisterFunction("current_timestamp_offset", new NoArgSQLFunction("sysdatetimeoffset", NHibernateUtil.DateTimeOffset, true));
50+
RegisterFunction(
51+
"current_timestamp",
52+
new NoArgSQLFunction(
53+
"sysdatetime",
54+
#pragma warning disable 618 // KeepDateTime & DateTime2 are obsoletes
55+
// If KeepDateTime, NHibernateUtil.DateTime will not be precise enough for sysdatetime
56+
KeepDateTime ? NHibernateUtil.DateTime2 : NHibernateUtil.DateTime,
57+
#pragma warning restore 618
58+
true));
59+
RegisterFunction(
60+
"current_timestamp_offset",
61+
new NoArgSQLFunction("sysdatetimeoffset", NHibernateUtil.DateTimeOffset, true));
2862
}
2963

3064
protected override void RegisterKeywords()
@@ -47,12 +81,17 @@ protected override void RegisterDefaultProperties()
4781
/// <inheritdoc />
4882
public override SqlType[] RefineSqlTypes(IType type, SqlType[] types)
4983
{
50-
switch (type)
84+
#pragma warning disable 618
85+
if (!KeepDateTime)
86+
#pragma warning restore 618
5187
{
52-
// Switch built-in DateTime types and their descendants to DateTime2.
53-
case DateTimeType _:
54-
case TimestampType _:
55-
return new[] { SqlTypeFactory.DateTime2 };
88+
switch (type)
89+
{
90+
// Switch built-in DateTime types and their descendants to DateTime2.
91+
case DateTimeType _:
92+
case TimestampType _:
93+
return new[] { SqlTypeFactory.DateTime2 };
94+
}
5695
}
5796
return base.RefineSqlTypes(type, types);
5897
}

src/NHibernate/nhibernate-configuration.xsd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@
161161
</xs:documentation>
162162
</xs:annotation>
163163
</xs:enumeration>
164+
<xs:enumeration value="sql_types.keep_datetime">
165+
<xs:annotation>
166+
<xs:documentation>
167+
Disable switching built-in NHibernate date-time types from DbType.DateTime to DbType.DateTime2
168+
for dialects supporting datetime2. Obsolete, consider migrating to datetime2 instead.
169+
</xs:documentation>
170+
</xs:annotation>
171+
</xs:enumeration>
164172
</xs:restriction>
165173
</xs:simpleType>
166174
</xs:attribute>

0 commit comments

Comments
 (0)