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 ( )
@@ -47,12 +81,17 @@ protected override void RegisterDefaultProperties()
47
81
/// <inheritdoc />
48
82
public override SqlType [ ] RefineSqlTypes ( IType type , SqlType [ ] types )
49
83
{
50
- switch ( type )
84
+ #pragma warning disable 618
85
+ if ( ! KeepDateTime )
86
+ #pragma warning restore 618
51
87
{
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
+ }
56
95
}
57
96
return base . RefineSqlTypes ( type , types ) ;
58
97
}
0 commit comments