Skip to content

Commit

Permalink
Add CancellationToken to Delete methods (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanieleSky authored Nov 4, 2021
1 parent f93a686 commit f13ce0f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Dommel/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Dapper;

Expand Down Expand Up @@ -33,12 +34,13 @@ public static bool Delete<TEntity>(this IDbConnection connection, TEntity entity
/// <param name="connection">The connection to the database. This can either be open or closed.</param>
/// <param name="entity">The entity to be deleted.</param>
/// <param name="transaction">Optional transaction for the command.</param>
/// <param name="cancellationToken">Optional cancellation token for the command.</param>
/// <returns>A value indicating whether the delete operation succeeded.</returns>
public static async Task<bool> DeleteAsync<TEntity>(this IDbConnection connection, TEntity entity, IDbTransaction? transaction = null)
public static async Task<bool> DeleteAsync<TEntity>(this IDbConnection connection, TEntity entity, IDbTransaction? transaction = null, CancellationToken cancellationToken = default)
{
var sql = BuildDeleteQuery(GetSqlBuilder(connection), typeof(TEntity));
LogQuery<TEntity>(sql);
return await connection.ExecuteAsync(sql, entity, transaction) > 0;
return await connection.ExecuteAsync(new CommandDefinition(sql, entity, transaction: transaction, cancellationToken: cancellationToken)) > 0;
}

internal static string BuildDeleteQuery(ISqlBuilder sqlBuilder, Type type)
Expand Down Expand Up @@ -81,13 +83,14 @@ public static int DeleteMultiple<TEntity>(this IDbConnection connection, Express
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="connection">The connection to the database. This can either be open or closed.</param>
/// <param name="predicate">A predicate to filter which entities are deleted.</param>
/// <param name="transaction">Optional transaction for the command.</param>
/// <param name="transaction">Optional transaction for the command.</param>
/// <param name="cancellationToken">Optional cancellation token for the command.</param>
/// <returns>The number of rows affected.</returns>
public static async Task<int> DeleteMultipleAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction? transaction = null)
public static async Task<int> DeleteMultipleAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction? transaction = null, CancellationToken cancellationToken = default)
{
var sql = BuildDeleteMultipleQuery(GetSqlBuilder(connection), predicate, out var parameters);
LogQuery<TEntity>(sql);
return await connection.ExecuteAsync(sql, parameters, transaction);
return await connection.ExecuteAsync(new CommandDefinition(sql, parameters, transaction: transaction, cancellationToken: cancellationToken));
}

private static string BuildDeleteMultipleQuery<TEntity>(ISqlBuilder sqlBuilder, Expression<Func<TEntity, bool>> predicate, out DynamicParameters parameters)
Expand Down Expand Up @@ -125,12 +128,13 @@ public static int DeleteAll<TEntity>(this IDbConnection connection, IDbTransacti
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="connection">The connection to the database. This can either be open or closed.</param>
/// <param name="transaction">Optional transaction for the command.</param>
/// <param name="cancellationToken">Optional cancellation token for the command.</param>
/// <returns>The number of rows affected.</returns>
public static async Task<int> DeleteAllAsync<TEntity>(this IDbConnection connection, IDbTransaction? transaction = null)
public static async Task<int> DeleteAllAsync<TEntity>(this IDbConnection connection, IDbTransaction? transaction = null, CancellationToken cancellationToken = default)
{
var sql = BuildDeleteAllQuery(GetSqlBuilder(connection), typeof(TEntity));
LogQuery<TEntity>(sql);
return await connection.ExecuteAsync(sql, transaction: transaction);
return await connection.ExecuteAsync(new CommandDefinition(sql, transaction: transaction, cancellationToken: cancellationToken));
}

internal static string BuildDeleteAllQuery(ISqlBuilder sqlBuilder, Type type)
Expand Down

0 comments on commit f13ce0f

Please sign in to comment.