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

Add a way for providers to filter out non-identity conditions. #25973

Merged
merged 1 commit into from
Sep 13, 2021
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
24 changes: 19 additions & 5 deletions src/EFCore.Relational/Update/UpdateSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -499,19 +500,32 @@ protected virtual void AppendWhereAffectedClause(
{
if (v.IsKey)
{
if (v.IsRead)
{
AppendIdentityWhereCondition(sb, v);
}
else
if (!v.IsRead)
{
AppendWhereCondition(sb, v, v.UseOriginalValueParameter);
return true;
}
}

if (IsIdentityOperation(v))
{
AppendIdentityWhereCondition(sb, v);
return true;
}

return false;
}, " AND ");
}
}

/// <summary>
/// Returns a value indicating whether the given modification represents an auto-incrementing column.
/// </summary>
/// <param name="modification"> The column modification. </param>
/// <returns> <see langword="true" /> if the given modification represents an auto-incrementing column. </returns>
protected virtual bool IsIdentityOperation(IColumnModification modification)
=> modification.IsKey && modification.IsRead;

/// <summary>
/// Appends a <c>WHERE</c> condition checking rows affected.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ private void AppendMergeCommandHeader(
.Append('(')
.AppendJoin(
writeOperations,
toInsertTableAlias,
SqlGenerationHelper,
(sb, o, alias, helper) =>
(toInsertTableAlias, SqlGenerationHelper),
static (sb, o, state) =>
{
var (alias, helper) = state;
sb.Append(alias).Append('.');
helper.DelimitIdentifier(sb, o.ColumnName);
})
Expand Down
22 changes: 11 additions & 11 deletions src/Shared/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ public static StringBuilder AppendJoin<T>(
return stringBuilder;
}

public static StringBuilder AppendJoin<T, TParam>(
public static StringBuilder AppendJoin<T>(
this StringBuilder stringBuilder,
IEnumerable<T> values,
TParam param,
Action<StringBuilder, T, TParam> joinAction,
Func<StringBuilder, T, bool> joinFunc,
string separator = ", ")
{
var appended = false;

foreach (var value in values)
{
joinAction(stringBuilder, value, param);
stringBuilder.Append(separator);
appended = true;
if (joinFunc(stringBuilder, value))
{
stringBuilder.Append(separator);
appended = true;
}
}

if (appended)
Expand All @@ -69,19 +70,18 @@ public static StringBuilder AppendJoin<T, TParam>(
return stringBuilder;
}

public static StringBuilder AppendJoin<T, TParam1, TParam2>(
public static StringBuilder AppendJoin<T, TParam>(
this StringBuilder stringBuilder,
IEnumerable<T> values,
TParam1 param1,
TParam2 param2,
Action<StringBuilder, T, TParam1, TParam2> joinAction,
TParam param,
Action<StringBuilder, T, TParam> joinAction,
string separator = ", ")
{
var appended = false;

foreach (var value in values)
{
joinAction(stringBuilder, value, param1, param2);
joinAction(stringBuilder, value, param);
stringBuilder.Append(separator);
appended = true;
}
Expand Down