psycopg.git / summary / log / commit / refs
commit 7253e30beefe6d705d5caeaf9e9db6868feed3ab
Author: Martin Baláž <balaz@brightpick.ai>
Commit: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Fri May 09 10:08:05 2025 +0000
feat: add Cursor.results() method
psycopg/psycopg/cursor.py | 15 +++++++++++++++
psycopg/psycopg/cursor_async.py | 15 +++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py
index 6b62898c..b5b127a6 100644
--- a/psycopg/psycopg/cursor.py
+++ b/psycopg/psycopg/cursor.py
@@ -175,6 +175,21 @@ class Cursor(BaseCursor["Connection[Any]", Row]):
except Exception:
pass
+ def results(self) -> Iterator[Self]:
+ """
+ Iterate across multiple record sets received by the cursor.
+
+ Multiple record sets are received after using `executemany()` with
+ `!returning=True` or using `execute()` with more than one query in the
+ command.
+ """
+ if self.pgresult:
+ while True:
+ yield self
+
+ if not self.nextset():
+ break
+
def fetchone(self) -> Row | None:
"""
Return the next record from the current result set.
diff --git a/psycopg/psycopg/cursor_async.py b/psycopg/psycopg/cursor_async.py
index 5fc93419..d237ab17 100644
--- a/psycopg/psycopg/cursor_async.py
+++ b/psycopg/psycopg/cursor_async.py
@@ -177,6 +177,21 @@ class AsyncCursor(BaseCursor["AsyncConnection[Any]", Row]):
except Exception:
pass
+ async def results(self) -> AsyncIterator[Self]:
+ """
+ Iterate across multiple record sets received by the cursor.
+
+ Multiple record sets are received after using `executemany()` with
+ `!returning=True` or using `execute()` with more than one query in the
+ command.
+ """
+ if self.pgresult:
+ while True:
+ yield self
+
+ if not self.nextset():
+ break
+
async def fetchone(self) -> Row | None:
"""
Return the next record from the current result set.
[parent: 37aeef9acf84]