Skip to content

Commit 0e7bc34

Browse files
authored
Make Render method virtual for Cast function (#2406)
1 parent b7213eb commit 0e7bc34

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/NHibernate/Dialect/Function/CastFunction.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public bool HasParenthesesIfNoArguments
5454
get { return true; }
5555
}
5656

57-
public SqlString Render(IList args, ISessionFactoryImplementor factory)
57+
public virtual SqlString Render(IList args, ISessionFactoryImplementor factory)
5858
{
5959
if (args.Count != 2)
6060
{
@@ -87,23 +87,36 @@ public SqlString Render(IList args, ISessionFactoryImplementor factory)
8787
throw new QueryException(string.Format("invalid Hibernate type for cast(): type {0} not found", typeName));
8888
}
8989

90-
if (CastingIsRequired(sqlType))
91-
{
92-
return new SqlString("cast(", args[0], " as ", sqlType, ")");
93-
}
94-
else
95-
{
90+
// TODO 6.0: Remove pragma block with its content
91+
#pragma warning disable 618
92+
if (!CastingIsRequired(sqlType))
9693
return new SqlString("(", args[0], ")");
97-
}
94+
#pragma warning restore 618
95+
96+
return Render(args[0], sqlType, factory);
9897
}
9998

10099
#endregion
101100

101+
// Since v5.3
102+
[Obsolete("This method has no usages and will be removed in a future version")]
102103
protected virtual bool CastingIsRequired(string sqlType)
103104
{
104105
return true;
105106
}
106107

108+
/// <summary>
109+
/// Renders the SQL fragment representing the SQL cast.
110+
/// </summary>
111+
/// <param name="expression">The cast argument.</param>
112+
/// <param name="sqlType">The SQL type to cast to.</param>
113+
/// <param name="factory">The session factory.</param>
114+
/// <returns>A SQL fragment.</returns>
115+
protected virtual SqlString Render(object expression, string sqlType, ISessionFactoryImplementor factory)
116+
{
117+
return new SqlString("cast(", expression, " as ", sqlType, ")");
118+
}
119+
107120
#region IFunctionGrammar Members
108121

109122
bool IFunctionGrammar.IsSeparator(string token)

src/NHibernate/Dialect/Function/TransparentCastFunction.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections;
3+
using NHibernate.Engine;
4+
using NHibernate.SqlCommand;
25

36
namespace NHibernate.Dialect.Function
47
{
@@ -8,9 +11,23 @@ namespace NHibernate.Dialect.Function
811
[Serializable]
912
public class TransparentCastFunction : CastFunction
1013
{
14+
// Since v5.3
15+
[Obsolete("This method has no usages and will be removed in a future version")]
1116
protected override bool CastingIsRequired(string sqlType)
1217
{
1318
return false;
1419
}
20+
21+
/// <summary>
22+
/// Renders the SQL fragment representing the casted expression without actually casting it.
23+
/// </summary>
24+
/// <param name="expression">The cast argument.</param>
25+
/// <param name="sqlType">The SQL type to cast to, ignored for rendering.</param>
26+
/// <param name="factory">The session factory.</param>
27+
/// <returns>A SQL fragment.</returns>
28+
protected override SqlString Render(object expression, string sqlType, ISessionFactoryImplementor factory)
29+
{
30+
return new SqlString("(", expression, ")");
31+
}
1532
}
1633
}

src/NHibernate/Dialect/SQLiteDialect.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ public override bool SupportsForeignKeyConstraintInAlterTable
510510
[Serializable]
511511
protected class SQLiteCastFunction : CastFunction
512512
{
513+
// Since v5.3
514+
[Obsolete("This method has no usages and will be removed in a future version")]
513515
protected override bool CastingIsRequired(string sqlType)
514516
{
515517
if (StringHelper.ContainsCaseInsensitive(sqlType, "date") ||

0 commit comments

Comments
 (0)