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

Optimize string.IndexOf(string, int) translation #1652

Merged
merged 1 commit into from
May 21, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,12 @@ private SqlExpression CharLength(SqlExpression value)

private SqlExpression Locate(SqlExpression sub, SqlExpression str, SqlExpression startIndex = null)
{
var args = startIndex is null
? new SqlExpression[] { sub, str }
: new SqlExpression[] { sub, str, _sqlExpressionFactory.Add(startIndex, _sqlExpressionFactory.Constant(1)) };
var args = startIndex switch
{
null => new SqlExpression[] { sub, str },
SqlConstantExpression { Value:int idx } => new SqlExpression[] { sub, str, _sqlExpressionFactory.Constant(idx + 1) },
_ => new SqlExpression[] { sub, str, _sqlExpressionFactory.Add(startIndex, _sqlExpressionFactory.Constant(1)) }
};
return _sqlExpressionFactory.NullableFunction("LOCATE", args, typeof(int));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,12 +705,27 @@ ELSE LOCATE(CONVERT(LCASE('nt') USING utf8mb4) COLLATE utf8mb4_bin, LCASE(`c`.`C
END = 1");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task StringIndexOf_with_constant_start_index(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.CustomerID.IndexOf("nt", 0, StringComparison.OrdinalIgnoreCase) == 1),
entryCount: 1);

AssertSql(
@"SELECT `c`.`CustomerID`, `c`.`Address`, `c`.`City`, `c`.`CompanyName`, `c`.`ContactName`, `c`.`ContactTitle`, `c`.`Country`, `c`.`Fax`, `c`.`Phone`, `c`.`PostalCode`, `c`.`Region`
FROM `Customers` AS `c`
WHERE (LOCATE(CONVERT(LCASE('nt') USING utf8mb4) COLLATE utf8mb4_bin, LCASE(`c`.`CustomerID`), 1) - 1) = 1");
}

[ConditionalTheory]
[InlineData(0, 1, false)]
[InlineData(2, 0, false)]
[InlineData(0, 1, true)]
[InlineData(2, 0, true)]
public async Task StringIndexOf_with_start_index(int startIndex, int expected, bool async)
public async Task StringIndexOf_with_parameter_start_index(int startIndex, int expected, bool async)
{
await AssertQuery(
async,
Expand Down