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

Fix missing type mapping for CONVERT #33311

Merged
merged 5 commits into from
Mar 14, 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,7 +37,7 @@ public SqlServerMethodCallTranslatorProvider(RelationalMethodCallTranslatorProvi
new SqlServerIsNumericFunctionTranslator(sqlExpressionFactory),
new SqlServerMathTranslator(sqlExpressionFactory),
new SqlServerNewGuidTranslator(sqlExpressionFactory),
new SqlServerObjectToStringTranslator(sqlExpressionFactory),
new SqlServerObjectToStringTranslator(sqlExpressionFactory, typeMappingSource),
new SqlServerStringMethodTranslator(sqlExpressionFactory),
new SqlServerTimeOnlyMethodTranslator(sqlExpressionFactory)
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ private static readonly Dictionary<Type, string> TypeMapping
};

private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly IRelationalTypeMappingSource _typeMappingSource;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFactory)
public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFactory, IRelationalTypeMappingSource typeMappingSource)
{
_sqlExpressionFactory = sqlExpressionFactory;
_typeMappingSource = typeMappingSource;
}

/// <summary>
Expand Down Expand Up @@ -107,7 +109,8 @@ public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFact
new[] { _sqlExpressionFactory.Fragment(storeType), instance },
nullable: true,
argumentsPropagateNullability: new[] { false, true },
typeof(string))
typeof(string),
_typeMappingSource.GetMapping(storeType))
cincuranet marked this conversation as resolved.
Show resolved Hide resolved
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,12 @@ public override Task String_Concat(bool async)
public override Task Where_DateOnly_FromDateTime(bool async)
=> AssertTranslationFailed(() => base.Where_DateOnly_FromDateTime(async));

public override Task Select_ToString_IndexOf(bool async)
=> AssertTranslationFailed(() => base.Select_ToString_IndexOf(async));

public override Task Select_IndexOf_ToString(bool async)
=> AssertTranslationFailed(() => base.Select_IndexOf_ToString(async));

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1852,4 +1852,18 @@ public virtual Task Where_DateOnly_FromDateTime(bool async)
ss => ss.Set<Order>()
.Where(o => o.OrderDate.HasValue && DateOnly.FromDateTime(o.OrderDate.Value) == new DateOnly(1996, 9, 16))
.AsTracking());

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_ToString_IndexOf(bool async)
=> AssertQuery(
async,
ss => ss.Set<Order>().Where(x => x.OrderID.ToString().IndexOf("123") == -1));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_IndexOf_ToString(bool async)
=> AssertQuery(
async,
ss => ss.Set<Order>().Where(x => "123".IndexOf(x.OrderID.ToString()) == -1));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2723,6 +2723,33 @@ public override Task Regex_IsMatch_MethodCall_constant_input(bool async)
public override Task Datetime_subtraction_TotalDays(bool async)
=> AssertTranslationFailed(() => base.Datetime_subtraction_TotalDays(async));

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

AssertSql(
"""
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE CHARINDEX('123', CONVERT(varchar(11), [o].[OrderID])) - 1 = -1
""");
}

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

AssertSql(
"""
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE CASE
WHEN CONVERT(varchar(11), [o].[OrderID]) = '' THEN 0
ELSE CHARINDEX(CONVERT(varchar(11), [o].[OrderID]), '123') - 1
END = -1
""");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task StandardDeviation(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3476,7 +3476,7 @@ public override async Task Query_expression_with_to_string_and_contains(bool asy
"""
SELECT [o].[CustomerID]
FROM [Orders] AS [o]
WHERE [o].[OrderDate] IS NOT NULL AND CONVERT(varchar(10), [o].[EmployeeID]) LIKE N'%7%'
WHERE [o].[OrderDate] IS NOT NULL AND CONVERT(varchar(10), [o].[EmployeeID]) LIKE '%7%'
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ public override async Task Like_with_non_string_column_using_ToString(bool async
"""
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE CONVERT(varchar(11), [o].[OrderID]) LIKE N'%20%'
WHERE CONVERT(varchar(11), [o].[OrderID]) LIKE '%20%'
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,30 @@ public override async Task Where_DateOnly_FromDateTime(bool async)
""");
}

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

AssertSql(
"""
SELECT "o"."OrderID", "o"."CustomerID", "o"."EmployeeID", "o"."OrderDate"
FROM "Orders" AS "o"
WHERE instr(CAST("o"."OrderID" AS TEXT), '123') - 1 = -1
""");
}

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

AssertSql(
"""
SELECT "o"."OrderID", "o"."CustomerID", "o"."EmployeeID", "o"."OrderDate"
FROM "Orders" AS "o"
WHERE instr('123', CAST("o"."OrderID" AS TEXT)) - 1 = -1
""");
}

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