1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Data ;
3- using NHibernate . Cfg ;
44using NHibernate . Dialect . Function ;
55using NHibernate . Driver ;
66using NHibernate . SqlTypes ;
77using NHibernate . Type ;
88using NHibernate . Util ;
9+ using Environment = NHibernate . Cfg . Environment ;
910
1011namespace 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 ( )
@@ -45,18 +79,27 @@ protected override void RegisterDefaultProperties()
4579 "SYSDATETIME()" ;
4680
4781 public override long TimestampResolutionInTicks =>
48- // MS SQL resolution with datetime2 is 100ns, one tick.
49- 1 ;
82+ #pragma warning disable 618
83+ KeepDateTime
84+ #pragma warning restore 618
85+ ? base . TimestampResolutionInTicks
86+ // MS SQL resolution with datetime2 is 100ns, one tick.
87+ : 1 ;
5088
5189 /// <inheritdoc />
5290 public override SqlType [ ] RefineSqlTypes ( IType type , SqlType [ ] types )
5391 {
54- switch ( type )
92+ #pragma warning disable 618
93+ if ( ! KeepDateTime )
94+ #pragma warning restore 618
5595 {
56- // Switch built-in DateTime types and their descendants to DateTime2.
57- case DateTimeType _:
58- case TimestampType _:
59- return new [ ] { SqlTypeFactory . DateTime2 } ;
96+ switch ( type )
97+ {
98+ // Switch built-in DateTime types and their descendants to DateTime2.
99+ case DateTimeType _:
100+ case TimestampType _:
101+ return new [ ] { SqlTypeFactory . DateTime2 } ;
102+ }
60103 }
61104 return base . RefineSqlTypes ( type , types ) ;
62105 }
0 commit comments