Skip to content
Merged
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
10 changes: 9 additions & 1 deletion fbconn/fbconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,8 @@ func NewQueryStatus(statementType int, rowsAffected int64) QueryStatus {
return QueryStatus{statementType: statementType, rowsAffected: rowsAffected}
}

// RowsAffected returns the number of rows affected
// RowsAffected returns the number of rows fetched for cursor statements, or the
// total number of inserted, updated, and deleted rows for other statements.
func (ct QueryStatus) RowsAffected() int64 {
return ct.rowsAffected
}
Expand Down Expand Up @@ -2419,6 +2420,7 @@ type ResultReader struct {
fetchExecuted bool
deferredExecute bool
fetchResp *fbproto.OpFetchResponse
rowsFetched int64
eoc bool
rowsInfoPopulated bool

Expand Down Expand Up @@ -2743,6 +2745,7 @@ func (rr *ResultReader) bufferCurrentFetchRows() bool {
panic(fmt.Sprintf("wrong response message type %T instead expected OpFetchResponse or ErrorResponse", m))
}

rr.rowsFetched++
for i := 0; i < colCount; i++ {
col := make([]byte, 0)
if row.RowData[i] == nil {
Expand Down Expand Up @@ -2812,6 +2815,7 @@ func (rr *ResultReader) drainCurrentFetch() {
default:
panic(fmt.Sprintf("wrong response message type %T instead expected OpFetchResponse or ErrorResponse", m))
}
rr.rowsFetched++
}
if rr.fetchResp.EndOfCursor() {
rr.eoc = true
Expand Down Expand Up @@ -2975,6 +2979,10 @@ func (rr *ResultReader) populateRowsAffected() error {
}

rows := rr.statementDescription.GetAffectedCount()
if rr.statementDescription.HaveCursorResult() {
// SelectCount includes reads inside PSQL and can differ from the rows returned.
rows = rr.rowsFetched
}

rr.queryStatus.rowsAffected = rows
rr.rowsInfoPopulated = true
Expand Down
2 changes: 1 addition & 1 deletion fbconn/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (sd *StatementDescription) HaveSingletonResult() bool {
}

func (sd *StatementDescription) GetAffectedCount() int64 {
return sd.StatementInfo.Counts.SelectCount + sd.StatementInfo.Counts.InsertCount + sd.StatementInfo.Counts.UpdateCount + sd.StatementInfo.Counts.DeleteCount
return sd.StatementInfo.Counts.InsertCount + sd.StatementInfo.Counts.UpdateCount + sd.StatementInfo.Counts.DeleteCount
}

func (sd *StatementDescription) EncodeBatchRow(values [][]byte) ([]byte, error) {
Expand Down
4 changes: 1 addition & 3 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ begin
end`, "rows_counts_test", "rows_counts_test", "rows_counts_test"))
require.NoError(t, err)

// Firebird reports counts for all operations inside the block (including internal reads),
// so the aggregated RowsAffected should reflect the sum returned by isc_info_sql_records.
require.EqualValues(t, 5, ct.RowsAffected())
require.EqualValues(t, 3, ct.RowsAffected())
})
}

Expand Down
27 changes: 27 additions & 0 deletions stdlib/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,33 @@ func TestStmtExec(t *testing.T) {
ensureDBValid(t, db)
}

func TestExecRowsAffectedExecuteBlock(t *testing.T) {
db := openDB(t)
defer closeDB(t, db)

tests := []struct {
name string
query string
}{
{"without internal reads", `execute block returns (v integer) as begin
v = 1; suspend; v = 2; suspend;
end`},
{"with internal reads", `execute block returns (v integer) as begin
select 1 from rdb$database into :v;
suspend; suspend;
end`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := db.Exec(tt.query)
require.NoError(t, err)
affected, err := result.RowsAffected()
require.NoError(t, err)
assert.EqualValues(t, 2, affected)
})
}
}

func TestExecBatchRows(t *testing.T) {
testWithAllQueryExecModes(t, func(t *testing.T, db *sql.DB) {
ctx := context.Background()
Expand Down