From: Daniele Varrazzo Date: Wed, 28 Oct 2020 11:38:59 +0000 (+0100) Subject: Move conversion of Composible to query string into PostgresQuery X-Git-Tag: 3.0.dev0~424^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9097cb62ef63fdbbce856529d2867be98bfa36f8;p=thirdparty%2Fpsycopg.git Move conversion of Composible to query string into PostgresQuery --- diff --git a/psycopg3/psycopg3/cursor.py b/psycopg3/psycopg3/cursor.py index 238e4f3a8..81bc86289 100644 --- a/psycopg3/psycopg3/cursor.py +++ b/psycopg3/psycopg3/cursor.py @@ -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) diff --git a/psycopg3/psycopg3/proto.py b/psycopg3/psycopg3/proto.py index dfdd9f2a6..da7137982 100644 --- a/psycopg3/psycopg3/proto.py +++ b/psycopg3/psycopg3/proto.py @@ -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): ... diff --git a/psycopg3/psycopg3/utils/queries.py b/psycopg3/psycopg3/utils/queries.py index 6aa91a7a3..43ded029e 100644 --- a/psycopg3/psycopg3/utils/queries.py +++ b/psycopg3/psycopg3/utils/queries.py @@ -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.