]> git.ipfire.org Git - thirdparty/psycopg.git/commit
fix: set rowcount to the first result in executemany(..., returning=True) 479/head
authorDenis Laxalde <denis@laxalde.org>
Sat, 7 Jan 2023 15:01:36 +0000 (16:01 +0100)
committerDenis Laxalde <denis.laxalde@dalibo.com>
Mon, 9 Jan 2023 11:31:25 +0000 (12:31 +0100)
commit526881a04fd69e35608619cc90f1133bea5c9a0c
tree85c02617e008611759bda2d57533fcc5cf9da2c5
parent873c61a441d366a1e25532787d0cb7a80121eb52
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