From: Daniele Varrazzo Date: Thu, 28 Jul 2022 09:34:57 +0000 (+0200) Subject: fix: fix Cursor.rowcount after non-SELECT tuple-returining queries X-Git-Tag: 3.0.16~2^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c79aa98512639f6fb80e86bb2601b0c455ed4e2f;p=thirdparty%2Fpsycopg.git fix: fix Cursor.rowcount after non-SELECT tuple-returining queries PQcmdTuples doesn't return a value after SHOW and other type of queries. Close #343 --- diff --git a/docs/news.rst b/docs/news.rst index 6b48e92ae..28c2d9acf 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -7,6 +7,15 @@ ``psycopg`` release notes ========================= +Future releases +--------------- + +Psycopg 3.0.16 (unreleased) +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Fix missing `~Cursor.rowcount` after SHOW (:ticket:`#343`). + + Current release --------------- diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index bd8380129..ebd1a8e6d 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -435,8 +435,11 @@ class BaseCursor(Generic[ConnectionType, Row]): self._make_row = self._make_row_maker() self._pos = 0 - nrows = self.pgresult.command_tuples - self._rowcount = nrows if nrows is not None else -1 + if res.status == ExecStatus.TUPLES_OK: + self._rowcount = self.pgresult.ntuples + else: + nrows = self.pgresult.command_tuples + self._rowcount = nrows if nrows is not None else -1 def _send_prepare(self, name: bytes, query: PostgresQuery) -> None: self._pgconn.send_prepare(name, query.query, param_types=query.types)