forked from olegil/SqlBulkTools
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DeleteAllRecords method for QueryOperations.
- Loading branch information
Greg Taylor
committed
Mar 20, 2017
1 parent
9638664
commit ffc52a8
Showing
5 changed files
with
120 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
SqlBulkTools/QueryOperations/Delete/DeleteAllRecordsQueryReady.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Threading.Tasks; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace SqlBulkTools | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class DeleteAllRecordsQueryReady<T> : ITransaction | ||
{ | ||
private readonly string _tableName; | ||
private readonly string _schema; | ||
private int? _batchQuantity; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="tableName"></param> | ||
/// <param name="schema"></param> | ||
public DeleteAllRecordsQueryReady(string tableName, string schema) | ||
{ | ||
_tableName = tableName; | ||
_schema = schema; | ||
_batchQuantity = null; | ||
} | ||
|
||
/// <summary> | ||
/// The maximum number of records to delete per transaction. | ||
/// </summary> | ||
/// <param name="batchQuantity"></param> | ||
/// <returns></returns> | ||
public DeleteAllRecordsQueryReady<T> SetBatchQuantity(int batchQuantity) | ||
{ | ||
_batchQuantity = batchQuantity; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Commits a transaction to database. A valid setup must exist for the operation to be | ||
/// successful. | ||
/// </summary> | ||
/// <param name="connection"></param> | ||
/// <returns></returns> | ||
public int Commit(SqlConnection connection) | ||
{ | ||
if (connection.State == ConnectionState.Closed) | ||
connection.Open(); | ||
|
||
SqlCommand command = connection.CreateCommand(); | ||
command.Connection = connection; | ||
|
||
command.CommandText = GetQuery(connection); | ||
|
||
int affectedRows = command.ExecuteNonQuery(); | ||
|
||
return affectedRows; | ||
} | ||
|
||
/// <summary> | ||
/// Commits a transaction to database asynchronously. A valid setup must exist for the operation to be | ||
/// successful. | ||
/// </summary> | ||
/// <param name="connection"></param> | ||
/// <returns></returns> | ||
public async Task<int> CommitAsync(SqlConnection connection) | ||
{ | ||
|
||
if (connection.State == ConnectionState.Closed) | ||
await connection.OpenAsync(); | ||
|
||
SqlCommand command = connection.CreateCommand(); | ||
command.Connection = connection; | ||
|
||
command.CommandText = command.CommandText = GetQuery(connection); | ||
|
||
int affectedRows = await command.ExecuteNonQueryAsync(); | ||
|
||
return affectedRows; | ||
} | ||
|
||
private string GetQuery(SqlConnection connection) | ||
{ | ||
string fullQualifiedTableName = BulkOperationsHelper.GetFullQualifyingTableName(connection.Database, _schema, | ||
_tableName); | ||
|
||
string batchQtyStart = _batchQuantity != null ? "DeleteMore:\n" : string.Empty; | ||
string batchQty = _batchQuantity != null ? $"TOP ({_batchQuantity}) " : string.Empty; | ||
string batchQtyRepeat = _batchQuantity != null ? $"\nIF @@ROWCOUNT != 0\ngoto DeleteMore" : string.Empty; | ||
|
||
string comm = $"{batchQtyStart}DELETE {batchQty}FROM {fullQualifiedTableName} {batchQtyRepeat}"; | ||
|
||
return comm; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters