Skip to content

Commit 97c9948

Browse files
NH-3919 - Letting the type choose being overridden or not.
1 parent 7ec6d87 commit 97c9948

19 files changed

+67
-78
lines changed

src/NHibernate/Async/Type/AbstractType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,4 @@ public abstract Task<object> ReplaceAsync(object original, object current, ISess
248248

249249
public abstract Task<bool> IsDirtyAsync(object old, object current, bool[] checkable, ISessionImplementor session, CancellationToken cancellationToken);
250250
}
251-
}
251+
}

src/NHibernate/Async/Type/ComponentType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,4 @@ public override async Task<bool> IsModifiedAsync(object old, object current, boo
357357
return false;
358358
}
359359
}
360-
}
360+
}

src/NHibernate/Async/Type/CompositeCustomType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,4 @@ public override Task<object> ReplaceAsync(object original, object current, ISess
181181
}
182182

183183
}
184-
}
184+
}

src/NHibernate/Async/Type/OneToOneType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,4 @@ public override Task<object> AssembleAsync(object cached, ISessionImplementor se
164164
}
165165
}
166166
}
167-
}
167+
}

src/NHibernate/Dialect/Dialect.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,11 @@ protected void RegisterColumnType(DbType code, string name)
293293
}
294294

295295
/// <summary>
296-
/// Refine the <see cref="SqlType"/>s for an <see cref="IType"/>.
296+
/// Refine provided <see cref="SqlType"/>s.
297297
/// </summary>
298-
/// <param name="type">The <see cref="IType"/>.</param>
299-
/// <param name="types">The original <see cref="SqlType"/>s for the <paramref name="type"/>.</param>
298+
/// <param name="types">The original <see cref="SqlType"/>s.</param>
300299
/// <returns>Refined <see cref="SqlType"/>s.</returns>
301-
public virtual SqlType[] RefineSqlTypes(IType type, SqlType[] types) =>
300+
public virtual SqlType[] RefineSqlTypes(SqlType[] types) =>
302301
types;
303302

304303
#endregion

src/NHibernate/Dialect/MsSql2008Dialect.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System.Collections.Generic;
22
using System.Data;
3+
using System.Linq;
34
using NHibernate.Dialect.Function;
45
using NHibernate.Driver;
56
using NHibernate.SqlTypes;
6-
using NHibernate.Type;
77
using NHibernate.Util;
88
using Environment = NHibernate.Cfg.Environment;
99

@@ -79,19 +79,16 @@ protected override void RegisterDefaultProperties()
7979
: 1;
8080

8181
/// <inheritdoc />
82-
public override SqlType[] RefineSqlTypes(IType type, SqlType[] types)
82+
public override SqlType[] RefineSqlTypes(SqlType[] types)
8383
{
84-
if (!KeepDateTime)
85-
{
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-
}
93-
}
94-
return base.RefineSqlTypes(type, types);
84+
types = base.RefineSqlTypes(types);
85+
86+
if (KeepDateTime)
87+
return types;
88+
89+
return types
90+
.Select(t => SqlTypeFactory.DateTime.Equals(t) ? SqlTypeFactory.DateTime2 : t)
91+
.ToArray();
9592
}
9693
}
9794
}

src/NHibernate/NHibernate.csproj

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="../../build-common/NHibernate.props" />
3-
43
<PropertyGroup>
54
<Description>An object persistence library for relational databases.</Description>
6-
75
<TargetFramework>net461</TargetFramework>
86
<NoWarn>$(NoWarn);3001;3002;3003;3005;1591</NoWarn>
97
<SignAssembly>True</SignAssembly>
108
<AssemblyOriginatorKeyFile>..\NHibernate.snk</AssemblyOriginatorKeyFile>
119
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1210
</PropertyGroup>
13-
1411
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
1512
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
1613
<TreatSpecificWarningsAsErrors />
1714
</PropertyGroup>
18-
1915
<ItemGroup>
2016
<None Remove="**\*.g" />
2117
<None Remove="**\*.xsd" />
2218
</ItemGroup>
23-
2419
<ItemGroup>
2520
<Antlr3 Include="**\*.g" />
2621
</ItemGroup>
27-
2822
<ItemGroup>
2923
<EmbeddedResource Include="**\*.xsd" />
3024
</ItemGroup>
31-
3225
<ItemGroup>
3326
<None Include="..\NHibernate.snk" Link="NHibernate.snk" />
3427
</ItemGroup>
35-
3628
<ItemGroup>
3729
<PackageReference Include="Antlr3.CodeGenerator" Version="3.5.2-beta1">
3830
<PrivateAssets>All</PrivateAssets>
@@ -42,11 +34,9 @@
4234
<PackageReference Include="Remotion.Linq" Version="2.1.2" />
4335
<PackageReference Include="Remotion.Linq.EagerFetching" Version="2.1.0" />
4436
</ItemGroup>
45-
4637
<ItemGroup>
4738
<Reference Include="System.ServiceModel" />
4839
<Reference Include="System.Transactions" />
4940
<Reference Include="System.Configuration" />
5041
</ItemGroup>
51-
52-
</Project>
42+
</Project>

src/NHibernate/Type/AbstractType.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,23 @@ public override int GetHashCode()
182182
/// <inheritdoc />
183183
public abstract SqlType[] SqlTypes(IMapping mapping);
184184

185+
/// <summary>
186+
/// Refine the sql types.
187+
/// </summary>
188+
/// <param name="types">The types to refine.</param>
189+
/// <param name="mapping">The mapping for which to refine <paramref name="types"/>.</param>
190+
/// <returns>The refined types.</returns>
191+
protected SqlType[] RefineSqlTypes(SqlType[] types, IMapping mapping) =>
192+
AllowSqlTypeOverride
193+
? mapping?.Dialect.RefineSqlTypes(types) ?? types
194+
: types;
195+
196+
/// <summary>
197+
/// Does this type allow its default <see cref="SqlType" /> to be overridden?
198+
/// <see langword="false" /> by default for <see cref="AbstractType" />.
199+
/// </summary>
200+
protected virtual bool AllowSqlTypeOverride => false;
201+
185202
/// <inheritdoc />
186203
public abstract int GetColumnSpan(IMapping mapping);
187204

@@ -275,4 +292,4 @@ public abstract object Replace(object original, object current, ISessionImplemen
275292

276293
public abstract bool IsDirty(object old, object current, bool[] checkable, ISessionImplementor session);
277294
}
278-
}
295+
}

src/NHibernate/Type/AnyType.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,8 @@ public override System.Type ReturnedClass
153153
get { return typeof(object); }
154154
}
155155

156-
public override SqlType[] SqlTypes(IMapping mapping)
157-
{
158-
return ArrayHelper.Join(metaType.SqlTypes(mapping), identifierType.SqlTypes(mapping));
159-
}
156+
public override SqlType[] SqlTypes(IMapping mapping) =>
157+
RefineSqlTypes(ArrayHelper.Join(metaType.SqlTypes(mapping), identifierType.SqlTypes(mapping)), mapping);
160158

161159
public override string ToLoggableString(object value, ISessionFactoryImplementor factory)
162160
{

src/NHibernate/Type/ClassMetaType.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ namespace NHibernate.Type
1414
[Serializable]
1515
public partial class ClassMetaType : AbstractType
1616
{
17-
public override SqlType[] SqlTypes(IMapping mapping)
18-
{
19-
var types = new[] { NHibernateUtil.String.SqlType };
20-
return mapping?.Dialect.RefineSqlTypes(this, types) ?? types;
21-
}
17+
public override SqlType[] SqlTypes(IMapping mapping) =>
18+
RefineSqlTypes(new[] { NHibernateUtil.String.SqlType }, mapping);
19+
20+
protected override bool AllowSqlTypeOverride => true;
2221

2322
public override int GetColumnSpan(IMapping mapping)
2423
{

src/NHibernate/Type/CollectionType.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,8 @@ public override void NullSafeSet(DbCommand cmd, object value, int index, ISessio
9898
{
9999
}
100100

101-
public override SqlType[] SqlTypes(IMapping session)
102-
{
103-
return NoSqlTypes;
104-
}
101+
public override SqlType[] SqlTypes(IMapping mapping) =>
102+
RefineSqlTypes(NoSqlTypes, mapping);
105103

106104
public override int GetColumnSpan(IMapping session)
107105
{

src/NHibernate/Type/ComponentType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override SqlType[] SqlTypes(IMapping mapping)
3636
sqlTypes[n++] = subtypes[j];
3737
}
3838
}
39-
return sqlTypes;
39+
return RefineSqlTypes(sqlTypes, mapping);
4040
}
4141

4242
public override int GetColumnSpan(IMapping mapping)
@@ -678,4 +678,4 @@ public int GetPropertyIndex(string name)
678678
throw new PropertyNotFoundException(ReturnedClass, name);
679679
}
680680
}
681-
}
681+
}

src/NHibernate/Type/CompositeCustomType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public override SqlType[] SqlTypes(IMapping mapping)
196196
result[n++] = sqlTypes[k];
197197
}
198198
}
199-
return result;
199+
return RefineSqlTypes(result, mapping);
200200
}
201201

202202
public override string ToLoggableString(object value, ISessionFactoryImplementor factory)
@@ -262,4 +262,4 @@ public override bool[] ToColumnNullness(object value, IMapping mapping)
262262
}
263263

264264
}
265-
}
265+
}

src/NHibernate/Type/CustomType.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,9 @@ public CustomType(System.Type userTypeClass, IDictionary<string, string> paramet
6464
sqlTypes = userType.SqlTypes;
6565
}
6666

67-
/// <summary>
68-
///
69-
/// </summary>
70-
/// <param name="mapping"></param>
71-
/// <returns></returns>
67+
/// <inheritdoc />
7268
public override SqlType[] SqlTypes(IMapping mapping) =>
73-
mapping?.Dialect.RefineSqlTypes(this, sqlTypes) ?? sqlTypes;
69+
RefineSqlTypes(sqlTypes, mapping);
7470

7571
/// <summary>
7672
///

src/NHibernate/Type/ManyToOneType.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ public override int GetColumnSpan(IMapping mapping)
4141
return GetIdentifierOrUniqueKeyType(mapping).GetColumnSpan(mapping);
4242
}
4343

44-
public override SqlType[] SqlTypes(IMapping mapping)
45-
{
46-
return GetIdentifierOrUniqueKeyType(mapping).SqlTypes(mapping);
47-
}
44+
public override SqlType[] SqlTypes(IMapping mapping) =>
45+
RefineSqlTypes(GetIdentifierOrUniqueKeyType(mapping).SqlTypes(mapping), mapping);
4846

4947
public override void NullSafeSet(DbCommand st, object value, int index, bool[] settable, ISessionImplementor session)
5048
{

src/NHibernate/Type/MetaType.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ public MetaType(IDictionary<object, string> values, IType baseType)
2424
}
2525
}
2626

27-
public override SqlType[] SqlTypes(IMapping mapping)
28-
{
29-
return baseType.SqlTypes(mapping);
30-
}
27+
public override SqlType[] SqlTypes(IMapping mapping) =>
28+
RefineSqlTypes(baseType.SqlTypes(mapping), mapping);
3129

3230
public override int GetColumnSpan(IMapping mapping)
3331
{

src/NHibernate/Type/NullableType.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,14 @@ public virtual SqlType SqlType
291291
/// column. All of their implementation should be in <see cref="NullableType.SqlType" />.
292292
/// </para>
293293
/// </remarks>
294-
public sealed override SqlType[] SqlTypes(IMapping mapping)
295-
{
296-
var types = new[] { SqlType };
297-
return mapping?.Dialect.RefineSqlTypes(this, types) ?? types;
298-
}
294+
public sealed override SqlType[] SqlTypes(IMapping mapping) =>
295+
RefineSqlTypes(new[] { SqlType }, mapping);
296+
297+
/// <summary>
298+
/// Does this type allow its default <see cref="SqlType" /> to be overridden?
299+
/// <see langword="true" /> by default for <see cref="NullableType" />.
300+
/// </summary>
301+
protected override bool AllowSqlTypeOverride => true;
299302

300303
/// <summary>
301304
/// Returns the number of columns spanned by this <see cref="NullableType"/>

src/NHibernate/Type/OneToOneType.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ public override int GetColumnSpan(IMapping session)
2424
return 0;
2525
}
2626

27-
public override SqlType[] SqlTypes(IMapping session)
28-
{
29-
return NoSqlTypes;
30-
}
27+
public override SqlType[] SqlTypes(IMapping mapping) =>
28+
RefineSqlTypes(NoSqlTypes, mapping);
3129

3230
public OneToOneType(string referencedEntityName, ForeignKeyDirection foreignKeyType, string uniqueKeyPropertyName, bool lazy, bool unwrapProxy, string entityName, string propertyName)
3331
: base(referencedEntityName, uniqueKeyPropertyName, !lazy, unwrapProxy)
@@ -154,4 +152,4 @@ public override bool[] ToColumnNullness(object value, IMapping mapping)
154152
return ArrayHelper.EmptyBoolArray;
155153
}
156154
}
157-
}
155+
}

src/NHibernate/Type/SpecialOneToOneType.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ public override int GetColumnSpan(Engine.IMapping mapping)
2020
return GetIdentifierOrUniqueKeyType(mapping).GetColumnSpan(mapping);
2121
}
2222

23-
public override SqlTypes.SqlType[] SqlTypes(Engine.IMapping mapping)
24-
{
25-
return GetIdentifierOrUniqueKeyType(mapping).SqlTypes(mapping);
26-
}
23+
public override SqlTypes.SqlType[] SqlTypes(Engine.IMapping mapping) =>
24+
RefineSqlTypes(GetIdentifierOrUniqueKeyType(mapping).SqlTypes(mapping), mapping);
2725

2826
public override bool UseLHSPrimaryKey
2927
{

0 commit comments

Comments
 (0)