Skip to content

Commit cfe9bfa

Browse files
NH-3919 - Letting the type choose being overridden or not.
1 parent 0106bae commit cfe9bfa

19 files changed

+67
-77
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4+
using System.Linq;
45
using NHibernate.Dialect.Function;
56
using NHibernate.Driver;
67
using NHibernate.SqlTypes;
@@ -87,21 +88,18 @@ protected override void RegisterDefaultProperties()
8788
: 1;
8889

8990
/// <inheritdoc />
90-
public override SqlType[] RefineSqlTypes(IType type, SqlType[] types)
91+
public override SqlType[] RefineSqlTypes(SqlType[] types)
9192
{
93+
types = base.RefineSqlTypes(types);
94+
9295
#pragma warning disable 618
93-
if (!KeepDateTime)
96+
if (KeepDateTime)
97+
return types;
9498
#pragma warning restore 618
95-
{
96-
switch (type)
97-
{
98-
// Switch built-in DateTime types and their descendants to DateTime2.
99-
case DateTimeType _:
100-
case TimestampType _:
101-
return new[] { SqlTypeFactory.DateTime2 };
102-
}
103-
}
104-
return base.RefineSqlTypes(type, types);
99+
100+
return types
101+
.Select(t => SqlTypeFactory.DateTime.Equals(t) ? SqlTypeFactory.DateTime2 : t)
102+
.ToArray();
105103
}
106104
}
107105
}

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
{

0 commit comments

Comments
 (0)