Skip to content

Commit

Permalink
Fix to #28595 - Use partial updates for JSON
Browse files Browse the repository at this point in the history
Currently, whenever we update/add/delete part of aggregate mapped to JSON column, we were replacing the entire structure.
Improvement is to use JSON_MODIFY which can alter just a portion of JSON structure, give a JSON path.
We analyze the entries that are being edited and for each JSON column we find the common denominator that needs to be replaced to accommodate all the requested changes

Note: Whenever we add/remove element from a collection we need to replace the entire collection in order to populate ordinal key values properly.
Also, modifying a single property is not yet supported - the smallest fragment that will be replaced is one that represents an entity.

Fixes #28595
  • Loading branch information
maumar committed Aug 23, 2022
1 parent c57ecee commit 5d9fd67
Show file tree
Hide file tree
Showing 11 changed files with 933 additions and 149 deletions.
6 changes: 4 additions & 2 deletions src/EFCore.Relational/Update/ColumnModification.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Data;

namespace Microsoft.EntityFrameworkCore.Update;

/// <summary>
Expand Down Expand Up @@ -54,6 +52,7 @@ public ColumnModification(in ColumnModificationParameters columnModificationPara
IsNullable = columnModificationParameters.IsNullable;
_generateParameterName = columnModificationParameters.GenerateParameterName;
Entry = columnModificationParameters.Entry;
JsonPath = columnModificationParameters.JsonPath;

UseParameter = _generateParameterName != null;
}
Expand Down Expand Up @@ -174,6 +173,9 @@ public virtual object? Value
}
}

/// <inheritdoc />
public virtual string? JsonPath { get; }

/// <inheritdoc />
public virtual void AddSharedColumnModification(IColumnModification modification)
{
Expand Down
60 changes: 58 additions & 2 deletions src/EFCore.Relational/Update/ColumnModificationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public readonly record struct ColumnModificationParameters
/// Indicates whether the column is part of a primary or alternate key.
/// </summary>
public bool IsKey { get; init; }

/// <summary>
/// The column.
/// </summary>
Expand All @@ -92,6 +92,11 @@ public readonly record struct ColumnModificationParameters
/// </summary>
public string? ColumnType { get; init; }

/// <summary>
/// In case of JSON column modification, the JSON path leading to the JSON element that needs to be updated.
/// </summary>
public string? JsonPath { get; init; }

/// <summary>
/// Creates a new <see cref="ColumnModificationParameters" /> instance.
/// </summary>
Expand Down Expand Up @@ -137,8 +142,9 @@ public ColumnModificationParameters(

GenerateParameterName = null;
Entry = null;
JsonPath = null;
}

/// <summary>
/// Creates a new <see cref="ColumnModificationParameters" /> instance.
/// </summary>
Expand Down Expand Up @@ -182,6 +188,7 @@ public ColumnModificationParameters(

GenerateParameterName = null;
Entry = null;
JsonPath = null;
}

/// <summary>
Expand Down Expand Up @@ -225,5 +232,54 @@ public ColumnModificationParameters(

GenerateParameterName = generateParameterName;
Entry = entry;
JsonPath = null;
}

/// <summary>
/// Creates a new <see cref="ColumnModificationParameters" /> instance.
/// </summary>
/// <param name="columnName">The name of the column.</param>
/// <param name="originalValue">The original value of the property mapped to this column.</param>
/// <param name="value">The current value of the property mapped to this column.</param>
/// <param name="jsonPath">The JSON path leading to the JSON element that needs to be updated.</param>
/// <param name="columnType">The database type of the column.</param>
/// <param name="typeMapping">The relational type mapping to be used for the command parameter.</param>
/// <param name="read">Indicates whether a value must be read from the database for the column.</param>
/// <param name="write">Indicates whether a value must be written to the database for the column.</param>
/// <param name="key">Indicates whether the column part of a primary or alternate key.</param>
/// <param name="condition">Indicates whether the column is used in the <c>WHERE</c> clause when updating.</param>
/// <param name="sensitiveLoggingEnabled">Indicates whether potentially sensitive data (e.g. database values) can be logged.</param>
/// <param name="isNullable">A value indicating whether the value could be null.</param>
public ColumnModificationParameters(
string columnName,
object? originalValue,
object? value,
string? columnType,
RelationalTypeMapping? typeMapping,
string jsonPath,
bool read,
bool write,
bool key,
bool condition,
bool sensitiveLoggingEnabled,
bool? isNullable = null)
{
Column = null;
ColumnName = columnName;
OriginalValue = originalValue;
Value = value;
Property = null;
ColumnType = columnType;
TypeMapping = typeMapping;
JsonPath = jsonPath;
IsRead = read;
IsWrite = write;
IsKey = key;
IsCondition = condition;
SensitiveLoggingEnabled = sensitiveLoggingEnabled;
IsNullable = isNullable;

GenerateParameterName = null;
Entry = null;
}
}
5 changes: 5 additions & 0 deletions src/EFCore.Relational/Update/IColumnModification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public interface IColumnModification
/// </summary>
public object? Value { get; set; }

/// <summary>
/// In case of JSON column modification, the JSON path leading to the JSON element that needs to be updated.
/// </summary>
public string? JsonPath { get; }

/// <summary>
/// Adds a modification affecting the same database value.
/// </summary>
Expand Down
Loading

0 comments on commit 5d9fd67

Please sign in to comment.