From 7c25152d7a40534d4928d446070d274d19c69202 Mon Sep 17 00:00:00 2001 From: Matvey Gubanikhin Date: Thu, 5 Nov 2026 15:36:05 +0300 Subject: [PATCH 1/3] Fix calculation of number of affected rows. --- fbconn/statement.go | 6 +++++- rows_test.go | 4 +--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/fbconn/statement.go b/fbconn/statement.go index 5dbc7db3..643421a1 100644 --- a/fbconn/statement.go +++ b/fbconn/statement.go @@ -78,7 +78,11 @@ 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 + modifiedCount := sd.StatementInfo.Counts.InsertCount + sd.StatementInfo.Counts.UpdateCount + sd.StatementInfo.Counts.DeleteCount + if modifiedCount != 0 { + return modifiedCount + } + return sd.StatementInfo.Counts.SelectCount } func (sd *StatementDescription) EncodeBatchRow(values [][]byte) ([]byte, error) { diff --git a/rows_test.go b/rows_test.go index 9a466306..75e8f4eb 100644 --- a/rows_test.go +++ b/rows_test.go @@ -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()) }) } From 59426559e07669cc558835e07db6674fd0c669f7 Mon Sep 17 00:00:00 2001 From: Matvey Gubanikhin Date: Tue, 17 Nov 2026 09:04:45 +0300 Subject: [PATCH 2/3] Fix affected rows calculation for queries with a cursor, including psql. --- fbconn/fbconn.go | 10 +++++++++- fbconn/statement.go | 2 +- stdlib/sql_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/fbconn/fbconn.go b/fbconn/fbconn.go index bdce3668..687a156d 100644 --- a/fbconn/fbconn.go +++ b/fbconn/fbconn.go @@ -1538,7 +1538,8 @@ type QueryStatus struct { rowsAffected int64 } -// 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 } @@ -2401,6 +2402,7 @@ type ResultReader struct { cursorOpened bool fetchExecuted bool fetchResp *fbproto.OpFetchResponse + rowsFetched int64 eoc bool rowsInfoPopulated bool @@ -2637,6 +2639,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 { @@ -2706,6 +2709,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 @@ -2862,6 +2866,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 diff --git a/fbconn/statement.go b/fbconn/statement.go index 643421a1..ad54bc35 100644 --- a/fbconn/statement.go +++ b/fbconn/statement.go @@ -82,7 +82,7 @@ func (sd *StatementDescription) GetAffectedCount() int64 { if modifiedCount != 0 { return modifiedCount } - return sd.StatementInfo.Counts.SelectCount + return 0 } func (sd *StatementDescription) EncodeBatchRow(values [][]byte) ([]byte, error) { diff --git a/stdlib/sql_test.go b/stdlib/sql_test.go index 9720ebc0..b3945c25 100644 --- a/stdlib/sql_test.go +++ b/stdlib/sql_test.go @@ -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 TestQueryCloseRowsEarly(t *testing.T) { db := openDB(t) defer closeDB(t, db) From 2d1298805958feec6822f96b981667de145af056 Mon Sep 17 00:00:00 2001 From: Matvey Gubanikhin Date: Tue, 17 Nov 2026 14:06:48 +0300 Subject: [PATCH 3/3] Remove the unnecessary condition from GetAffectedCount. --- fbconn/statement.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/fbconn/statement.go b/fbconn/statement.go index ad54bc35..e8441991 100644 --- a/fbconn/statement.go +++ b/fbconn/statement.go @@ -78,11 +78,7 @@ func (sd *StatementDescription) HaveSingletonResult() bool { } func (sd *StatementDescription) GetAffectedCount() int64 { - modifiedCount := sd.StatementInfo.Counts.InsertCount + sd.StatementInfo.Counts.UpdateCount + sd.StatementInfo.Counts.DeleteCount - if modifiedCount != 0 { - return modifiedCount - } - return 0 + return sd.StatementInfo.Counts.InsertCount + sd.StatementInfo.Counts.UpdateCount + sd.StatementInfo.Counts.DeleteCount } func (sd *StatementDescription) EncodeBatchRow(values [][]byte) ([]byte, error) {