Skip to content

Commit 9f67be2

Browse files
committed
Generalize detection of collection parameters
1 parent 1dc192d commit 9f67be2

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

src/NHibernate/Linq/Functions/BaseHqlGeneratorForMethod.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@ public abstract class BaseHqlGeneratorForMethod : IHqlGeneratorForMethod, IHqlGe
1414
public abstract HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor);
1515

1616
public virtual bool AllowsNullableReturnType(MethodInfo method) => true;
17+
18+
/// <inheritdoc />
19+
public virtual bool TryGetCollectionParameter(MethodCallExpression expression, out ConstantExpression collectionParameter)
20+
{
21+
collectionParameter = null;
22+
return false;
23+
}
1724
}
1825
}

src/NHibernate/Linq/Functions/GetValueOrDefaultGenerator.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,12 @@ private static HqlExpression GetRhs(MethodInfo method, ReadOnlyCollection<Expres
4242
}
4343

4444
public bool AllowsNullableReturnType(MethodInfo method) => !method.ReturnType.IsValueType;
45+
46+
/// <inheritdoc />
47+
public bool TryGetCollectionParameter(MethodCallExpression expression, out ConstantExpression collectionParameter)
48+
{
49+
collectionParameter = null;
50+
return false;
51+
}
4552
}
4653
}

src/NHibernate/Linq/Functions/IHqlGeneratorForMethod.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ public interface IHqlGeneratorForMethod
1818
internal interface IHqlGeneratorForMethodExtended
1919
{
2020
bool AllowsNullableReturnType(MethodInfo method);
21+
22+
/// <summary>
23+
/// Try getting a collection parameter from <see cref="MethodCallExpression"/>.
24+
/// </summary>
25+
/// <param name="expression">The method call expression.</param>
26+
/// <param name="collectionParameter">Output parameter for the retrieved collection parameter.</param>
27+
/// <returns>Whether collection parameter was retrieved.</returns>
28+
bool TryGetCollectionParameter(MethodCallExpression expression, out ConstantExpression collectionParameter);
2129
}
2230

2331
internal static class HqlGeneratorForMethodExtensions
@@ -33,6 +41,21 @@ public static bool AllowsNullableReturnType(this IHqlGeneratorForMethod generato
3341
return true;
3442
}
3543

44+
// 6.0 TODO: Remove
45+
public static bool TryGetCollectionParameters(
46+
this IHqlGeneratorForMethod generator,
47+
MethodCallExpression expression,
48+
out ConstantExpression collectionParameter)
49+
{
50+
if (generator is IHqlGeneratorForMethodExtended extendedGenerator)
51+
{
52+
return extendedGenerator.TryGetCollectionParameter(expression, out collectionParameter);
53+
}
54+
55+
collectionParameter = null;
56+
return false;
57+
}
58+
3659
// 6.0 TODO: merge into IHqlGeneratorForMethod
3760
/// <summary>
3861
/// Should pre-evaluation be allowed for this method?

src/NHibernate/Linq/Functions/QueryableGenerator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ public CollectionContainsGenerator()
155155

156156
public override bool AllowsNullableReturnType(MethodInfo method) => false;
157157

158+
/// <inheritdoc />
159+
public override bool TryGetCollectionParameter(MethodCallExpression expression, out ConstantExpression collectionParameter)
160+
{
161+
var argument = expression.Method.IsStatic ? expression.Arguments[0] : expression.Object;
162+
collectionParameter = argument as ConstantExpression;
163+
164+
return collectionParameter != null;
165+
}
166+
158167
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
159168
{
160169
// TODO - alias generator

src/NHibernate/Linq/Functions/StringGenerator.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
5959
}
6060

6161
public bool AllowsNullableReturnType(MethodInfo method) => false;
62+
63+
/// <inheritdoc />
64+
public bool TryGetCollectionParameter(MethodCallExpression expression, out ConstantExpression collectionParameter)
65+
{
66+
collectionParameter = null;
67+
return false;
68+
}
6269
}
6370

6471
public class LengthGenerator : BaseHqlGeneratorForProperty

src/NHibernate/Linq/Visitors/ExpressionParameterVisitor.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,11 @@ protected override Expression VisitMethodCall(MethodCallExpression expression)
102102
return Expression.Call(null, expression.Method, query, arg);
103103
}
104104

105-
if (_functionRegistry != null &&
106-
method.Name == nameof(Queryable.Contains) &&
107-
_functionRegistry.TryGetGenerator(method, out var generator) &&
108-
generator is CollectionContainsGenerator)
105+
if (_functionRegistry != null &&
106+
_functionRegistry.TryGetGenerator(method, out var generator) &&
107+
generator.TryGetCollectionParameters(expression, out var collectionParameter))
109108
{
110-
var argument = method.IsStatic ? expression.Arguments[0] : expression.Object;
111-
if (argument is ConstantExpression constantExpression)
112-
{
113-
_collectionParameters.Add(constantExpression);
114-
}
109+
_collectionParameters.Add(collectionParameter);
115110
}
116111

117112
if (VisitorUtil.IsDynamicComponentDictionaryGetter(expression, _sessionFactory))

0 commit comments

Comments
 (0)