Skip to content

Commit

Permalink
Added DeleteAllRecords method for QueryOperations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Taylor committed Mar 20, 2017
1 parent 9638664 commit ffc52a8
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 9 deletions.
17 changes: 9 additions & 8 deletions SqlBulkTools.IntegrationTests/BulkOperationsIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace SqlBulkTools.IntegrationTests
{
[TestClass]
public class BulkOperationsIT
public class BulkOperationsIt
{
private const int RepeatTimes = 1;
private DataAccess _dataAccess;
Expand Down Expand Up @@ -509,7 +509,7 @@ public void SqlBulkTools_BulkInsertWithColumnMappings_CorrectlyMapsColumns()
.ForDeleteQuery()
.WithTable("CustomColumnMappingTests")
.Delete()
.Where(x => x.NaturalId != -1)
.AllRecords()
.Commit(conn);

bulk.Setup<CustomColumnMappingTest>()
Expand Down Expand Up @@ -550,7 +550,8 @@ public void SqlBulkTools_BulkInsertOrUpdateWithColumnMappings_CorrectlyMapsColum
.ForDeleteQuery()
.WithTable("CustomColumnMappingTests")
.Delete()
.Where(x => x.NaturalId != -1)
.AllRecords()
.SetBatchQuantity(5)
.Commit(conn);

bulk.Setup<CustomColumnMappingTest>()
Expand Down Expand Up @@ -594,7 +595,7 @@ public void SqlBulkTools_BulkInsertOrUpdateWithManualColumnMappings_CorrectlyMap
.ForDeleteQuery()
.WithTable("CustomColumnMappingTests")
.Delete()
.Where(x => x.NaturalId != -1)
.AllRecords()
.Commit(conn);

bulk.Setup<CustomColumnMappingTest>()
Expand Down Expand Up @@ -636,7 +637,7 @@ public void SqlBulkTools_BulkUpdateWithManualColumnMappings_CorrectlyMapsColumns
.ForDeleteQuery()
.WithTable("CustomColumnMappingTests")
.Delete()
.Where(x => x.NaturalId != -1)
.AllRecords()
.Commit(conn);

bulk.Setup<CustomColumnMappingTest>()
Expand Down Expand Up @@ -691,7 +692,7 @@ public void SqlBulkTools_BulkUpdateWithColumnMappings_CorrectlyMapsColumns()
.ForDeleteQuery()
.WithTable("CustomColumnMappingTests")
.Delete()
.Where(x => x.NaturalId != -1)
.AllRecords()
.Commit(conn);

bulk.Setup<CustomColumnMappingTest>()
Expand Down Expand Up @@ -749,7 +750,7 @@ public void SqlBulkTools_WhenUsingReservedSqlKeywords()
.ForDeleteQuery()
.WithTable("ReservedColumnNameTests")
.Delete()
.Where(x => x.Id != -1)
.AllRecords()
.Commit(conn);

bulk.Setup<ReservedColumnNameTest>()
Expand Down Expand Up @@ -1824,7 +1825,7 @@ public void SqlBulkTools_BulkInsertOrUpdate_TestDataTypes()
.ForDeleteQuery()
.WithTable("TestDataTypes")
.Delete()
.Where(x => x.GuidTest != Guid.NewGuid())
.AllRecords()
.Commit(conn);

bulk.Setup<TestDataType>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
</Choose>
<ItemGroup>
<Compile Include="BulkOperationsAsyncIT.cs" />
<Compile Include="BulkOperationsIT.cs" />
<Compile Include="BulkOperationsIt.cs" />
<Compile Include="Helper\BookRandomizer.cs" />
<Compile Include="Helper\DataAccess.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
100 changes: 100 additions & 0 deletions SqlBulkTools/QueryOperations/Delete/DeleteAllRecordsQueryReady.cs
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;
}
}
}
9 changes: 9 additions & 0 deletions SqlBulkTools/QueryOperations/Delete/DeleteQueryCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,14 @@ public DeleteQueryReady<T> Where(Expression<Func<T, bool>> expression, string co
_whereConditions, _parameters, _collationColumnDic);
}

/// <summary>
/// Please understand the consequences before using this method. This will delete all records in the table.
/// </summary>
/// <returns></returns>
public DeleteAllRecordsQueryReady<T> AllRecords()
{
return new DeleteAllRecordsQueryReady<T>(_tableName, _schema);
}

}
}
1 change: 1 addition & 0 deletions SqlBulkTools/SqlBulkTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="BulkOperations\BulkCopyOptions.cs" />
<Compile Include="Core\BulkOperationsUtility.cs" />
<Compile Include="Interface\IBulkOperations.cs" />
<Compile Include="QueryOperations\Delete\DeleteAllRecordsQueryReady.cs" />
<Compile Include="QueryOperations\Delete\DeleteQueryCondition.cs" />
<Compile Include="QueryOperations\Delete\DeleteQueryReady.cs" />
<Compile Include="QueryOperations\Delete\DeleteQueryTable.cs" />
Expand Down

0 comments on commit ffc52a8

Please sign in to comment.