From d1531574dc1da55b8ebc0a42cee1640e2b05b424 Mon Sep 17 00:00:00 2001 From: Amin Ghadersohi Date: Sat, 26 Sep 2026 03:07:33 +0000 Subject: [PATCH] Return -1 from Cursor.affected_rows for statements without row counts Cursor.affected_rows requested the RECORDS statement info before checking the statement type. For DDL statements the server does not answer that request, so reading rowcount after e.g. CREATE TABLE raised InterfaceError("Result code does not match request code") instead of returning -1 as documented (and as PEP 249 requires). Check the statement type first and only request the row counts for SELECT/INSERT/UPDATE/DELETE statements. This matches the behaviour of the 2.x line. --- src/firebird/driver/core.py | 4 +++- tests/test_driver.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/firebird/driver/core.py b/src/firebird/driver/core.py index e5ab0f0..e69040c 100644 --- a/src/firebird/driver/core.py +++ b/src/firebird/driver/core.py @@ -4134,7 +4134,6 @@ def affected_rows(self) -> int: """ if self._stmt is None: return -1 - rows: Dict[ReqInfoCode, int] = self._stmt.info.get_info(StmtInfoCode.RECORDS) code: ReqInfoCode = None if self._stmt.type in (StatementType.SELECT, StatementType.SELECT_FOR_UPD): code = ReqInfoCode.SELECT_COUNT @@ -4145,7 +4144,10 @@ def affected_rows(self) -> int: elif self._stmt.type == StatementType.DELETE: code = ReqInfoCode.DELETE_COUNT else: + # Row counts are not available for other statement types (e.g. DDL), + # and the RECORDS info request is not answered for them. return -1 + rows: Dict[ReqInfoCode, int] = self._stmt.info.get_info(StmtInfoCode.RECORDS) return rows[code] rowcount = affected_rows @property diff --git a/tests/test_driver.py b/tests/test_driver.py index 29d2f1b..b3dcecf 100644 --- a/tests/test_driver.py +++ b/tests/test_driver.py @@ -1054,6 +1054,12 @@ def test_affected_rows_with_multiple_execute_statements_and_different_command(se rcount = 1 self.assertEqual(cur.affected_rows, rcount) self.assertEqual(cur.rowcount, rcount) + def test_affected_rows_ddl(self): + with self.con.cursor() as cur: + cur.execute("create table affected_rows_ddl (c1 integer)") + self.assertEqual(cur.affected_rows, -1) + self.assertEqual(cur.rowcount, -1) + self.con.rollback() def test_name(self): def assign_name(): cur.set_cursor_name('testx')