]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor: compose DECLARE in the helper common path
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 28 Oct 2021 15:55:37 +0000 (17:55 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 28 Oct 2021 15:55:37 +0000 (17:55 +0200)
...instead of in the separate sync/async branches.

psycopg/psycopg/server_cursor.py

index d936b7b6f8a447e7fb63424b2376f2db146ba16d..7c2e2e7e9b8013a8db8560a3a2ead4427bce3a58 100644 (file)
@@ -62,7 +62,9 @@ class ServerCursorHelper(Generic[ConnectionType, Row]):
         params: Optional[Params] = None,
     ) -> PQGen[None]:
         """Generator implementing `ServerCursor.execute()`."""
+
         conn = cur._conn
+        query = self._make_declare_statement(conn, query)
 
         # If the cursor is being reused, the previous one must be closed.
         if self.described:
@@ -153,13 +155,11 @@ class ServerCursorHelper(Generic[ConnectionType, Row]):
         yield from cur._conn._exec_command(query)
 
     def _make_declare_statement(
-        self,
-        cur: BaseCursor[ConnectionType, Row],
-        query: Query,
+        self, conn: ConnectionType, query: Query
     ) -> sql.Composable:
 
         if isinstance(query, bytes):
-            query = query.decode(pgconn_encoding(cur._conn.pgconn))
+            query = query.decode(pgconn_encoding(conn.pgconn))
         if not isinstance(query, sql.Composable):
             query = sql.SQL(query)
 
@@ -257,7 +257,6 @@ class ServerCursor(Cursor[Row]):
         if kwargs:
             raise TypeError(f"keyword not supported: {list(kwargs)[0]}")
         helper = self._helper
-        query = helper._make_declare_statement(self, query)
 
         if binary is None:
             helper.format = self.format
@@ -378,7 +377,6 @@ class AsyncServerCursor(AsyncCursor[Row]):
         if kwargs:
             raise TypeError(f"keyword not supported: {list(kwargs)[0]}")
         helper = self._helper
-        query = helper._make_declare_statement(self, query)
 
         if binary is None:
             helper.format = self.format