Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/FirebirdSql.Data.FirebirdClient.Tests/FbDataReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,18 @@ public async Task GetFieldValueTest()
await transaction.RollbackAsync();
}
}

[Test]
public async Task RecordsAffectedIsMinusOneForSelectTest()
{
// DbDataReader.RecordsAffected: "-1 for SELECT statements; 0 if no rows were affected or the statement failed."
await using (var command = new FbCommand("select INT_FIELD from TEST where 1 = 0", Connection))
{
var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{ }
await reader.CloseAsync();
Assert.AreEqual(-1, reader.RecordsAffected);
}
}
}
34 changes: 20 additions & 14 deletions src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ internal int RecordsAffected
}
}

/// <summary>
/// The rows an INSERT, UPDATE or DELETE affected, or -1 for any other statement (DbCommand.ExecuteNonQuery: "For all
/// other types of statements, the return value is -1"; DbDataReader.RecordsAffected: "-1 for SELECT statements").
/// </summary>
internal int StatementRecordsAffected
{
get
{
return _statement?.StatementType switch
{
DbStatementType.Insert => RecordsAffected,
DbStatementType.Update => RecordsAffected,
DbStatementType.Delete => RecordsAffected,
_ => -1,
};
}
}

internal bool IsDisposed
{
get { return _disposed; }
Expand Down Expand Up @@ -529,13 +547,7 @@ public override int ExecuteNonQuery()
}
}

return _statement.StatementType switch
{
DbStatementType.Insert => RecordsAffected,
DbStatementType.Update => RecordsAffected,
DbStatementType.Delete => RecordsAffected,
_ => -1,
};
return StatementRecordsAffected;
}
public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -566,13 +578,7 @@ public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellat
}
}

return _statement.StatementType switch
{
DbStatementType.Insert => RecordsAffected,
DbStatementType.Update => RecordsAffected,
DbStatementType.Delete => RecordsAffected,
_ => -1,
};
return StatementRecordsAffected;
}

public new FbDataReader ExecuteReader() => ExecuteReader(CommandBehavior.Default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public sealed class FbDataReader : DbDataReader
private bool _eof;
private bool _isClosed;
private int _position;
private int _recordsAffected;
private int _recordsAffected = -1;
private Dictionary<string, int> _columnsIndexesOrdinal;
private Dictionary<string, int> _columnsIndexesOrdinalCI;

Expand Down Expand Up @@ -1016,10 +1016,11 @@ private void UpdateRecordsAffected()
{
if (_command != null && !_command.IsDisposed)
{
if (_command.RecordsAffected != -1)
var recordsAffected = _command.StatementRecordsAffected;
if (recordsAffected != -1)
{
_recordsAffected = _recordsAffected == -1 ? 0 : _recordsAffected;
_recordsAffected += _command.RecordsAffected;
_recordsAffected += recordsAffected;
}
}
}
Expand Down