]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Fix Cursor.rowcount after an executemany hitting no result
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 14 Dec 2021 21:45:22 +0000 (22:45 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 14 Dec 2021 22:14:56 +0000 (23:14 +0100)
Close #178

docs/news.rst
psycopg/psycopg/cursor.py
tests/test_cursor.py
tests/test_cursor_async.py

index db6e3bf5be079febbbd401c35cd255763f783444..bb70910ba00eab31a4954ab4bbc20e874e837096 100644 (file)
@@ -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
index 03db0a50b3e6c3b2a949b3de74647e85dc5f358b..ee3559510e6652237275f899fb13c22e8c00f36b 100644 (file)
@@ -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():
index 42c8bed37b5fb936a70e0ff295d10586b4d19198..c4a4da8727b54d8058f89cc912d67ddb02dc3fa9 100644 (file)
@@ -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",
     [
index ef92692c63751021d6b5907aa724061a709b41fd..dd9009b49fbc6963129dbfdfc8e42e39e34b41dc 100644 (file)
@@ -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",
     [