Skip to content

Rewrite string.Contains/string.StartsWith/string.EndsWith to SqlMethods.Like so DB can reuse query plan #2396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

# ReSharper properties
resharper_space_within_single_line_array_initializer_braces = true

[*.xsd]
indent_style = tab

Expand Down
63 changes: 63 additions & 0 deletions src/NHibernate/Linq/ExpressionTransformers/LikeTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Util;
using Remotion.Linq.Parsing.ExpressionVisitors.Transformation;

namespace NHibernate.Linq.ExpressionTransformers
{
/// <summary>
/// Replace <see cref="string.StartsWith(string)"/>, <see cref="string.EndsWith(string)"/> and <see cref="string.Contains(string)"/>
/// with <see cref="SqlMethods.Like(string, string)"/>
/// </summary>
internal class LikeTransformer : IExpressionTransformer<MethodCallExpression>
{
private static readonly MethodInfo Like = ReflectHelper.FastGetMethod(SqlMethods.Like, default(string), default(string));

public ExpressionType[] SupportedExpressionTypes { get; } = {ExpressionType.Call};

public Expression Transform(MethodCallExpression expression)
{
if (IsLike(expression, out var value))
{
return Expression.Call(
Like,
expression.Object,
Expression.Constant(value)
);
}

return expression;
}

private static bool IsLike(MethodCallExpression expression, out string value)
{
if (expression.Method == ReflectionCache.StringMethods.StartsWith)
{
if (expression.Arguments[0] is ConstantExpression constantExpression)
{
value = string.Concat(constantExpression.Value, "%");
return true;
}
}
else if (expression.Method == ReflectionCache.StringMethods.EndsWith)
{
if (expression.Arguments[0] is ConstantExpression constantExpression)
{
value = string.Concat("%", constantExpression.Value);
return true;
}
}
else if (expression.Method == ReflectionCache.StringMethods.Contains)
{
if (expression.Arguments[0] is ConstantExpression constantExpression)
{
value = string.Concat("%", constantExpression.Value, "%");
return true;
}
}

value = null;
return false;
}
}
}
6 changes: 3 additions & 3 deletions src/NHibernate/Linq/Functions/StringGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class StartsWithGenerator : BaseHqlGeneratorForMethod
{
public StartsWithGenerator()
{
SupportedMethods = new[] { ReflectHelper.GetMethodDefinition<string>(x => x.StartsWith(null)) };
SupportedMethods = new[] { ReflectionCache.StringMethods.StartsWith };
}

public override bool AllowsNullableReturnType(MethodInfo method) => false;
Expand All @@ -97,7 +97,7 @@ public class EndsWithGenerator : BaseHqlGeneratorForMethod
{
public EndsWithGenerator()
{
SupportedMethods = new[] { ReflectHelper.GetMethodDefinition<string>(x => x.EndsWith(null)) };
SupportedMethods = new[] { ReflectionCache.StringMethods.EndsWith };
}

public override bool AllowsNullableReturnType(MethodInfo method) => false;
Expand All @@ -116,7 +116,7 @@ public class ContainsGenerator : BaseHqlGeneratorForMethod
{
public ContainsGenerator()
{
SupportedMethods = new[] { ReflectHelper.GetMethodDefinition<string>(x => x.Contains(null)) };
SupportedMethods = new[] { ReflectionCache.StringMethods.Contains };
}

public override bool AllowsNullableReturnType(MethodInfo method) => false;
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Linq/NhRelinqQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static NhRelinqQueryParser()
// NH-3247: must remove .Net compiler char to int conversion before
// parameterization occurs.
preTransformerRegistry.Register(new RemoveCharToIntConversion());
preTransformerRegistry.Register(new LikeTransformer());
PreProcessor = new TransformingExpressionTreeProcessor(preTransformerRegistry);

var transformerRegistry = ExpressionTransformerRegistry.CreateDefault();
Expand Down
7 changes: 7 additions & 0 deletions src/NHibernate/Util/ReflectionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,12 @@ internal static class TypeMethods
internal static readonly MethodInfo GetTypeFromHandle =
ReflectHelper.FastGetMethod(System.Type.GetTypeFromHandle, default(RuntimeTypeHandle));
}

internal static class StringMethods
{
public static readonly MethodInfo EndsWith = ReflectHelper.GetMethodDefinition<string>(x => x.EndsWith(null));
public static readonly MethodInfo StartsWith = ReflectHelper.GetMethodDefinition<string>(x => x.StartsWith(null));
public static readonly MethodInfo Contains = ReflectHelper.GetMethodDefinition<string>(x => x.Contains(null));
}
}
}