Skip to content

Commit 430a7e3

Browse files
NH-3919 - Use DateTime2 for dialects supporting it.
* NHibernate date time types (datetime, timestamp, localdatetime, utcdatetime, dbtimestamp, timestamputc) use db type datetime2 if dialect supports it. * NHibernate type datetime2 is obsolete.
1 parent 60839bd commit 430a7e3

31 files changed

+637
-72
lines changed

src/NHibernate.Test/Async/TypesTest/DateTime2TypeFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace NHibernate.Test.TypesTest
2020
/// TestFixtures for the <see cref="DateTimeType"/>.
2121
/// </summary>
2222
[TestFixture]
23+
[Obsolete]
2324
public class DateTime2TypeFixtureAsync
2425
{
2526
[Test]
@@ -39,4 +40,4 @@ public async Task SeedAsync()
3940
Assert.IsTrue(await (type.SeedAsync(null, CancellationToken.None)) is DateTime, "seed should be DateTime");
4041
}
4142
}
42-
}
43+
}

src/NHibernate.Test/Async/TypesTest/DateTimeTypeFixture.cs

Lines changed: 179 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@
99

1010

1111
using System;
12+
using System.Collections.Generic;
13+
using System.Data;
14+
using System.Data.Common;
15+
using System.Linq;
16+
using NHibernate.Cfg;
17+
using NHibernate.Driver;
18+
using NHibernate.Engine;
19+
using NHibernate.SqlCommand;
20+
using NHibernate.SqlTypes;
21+
using NHibernate.Tool.hbm2ddl;
1222
using NHibernate.Type;
23+
using NHibernate.Util;
1324
using NUnit.Framework;
1425

1526
namespace NHibernate.Test.TypesTest
@@ -25,20 +36,183 @@ public class DateTimeTypeFixtureAsync
2536
[Test]
2637
public async Task NextAsync()
2738
{
28-
DateTimeType type = (DateTimeType) NHibernateUtil.DateTime;
39+
var type = NHibernateUtil.DateTime;
2940
object current = DateTime.Parse("2004-01-01");
3041
object next = await (type.NextAsync(current, null, CancellationToken.None));
3142

3243
Assert.IsTrue(next is DateTime, "Next should be DateTime");
33-
Assert.IsTrue((DateTime) next > (DateTime) current,
34-
"next should be greater than current (could be equal depending on how quickly this occurs)");
44+
Assert.IsTrue(
45+
(DateTime) next > (DateTime) current,
46+
"next should be greater than current (could be equal depending on how quickly this occurs)");
3547
}
3648

3749
[Test]
3850
public async Task SeedAsync()
3951
{
40-
DateTimeType type = (DateTimeType) NHibernateUtil.DateTime;
52+
var type = NHibernateUtil.DateTime;
4153
Assert.IsTrue(await (type.SeedAsync(null, CancellationToken.None)) is DateTime, "seed should be DateTime");
4254
}
4355
}
44-
}
56+
57+
[TestFixture]
58+
public class DateTimeSqlTypeFixtureAsync : TypeFixtureBase
59+
{
60+
protected override string TypeName => "DateTime";
61+
private const int _dateId = 1;
62+
63+
protected override void Configure(Configuration configuration)
64+
{
65+
base.Configure(configuration);
66+
67+
var driverClass = ReflectHelper.ClassForName(configuration.GetProperty(Cfg.Environment.ConnectionDriver));
68+
ClientDriverWithParamsStats.DriverClass = driverClass;
69+
70+
configuration.SetProperty(
71+
Cfg.Environment.ConnectionDriver,
72+
typeof(ClientDriverWithParamsStats).AssemblyQualifiedName);
73+
}
74+
75+
protected override void OnSetUp()
76+
{
77+
base.OnSetUp();
78+
79+
using (var s = OpenSession())
80+
using (var t = s.BeginTransaction())
81+
{
82+
var d = new DateTimeClass
83+
{
84+
Id = _dateId,
85+
LocalDateTimeValue = DateTime.Now.AddDays(-1),
86+
UtcDateTimeValue = DateTime.UtcNow.AddDays(-1),
87+
NormalDateTimeValue = DateTime.Now.AddDays(-1)
88+
};
89+
s.Save(d);
90+
t.Commit();
91+
}
92+
}
93+
94+
protected override void OnTearDown()
95+
{
96+
base.OnTearDown();
97+
98+
using (var s = OpenSession())
99+
using (var t = s.BeginTransaction())
100+
{
101+
s.CreateQuery("delete from DateTimeClass").ExecuteUpdate();
102+
t.Commit();
103+
}
104+
}
105+
106+
[Test]
107+
public Task DbHasExpectedTypeAsync()
108+
{
109+
try
110+
{
111+
var validator = new SchemaValidator(cfg);
112+
return validator.ValidateAsync();
113+
}
114+
catch (Exception ex)
115+
{
116+
return Task.FromException<object>(ex);
117+
}
118+
}
119+
120+
[Test]
121+
public async Task SaveUseExpectedSqlTypeAsync()
122+
{
123+
var driver = (ClientDriverWithParamsStats) Sfi.ConnectionProvider.Driver;
124+
125+
using (var s = OpenSession())
126+
using (var t = s.BeginTransaction())
127+
{
128+
var d = new DateTimeClass
129+
{
130+
Id = 2,
131+
LocalDateTimeValue = DateTime.Now,
132+
UtcDateTimeValue = DateTime.UtcNow,
133+
NormalDateTimeValue = DateTime.Now
134+
};
135+
driver.ClearStats();
136+
await (s.SaveAsync(d));
137+
await (t.CommitAsync());
138+
}
139+
140+
AssertSqlType(driver);
141+
}
142+
143+
[Test]
144+
public async Task UpdateUseExpectedSqlTypeAsync()
145+
{
146+
var driver = (ClientDriverWithParamsStats) Sfi.ConnectionProvider.Driver;
147+
148+
using (var s = OpenSession())
149+
using (var t = s.BeginTransaction())
150+
{
151+
var d = await (s.GetAsync<DateTimeClass>(_dateId));
152+
d.LocalDateTimeValue = DateTime.Now;
153+
d.UtcDateTimeValue = DateTime.UtcNow;
154+
d.NormalDateTimeValue = DateTime.Now;
155+
driver.ClearStats();
156+
await (t.CommitAsync());
157+
}
158+
159+
AssertSqlType(driver);
160+
}
161+
162+
[Test]
163+
public async Task QueryUseExpectedSqlTypeAsync()
164+
{
165+
var driver = (ClientDriverWithParamsStats) Sfi.ConnectionProvider.Driver;
166+
167+
using (var s = OpenSession())
168+
using (var t = s.BeginTransaction())
169+
{
170+
var q = s
171+
.CreateQuery(
172+
"from DateTimeClass d where d.LocalDateTimeValue = :local and " +
173+
"d.UtcDateTimeValue = :utc and d.NormalDateTimeValue = :normal and " +
174+
":other1 = :other2")
175+
.SetDateTime("local", DateTime.Now)
176+
.SetDateTime("utc", DateTime.UtcNow)
177+
.SetDateTime("normal", DateTime.Now)
178+
.SetDateTime("other1", DateTime.Now)
179+
.SetDateTime("other2", DateTime.Now);
180+
driver.ClearStats();
181+
await (q.ListAsync<DateTimeClass>());
182+
await (t.CommitAsync());
183+
}
184+
185+
AssertSqlType(driver);
186+
}
187+
188+
private void AssertSqlType(ClientDriverWithParamsStats driver)
189+
{
190+
if (NHibernateUtil.DateTime.SqlTypes(Sfi).Any(t => Equals(t, SqlTypeFactory.DateTime2)))
191+
{
192+
Assert.That(
193+
driver.GetCount(SqlTypeFactory.DateTime),
194+
Is.EqualTo(0),
195+
"Found unexpected SqlTypeFactory.DateTime usages.");
196+
Assert.That(
197+
driver.GetCount(SqlTypeFactory.DateTime2),
198+
Is.GreaterThan(0),
199+
"Missing SqlTypeFactory.DateTime2 usages.");
200+
Assert.That(driver.GetCount(DbType.DateTime), Is.EqualTo(0), "Found unexpected DbType.DateTime usages.");
201+
Assert.That(driver.GetCount(DbType.DateTime2), Is.GreaterThan(0), "Missing DbType.DateTime2 usages.");
202+
}
203+
else
204+
{
205+
Assert.That(
206+
driver.GetCount(SqlTypeFactory.DateTime2),
207+
Is.EqualTo(0),
208+
"Found unexpected SqlTypeFactory.DateTime2 usages.");
209+
Assert.That(
210+
driver.GetCount(SqlTypeFactory.DateTime),
211+
Is.GreaterThan(0),
212+
"Missing SqlTypeFactory.DateTime usages.");
213+
Assert.That(driver.GetCount(DbType.DateTime2), Is.EqualTo(0), "Found unexpected DbType.DateTime2 usages.");
214+
Assert.That(driver.GetCount(DbType.DateTime), Is.GreaterThan(0), "Missing DbType.DateTime usages.");
215+
}
216+
}
217+
}
218+
}

src/NHibernate.Test/DebugSessionFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ bool IMapping.HasNonIdentifierPropertyNamedId(string className)
179179
return ActualFactory.HasNonIdentifierPropertyNamedId(className);
180180
}
181181

182+
Dialect.Dialect IMapping.Dialect => ActualFactory.Dialect;
183+
182184
void IDisposable.Dispose()
183185
{
184186
ActualFactory.Dispose();
@@ -270,8 +272,6 @@ ISession ISessionFactory.GetCurrentSession()
270272

271273
ICollection<string> ISessionFactory.DefinedFilterNames => ActualFactory.DefinedFilterNames;
272274

273-
Dialect.Dialect ISessionFactoryImplementor.Dialect => ActualFactory.Dialect;
274-
275275
IInterceptor ISessionFactoryImplementor.Interceptor => ActualFactory.Interceptor;
276276

277277
QueryPlanCache ISessionFactoryImplementor.QueryPlanCache => ActualFactory.QueryPlanCache;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Reflection;
2+
using NHibernate.Cfg;
3+
using NHibernate.Dialect;
4+
using NHibernate.Engine;
5+
using NHibernate.SqlTypes;
6+
using NHibernate.Type;
7+
using NUnit.Framework;
8+
9+
namespace NHibernate.Test.DialectTest
10+
{
11+
[TestFixture]
12+
public class MsSql2008DialectFixture
13+
{
14+
[Test]
15+
public void CheckSql2005DateTimeTypes()
16+
{
17+
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
18+
cfg.SetProperty(Environment.Dialect, typeof(MsSql2005Dialect).FullName);
19+
var mapping = GetMapping(cfg);
20+
AssertSqlType(NHibernateUtil.DateTime, SqlTypeFactory.DateTime, mapping);
21+
AssertSqlType(NHibernateUtil.Timestamp, SqlTypeFactory.DateTime, mapping);
22+
AssertSqlType(NHibernateUtil.TimestampUtc, SqlTypeFactory.DateTime, mapping);
23+
AssertSqlType(NHibernateUtil.DbTimestamp, SqlTypeFactory.DateTime, mapping);
24+
AssertSqlType(NHibernateUtil.LocalDateTime, SqlTypeFactory.DateTime, mapping);
25+
AssertSqlType(NHibernateUtil.UtcDateTime, SqlTypeFactory.DateTime, mapping);
26+
#pragma warning disable 618 // DateTime2 is obsolete
27+
AssertSqlType(NHibernateUtil.DateTime2, SqlTypeFactory.DateTime2, mapping);
28+
#pragma warning restore 618
29+
}
30+
31+
[Test]
32+
public void CheckSql2008DateTimeTypes()
33+
{
34+
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
35+
cfg.SetProperty(Environment.Dialect, typeof(MsSql2008Dialect).FullName);
36+
var mapping = GetMapping(cfg);
37+
AssertSqlType(NHibernateUtil.DateTime, SqlTypeFactory.DateTime2, mapping);
38+
AssertSqlType(NHibernateUtil.Timestamp, SqlTypeFactory.DateTime2, mapping);
39+
AssertSqlType(NHibernateUtil.TimestampUtc, SqlTypeFactory.DateTime2, mapping);
40+
AssertSqlType(NHibernateUtil.DbTimestamp, SqlTypeFactory.DateTime2, mapping);
41+
AssertSqlType(NHibernateUtil.LocalDateTime, SqlTypeFactory.DateTime2, mapping);
42+
AssertSqlType(NHibernateUtil.UtcDateTime, SqlTypeFactory.DateTime2, mapping);
43+
#pragma warning disable 618 // DateTime2 is obsolete
44+
AssertSqlType(NHibernateUtil.DateTime2, SqlTypeFactory.DateTime2, mapping);
45+
#pragma warning restore 618
46+
}
47+
48+
private static readonly FieldInfo _mappingField =
49+
typeof(Configuration).GetField("mapping", BindingFlags.Instance | BindingFlags.NonPublic);
50+
51+
private static IMapping GetMapping(Configuration cfg)
52+
{
53+
Assert.That(_mappingField, Is.Not.Null, "Unable to find field mapping");
54+
var mapping = _mappingField.GetValue(cfg) as IMapping;
55+
Assert.That(mapping, Is.Not.Null, "Unable to find mapping object");
56+
return mapping;
57+
}
58+
59+
private static void AssertSqlType(IType type, SqlType sqlType, IMapping mapping)
60+
{
61+
Assert.That(type.SqlTypes(mapping), Has.Length.EqualTo(1).And.Contain(sqlType));
62+
}
63+
}
64+
}

src/NHibernate.Test/NHSpecificTest/NH283/Fixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public void ForeignKeyNames()
2121

2222

2323
string script = string.Join("\n",
24-
cfg.GenerateSchemaCreationScript(new MsSql2000Dialect()));
24+
cfg.GenerateSchemaCreationScript(new MsSql2008Dialect()));
2525

2626
Assert.IsTrue(script.IndexOf("add constraint AA") >= 0);
2727
Assert.IsTrue(script.IndexOf("add constraint BB") >= 0);
2828
Assert.IsTrue(script.IndexOf("add constraint CC") >= 0);
2929
}
3030
}
31-
}
31+
}
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="../../build-common/NHibernate.props" />
3-
43
<PropertyGroup>
54
<Description>The Unit Tests for NHibernate.</Description>
6-
75
<TargetFramework>net461</TargetFramework>
86
<NoWarn>$(NoWarn);3001;3002;3003;3005</NoWarn>
97
</PropertyGroup>
10-
118
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
129
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
1310
<TreatSpecificWarningsAsErrors />
1411
</PropertyGroup>
15-
1612
<ItemGroup>
1713
<None Remove="**\*.hbm.xml" />
1814
<None Remove="**\*.jpg" />
1915
<None Update="DbScripts\*.sql">
2016
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2117
</None>
2218
</ItemGroup>
23-
2419
<ItemGroup>
2520
<EmbeddedResource Include="**\*.hbm.xml" Exclude="bin\**\*.*" />
2621
<EmbeddedResource Include="**\*.jpg" />
@@ -32,13 +27,11 @@
3227
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3328
</EmbeddedResource>
3429
</ItemGroup>
35-
3630
<ItemGroup>
3731
<Content Include="..\NHibernate.DomainModel\ABC.hbm.xml" Link="ABC.hbm.xml">
3832
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3933
</Content>
4034
</ItemGroup>
41-
4235
<ItemGroup>
4336
<PackageReference Include="Antlr3.Runtime" Version="3.5.1" />
4437
<PackageReference Include="Iesi.Collections" Version="4.0.2" />
@@ -51,20 +44,16 @@
5144
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
5245
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
5346
</ItemGroup>
54-
5547
<ItemGroup>
5648
<ProjectReference Include="..\NHibernate.DomainModel\NHibernate.DomainModel.csproj" />
5749
<ProjectReference Include="..\NHibernate\NHibernate.csproj" />
5850
</ItemGroup>
59-
6051
<ItemGroup>
6152
<Reference Include="System.Configuration" />
6253
<Reference Include="System.Data.OracleClient" />
6354
<Reference Include="System.Transactions" />
6455
</ItemGroup>
65-
6656
<ItemGroup>
6757
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
6858
</ItemGroup>
69-
70-
</Project>
59+
</Project>

0 commit comments

Comments
 (0)