Skip to content
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

Backport handling of null DateTime parameters in Npgsql 6+ #3300

Merged
merged 3 commits into from
May 11, 2023
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
82 changes: 82 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3291/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//------------------------------------------------------------------------------
// <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.Linq;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH3291
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Person { Name = "Bob", DateOfBirth = new DateTime(2009, 12, 23) };
session.Save(e1);

var e2 = new Person { Name = "Sally", DateOfBirth = new DateTime(2018, 9, 30) };
session.Save(e2);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task LinqAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
DateTime? dateOfSearch = null;

var result = await ((
from person in session.Query<Person>()
where dateOfSearch == null || person.DateOfBirth > dateOfSearch
select person).ToListAsync());

Assert.That(result, Has.Count.EqualTo(2));
}
}

[Test]
public async Task HqlAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
DateTime? dateOfSearch = null;

var result =
await (session.CreateQuery("from Person where :DateOfSearch is null OR DateOfBirth > :DateOfSearch")
.SetParameter("DateOfSearch", dateOfSearch, NHibernateUtil.DateTime)
.ListAsync<Person>());

Assert.That(result, Has.Count.EqualTo(2));
}
}
}
}
70 changes: 70 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3291/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3291
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Person { Name = "Bob", DateOfBirth = new DateTime(2009, 12, 23) };
session.Save(e1);

var e2 = new Person { Name = "Sally", DateOfBirth = new DateTime(2018, 9, 30) };
session.Save(e2);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void Linq()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
DateTime? dateOfSearch = null;

var result = (
from person in session.Query<Person>()
where dateOfSearch == null || person.DateOfBirth > dateOfSearch
select person).ToList();

Assert.That(result, Has.Count.EqualTo(2));
}
}

[Test]
public void Hql()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
DateTime? dateOfSearch = null;

var result =
session.CreateQuery("from Person where :DateOfSearch is null OR DateOfBirth > :DateOfSearch")
.SetParameter("DateOfSearch", dateOfSearch, NHibernateUtil.DateTime)
.List<Person>();

Assert.That(result, Has.Count.EqualTo(2));
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3291/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH3291">

<class name="Person">
<id name="Id" generator="guid.comb"/>
<property name="Name"/>
<property name="DateOfBirth" />
</class>

</hibernate-mapping>
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3291/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH3291
{
class Person
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual DateTime? DateOfBirth { get; set; }
}
}
6 changes: 1 addition & 5 deletions src/NHibernate.TestDatabaseSetup/TestDatabaseSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,7 @@ private static void SetupNpgsql(Cfg.Configuration cfg)

using (var cmd = conn.CreateCommand())
{
cmd.CommandText =
@"CREATE OR REPLACE FUNCTION uuid_generate_v4()
RETURNS uuid
AS '$libdir/uuid-ossp', 'uuid_generate_v4'
VOLATILE STRICT LANGUAGE C;";
cmd.CommandText = "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";";

cmd.ExecuteNonQuery();
}
Expand Down
30 changes: 27 additions & 3 deletions src/NHibernate/Driver/NpgsqlDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using System.Data.Common;
using NHibernate.AdoNet;
Expand Down Expand Up @@ -74,14 +75,37 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
// Since the .NET currency type has 4 decimal places, we use a decimal type in PostgreSQL instead of its native 2 decimal currency type.
dbParam.DbType = DbType.Decimal;
}
else if (DriverVersionMajor < 6 || sqlType.DbType != DbType.DateTime)
else
{
dbParam.DbType = sqlType.DbType;
}
else
}

public override void AdjustCommand(DbCommand command)
{
if (DriverVersionMajor >= 6)
{
// Let Npgsql 6 driver to decide parameter type
for (var i = 0; i < command.Parameters.Count; i++)
{
var parameter = command.Parameters[i];
if (parameter.DbType == DbType.DateTime &&
parameter.Value is DateTime dateTime &&
dateTime.Kind != DateTimeKind.Utc)
{
// There are breaking changes in Npgsql 6 as following:
// UTC DateTime is now strictly mapped to timestamptz,
// while Local/Unspecified DateTime is now strictly mapped to timestamp.
//
// DbType.DateTime now maps to timestamptz, not timestamp.
// DbType.DateTime2 continues to map to timestamp
//
// See more details here: https://www.npgsql.org/doc/release-notes/6.0.html#detailed-notes
parameter.DbType = DbType.DateTime2;
}
}
}

base.AdjustCommand(command);
}

// Prior to v3, Npgsql was expecting DateTime for time.
Expand Down