]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Move conversion of Composible to query string into PostgresQuery
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 28 Oct 2020 11:38:59 +0000 (12:38 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 28 Oct 2020 11:38:59 +0000 (12:38 +0100)
psycopg3/psycopg3/cursor.py
psycopg3/psycopg3/proto.py
psycopg3/psycopg3/utils/queries.py

index 238e4f3a8b18d5c87c5b589d2e8ab98878c4664d..81bc86289e6a7414537b51acaf480e61cd296e2d 100644 (file)
@@ -13,7 +13,6 @@ from . import proto
 from .proto import Query, Params, DumpersMap, LoadersMap, PQGen
 from .utils.queries import PostgresQuery
 from .copy import Copy, AsyncCopy
-from .sql import Composable
 
 if TYPE_CHECKING:
     from .connection import BaseConnection, Connection, AsyncConnection
@@ -162,9 +161,6 @@ class BaseCursor:
         """
         Implement part of execute() before waiting common to sync and async
         """
-        if isinstance(query, Composable):
-            query = query.as_string(self)
-
         pgq = PostgresQuery(self._transformer)
         pgq.convert(query, vars)
 
@@ -217,9 +213,6 @@ class BaseCursor:
         """
         Implement part of execute() before waiting common to sync and async
         """
-        if isinstance(query, Composable):
-            query = query.as_string(self)
-
         pgq = PostgresQuery(self._transformer)
         pgq.convert(query, vars)
 
index dfdd9f2a6714bf0524ea26bc479b9fd5281cc869..da713798243a3010e6f282738cdef2f606731aa1 100644 (file)
@@ -18,6 +18,7 @@ if TYPE_CHECKING:
     from .cursor import BaseCursor
     from .adapt import Dumper, Loader
     from .waiting import Wait, Ready
+    from .sql import Composable
 
 EncodeFunc = Callable[[str], Tuple[bytes, int]]
 DecodeFunc = Callable[[bytes], Tuple[str, int]]
@@ -45,11 +46,6 @@ LoaderType = Type["Loader"]
 LoadersMap = Dict[Tuple[int, Format], LoaderType]
 
 
-class Composable(Protocol):
-    def as_string(self, context: AdaptContext) -> str:
-        ...
-
-
 class Transformer(Protocol):
     def __init__(self, context: AdaptContext = None):
         ...
index 6aa91a7a3fafaf5f0fa0a2a6a38fb5257ee6b7c9..43ded029e91b28ebdb0005d1d6c38f1aa6eb0b48 100644 (file)
@@ -12,6 +12,7 @@ from typing import Sequence, Tuple, Union, TYPE_CHECKING
 from .. import errors as e
 from ..pq import Format
 from ..proto import Query, Params
+from ..sql import Composable
 
 if TYPE_CHECKING:
     from ..proto import Transformer
@@ -48,6 +49,9 @@ class PostgresQuery:
         The results of this function can be obtained accessing the object
         attributes (`query`, `params`, `types`, `formats`).
         """
+        if isinstance(query, Composable):
+            query = query.as_string(self._tx)
+
         codec = self._tx.codec
         if vars is not None:
             self.query, self.formats, self._order, self._parts = _query2pg(
@@ -99,7 +103,7 @@ class PostgresQuery:
 
 @lru_cache()
 def _query2pg(
-    query: Query, encoding: str
+    query: Union[bytes, str], encoding: str
 ) -> Tuple[bytes, List[Format], Optional[List[str]], List[QueryPart]]:
     """
     Convert Python query and params into something Postgres understands.