Skip to content

Commit

Permalink
Function alias
Browse files Browse the repository at this point in the history
  • Loading branch information
gliljas committed May 28, 2024
1 parent a0aa93e commit 3b91d68
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/NHibernate/Dialect/Function/SQLFunctionRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class SQLFunctionRegistry
{
private readonly Dialect dialect;
private readonly IDictionary<string, ISQLFunction> userFunctions;
//Temporary alias support
private static Dictionary<string, string> _functionAliases = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { "secondtruncated", "second" } };

public SQLFunctionRegistry(Dialect dialect, IDictionary<string, ISQLFunction> userFunctions)
{
Expand All @@ -20,8 +22,11 @@ public SQLFunctionRegistry(Dialect dialect, IDictionary<string, ISQLFunction> us
/// </summary>
public ISQLFunction FindSQLFunction(string functionName)
{
ISQLFunction result;
if (!userFunctions.TryGetValue(functionName, out result))
if (!userFunctions.ContainsKey(functionName) && !dialect.Functions.ContainsKey(functionName) && _functionAliases.TryGetValue(functionName, out var sqlFunction))
{
functionName = sqlFunction;
}
if (!userFunctions.TryGetValue(functionName, out ISQLFunction result))
{
dialect.Functions.TryGetValue(functionName, out result);
}
Expand All @@ -30,6 +35,10 @@ public ISQLFunction FindSQLFunction(string functionName)

public bool HasFunction(string functionName)
{
if (!userFunctions.ContainsKey(functionName) && !dialect.Functions.ContainsKey(functionName) && _functionAliases.TryGetValue(functionName, out var sqlFunction))
{
functionName = sqlFunction;
}
return userFunctions.ContainsKey(functionName) || dialect.Functions.ContainsKey(functionName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DateTimePropertiesHqlGenerator()
public override HqlTreeNode BuildHql(MemberInfo member, Expression expression, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
var functionName = member.Name.ToLowerInvariant();
if (functionName == "second" && (visitor.SessionFactory as ISessionFactoryImplementor)?.Dialect.Functions.ContainsKey("secondtruncated") == true)
if (functionName == "second")
{
functionName = "secondtruncated";
}
Expand Down

0 comments on commit 3b91d68

Please sign in to comment.