Skip to content

Commit 8fb52b1

Browse files
committed
Add MySqlBulkCopy.RowsCopied property. Fixes #809
1 parent f41ba13 commit 8fb52b1

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlBulkCopy.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public MySqlBulkCopy(MySqlConnection connection, MySqlTransaction? transaction =
5555
/// </summary>
5656
public List<MySqlBulkCopyColumnMapping> ColumnMappings { get; }
5757

58+
/// <summary>
59+
/// Returns the number of rows that were copied (after <code>WriteToServer(Async)</code> finishes).
60+
/// </summary>
61+
public int RowsCopied { get; private set; }
62+
5863
#if !NETSTANDARD1_3
5964
public void WriteToServer(DataTable dataTable)
6065
{
@@ -218,7 +223,7 @@ internal async Task SendDataReaderAsync(IOBehavior ioBehavior, CancellationToken
218223
var outputIndex = 0;
219224

220225
// allocate a reusable MySqlRowsCopiedEventArgs if event notification is necessary
221-
var rowsCopied = 0;
226+
RowsCopied = 0;
222227
MySqlRowsCopiedEventArgs? eventArgs = null;
223228
if (NotifyAfter > 0 && MySqlRowsCopied is object)
224229
eventArgs = new MySqlRowsCopiedEventArgs();
@@ -267,10 +272,10 @@ await m_valuesEnumerator.MoveNextAsync().ConfigureAwait(false) :
267272
{
268273
buffer[outputIndex++] = (byte) '\n';
269274

270-
rowsCopied++;
271-
if (eventArgs is object && rowsCopied % NotifyAfter == 0)
275+
RowsCopied++;
276+
if (eventArgs is object && RowsCopied % NotifyAfter == 0)
272277
{
273-
eventArgs.RowsCopied = rowsCopied;
278+
eventArgs.RowsCopied = RowsCopied;
274279
MySqlRowsCopied!(this, eventArgs);
275280
if (eventArgs.Abort)
276281
break;

tests/SideBySide/BulkLoaderAsync.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ public async Task BulkCopyNotifyAfter(int notifyAfter, int rowCount, int expecte
529529
{
530530
eventCount++;
531531
rowsCopied = e.RowsCopied;
532+
Assert.Equal(bulkCopy.RowsCopied, e.RowsCopied);
532533
};
533534

534535
var dataTable = new DataTable()
@@ -541,6 +542,7 @@ public async Task BulkCopyNotifyAfter(int notifyAfter, int rowCount, int expecte
541542
await bulkCopy.WriteToServerAsync(dataTable);
542543
Assert.Equal(expectedEventCount, eventCount);
543544
Assert.Equal(expectedRowsCopied, rowsCopied);
545+
Assert.Equal(rowCount, bulkCopy.RowsCopied);
544546
}
545547

546548
[Theory]

tests/SideBySide/BulkLoaderSync.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ public void BulkCopyNotifyAfter(int notifyAfter, int rowCount, int expectedEvent
725725
{
726726
eventCount++;
727727
rowsCopied = e.RowsCopied;
728+
Assert.Equal(bulkCopy.RowsCopied, e.RowsCopied);
728729
};
729730

730731
var dataTable = new DataTable()
@@ -737,6 +738,7 @@ public void BulkCopyNotifyAfter(int notifyAfter, int rowCount, int expectedEvent
737738
bulkCopy.WriteToServer(dataTable);
738739
Assert.Equal(expectedEventCount, eventCount);
739740
Assert.Equal(expectedRowsCopied, rowsCopied);
741+
Assert.Equal(rowCount, bulkCopy.RowsCopied);
740742
}
741743

742744
[Theory]

0 commit comments

Comments
 (0)