methods. Each input parameter will produce a separate result set: use
`nextset()` to read the results of the queries after the first one.
+ The value of `rowcount` is set to the cumulated number of rows
+ affected by queries; except when using `!returning=True`, in which
+ case it is set to the number of rows in the current result set (i.e.
+ the first one, until `nextset()` gets called).
+
See :ref:`query-parameters` for all the details about executing
queries.
a successful command, such as ``CREATE TABLE`` or ``UPDATE 42``.
.. autoattribute:: rowcount
+
+ From `executemany()`, unless called with `!returning=True`, this is
+ the cumulated number of rows affected by executed commands.
+
.. autoattribute:: rownumber
.. attribute:: _query
- Don't pollute server logs when types looked for by `TypeInfo.fetch()`
are not found (:ticket:`#473`).
+- Set `Cursor.rowcount` to the number of rows of each result set from
+ `~Cursor.executemany()` when called with `!returning=True` (:ticket:`#479`).
Current release
---------------
assert pipeline
yield from self._start_query(query)
- self._rowcount = 0
+ if not returning:
+ self._rowcount = 0
assert self._execmany_returning is None
self._execmany_returning = returning
Generator implementing `Cursor.executemany()` with pipelines not available.
"""
yield from self._start_query(query)
+ if not returning:
+ self._rowcount = 0
first = True
- nrows = 0
for params in params_seq:
if first:
pgq = self._convert_query(query, params)
self._check_results(results)
if returning:
self._results.extend(results)
-
- for res in results:
- nrows += res.command_tuples or 0
+ else:
+ # In non-returning case, set rowcount to the cumulated number
+ # of rows of executed queries.
+ for res in results:
+ self._rowcount += res.command_tuples or 0
if self._results:
self._select_current_result(0)
- # Override rowcount 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
self._last_query = query
for cmd in self._conn._prepared.get_maintenance_commands():
self._results.extend(results)
if first_batch:
self._select_current_result(0)
- self._rowcount = 0
-
- # Override rowcount 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.
- for res in results:
- self._rowcount += res.command_tuples or 0
+ else:
+ # In non-returning case, set rowcount to the cumulated number of
+ # rows of executed queries.
+ for res in results:
+ self._rowcount += res.command_tuples or 0
def _send_prepare(self, name: bytes, query: PostgresQuery) -> None:
if self._conn._pipeline:
[(10, "hello"), (20, "world")],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert cur.fetchone() == (10,)
assert cur.nextset()
+ assert cur.rowcount == 1
assert cur.fetchone() == (20,)
assert cur.nextset() is None
[(10, "hello"), (20, "world")],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
with pytest.raises(psycopg.ProgrammingError):
cur.fetchone()
pgresult = cur.pgresult
assert cur.nextset()
+ assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
assert pgresult is not cur.pgresult
assert cur.nextset() is None
[(10, "hello"), (20, "world")],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert (await cur.fetchone()) == (10,)
assert cur.nextset()
+ assert cur.rowcount == 1
assert (await cur.fetchone()) == (20,)
assert cur.nextset() is None
[(10, "hello"), (20, "world")],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
with pytest.raises(psycopg.ProgrammingError):
await cur.fetchone()
pgresult = cur.pgresult
assert cur.nextset()
+ assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
assert pgresult is not cur.pgresult
assert cur.nextset() is None
[(10, "hello"), (20, "world")],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert cur.fetchone() == (10,)
assert cur.nextset()
+ assert cur.rowcount == 1
assert cur.fetchone() == (20,)
assert cur.nextset() is None
[(10, "hello"), (20, "world")],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
with pytest.raises(psycopg.ProgrammingError):
cur.fetchone()
pgresult = cur.pgresult
assert cur.nextset()
+ assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
assert pgresult is not cur.pgresult
assert cur.nextset() is None
[(10, "hello"), (20, "world")],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert (await cur.fetchone()) == (10,)
assert cur.nextset()
+ assert cur.rowcount == 1
assert (await cur.fetchone()) == (20,)
assert cur.nextset() is None
[(10, "hello"), (20, "world")],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
with pytest.raises(psycopg.ProgrammingError):
await cur.fetchone()
pgresult = cur.pgresult
assert cur.nextset()
+ assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
assert pgresult is not cur.pgresult
assert cur.nextset() is None
[(10,), (20,)],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert cur.fetchone() == (10,)
assert cur.nextset()
+ assert cur.rowcount == 1
assert cur.fetchone() == (20,)
assert cur.nextset() is None
[(10,), (20,)],
returning=True,
)
- assert cur.rowcount == 2
+ assert cur.rowcount == 1
assert (await cur.fetchone()) == (10,)
assert cur.nextset()
+ assert cur.rowcount == 1
assert (await cur.fetchone()) == (20,)
assert cur.nextset() is None