]> git.ipfire.org Git - thirdparty/psycopg.git/commit
fix: set rowcount to the first result in executemany(..., returning=True)
authorDenis Laxalde <denis@laxalde.org>
Sat, 7 Jan 2023 15:01:36 +0000 (16:01 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 9 Jan 2023 17:44:49 +0000 (17:44 +0000)
commit824f3c055099890717fcb6f48a889d4ec562f96b
treedf46ebf65e5851e29e7307583f15665482cf7ffc
parent93f1840651176406c6bf9bc16280124a78228eb4
fix: set rowcount to the first result in executemany(..., returning=True)

Previously, _rowcount was unconditionally set to the overall number of
rows of all queries in executemany() and then reset only upon the first
call to nextset(). In the returning=True case, this lead the rowcount
attribute to be wrong for the first result (i.e. it didn't match the
number of rows that would be returned by .fetchall(), as can be seen in
updated tests).

Now we only set _rowcount to the cumulated number of rows of executed
queries *if* executemany() is not returning (so the value is still
useful, e.g., in to check the number of INSERTed rows):

    >>> cur.executemany("INSERT INTO t(r) VALUES (%s)", [(1,), (2,)])
    >>> cur.rowcount
    2  # number of inserted rows
    >>> cur.nextset()
    >>> cur.executemany("INSERT INTO t(r) VALUES (%s) RETURNING r", [(1,), (2,)], returning=True)
    >>> cur.rowcount
    1  # number of rows in the first result set
    >>> cur.fetchall()
    [(1,)]
    >>> cur.nextset()
    True
    >>> cur.rowcount
    1
    >>> cur.fetchall()
    [(2,)]
    >>> cur.nextset()

Besides, the code for processing results from executemany() in
_executemany_gen_no_pipeline() is now similar to that of
_set_results_from_pipeline().
docs/api/cursors.rst
docs/news.rst
psycopg/psycopg/cursor.py
tests/test_client_cursor.py
tests/test_client_cursor_async.py
tests/test_cursor.py
tests/test_cursor_async.py
tests/test_pipeline.py
tests/test_pipeline_async.py