Skip to content

NH-3884 - Default DateTime type can not be overridden #702

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 3 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions src/NHibernate.Test/TypesTest/ChangeDefaultTypeClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.TypesTest
{
public class ChangeDefaultTypeClass
{
public int Id { get; set; }

public DateTime NormalDateTimeValue { get; set; } = DateTime.Today;
}
}
16 changes: 16 additions & 0 deletions src/NHibernate.Test/TypesTest/ChangeDefaultTypeClass.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">

<class
name="NHibernate.Test.TypesTest.ChangeDefaultTypeClass, NHibernate.Test"
table="bc_datetime"
>

<id name="Id" column="id">
<generator class="assigned"/>
</id>

<property name="NormalDateTimeValue"/>
</class>
</hibernate-mapping>
85 changes: 85 additions & 0 deletions src/NHibernate.Test/TypesTest/ChangeDefaultTypeFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using NHibernate.Cfg;
using NHibernate.Engine;
using NHibernate.Impl;
using NHibernate.Type;
using NUnit.Framework;

namespace NHibernate.Test.TypesTest
{
/// <summary>
/// TestFixtures for changing a default .Net type.
/// </summary>
[TestFixture]
public class ChangeDefaultTypeFixture : TypeFixtureBase
{
protected override string TypeName => "ChangeDefaultType";

private IType _originalDefaultDateTimeType;
private IType _testDefaultDateTimeType;

protected override void Configure(Configuration configuration)
{
_originalDefaultDateTimeType = TypeFactory.GetDefaultTypeFor(typeof(DateTime));
Assert.That(_originalDefaultDateTimeType, Is.Not.Null);
_testDefaultDateTimeType = NHibernateUtil.DateTime.Equals(_originalDefaultDateTimeType)
? (IType) NHibernateUtil.DateTimeNoMs
: NHibernateUtil.DateTime;
TypeFactory.RegisterType(typeof(DateTime), _testDefaultDateTimeType, TypeFactory.EmptyAliases);
base.Configure(configuration);
}

protected override void DropSchema()
{
base.DropSchema();
if (_originalDefaultDateTimeType != null)
TypeFactory.RegisterType(typeof(DateTime), _originalDefaultDateTimeType, TypeFactory.EmptyAliases);
}

[Test]
public void DefaultType()
{
Assert.That(TypeFactory.GetDefaultTypeFor(typeof(DateTime)), Is.EqualTo(_testDefaultDateTimeType));
}

[Test]
public void PropertyType()
{
Assert.That(
Sfi.GetClassMetadata(typeof(ChangeDefaultTypeClass))
.GetPropertyType(nameof(ChangeDefaultTypeClass.NormalDateTimeValue)),
Is.EqualTo(_testDefaultDateTimeType));
}

[Test]
public void GuessType()
{
Assert.That(NHibernateUtil.GuessType(typeof(DateTime)), Is.EqualTo(_testDefaultDateTimeType));
}

[Test]
public void ParameterType()
{
var namedParametersField = typeof(AbstractQueryImpl)
.GetField("namedParameters", BindingFlags.Instance | BindingFlags.NonPublic);
Assert.That(namedParametersField, Is.Not.Null, "Missing internal field");

using (var s = OpenSession())
{
// Query where the parameter type cannot be deducted from compared entity property
var q = s.CreateQuery($"from {nameof(ChangeDefaultTypeClass)} where :date1 = :date2 or :date1 = :date3")
.SetParameter("date1", DateTime.Now)
.SetDateTime("date2", DateTime.Now)
.SetDateTimeNoMs("date3", DateTime.Now);

var namedParameters = namedParametersField.GetValue(q) as Dictionary<string, TypedValue>;
Assert.That(namedParameters, Is.Not.Null, "Unable to retrieve parameters internal field");
Assert.That(namedParameters["date1"].Type, Is.EqualTo(_testDefaultDateTimeType));
Assert.That(namedParameters["date2"].Type, Is.EqualTo(NHibernateUtil.DateTime));
Assert.That(namedParameters["date3"].Type, Is.EqualTo(NHibernateUtil.DateTimeNoMs));
}
}
}
}
14 changes: 12 additions & 2 deletions src/NHibernate/Type/TypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Xml;
using System.Xml.Linq;
using NHibernate.Bytecode;
using NHibernate.Cfg;
using NHibernate.Classic;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;
Expand Down Expand Up @@ -33,8 +34,9 @@ private enum TypeClassification
PrecisionScale
}

public static readonly string[] EmptyAliases = System.Array.Empty<string>();

private static readonly INHibernateLogger _log = NHibernateLogger.For(typeof(TypeFactory));
private static readonly string[] EmptyAliases= System.Array.Empty<string>();
private static readonly char[] PrecisionScaleSplit = { '(', ')', ',' };
private static readonly char[] LengthSplit = { '(', ')' };

Expand Down Expand Up @@ -95,7 +97,15 @@ private enum TypeClassification

private delegate NullableType NullableTypeCreatorDelegate(SqlType sqlType);

private static void RegisterType(System.Type systemType, IType nhibernateType, IEnumerable<string> aliases)
/// <summary>
/// <para>Defines which NHibernate type should be chosen by default for handling a given .Net type.</para>
/// <para>This must be done before any operation on NHibernate, including building its
/// <see cref="Configuration" /> and building session factory. Otherwise the behavior will be undefined.</para>
/// </summary>
/// <param name="systemType">The .Net type.</param>
/// <param name="nhibernateType">The NHibernate type.</param>
/// <param name="aliases">The additional aliases to map to the type. Use <see cref="EmptyAliases"/> if none.</param>
public static void RegisterType(System.Type systemType, IType nhibernateType, IEnumerable<string> aliases)
{
var typeAliases = new List<string>(aliases);
typeAliases.AddRange(GetClrTypeAliases(systemType));
Expand Down