Skip to content
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

Revert case insensitive changes #205

Merged
merged 3 commits into from
Aug 21, 2024
Merged
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
20 changes: 17 additions & 3 deletions src/Gridify/Builder/BaseQueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using Gridify.Reflection;
using Gridify.Syntax;

namespace Gridify.Builder;
Expand Down Expand Up @@ -123,7 +124,7 @@ public TQuery Build(ExpressionSyntax expression)
return (result, isNested);
}

var query = BuildQuery(mapTarget.Body, mapTarget.Parameters[0], right, op, gMap.Convertor);
var query = BuildQuery(mapTarget.Body, mapTarget.Parameters[0], right, op, gMap.Convertor, false);
if (query == null) return null;

if (hasIndexer)
Expand Down Expand Up @@ -157,7 +158,8 @@ private static object AddIndexerNullCheck(LambdaExpression mapTarget, object que
ParameterExpression parameter,
ValueExpressionSyntax valueExpression,
ISyntaxNode op,
Func<string, object>? convertor)
Func<string, object>? convertor,
bool isNested)
{
// Remove the boxing for value types
if (body.NodeType == ExpressionType.Convert) body = ((UnaryExpression)body).Operand;
Expand Down Expand Up @@ -197,7 +199,7 @@ private static object AddIndexerNullCheck(LambdaExpression mapTarget, object que
{
return BuildAlwaysFalseQuery(parameter);
}

if (value is DateTime dateTime)
{
if (mapper.Configuration.DefaultDateTimeKind.HasValue)
Expand All @@ -207,6 +209,18 @@ private static object AddIndexerNullCheck(LambdaExpression mapTarget, object que
}
}

// handle case-Insensitive search
if (value is not null && (valueExpression.IsCaseInsensitive
|| (mapper.Configuration.CaseInsensitiveFiltering && !isNested && body.Type == typeof(string)))
&& op.Kind is not SyntaxKind.GreaterThan
&& op.Kind is not SyntaxKind.LessThan
&& op.Kind is not SyntaxKind.GreaterOrEqualThan
&& op.Kind is not SyntaxKind.LessOrEqualThan)
{
value = value.ToString()?.ToLower();
body = Expression.Call(body, MethodInfoHelper.GetToLowerMethod());
}

var query = BuildQueryAccordingToValueType(body, parameter, value, op, valueExpression);
return query;
}
Expand Down
135 changes: 40 additions & 95 deletions src/Gridify/Builder/LinqQueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using Gridify.Reflection;
Expand All @@ -9,7 +7,7 @@

namespace Gridify.Builder;

public class LinqQueryBuilder<T>(IGridifyMapper<T> mapper) : BaseQueryBuilder<Expression<Func<T, bool>>, T>(mapper)

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.
{
protected override Expression<Func<T, bool>>? BuildNestedQuery(
Expression body, IGMap<T> gMap, ValueExpressionSyntax value, ISyntaxNode op)
Expand All @@ -25,7 +23,8 @@
targetExp.Parameters[0],
value,
op,
gMap.Convertor);
gMap.Convertor,
true);

if (conditionExp is not LambdaExpression lambdaExp) return null;

Expand Down Expand Up @@ -117,15 +116,6 @@

switch (op.Kind)
{
case SyntaxKind.Equal when !valueExpression.IsNullOrDefault && areBothStrings && (valueExpression.IsCaseInsensitive || mapper.Configuration.CaseInsensitiveFiltering):
be = Expression.Call(
null,
MethodInfoHelper.GetCaseAwareEqualsMethod(),
body,
GetValueExpression(body.Type, value),
Expression.Constant(StringComparison.InvariantCultureIgnoreCase)
);
break;
case SyntaxKind.Equal when !valueExpression.IsNullOrDefault:
be = Expression.Equal(body, GetValueExpression(body.Type, value));
break;
Expand All @@ -142,15 +132,6 @@
: Expression.Equal(body, Expression.Default(body.Type));
}

break;
case SyntaxKind.NotEqual when !valueExpression.IsNullOrDefault && areBothStrings && (valueExpression.IsCaseInsensitive || mapper.Configuration.CaseInsensitiveFiltering):
be = Expression.Not(Expression.Call(
null,
MethodInfoHelper.GetCaseAwareEqualsMethod(),
body,
GetValueExpression(body.Type, value),
Expression.Constant(StringComparison.InvariantCultureIgnoreCase))
);
break;
case SyntaxKind.NotEqual when !valueExpression.IsNullOrDefault:
be = Expression.NotEqual(body, GetValueExpression(body.Type, value));
Expand Down Expand Up @@ -194,74 +175,60 @@
case SyntaxKind.LessOrEqualThan when areBothStrings:
be = GetLessThanOrEqualExpression(body, valueExpression, value);
break;
case SyntaxKind.Like or SyntaxKind.NotLike:
if (areBothStrings && (valueExpression.IsCaseInsensitive || mapper.Configuration.CaseInsensitiveFiltering))
case SyntaxKind.Like:
be = Expression.Call(body, MethodInfoHelper.GetStringContainsMethod(), GetValueExpression(body.Type, value));
break;
case SyntaxKind.NotLike:
be = Expression.Not(Expression.Call(body, MethodInfoHelper.GetStringContainsMethod(), GetValueExpression(body.Type, value)));
break;
case SyntaxKind.StartsWith:
if (body.Type != typeof(string))
{
be = Expression.Call(
body,
MethodInfoHelper.GetCaseAwareStringContainsMethod(),
GetValueExpression(body.Type, value),
Expression.Constant(StringComparison.InvariantCultureIgnoreCase)
);
body = Expression.Call(body, MethodInfoHelper.GetToStringMethod());
be = Expression.Call(body, MethodInfoHelper.GetStartWithMethod(), GetValueExpression(body.Type, value?.ToString()));
}
else
{
be = Expression.Call(body, MethodInfoHelper.GetStringContainsMethod(), GetValueExpression(body.Type, value));
be = Expression.Call(body, MethodInfoHelper.GetStartWithMethod(), GetValueExpression(body.Type, value));
}

if (op.Kind == SyntaxKind.NotLike)
be = Expression.Not(be);

break;
case SyntaxKind.StartsWith or SyntaxKind.NotStartsWith:
case SyntaxKind.EndsWith:
if (body.Type != typeof(string))
{
body = Expression.Call(body, MethodInfoHelper.GetToStringMethod());
be = Expression.Call(body, MethodInfoHelper.GetStartWithMethod(), GetValueExpression(body.Type, value?.ToString()));
}
else if (areBothStrings && (valueExpression.IsCaseInsensitive || mapper.Configuration.CaseInsensitiveFiltering))
{
be = Expression.Call(
body,
MethodInfoHelper.GetCaseAwareStartsWithMethod(),
GetValueExpression(body.Type, value),
Expression.Constant(StringComparison.InvariantCultureIgnoreCase)
);
be = Expression.Call(body, MethodInfoHelper.GetEndsWithMethod(), GetValueExpression(body.Type, value?.ToString()));
}
else
{
be = Expression.Call(body, MethodInfoHelper.GetStartWithMethod(), GetValueExpression(body.Type, value));
be = Expression.Call(body, MethodInfoHelper.GetEndsWithMethod(), GetValueExpression(body.Type, value));
}

if (op.Kind == SyntaxKind.NotStartsWith)
be = Expression.Not(be);

break;
case SyntaxKind.EndsWith or SyntaxKind.NotEndsWith:
case SyntaxKind.NotStartsWith:
if (body.Type != typeof(string))
{
body = Expression.Call(body, MethodInfoHelper.GetToStringMethod());
be = Expression.Call(body, MethodInfoHelper.GetStartWithMethod(), GetValueExpression(body.Type, value?.ToString()));
be = Expression.Not(Expression.Call(body, MethodInfoHelper.GetStartWithMethod(), GetValueExpression(body.Type, value?.ToString())));
}
else if (areBothStrings && (valueExpression.IsCaseInsensitive || mapper.Configuration.CaseInsensitiveFiltering))
else
{
be = Expression.Call(
body,
MethodInfoHelper.GetCaseAwareEndsWithMethod(),
GetValueExpression(body.Type, value),
Expression.Constant(StringComparison.InvariantCultureIgnoreCase)
);
be = Expression.Not(Expression.Call(body, MethodInfoHelper.GetStartWithMethod(), GetValueExpression(body.Type, value)));
}

break;
case SyntaxKind.NotEndsWith:
if (body.Type != typeof(string))
{
body = Expression.Call(body, MethodInfoHelper.GetToStringMethod());
be = Expression.Not(Expression.Call(body, MethodInfoHelper.GetEndsWithMethod(), GetValueExpression(body.Type, value?.ToString())));
}
else
{
be = Expression.Call(body, MethodInfoHelper.GetEndsWithMethod(), GetValueExpression(body.Type, value));
be = Expression.Not(Expression.Call(body, MethodInfoHelper.GetEndsWithMethod(), GetValueExpression(body.Type, value)));
}

if (op.Kind == SyntaxKind.NotEndsWith)
be = Expression.Not(be);

break;

case SyntaxKind.CustomOperator:
var token = (SyntaxToken)op;
var customOperator = GridifyGlobalConfiguration.CustomOperators.Operators.First(q => q.GetOperator() == token!.Text);
Expand Down Expand Up @@ -299,7 +266,7 @@
case MethodCallExpression { Method.Name: "Select" } selectExp:
{
var targetExp = selectExp.Arguments.Single(a => a.NodeType == ExpressionType.Lambda) as LambdaExpression;
var conditionExp = BuildQuery(targetExp!.Body, targetExp.Parameters[0], value, op, gMap.Convertor);
var conditionExp = BuildQuery(targetExp!.Body, targetExp.Parameters[0], value, op, gMap.Convertor, true);

if (conditionExp is not LambdaExpression lambdaExp) return null;

Expand Down Expand Up @@ -336,14 +303,14 @@
{
case MemberExpression member:
{
if (op.Kind is not (SyntaxKind.Equal or SyntaxKind.NotEqual) ||
!member.Type.IsSimpleTypeCollection(out _)) return GetAnyExpression(member, predicate);
return predicate.Body switch
if (op.Kind is SyntaxKind.Equal or SyntaxKind.NotEqual &&
member.Type.IsSimpleTypeCollection(out _) &&
predicate.Body is BinaryExpression binaryExpression)
{
BinaryExpression binaryExpression => GetContainsExpression(member, binaryExpression, op),
MethodCallExpression { Method.Name: "Equals" } methodCallExpression => GetCaseSensitiveContainsExpression(member, methodCallExpression, op),
_ => GetAnyExpression(member, predicate)
};
return GetContainsExpression(member, binaryExpression, op);
}

return GetAnyExpression(member, predicate);
}
case MethodCallExpression { Method.Name: "SelectMany" } subExp
when subExp.Arguments.Last()
Expand Down Expand Up @@ -382,28 +349,6 @@
}
}

private static LambdaExpression GetCaseSensitiveContainsExpression(MemberExpression member, MethodCallExpression methodCallExpression, ISyntaxNode op)
{
var param = GetParameterExpression(member);
var prop = GetPropertyOrField(member, param);

var tp = prop.Type.IsGenericType
? prop.Type.GenericTypeArguments.First() // list
: prop.Type.GetElementType(); // array

if (tp == null) throw new GridifyFilteringException($"Can not detect the '{member.Member.Name}' property type.");

var containsMethod = MethodInfoHelper.GetCaseAwareContainsMethod(tp);
var ignoreCaseComparerExpression = Expression.Constant(StringComparer.InvariantCultureIgnoreCase);
var value = methodCallExpression.Arguments[1];
Expression containsExp = Expression.Call(containsMethod, prop, value, ignoreCaseComparerExpression);
if (op.Kind == SyntaxKind.NotEqual)
{
containsExp = Expression.Not(containsExp);
}
return GetExpressionWithNullCheck(prop, param, containsExp);
}

private static LambdaExpression GetContainsExpression(MemberExpression member, BinaryExpression binaryExpression, ISyntaxNode op)
{
var param = GetParameterExpression(member);
Expand Down Expand Up @@ -530,10 +475,10 @@
GetStringComparisonCaseExpression(valueExpression.IsCaseInsensitive)), Expression.Constant(0));
}

private static ConstantExpression GetStringComparisonCaseExpression(bool isCaseInsensitive)
private ConstantExpression GetStringComparisonCaseExpression(bool isCaseInsensitive)
{
return isCaseInsensitive
? Expression.Constant(StringComparison.InvariantCultureIgnoreCase)
return isCaseInsensitive || mapper.Configuration.CaseInsensitiveFiltering
? Expression.Constant(StringComparison.OrdinalIgnoreCase)
: Expression.Constant(StringComparison.Ordinal);
}

Expand Down
30 changes: 5 additions & 25 deletions src/Gridify/Reflection/MethodInfoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace Gridify.Reflection;

public static class MethodInfoHelper
{
public static MethodInfo GetToLowerMethod()
{
return typeof(string).GetMethod("ToLower", [])!;
}

public static MethodInfo GetAnyMethod(Type type)
{
return typeof(Enumerable).GetMethods().Single(m => m.Name == "Any" && m.GetParameters().Length == 2).MakeGenericMethod(type);
Expand Down Expand Up @@ -55,29 +60,4 @@ public static MethodInfo GetSelectMethod(this Type type)
{
return typeof(Enumerable).GetMethods().First(m => m.Name == "Select").MakeGenericMethod([type, type]);
}

public static MethodInfo GetCaseAwareContainsMethod(Type tp)
{
return typeof(Enumerable).GetMethods().Last(x => x.Name == "Contains").MakeGenericMethod(tp);
}

public static MethodInfo GetCaseAwareStringContainsMethod()
{
return typeof(string).GetMethod("Contains", [typeof(string), typeof(StringComparison)])!;
}

public static MethodInfo GetCaseAwareEqualsMethod()
{
return typeof(string).GetMethod("Equals", [typeof(string), typeof(string), typeof(StringComparison)])!;
}

public static MethodInfo GetCaseAwareStartsWithMethod()
{
return typeof(string).GetMethod("StartsWith", [typeof(string), typeof(StringComparison)])!;
}

public static MethodInfo GetCaseAwareEndsWithMethod()
{
return typeof(string).GetMethod("EndsWith", [typeof(string), typeof(StringComparison)])!;
}
}
65 changes: 0 additions & 65 deletions test/EntityFrameworkPostgreSqlIntegrationTests/Issue193Tests.cs

This file was deleted.

2 changes: 1 addition & 1 deletion test/Gridify.Tests/GridifyExtensionsShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ public void ApplyFiltering_GreaterThanOrEqualBetweenTwoStrings()
public void ApplyFiltering_GreaterThanOrEqual_CaseInsensitive_BetweenTwoStrings()
{
var actual = _fakeRepository.AsQueryable().ApplyFiltering("name >= j/i").ToList();
var expected = _fakeRepository.Where(q => string.Compare(q.Name, "j", StringComparison.InvariantCultureIgnoreCase) >= 0).ToList();
var expected = _fakeRepository.Where(q => string.Compare(q.Name, "j", StringComparison.OrdinalIgnoreCase) >= 0).ToList();

Assert.Equal(expected.Count, actual.Count);
Assert.Equal(expected, actual);
Expand Down
Loading
Loading