-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix to #30410 - JSON: optimize update path for single property elemen…
…t - don't wrap the value in json array Rather than generate a JSON parameter and extract correct value from it, we generate the parameter value directly. Special casing is handled on the provider level by *ModificationCommand and *UpdateSqlGenerator Fixes #30410
- Loading branch information
Showing
9 changed files
with
339 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/EFCore.SqlServer/Update/Internal/SqlServerModificationCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.Json.Nodes; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Update.Internal; | ||
|
||
/// <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 class SqlServerModificationCommand : ModificationCommand | ||
{ | ||
/// <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 SqlServerModificationCommand(in ModificationCommandParameters modificationCommandParameters) | ||
: base(modificationCommandParameters) | ||
{ | ||
} | ||
|
||
/// <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 SqlServerModificationCommand(in NonTrackedModificationCommandParameters modificationCommandParameters) | ||
: base(modificationCommandParameters) | ||
{ | ||
} | ||
|
||
/// <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> | ||
protected override object? GenerateValueForSinglePropertyUpdate(IProperty property, object? propertyValue) | ||
{ | ||
var propertyProviderClrType = (property.GetTypeMapping().Converter?.ProviderClrType ?? property.ClrType).UnwrapNullableType(); | ||
|
||
// when we generate SqlParameter when updating single property in JSON entity | ||
// we always use SqlServerJsonTypeMapping as type mapping for the parameter | ||
// (since we don't have dedicated type mapping for individual JSON properties) | ||
// later, when we generate DbParameter we assign the value and then the DbType from the type mapping. | ||
// in case of byte value, when we assign the value to the DbParameter it sets its type to Byte and its size to 1 | ||
// then, we change DbType to String, but keep size as is | ||
// so, if value was, say, 15 we initially generate DbParameter of type Byte, value 25 and size 1 | ||
// but when we change the type we end up with type String, value 25 and size 1, which effectively is "2" | ||
// to mitigate this, we convert the value to string, to guarantee the correct parameter size. | ||
// this can be removed when we have dedicated JSON type mapping for individual (leaf) properties | ||
if (propertyProviderClrType == typeof(byte)) | ||
{ | ||
return JsonValue.Create(propertyValue)?.ToJsonString().Replace("\"", ""); | ||
} | ||
|
||
#pragma warning disable EF1001 // Internal EF Core API usage. | ||
return base.GenerateValueForSinglePropertyUpdate(property, propertyValue); | ||
#pragma warning restore EF1001 // Internal EF Core API usage. | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/EFCore.SqlServer/Update/Internal/SqlServerModificationCommandFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Update.Internal; | ||
|
||
/// <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 class SqlServerModificationCommandFactory : IModificationCommandFactory | ||
{ | ||
/// <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 virtual IModificationCommand CreateModificationCommand( | ||
in ModificationCommandParameters modificationCommandParameters) | ||
=> new SqlServerModificationCommand(modificationCommandParameters); | ||
|
||
/// <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 virtual INonTrackedModificationCommand CreateNonTrackedModificationCommand( | ||
in NonTrackedModificationCommandParameters modificationCommandParameters) | ||
=> new SqlServerModificationCommand(modificationCommandParameters); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.