Skip to content

Commit

Permalink
Add method postfix when rewriting parameters for StartsWith/EndsWith/…
Browse files Browse the repository at this point in the history
…Contains

Fixes #2994
  • Loading branch information
roji committed Nov 28, 2023
1 parent 8402038 commit 397c916
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down Expand Up @@ -450,7 +451,8 @@ when patternParameter.Name.StartsWith(QueryCompilationContext.QueryParameterPref
QueryCompilationContext.QueryContextParameter);

var escapedPatternParameter =
_queryCompilationContext.RegisterRuntimeParameter(patternParameter.Name + "_rewritten", lambda);
_queryCompilationContext.RegisterRuntimeParameter(
$"{patternParameter.Name}_{methodType.ToString().ToLower(CultureInfo.InvariantCulture)}", lambda);

translation = _sqlExpressionFactory.Like(
translatedInstance,
Expand Down
16 changes: 8 additions & 8 deletions test/EFCore.PG.FunctionalTests/Query/CitextQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public void StartsWith_param_pattern()

Assert.Equal(1, result.Id);
AssertSql(
"""
@__param_0_rewritten='some%'
"""
@__param_0_startswith='some%'

SELECT s."Id", s."CaseInsensitiveText"
FROM "SomeEntities" AS s
WHERE s."CaseInsensitiveText" LIKE @__param_0_rewritten ESCAPE '\'
WHERE s."CaseInsensitiveText" LIKE @__param_0_startswith ESCAPE '\'
LIMIT 2
""");
}
Expand Down Expand Up @@ -98,11 +98,11 @@ public void EndsWith_param_pattern()
Assert.Equal(1, result.Id);
AssertSql(
"""
@__param_0_rewritten='%sometext'
@__param_0_endswith='%sometext'

SELECT s."Id", s."CaseInsensitiveText"
FROM "SomeEntities" AS s
WHERE s."CaseInsensitiveText" LIKE @__param_0_rewritten ESCAPE '\'
WHERE s."CaseInsensitiveText" LIKE @__param_0_endswith ESCAPE '\'
LIMIT 2
""");
}
Expand Down Expand Up @@ -151,12 +151,12 @@ public void Contains_param_pattern()

Assert.Equal(1, result.Id);
AssertSql(
"""
@__param_0_rewritten='%ometex%'
"""
@__param_0_contains='%ometex%'

SELECT s."Id", s."CaseInsensitiveText"
FROM "SomeEntities" AS s
WHERE s."CaseInsensitiveText" LIKE @__param_0_rewritten ESCAPE '\'
WHERE s."CaseInsensitiveText" LIKE @__param_0_contains ESCAPE '\'
LIMIT 2
""");
}
Expand Down
27 changes: 27 additions & 0 deletions test/EFCore.PG.FunctionalTests/Query/FunkyDataQueryNpgsqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ await AssertQuery(
ss => ss.Set<FunkyCustomer>().Where(c => c.FirstName != null && c.FirstName.StartsWith(param)));
}

[ConditionalTheory] // TODO: Remove, test was introduced upstream
[MemberData(nameof(IsAsyncData))]
public virtual async Task String_Contains_and_StartsWith_with_same_parameter(bool async)
{
var s = "B";

await AssertQuery(
async,
ss => ss.Set<FunkyCustomer>().Where(
c => c.FirstName.Contains(s) || c.LastName.StartsWith(s)),
ss => ss.Set<FunkyCustomer>().Where(
c => c.FirstName.MaybeScalar(f => f.Contains(s)) == true || c.LastName.MaybeScalar(l => l.StartsWith(s)) == true));

AssertSql(
"""
@__s_0_contains='%B%'
@__s_0_startswith='B%'

SELECT f."Id", f."FirstName", f."LastName", f."NullableBool"
FROM "FunkyCustomers" AS f
WHERE f."FirstName" LIKE @__s_0_contains ESCAPE '\' OR f."LastName" LIKE @__s_0_startswith ESCAPE '\'
""");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

public class FunkyDataQueryNpgsqlFixture : FunkyDataQueryFixtureBase
{
private FunkyDataData _expectedData;
Expand Down

0 comments on commit 397c916

Please sign in to comment.