Skip to content

Commit 8b3fe52

Browse files
committed
Allow RecordsAffected to be read after Close. Fixes #1122
1 parent 65e06ec commit 8b3fe52

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

src/MySqlConnector/Core/ResultSet.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task ReadResultSetHeaderAsync(IOBehavior ioBehavior)
4949
// if we've read a result set header then this is a SELECT statement, so we shouldn't overwrite RecordsAffected
5050
// (which should be -1 for SELECT) unless the server reports a non-zero count
5151
if (State != ResultSetState.ReadResultSetHeader || ok.AffectedRowCount != 0)
52-
RecordsAffected = (RecordsAffected ?? 0) + ok.AffectedRowCount;
52+
DataReader.RealRecordsAffected = (DataReader.RealRecordsAffected ?? 0) + ok.AffectedRowCount;
5353

5454
if (ok.LastInsertId != 0)
5555
Command?.SetLastInsertedId((long) ok.LastInsertId);
@@ -365,7 +365,6 @@ public Row GetCurrentRow()
365365
public ResultSetState BufferState { get; private set; }
366366
public ColumnDefinitionPayload[]? ColumnDefinitions { get; private set; }
367367
public MySqlDbType[]? ColumnTypes { get; private set; }
368-
public ulong? RecordsAffected { get; private set; }
369368
public int WarningCount { get; private set; }
370369
public ResultSetState State { get; private set; }
371370
public bool ContainsCommandParameters { get; private set; }

src/MySqlConnector/MySqlDataReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public override bool HasRows
202202
}
203203

204204
public override bool IsClosed => Command is null;
205-
public override int RecordsAffected => GetResultSet().RecordsAffected is ulong recordsAffected ? checked((int) recordsAffected) : -1;
205+
public override int RecordsAffected => RealRecordsAffected is ulong recordsAffected ? checked((int) recordsAffected) : -1;
206206

207207
public override int GetOrdinal(string name) => GetResultSet().GetOrdinal(name);
208208

@@ -442,6 +442,7 @@ protected override void Dispose(bool disposing)
442442
internal Activity? Activity { get; }
443443
internal IMySqlCommand? Command { get; private set; }
444444
internal MySqlConnection? Connection => Command?.Connection;
445+
internal ulong? RealRecordsAffected { get; set; }
445446
internal ServerSession? Session => Command?.Connection!.Session;
446447

447448
internal static async Task<MySqlDataReader> CreateAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary<string, CachedProcedure?>? cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity? activity, IOBehavior ioBehavior, CancellationToken cancellationToken)

tests/SideBySide/UpdateTests.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ public void Dispose()
1414
}
1515

1616
[Theory]
17-
[InlineData(1, 2)]
18-
[InlineData(2, 1)]
19-
[InlineData(3, 0)]
20-
[InlineData(4, 1)]
21-
public async Task UpdateRowsExecuteReader(int oldValue, int expectedRowsUpdated)
17+
[MemberData(nameof(GetUpdateRowsExecuteReaderData))]
18+
public async Task UpdateRowsExecuteReader(int oldValue, int expectedRowsUpdated, int closeDispose)
2219
{
2320
using (var cmd = m_database.Connection.CreateCommand())
2421
{
@@ -43,11 +40,22 @@ public async Task UpdateRowsExecuteReader(int oldValue, int expectedRowsUpdated)
4340

4441
using var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false);
4542
Assert.False(await reader.ReadAsync().ConfigureAwait(false));
43+
44+
if (closeDispose == 1)
45+
reader.Close();
46+
else if (closeDispose == 2)
47+
reader.Dispose();
4648
Assert.Equal(expectedRowsUpdated, reader.RecordsAffected);
47-
Assert.False(await reader.NextResultAsync().ConfigureAwait(false));
49+
50+
if (closeDispose == 0)
51+
Assert.False(await reader.NextResultAsync().ConfigureAwait(false));
4852
}
4953
}
5054

55+
public static IEnumerable<object[]> GetUpdateRowsExecuteReaderData() =>
56+
new[] { new KeyValuePair<int, int>(1, 2), new KeyValuePair<int, int>(2, 1), new KeyValuePair<int, int>(3, 0), new KeyValuePair<int, int>(4, 1) }
57+
.SelectMany(x => new[] { 0, 1, 2 }.Select(y => new object[] { x.Key, x.Value, y }));
58+
5159
[Theory]
5260
[InlineData(1, 2)]
5361
[InlineData(2, 1)]

0 commit comments

Comments
 (0)