]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
feat: add Cursor.results() method
authorMartin Baláž <balaz@brightpick.ai>
Fri, 9 May 2025 10:08:05 +0000 (12:08 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 12 Jul 2025 04:55:53 +0000 (06:55 +0200)
psycopg/psycopg/cursor.py
psycopg/psycopg/cursor_async.py

index 6b62898ca7bc15de66ce2b57a6aea6461e0ec49a..b5b127a6ba45ff35453e348f5aaf6aa0d7ce457a 100644 (file)
@@ -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.
index 5fc93419f878ec9e64c58d24a322b6a0d22c8fc3..d237ab17b82559c0c59ac90be6c610cfd51bf164 100644 (file)
@@ -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.