Skip to content

Fix string.Length translation after SqlQuery #33580

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

Merged
merged 2 commits into from
Apr 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public CosmosStringMemberTranslator(ISqlExpressionFactory sqlExpressionFactory)
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (member.Name == nameof(string.Length)
&& instance?.Type == typeof(string))
&& member.DeclaringType == typeof(string))
{
return _sqlExpressionFactory.Function(
"LENGTH",
new[] { instance },
new[] { instance! },
returnType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public SqlServerStringMemberTranslator(ISqlExpressionFactory sqlExpressionFactor
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (member.Name == nameof(string.Length)
&& instance?.Type == typeof(string))
&& member.DeclaringType == typeof(string))
{
return _sqlExpressionFactory.Convert(
_sqlExpressionFactory.Function(
"LEN",
new[] { instance },
new[] { instance! },
nullable: true,
argumentsPropagateNullability: new[] { true },
typeof(long)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public SqliteStringLengthTranslator(ISqlExpressionFactory sqlExpressionFactory)
MemberInfo member,
Type returnType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
=> instance?.Type == typeof(string)
=> member.DeclaringType == typeof(string)
&& member.Name == nameof(string.Length)
? _sqlExpressionFactory.Function(
"length",
new[] { instance },
new[] { instance! },
nullable: true,
argumentsPropagateNullability: new[] { true },
returnType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,42 @@ public virtual void Ad_hoc_type_with_unmapped_property_throws()
() => context.Database.SqlQueryRaw<Person>(NormalizeDelimitersInRawString(@"SELECT * FROM [People]"))).Message);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task SqlQueryRaw_then_String_Length(bool async)
{
using var context = CreateContext();
var rawQuery = NormalizeDelimitersInRawString("SELECT 'x' AS [Value] FROM [Customers]");
var query = context.Database.SqlQueryRaw<string>(rawQuery).Where(s => s.Length == 0);

if (async)
{
await query.LoadAsync();
}
else
{
query.Load();
}
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task SqlQueryRaw_then_String_ToUpper_String_Length(bool async)
{
using var context = CreateContext();
var rawQuery = NormalizeDelimitersInRawString("SELECT 'x' AS [Value] FROM [Customers]");
var query = context.Database.SqlQueryRaw<string>(rawQuery).Where(s => s.ToUpper().Length == 0);

if (async)
{
await query.LoadAsync();
}
else
{
query.Load();
}
}

protected class Blog
{
public int Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,34 @@ public override async Task SqlQueryRaw_composed_with_common_table_expression(boo
Assert.Equal(RelationalStrings.FromSqlNonComposable, exception.Message);
}

public override async Task SqlQueryRaw_then_String_Length(bool async)
{
await base.SqlQueryRaw_then_String_Length(async);

AssertSql(
"""
SELECT [s].[Value]
FROM (
SELECT 'x' AS "Value" FROM "Customers"
) AS [s]
WHERE CAST(LEN([s].[Value]) AS int) = 0
""");
}

public override async Task SqlQueryRaw_then_String_ToUpper_String_Length(bool async)
{
await base.SqlQueryRaw_then_String_ToUpper_String_Length(async);

AssertSql(
"""
SELECT [s].[Value]
FROM (
SELECT 'x' AS "Value" FROM "Customers"
) AS [s]
WHERE CAST(LEN(UPPER([s].[Value])) AS int) = 0
""");
}

protected override DbParameter CreateDbParameter(string name, object value)
=> new SqlParameter { ParameterName = name, Value = value };

Expand Down
28 changes: 28 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/Query/SqlQuerySqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ WHERE instr("m"."ContactName", 'z') > 0
""");
}

public override async Task SqlQueryRaw_then_String_Length(bool async)
{
await base.SqlQueryRaw_then_String_Length(async);

AssertSql(
"""
SELECT "s"."Value"
FROM (
SELECT 'x' AS "Value" FROM "Customers"
) AS "s"
WHERE length("s"."Value") = 0
""");
}

public override async Task SqlQueryRaw_then_String_ToUpper_String_Length(bool async)
{
await base.SqlQueryRaw_then_String_ToUpper_String_Length(async);

AssertSql(
"""
SELECT "s"."Value"
FROM (
SELECT 'x' AS "Value" FROM "Customers"
) AS "s"
WHERE length(upper("s"."Value")) = 0
""");
}

protected override DbParameter CreateDbParameter(string name, object value)
=> new SqliteParameter { ParameterName = name, Value = value };

Expand Down
Loading