Skip to content

Commit 8934d05

Browse files
committed
Report records affected according to ADO.NET
1 parent b12039a commit 8934d05

3 files changed

Lines changed: 38 additions & 17 deletions

File tree

‎src/FirebirdSql.Data.FirebirdClient.Tests/FbDataReaderTests.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,18 @@ public async Task GetFieldValueTest()
535535
await transaction.RollbackAsync();
536536
}
537537
}
538+
539+
[Test]
540+
public async Task RecordsAffectedIsMinusOneForSelectTest()
541+
{
542+
// DbDataReader.RecordsAffected: "-1 for SELECT statements; 0 if no rows were affected or the statement failed."
543+
await using (var command = new FbCommand("select INT_FIELD from TEST where 1 = 0", Connection))
544+
{
545+
var reader = await command.ExecuteReaderAsync();
546+
while (await reader.ReadAsync())
547+
{ }
548+
await reader.CloseAsync();
549+
Assert.AreEqual(-1, reader.RecordsAffected);
550+
}
551+
}
538552
}

‎src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbCommand.cs‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ internal int RecordsAffected
263263
}
264264
}
265265

266+
/// <summary>
267+
/// The rows an INSERT, UPDATE or DELETE affected, or -1 for any other statement (DbCommand.ExecuteNonQuery: "For all
268+
/// other types of statements, the return value is -1"; DbDataReader.RecordsAffected: "-1 for SELECT statements").
269+
/// </summary>
270+
internal int StatementRecordsAffected
271+
{
272+
get
273+
{
274+
return _statement?.StatementType switch
275+
{
276+
DbStatementType.Insert => RecordsAffected,
277+
DbStatementType.Update => RecordsAffected,
278+
DbStatementType.Delete => RecordsAffected,
279+
_ => -1,
280+
};
281+
}
282+
}
283+
266284
internal bool IsDisposed
267285
{
268286
get { return _disposed; }
@@ -529,13 +547,7 @@ public override int ExecuteNonQuery()
529547
}
530548
}
531549

532-
return _statement.StatementType switch
533-
{
534-
DbStatementType.Insert => RecordsAffected,
535-
DbStatementType.Update => RecordsAffected,
536-
DbStatementType.Delete => RecordsAffected,
537-
_ => -1,
538-
};
550+
return StatementRecordsAffected;
539551
}
540552
public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
541553
{
@@ -566,13 +578,7 @@ public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellat
566578
}
567579
}
568580

569-
return _statement.StatementType switch
570-
{
571-
DbStatementType.Insert => RecordsAffected,
572-
DbStatementType.Update => RecordsAffected,
573-
DbStatementType.Delete => RecordsAffected,
574-
_ => -1,
575-
};
581+
return StatementRecordsAffected;
576582
}
577583

578584
public new FbDataReader ExecuteReader() => ExecuteReader(CommandBehavior.Default);

‎src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbDataReader.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public sealed class FbDataReader : DbDataReader
5151
private bool _eof;
5252
private bool _isClosed;
5353
private int _position;
54-
private int _recordsAffected;
54+
private int _recordsAffected = -1;
5555
private Dictionary<string, int> _columnsIndexesOrdinal;
5656
private Dictionary<string, int> _columnsIndexesOrdinalCI;
5757

@@ -1016,10 +1016,11 @@ private void UpdateRecordsAffected()
10161016
{
10171017
if (_command != null && !_command.IsDisposed)
10181018
{
1019-
if (_command.RecordsAffected != -1)
1019+
var recordsAffected = _command.StatementRecordsAffected;
1020+
if (recordsAffected != -1)
10201021
{
10211022
_recordsAffected = _recordsAffected == -1 ? 0 : _recordsAffected;
1022-
_recordsAffected += _command.RecordsAffected;
1023+
_recordsAffected += recordsAffected;
10231024
}
10241025
}
10251026
}

0 commit comments

Comments
 (0)