Skip to content

Commit

Permalink
Merge always updates for batch operations (#18446)
Browse files Browse the repository at this point in the history
  • Loading branch information
christothes authored Feb 5, 2021
1 parent d6e3311 commit 769e473
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 194 deletions.
24 changes: 10 additions & 14 deletions sdk/tables/Azure.Data.Tables/src/TableTransactionalBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,7 @@ internal virtual void SetBatchGuids(Guid batchGuid, Guid changesetGuid)
/// <param name="mode">Determines the behavior of the Update operation.</param>
public virtual void UpdateEntity<T>(T entity, ETag ifMatch, TableUpdateMode mode = TableUpdateMode.Merge) where T : class, ITableEntity, new()
{
var message = _batchOperations.CreateUpdateEntityRequest(
_table,
entity.PartitionKey,
entity.RowKey,
null,
ifMatch: ifMatch.ToString(),
tableEntityProperties: entity.ToOdataAnnotatedDictionary(),
queryOptions: new QueryOptions() { Format = _format });

HttpMessage message = CreateUpdateOrMergeRequest<T>(entity, mode, ifMatch);
AddMessage(entity, message, RequestType.Update);
}

Expand All @@ -141,28 +133,32 @@ internal virtual void SetBatchGuids(Guid batchGuid, Guid changesetGuid)
/// <param name="mode">Determines the behavior of the update operation when the entity already exists in the table. See <see cref="TableUpdateMode"/> for more details.</param>
public virtual void UpsertEntity<T>(T entity, TableUpdateMode mode = TableUpdateMode.Merge) where T : class, ITableEntity, new()
{
var message = mode switch
HttpMessage message = CreateUpdateOrMergeRequest<T>(entity, mode, default);
AddMessage(entity, message, RequestType.Upsert);
}

private HttpMessage CreateUpdateOrMergeRequest<T>(T entity, TableUpdateMode mode, ETag ifMatch = default) where T : class, ITableEntity, new()
{
return mode switch
{
TableUpdateMode.Replace => _batchOperations.CreateUpdateEntityRequest(
_table,
entity.PartitionKey,
entity.RowKey,
null,
ifMatch: null,
ifMatch: ifMatch == default ? null : ifMatch.ToString(),
tableEntityProperties: entity.ToOdataAnnotatedDictionary(),
queryOptions: new QueryOptions() { Format = _format }),
TableUpdateMode.Merge => _batchOperations.CreateMergeEntityRequest(
_table,
entity.PartitionKey,
entity.RowKey,
null,
ifMatch: null,
ifMatch: ifMatch == default ? null : ifMatch.ToString(),
entity.ToOdataAnnotatedDictionary(),
new QueryOptions() { Format = _format }),
_ => throw new ArgumentException($"Unexpected value for {nameof(mode)}: {mode}")
};

AddMessage(entity, message, RequestType.Upsert);
}

/// <summary>
Expand Down
Loading

0 comments on commit 769e473

Please sign in to comment.