1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Data ;
3
- using NHibernate . Cfg ;
4
4
using NHibernate . Dialect . Function ;
5
5
using NHibernate . Driver ;
6
6
using NHibernate . SqlTypes ;
7
7
using NHibernate . Type ;
8
8
using NHibernate . Util ;
9
+ using Environment = NHibernate . Cfg . Environment ;
9
10
10
11
namespace NHibernate . Dialect
11
12
{
12
13
public class MsSql2008Dialect : MsSql2005Dialect
13
14
{
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
+
14
38
protected override void RegisterDateTimeTypeMappings ( )
15
39
{
16
40
base . RegisterDateTimeTypeMappings ( ) ;
@@ -23,8 +47,18 @@ protected override void RegisterDateTimeTypeMappings()
23
47
protected override void RegisterFunctions ( )
24
48
{
25
49
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 ) ) ;
28
62
}
29
63
30
64
protected override void RegisterKeywords ( )
@@ -45,18 +79,27 @@ protected override void RegisterDefaultProperties()
45
79
"SYSDATETIME()" ;
46
80
47
81
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 ;
50
88
51
89
/// <inheritdoc />
52
90
public override SqlType [ ] RefineSqlTypes ( IType type , SqlType [ ] types )
53
91
{
54
- switch ( type )
92
+ #pragma warning disable 618
93
+ if ( ! KeepDateTime )
94
+ #pragma warning restore 618
55
95
{
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
+ }
60
103
}
61
104
return base . RefineSqlTypes ( type , types ) ;
62
105
}
0 commit comments