Skip to content

Commit

Permalink
#343 CancellationToken Support for SQL Server BulkDelete
Browse files Browse the repository at this point in the history
  • Loading branch information
mikependon committed Oct 2, 2020
1 parent eda8e5b commit 821e017
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

namespace RepoDb
Expand Down Expand Up @@ -126,20 +127,23 @@ public static int BulkDelete<TEntity>(this BaseRepository<TEntity, SqlConnection
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static Task<int> BulkDeleteAsync<TEntity>(this BaseRepository<TEntity, SqlConnection> repository,
IEnumerable<object> primaryKeys,
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
where TEntity : class
{
return repository.DbRepository.BulkDeleteAsync<TEntity>(primaryKeys: primaryKeys,
hints: hints,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}

/// <summary>
Expand All @@ -155,6 +159,7 @@ public static Task<int> BulkDeleteAsync<TEntity>(this BaseRepository<TEntity, Sq
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static Task<int> BulkDeleteAsync<TEntity>(this BaseRepository<TEntity, SqlConnection> repository,
IEnumerable<TEntity> entities,
Expand All @@ -164,7 +169,8 @@ public static Task<int> BulkDeleteAsync<TEntity>(this BaseRepository<TEntity, Sq
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
where TEntity : class
{
return repository.DbRepository.BulkDeleteAsync<TEntity>(entities: entities,
Expand All @@ -174,7 +180,8 @@ public static Task<int> BulkDeleteAsync<TEntity>(this BaseRepository<TEntity, Sq
hints: hints,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}

/// <summary>
Expand All @@ -191,6 +198,7 @@ public static Task<int> BulkDeleteAsync<TEntity>(this BaseRepository<TEntity, Sq
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static Task<int> BulkDeleteAsync<TEntity>(this BaseRepository<TEntity, SqlConnection> repository,
string tableName,
Expand All @@ -201,7 +209,8 @@ public static Task<int> BulkDeleteAsync<TEntity>(this BaseRepository<TEntity, Sq
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
where TEntity : class
{
return repository.DbRepository.BulkDeleteAsync<TEntity>(tableName: tableName,
Expand All @@ -212,7 +221,8 @@ public static Task<int> BulkDeleteAsync<TEntity>(this BaseRepository<TEntity, Sq
hints: hints,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Data;
using System.Data.Common;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

namespace RepoDb
Expand Down Expand Up @@ -500,13 +501,15 @@ public static int BulkDelete(this DbRepository<SqlConnection> repository,
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConnection> repository,
IEnumerable<object> primaryKeys,
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
where TEntity : class
{
// Create a connection
Expand All @@ -520,7 +523,8 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
bulkCopyTimeout: repository.CommandTimeout,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}
catch
{
Expand Down Expand Up @@ -553,6 +557,7 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConnection> repository,
IEnumerable<TEntity> entities,
Expand All @@ -562,7 +567,8 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
where TEntity : class
{
// Create a connection
Expand All @@ -579,7 +585,8 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
bulkCopyTimeout: repository.CommandTimeout,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}
catch
{
Expand Down Expand Up @@ -613,6 +620,7 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConnection> repository,
string tableName,
Expand All @@ -623,7 +631,8 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
where TEntity : class
{
// Create a connection
Expand All @@ -641,7 +650,8 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
bulkCopyTimeout: repository.CommandTimeout,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}
catch
{
Expand Down Expand Up @@ -674,6 +684,7 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConnection> repository,
DbDataReader reader,
Expand All @@ -683,7 +694,8 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
where TEntity : class
{
// Create a connection
Expand All @@ -700,7 +712,8 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
bulkCopyTimeout: repository.CommandTimeout,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}
catch
{
Expand Down Expand Up @@ -734,6 +747,7 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConnection> repository,
DataTable dataTable,
Expand All @@ -744,7 +758,8 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
where TEntity : class
{
// Create a connection
Expand All @@ -762,7 +777,8 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
bulkCopyTimeout: repository.CommandTimeout,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}
catch
{
Expand Down Expand Up @@ -796,14 +812,16 @@ public static async Task<int> BulkDeleteAsync<TEntity>(this DbRepository<SqlConn
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> repository,
string tableName,
IEnumerable<object> primaryKeys,
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
{
// Create a connection
var connection = (transaction?.Connection ?? repository.CreateConnection());
Expand All @@ -817,7 +835,8 @@ public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> r
bulkCopyTimeout: repository.CommandTimeout,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}
catch
{
Expand Down Expand Up @@ -850,6 +869,7 @@ public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> r
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> repository,
string tableName,
Expand All @@ -860,7 +880,8 @@ public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> r
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
{
// Create a connection
var connection = (transaction?.Connection ?? repository.CreateConnection());
Expand All @@ -877,7 +898,8 @@ public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> r
bulkCopyTimeout: repository.CommandTimeout,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}
catch
{
Expand Down Expand Up @@ -911,6 +933,7 @@ public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> r
/// <param name="batchSize">The size per batch to be used.</param>
/// <param name="usePhysicalPseudoTempTable">The flags that signify whether to create a physical pseudo table.</param>
/// <param name="transaction">The transaction to be used.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
/// <returns>The number of rows affected by the execution.</returns>
public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> repository,
string tableName,
Expand All @@ -922,7 +945,8 @@ public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> r
string hints = null,
int? batchSize = null,
bool? usePhysicalPseudoTempTable = null,
SqlTransaction transaction = null)
SqlTransaction transaction = null,
CancellationToken cancellationToken = default)
{
// Create a connection
var connection = (transaction?.Connection ?? repository.CreateConnection());
Expand All @@ -940,7 +964,8 @@ public static async Task<int> BulkDeleteAsync(this DbRepository<SqlConnection> r
bulkCopyTimeout: repository.CommandTimeout,
batchSize: batchSize,
usePhysicalPseudoTempTable: usePhysicalPseudoTempTable,
transaction: transaction);
transaction: transaction,
cancellationToken: cancellationToken);
}
catch
{
Expand Down
Loading

0 comments on commit 821e017

Please sign in to comment.