Skip to content

NH-1752 - NHibernate Date type converts to NULL #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build-common/nhibernate-properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<project xmlns="http://nant.sf.net/release/0.85-rc3/nant.xsd">
<property name="nhibernate.dialect" value="NHibernate.Dialect.MsSql2008Dialect" overwrite="false"/>
<property name="nhibernate.command_timeout" value="444" overwrite="false"/>
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" overwrite="false"/>
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.Sql2008ClientDriver" overwrite="false"/>
<property name="nhibernate.connection.connection_string" value="Server=.\SQLExpress;initial catalog=nhibernate;Integrated Security=SSPI" overwrite="false"/>
</project>
2 changes: 1 addition & 1 deletion build-common/teamcity-hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<property name="format_sql">true</property>

<!-- This is the System.Data.dll provider for MSSQL Server -->
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.driver_class">NHibernate.Driver.Sql2008ClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.connection_string">Server=.\SQLExpress;initial catalog=nhibernate;Integrated Security=SSPI</property>
<property name="show_sql">false</property>
Expand Down
6 changes: 0 additions & 6 deletions doc/reference/modules/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1002,12 +1002,6 @@ ISession session = sessions.OpenSession(conn);
<session-factory>

<!-- properties -->
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our doc examples incite users to use an obsolete driver for SqlServer: I have purged unneeded parameters.

<property name="connection.connection_string">
Server=localhost;initial catalog=nhibernate;User Id=;Password=
</property>
Expand Down
3 changes: 0 additions & 3 deletions doc/reference/modules/example_weblog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,6 @@ namespace Eg
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.connection_string">
Server=localhost\SQLEXPRESS;initial catalog=Eg;Integrated Security=True
</property>
Expand Down
3 changes: 0 additions & 3 deletions doc/reference/modules/quickstart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.connection_string">
Server=localhost\SQLEXPRESS;initial catalog=quickstart;Integrated Security=True
</property>
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate.Config.Templates/MSSQL.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ for your own use before compile tests in VisualStudio.
<!-- This is the System.Data.dll provider for SQL Server -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.driver_class">NHibernate.Driver.Sql2008ClientDriver</property>
<property name="connection.connection_string">
Server=(local);initial catalog=nhibernate;Integrated Security=SSPI
</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
</session-factory>
</hibernate-configuration>
</hibernate-configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public AppSessionFactory(Microsoft.Extensions.Logging.ILoggerFactory loggerFacto
{
db.ConnectionString = @"Server=(local)\SQLEXPRESS;initial catalog=nhibernate;Integrated Security=true";
db.Dialect<MsSql2008Dialect>();
db.Driver<SqlClientDriver>();
db.Driver<Sql2008ClientDriver>();
})
.AddMapping(domainMapping);
Configuration.SessionFactory().GenerateStatistics();
Expand All @@ -37,4 +37,4 @@ public ISession OpenSession()
return SessionFactory.OpenSession();
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate.Test/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>

<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <!-- Shouldn't be necessary, but is required by some tests -->
<property name="connection.driver_class">NHibernate.Driver.Sql2008ClientDriver</property> <!-- Shouldn't be necessary, but is required by some tests -->
<property name="connection.connection_string_name">TestConnectionString</property>
<property name="connection.provider">NHibernate.Test.DebugConnectionProvider, NHibernate.Test</property>
<property name="connection.isolation">ReadCommitted</property> <!-- See System.Data.IsolationLevel for valid values -->
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Async/FilterTest/DynamicFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -773,4 +773,4 @@ public void Dispose()
}
}
}
}
}
7 changes: 5 additions & 2 deletions src/NHibernate.Test/Async/Hql/Ast/LimitClauseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

using System.Linq;
using NHibernate.Cfg;
using NHibernate.Driver;
using NHibernate.Hql.Ast.ANTLR;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.Hql.Ast
Expand All @@ -22,7 +24,8 @@ public class LimitClauseFixtureAsync : BaseFixture
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect.SupportsVariableLimit
&& !(Dialect is Dialect.MsSql2000Dialect && cfg.Properties[Environment.ConnectionDriver] == typeof(Driver.OdbcDriver).FullName); // don't know why, but these tests don't work on SQL Server using ODBC
&& !(Dialect is Dialect.MsSql2000Dialect && // don't know why, but these tests don't work on SQL Server using ODBC
typeof(OdbcDriver).IsAssignableFrom(ReflectHelper.ClassForName(cfg.GetProperty(Environment.ConnectionDriver))));
}

protected override void OnSetUp()
Expand Down Expand Up @@ -186,4 +189,4 @@ public async Task TakeWithParameterAsync()
s.Close();
}
}
}
}
5 changes: 3 additions & 2 deletions src/NHibernate.Test/Async/NHSpecificTest/NH1144/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using NHibernate.AdoNet;
using NHibernate.Cfg;
using NHibernate.Driver;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH1144
Expand All @@ -36,7 +37,7 @@ protected override void Configure(Configuration configuration)
[Test]
public async Task CanSaveInSingleBatchAsync()
{
if (configuration.Properties[Environment.ConnectionDriver].Contains(typeof (OracleDataClientDriver).Name) == false)
if (!typeof(OracleDataClientDriver).IsAssignableFrom(ReflectHelper.ClassForName(cfg.GetProperty(Environment.ConnectionDriver))))
{
Assert.Ignore("Only applicable for Oracle Data Client driver");
}
Expand Down Expand Up @@ -85,4 +86,4 @@ public async Task CanSaveInSingleBatchAsync()
Assert.IsTrue(executedBatch);
}
}
}
}
107 changes: 107 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH2207/SampleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Data;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Engine;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH2207
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class SampleTestAsync : BugTestCase
{
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is MsSql2008Dialect;
}

protected override bool AppliesTo(ISessionFactoryImplementor factory)
{
return factory.ConnectionProvider.Driver is Sql2008ClientDriver;
}

[Test]
public async Task WithoutUseNHSqlDataProviderWorkProperlyAsync()
{
var createTable = "CREATE TABLE TryDate([Id] [int] IDENTITY(1,1) NOT NULL,[MyDate] [date] NOT NULL)";
var dropTable = "DROP TABLE TryDate";
var insertTable = "INSERT INTO TryDate([MyDate]) VALUES(@p0)";
using(var sqlConnection = new System.Data.SqlClient.SqlConnection(cfg.Properties[Cfg.Environment.ConnectionString]))
{
await (sqlConnection.OpenAsync(CancellationToken.None));
using (var tx = sqlConnection.BeginTransaction())
{
var command = sqlConnection.CreateCommand();
command.Transaction = tx;
command.CommandText = createTable;
await (command.ExecuteNonQueryAsync(CancellationToken.None));
tx.Commit();
}

try
{
using (var tx = sqlConnection.BeginTransaction())
{
var command = sqlConnection.CreateCommand();
command.Transaction = tx;
command.CommandText = insertTable;
var dateParam = command.CreateParameter();
dateParam.ParameterName = "@p0";
dateParam.DbType = DbType.Date;
dateParam.SqlDbType = SqlDbType.Date;
dateParam.Value = DateTime.MinValue.Date;
command.Parameters.Add(dateParam);
await (command.ExecuteNonQueryAsync(CancellationToken.None));
tx.Commit();
}
}
finally
{
using (var tx = sqlConnection.BeginTransaction())
{
var command = sqlConnection.CreateCommand();
command.Transaction = tx;
command.CommandText = dropTable;
await (command.ExecuteNonQueryAsync(CancellationToken.None));
tx.Commit();
}
}
}
}

[Test]
public async Task Dates_Before_1753_Should_Not_Insert_NullAsync()
{
object savedId;
var expectedStoredValue = DateTime.MinValue.Date.AddDays(1).Date;
using (ISession session = OpenSession())
using (var tx = session.BeginTransaction())
{
var concrete = new DomainClass{Date = expectedStoredValue.AddMinutes(90)};
savedId = await (session.SaveAsync(concrete));
await (tx.CommitAsync());
}

using (ISession session = OpenSession())
using (var tx = session.BeginTransaction())
{
var savedObj = await (session.GetAsync<DomainClass>(savedId));
Assert.That(savedObj.Date, Is.EqualTo(expectedStoredValue));
await (session.DeleteAsync(savedObj));
await (tx.CommitAsync());
}
}
}
}
3 changes: 2 additions & 1 deletion src/NHibernate.Test/Async/NHSpecificTest/NH3202/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3202
Expand All @@ -26,7 +27,7 @@ protected override void Configure(Configuration configuration)
if (!(Dialect is MsSql2008Dialect))
Assert.Ignore("Test is for MS SQL Server dialect only (custom dialect).");

if (!Environment.ConnectionDriver.Contains("SqlClientDriver"))
if (!typeof(SqlClientDriver).IsAssignableFrom(ReflectHelper.ClassForName(cfg.GetProperty(Environment.ConnectionDriver))))
Assert.Ignore("Test is for MS SQL Server driver only (custom driver is used).");

cfg.SetProperty(Environment.Dialect, typeof(OffsetStartsAtOneTestDialect).AssemblyQualifiedName);
Expand Down
97 changes: 97 additions & 0 deletions src/NHibernate.Test/Async/TypesTest/DateTypeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Collections.Generic;
using NHibernate.Dialect;
using NHibernate.Type;
using NUnit.Framework;
using System;

namespace NHibernate.Test.TypesTest
{
using System.Threading.Tasks;
using System.Threading;

[TestFixture]
public class DateTypeFixtureAsync : TypeFixtureBase
{
protected override string TypeName
{
get { return "Date"; }
}

[Test]
public Task ReadWriteNormalAsync()
{
try
{
var expected = DateTime.Today;

return ReadWriteAsync(expected);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}

[Test]
public Task ReadWriteMinAsync()
{
try
{
var expected = Sfi.ConnectionProvider.Driver.MinDate;

return ReadWriteAsync(expected);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}

[Test]
public Task ReadWriteYear750Async()
{
try
{
var expected = new DateTime(750, 5, 13);
if (Sfi.ConnectionProvider.Driver.MinDate > expected)
{
Assert.Ignore($"The driver does not support dates below {Sfi.ConnectionProvider.Driver.MinDate:O}");
}
return ReadWriteAsync(expected);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}

private async Task ReadWriteAsync(DateTime expected, CancellationToken cancellationToken = default(CancellationToken))
{
// Add an hour to check it is correctly ignored once read back from db.
var basic = new DateClass { DateValue = expected.AddHours(1) };
object savedId;
using (var s = OpenSession())
{
savedId = await (s.SaveAsync(basic, cancellationToken));
await (s.FlushAsync(cancellationToken));
}
using (var s = OpenSession())
{
basic = await (s.GetAsync<DateClass>(savedId, cancellationToken));
Assert.That(basic.DateValue, Is.EqualTo(expected));
await (s.DeleteAsync(basic, cancellationToken));
await (s.FlushAsync(cancellationToken));
}
}
}
}
Loading