From: Daniele Varrazzo Date: Tue, 14 Dec 2021 21:45:22 +0000 (+0100) Subject: Fix Cursor.rowcount after an executemany hitting no result X-Git-Tag: pool-3.1~74 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b2829f91ba1809af00e913e24ee0d59f7c984f6;p=thirdparty%2Fpsycopg.git Fix Cursor.rowcount after an executemany hitting no result Close #178 --- diff --git a/docs/news.rst b/docs/news.rst index db6e3bf5b..bb70910ba 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -19,6 +19,8 @@ Psycopg 3.0.7 (unreleased) - Fix crash in `~Cursor.executemany()` with no input sequence (:ticket:`#179`). +- Fix wrong `~Cursor.rowcount` after an `~Cursor.executemany()` returning no + rows (:ticket:`#178`). Current release diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 03db0a50b..ee3559510 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -231,7 +231,7 @@ class BaseCursor(Generic[ConnectionType, Row]): # Override rowcout for the first result. Calls to nextset() will change # it to the value of that result only, but we hope nobody will notice. # You haven't read this comment. - self._rowcount = nrows or -1 + self._rowcount = nrows self._last_query = query for cmd in self._conn._prepared.get_maintenance_commands(): diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 42c8bed37..c4a4da872 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -291,6 +291,18 @@ def test_executemany_returning_rowcount(conn, execmany): assert cur.rowcount == 2 +def test_executemany_rowcount_no_hit(conn, execmany): + cur = conn.cursor() + cur.executemany("delete from execmany where id = %s", [(-1,), (-2,)]) + assert cur.rowcount == 0 + cur.executemany("delete from execmany where id = %s", []) + assert cur.rowcount == 0 + cur.executemany( + "delete from execmany where id = %s returning num", [(-1,), (-2,)] + ) + assert cur.rowcount == 0 + + @pytest.mark.parametrize( "query", [ diff --git a/tests/test_cursor_async.py b/tests/test_cursor_async.py index ef92692c6..dd9009b49 100644 --- a/tests/test_cursor_async.py +++ b/tests/test_cursor_async.py @@ -292,6 +292,18 @@ async def test_executemany_returning_rowcount(aconn, execmany): assert cur.rowcount == 2 +async def test_executemany_rowcount_no_hit(aconn, execmany): + cur = aconn.cursor() + await cur.executemany("delete from execmany where id = %s", [(-1,), (-2,)]) + assert cur.rowcount == 0 + await cur.executemany("delete from execmany where id = %s", []) + assert cur.rowcount == 0 + await cur.executemany( + "delete from execmany where id = %s returning num", [(-1,), (-2,)] + ) + assert cur.rowcount == 0 + + @pytest.mark.parametrize( "query", [