Skip to content

Commit

Permalink
Fix Nullable<T>.ToString() (#34014)
Browse files Browse the repository at this point in the history
* Fix `Nullable<T>` `ToString` conversion
Nullable values convert to an empty string.
  • Loading branch information
ranma42 committed Jul 16, 2024
1 parent 73db4f0 commit 61bb028
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFact
// Enums are handled by EnumMethodTranslator

return TypeMapping.TryGetValue(instance.Type, out var storeType)
? _sqlExpressionFactory.Function(
"CONVERT",
new[] { _sqlExpressionFactory.Fragment(storeType), instance },
nullable: true,
argumentsPropagateNullability: new[] { false, true },
typeof(string),
_typeMappingSource.GetMapping(storeType))
? _sqlExpressionFactory.Coalesce(
_sqlExpressionFactory.Function(
"CONVERT",
new[] { _sqlExpressionFactory.Fragment(storeType), instance },
nullable: true,
argumentsPropagateNullability: new[] { false, true },
typeof(string),
_typeMappingSource.GetMapping(storeType)),
_sqlExpressionFactory.Constant(string.Empty))
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public SqliteObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFactory
// Enums are handled by EnumMethodTranslator

return TypeMapping.Contains(instance.Type)
? _sqlExpressionFactory.Convert(instance, typeof(string))
? _sqlExpressionFactory.Coalesce(
_sqlExpressionFactory.Convert(instance, typeof(string)),
_sqlExpressionFactory.Constant(string.Empty))
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1019,18 +1019,7 @@ public virtual Task Update_Where_Join_set_property_from_joined_single_result_tab
e => e.c,
s => s.SetProperty(c => c.c.City, c => c.LastOrder.OrderDate.Value.Year.ToString()),
rowsAffectedCount: 8,
(b, a) => Assert.All(
a, c =>
{
if (c.CustomerID == "FISSA")
{
Assert.Null(c.City);
}
else
{
Assert.NotNull(c.City);
}
}));
(b, a) => Assert.All(a, c => Assert.NotNull(c.City)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
Expand All @@ -1055,18 +1044,7 @@ public virtual Task Update_Where_Join_set_property_from_joined_single_result_sca
e => e.c,
s => s.SetProperty(c => c.c.City, c => c.LastOrderDate.ToString()),
rowsAffectedCount: 8,
(b, a) => Assert.All(
a, c =>
{
if (c.CustomerID == "FISSA")
{
Assert.Null(c.City);
}
else
{
Assert.NotNull(c.City);
}
}));
(b, a) => Assert.All(a, c => Assert.NotNull(c.City)));
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public override async Task Update_owned_and_non_owned_properties_with_table_shar
"""
UPDATE [o]
SET [o].[OwnedReference_Number] = CAST(LEN([o].[Title]) AS int),
[o].[Title] = CONVERT(varchar(11), [o].[OwnedReference_Number])
[o].[Title] = COALESCE(CONVERT(varchar(11), [o].[OwnedReference_Number]), '')
FROM [Owner] AS [o]
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1414,11 +1414,11 @@ public override async Task Update_Where_Join_set_property_from_joined_single_res
AssertExecuteUpdateSql(
"""
UPDATE [c]
SET [c].[City] = CONVERT(varchar(11), DATEPART(year, (
SET [c].[City] = COALESCE(CONVERT(varchar(11), DATEPART(year, (
SELECT TOP(1) [o].[OrderDate]
FROM [Orders] AS [o]
WHERE [c].[CustomerID] = [o].[CustomerID]
ORDER BY [o].[OrderDate] DESC)))
ORDER BY [o].[OrderDate] DESC))), '')
FROM [Customers] AS [c]
WHERE [c].[CustomerID] LIKE N'F%'
""");
Expand Down Expand Up @@ -1449,11 +1449,11 @@ public override async Task Update_Where_Join_set_property_from_joined_single_res
AssertExecuteUpdateSql(
"""
UPDATE [c]
SET [c].[City] = CONVERT(varchar(11), DATEPART(year, (
SET [c].[City] = COALESCE(CONVERT(varchar(11), DATEPART(year, (
SELECT TOP(1) [o].[OrderDate]
FROM [Orders] AS [o]
WHERE [c].[CustomerID] = [o].[CustomerID]
ORDER BY [o].[OrderDate] DESC)))
ORDER BY [o].[OrderDate] DESC))), '')
FROM [Customers] AS [c]
WHERE [c].[CustomerID] LIKE N'F%'
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,9 +840,9 @@ public virtual async Task IsDate_valid(bool async)

AssertSql(
"""
SELECT CAST(ISDATE(CONVERT(varchar(100), [o].[OrderDate])) AS bit)
SELECT CAST(ISDATE(COALESCE(CONVERT(varchar(100), [o].[OrderDate]), '')) AS bit)
FROM [Orders] AS [o]
WHERE CAST(ISDATE(CONVERT(varchar(100), [o].[OrderDate])) AS bit) = CAST(1 AS bit)
WHERE CAST(ISDATE(COALESCE(CONVERT(varchar(100), [o].[OrderDate]), '')) AS bit) = CAST(1 AS bit)
""");
}

Expand Down Expand Up @@ -888,9 +888,9 @@ public virtual async Task IsNumeric_not_valid(bool async)

AssertSql(
"""
SELECT CAST(ISNUMERIC(CONVERT(varchar(100), [o].[OrderDate])) ^ 1 AS bit) ^ CAST(1 AS bit)
SELECT CAST(ISNUMERIC(COALESCE(CONVERT(varchar(100), [o].[OrderDate]), '')) ^ 1 AS bit) ^ CAST(1 AS bit)
FROM [Orders] AS [o]
WHERE ISNUMERIC(CONVERT(varchar(100), [o].[OrderDate])) <> 1
WHERE ISNUMERIC(COALESCE(CONVERT(varchar(100), [o].[OrderDate]), '')) <> 1
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3436,7 +3436,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 '%7%'
WHERE [o].[OrderDate] IS NOT NULL AND COALESCE(CONVERT(varchar(10), [o].[EmployeeID]), '') LIKE '%7%'
""");
}

Expand Down Expand Up @@ -3488,7 +3488,7 @@ public override async Task Select_expression_other_to_string(bool async)

AssertSql(
"""
SELECT CONVERT(varchar(100), [o].[OrderDate]) AS [ShipName]
SELECT COALESCE(CONVERT(varchar(100), [o].[OrderDate]), '') AS [ShipName]
FROM [Orders] AS [o]
WHERE [o].[OrderDate] IS NOT NULL
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public override async Task Update_owned_and_non_owned_properties_with_table_shar
"""
UPDATE "Owner" AS "o"
SET "OwnedReference_Number" = length("o"."Title"),
"Title" = CAST("o"."OwnedReference_Number" AS TEXT)
"Title" = COALESCE(CAST("o"."OwnedReference_Number" AS TEXT), '')
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1328,12 +1328,12 @@ public override async Task Update_Where_Join_set_property_from_joined_single_res
AssertExecuteUpdateSql(
"""
UPDATE "Customers" AS "c"
SET "City" = CAST(CAST(strftime('%Y', (
SET "City" = COALESCE(CAST(CAST(strftime('%Y', (
SELECT "o"."OrderDate"
FROM "Orders" AS "o"
WHERE "c"."CustomerID" = "o"."CustomerID"
ORDER BY "o"."OrderDate" DESC
LIMIT 1)) AS INTEGER) AS TEXT)
LIMIT 1)) AS INTEGER) AS TEXT), '')
WHERE "c"."CustomerID" LIKE 'F%'
""");
}
Expand Down Expand Up @@ -1362,12 +1362,12 @@ public override async Task Update_Where_Join_set_property_from_joined_single_res
AssertExecuteUpdateSql(
"""
UPDATE "Customers" AS "c"
SET "City" = CAST(CAST(strftime('%Y', (
SET "City" = COALESCE(CAST(CAST(strftime('%Y', (
SELECT "o"."OrderDate"
FROM "Orders" AS "o"
WHERE "c"."CustomerID" = "o"."CustomerID"
ORDER BY "o"."OrderDate" DESC
LIMIT 1)) AS INTEGER) AS TEXT)
LIMIT 1)) AS INTEGER) AS TEXT), '')
WHERE "c"."CustomerID" LIKE 'F%'
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 instr(CAST("o"."EmployeeID" AS TEXT), '7') > 0
WHERE "o"."OrderDate" IS NOT NULL AND instr(COALESCE(CAST("o"."EmployeeID" AS TEXT), ''), '7') > 0
""");
}

Expand Down

0 comments on commit 61bb028

Please sign in to comment.