From: Martin Baláž Date: Fri, 9 May 2025 10:08:05 +0000 (+0200) Subject: feat: add Cursor.results() method X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=7253e30beefe6d705d5caeaf9e9db6868feed3ab;p=thirdparty%2Fpsycopg.git feat: add Cursor.results() method --- diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 6b62898ca..b5b127a6b 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 5fc93419f..d237ab17b 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.