Skip to content

Commit

Permalink
[Azure.Data.Tables] DeleteEntity overload (#44986)
Browse files Browse the repository at this point in the history
* overload method

* Update API

* Update samples

* Update changelog

* Fix error in parameter description
  • Loading branch information
JonathanCrd authored Jul 15, 2024
1 parent b10e21d commit c425976
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 7 deletions.
11 changes: 11 additions & 0 deletions sdk/tables/Azure.Data.Tables/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Release History

## 12.8.4

### Features Added
- Overload the `DeleteEntity` method to allow an `ITableEntity` object as parameter.

### Breaking Changes

### Bugs Fixed

### Other changes

## 12.9.0-beta.1 (Unreleased)

### Features Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public TableClient(System.Uri endpoint, string tableName, Azure.Data.Tables.Tabl
public static string CreateQueryFilter<T>(System.Linq.Expressions.Expression<System.Func<T, bool>> filter) { throw null; }
public virtual Azure.Response Delete(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> DeleteAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response DeleteEntity(Azure.Data.Tables.ITableEntity entity, Azure.ETag ifMatch = default(Azure.ETag), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response DeleteEntity(string partitionKey, string rowKey, Azure.ETag ifMatch = default(Azure.ETag), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> DeleteEntityAsync(Azure.Data.Tables.ITableEntity entity, Azure.ETag ifMatch = default(Azure.ETag), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> DeleteEntityAsync(string partitionKey, string rowKey, Azure.ETag ifMatch = default(Azure.ETag), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Uri GenerateSasUri(Azure.Data.Tables.Sas.TableSasBuilder builder) { throw null; }
public virtual System.Uri GenerateSasUri(Azure.Data.Tables.Sas.TableSasPermissions permissions, System.DateTimeOffset expiresOn) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ To delete an entity, invoke `DeleteEntity` and pass in its partition and row key
// Delete the entity given the partition and row key.
tableClient.DeleteEntity(partitionKey, rowKey);
```

Alternatively, you can pass a `TableEntity`object.

```C# Snippet:TablesSample2DeleteEntityUsingObject
// Delete an entity given a TableEntity object
tableClient.DeleteEntity(tableEntity);
```
46 changes: 46 additions & 0 deletions sdk/tables/Azure.Data.Tables/src/TableClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,31 @@ public virtual async Task<Response> DeleteEntityAsync(
CancellationToken cancellationToken = default)
=> await DeleteEntityInternal(true, partitionKey, rowKey, ifMatch, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Deletes the specified table entity.
/// </summary>
/// <remarks>Note: This method should not fail because the entity does not exist, however if delete operations are submitted in a <see cref="TableTransactionAction"/>, the transaction will fail if the entity does not exist.</remarks>
/// <param name="entity">The table entity to delete.</param>
/// <param name="ifMatch">
/// The If-Match value to be used for optimistic concurrency.
/// If <see cref="ETag.All"/> is specified, the operation will be executed unconditionally.
/// If the <see cref="ITableEntity.ETag"/> value is specified, the operation will fail with a status of 412 (Precondition Failed)
/// if the <see cref="ETag"/> value of the entity in the table does not match.
/// The default is to delete unconditionally.
/// </param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
/// <returns>The <see cref="Response"/> indicating the result of the operation.</returns>

public virtual async Task<Response> DeleteEntityAsync(
ITableEntity entity,
ETag ifMatch = default,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(entity, nameof(entity));
return await DeleteEntityInternal(true, entity.PartitionKey, entity.RowKey, ifMatch, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Deletes the specified table entity.
/// </summary>
Expand All @@ -1316,6 +1341,27 @@ public virtual async Task<Response> DeleteEntityAsync(
public virtual Response DeleteEntity(string partitionKey, string rowKey, ETag ifMatch = default, CancellationToken cancellationToken = default)
=> DeleteEntityInternal(false, partitionKey, rowKey, ifMatch, cancellationToken).EnsureCompleted();

/// <summary>
/// Deletes the specified table entity.
/// </summary>
/// <remarks>Note: This method should not fail because the entity does not exist, however if delete operations are submitted in a <see cref="TableTransactionAction"/>, the transaction will fail if the entity does not exist.</remarks>
/// <param name="entity">The table entity to delete.</param>
/// <param name="ifMatch">
/// The If-Match value to be used for optimistic concurrency.
/// If <see cref="ETag.All"/> is specified, the operation will be executed unconditionally.
/// If the <see cref="ITableEntity.ETag"/> value is specified, the operation will fail with a status of 412 (Precondition Failed)
/// if the <see cref="ETag"/> value of the entity in the table does not match.
/// The default is to delete unconditionally.
/// </param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
/// <returns>The <see cref="Response"/> indicating the result of the operation.</returns>
public virtual Response DeleteEntity(ITableEntity entity, ETag ifMatch = default, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(entity, nameof(entity));
return DeleteEntityInternal(false, entity.PartitionKey, entity.RowKey, ifMatch, cancellationToken).EnsureCompleted();
}

internal async Task<Response> DeleteEntityInternal(
bool async,
string partitionKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading;

namespace Azure.Data.Tables
{
/// <summary>
Expand All @@ -21,7 +23,7 @@ public enum TableTransactionActionType
/// </summary>
UpdateReplace,
/// <summary>
/// Delete the entity. This is equivalent to <see cref="TableClient.DeleteEntity"/>
/// Delete the entity. This is equivalent to <see cref="TableClient.DeleteEntity(string, string, ETag, CancellationToken)"/>
/// </summary>
Delete,
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using Azure.Core.TestFramework;
using NUnit.Framework;
using Azure.Data.Tables.Tests;
using System.Xml;

namespace Azure.Data.Tables.Samples
{
public partial class TablesSamples : TablesTestEnvironment
{
[Test]
public void CreateDeleteEntity()
[TestCase(true)]
[TestCase(false)]
public void CreateDeleteEntity(bool useEntity)
{
string storageUri = StorageUri;
string accountName = StorageAccountName;
Expand Down Expand Up @@ -95,10 +97,20 @@ public void CreateDeleteEntity()
Console.WriteLine($"{marker.PartitionKey}, {marker.RowKey}, {marker.Product}, {marker.Price}, {marker.Quantity}");
#endregion

#region Snippet:TablesSample2DeleteEntity
// Delete the entity given the partition and row key.
tableClient.DeleteEntity(partitionKey, rowKey);
#endregion
if (useEntity)
{
#region Snippet:TablesSample2DeleteEntity
// Delete the entity given the partition and row key.
tableClient.DeleteEntity(partitionKey, rowKey);
#endregion
}
else
{
#region Snippet:TablesSample2DeleteEntityUsingObject
// Delete an entity given a TableEntity object
tableClient.DeleteEntity(tableEntity);
#endregion
}

tableClient.Delete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public async Task CreateDeleteEntitiesAsync()

// Delete the entity given the partition and row key.
await client.DeleteEntityAsync(partitionKey, rowKey);

// Delete the strong entity given the entity object.
await client.DeleteEntityAsync(strongEntity);

await client.DeleteAsync();
}
Expand Down

0 comments on commit c425976

Please sign in to comment.