1
1
using System . Collections . Generic ;
2
2
using System . Data ;
3
- using NHibernate . Cfg ;
4
3
using NHibernate . Dialect . Function ;
5
4
using NHibernate . Driver ;
6
5
using NHibernate . SqlTypes ;
7
6
using NHibernate . Type ;
8
7
using NHibernate . Util ;
8
+ using Environment = NHibernate . Cfg . Environment ;
9
9
10
10
namespace NHibernate . Dialect
11
11
{
12
12
public class MsSql2008Dialect : MsSql2005Dialect
13
13
{
14
+ /// <summary>
15
+ /// Should <see cref="DbType.DateTime" /> be preserved instead of switching it to <see cref="DbType.DateTime2" />?
16
+ /// </summary>
17
+ /// <value>
18
+ /// <see langword="true" /> for preserving <see cref="DbType.DateTime" />, <see langword="false" /> for
19
+ /// replacing it with <see cref="DbType.DateTime2" />.
20
+ /// </value>
21
+ protected bool KeepDateTime { get ; private set ; }
22
+
23
+ public override void Configure ( IDictionary < string , string > settings )
24
+ {
25
+ base . Configure ( settings ) ;
26
+
27
+ KeepDateTime = PropertiesHelper . GetBoolean ( Environment . SqlTypesKeepDateTime , settings , false ) ;
28
+ if ( KeepDateTime )
29
+ {
30
+ // Re-register functions, they depend on this setting.
31
+ RegisterFunctions ( ) ;
32
+ }
33
+ }
34
+
14
35
protected override void RegisterDateTimeTypeMappings ( )
15
36
{
16
37
base . RegisterDateTimeTypeMappings ( ) ;
@@ -23,8 +44,15 @@ protected override void RegisterDateTimeTypeMappings()
23
44
protected override void RegisterFunctions ( )
24
45
{
25
46
base . RegisterFunctions ( ) ;
26
- RegisterFunction ( "current_timestamp" , new NoArgSQLFunction ( "sysdatetime" , NHibernateUtil . DateTime , true ) ) ;
27
- RegisterFunction ( "current_timestamp_offset" , new NoArgSQLFunction ( "sysdatetimeoffset" , NHibernateUtil . DateTimeOffset , true ) ) ;
47
+ if ( ! KeepDateTime )
48
+ {
49
+ RegisterFunction (
50
+ "current_timestamp" ,
51
+ new NoArgSQLFunction ( "sysdatetime" , NHibernateUtil . DateTime , true ) ) ;
52
+ }
53
+ RegisterFunction (
54
+ "current_timestamp_offset" ,
55
+ new NoArgSQLFunction ( "sysdatetimeoffset" , NHibernateUtil . DateTimeOffset , true ) ) ;
28
56
}
29
57
30
58
protected override void RegisterKeywords ( )
@@ -42,21 +70,26 @@ protected override void RegisterDefaultProperties()
42
70
}
43
71
44
72
public override string CurrentTimestampSQLFunctionName =>
45
- "SYSDATETIME()" ;
73
+ KeepDateTime ? base . CurrentTimestampSQLFunctionName : "SYSDATETIME()" ;
46
74
47
75
public override long TimestampResolutionInTicks =>
48
- // MS SQL resolution with datetime2 is 100ns, one tick.
49
- 1 ;
76
+ KeepDateTime
77
+ ? base . TimestampResolutionInTicks
78
+ // MS SQL resolution with datetime2 is 100ns, one tick.
79
+ : 1 ;
50
80
51
81
/// <inheritdoc />
52
82
public override SqlType [ ] RefineSqlTypes ( IType type , SqlType [ ] types )
53
83
{
54
- switch ( type )
84
+ if ( ! KeepDateTime )
55
85
{
56
- // Switch built-in DateTime types and their descendants to DateTime2.
57
- case DateTimeType _:
58
- case TimestampType _:
59
- return new [ ] { SqlTypeFactory . DateTime2 } ;
86
+ switch ( type )
87
+ {
88
+ // Switch built-in DateTime types and their descendants to DateTime2.
89
+ case DateTimeType _:
90
+ case TimestampType _:
91
+ return new [ ] { SqlTypeFactory . DateTime2 } ;
92
+ }
60
93
}
61
94
return base . RefineSqlTypes ( type , types ) ;
62
95
}
0 commit comments