From: Daniele Varrazzo Date: Wed, 13 Jan 2021 14:28:06 +0000 (+0100) Subject: Transformer.dump_sequence returns a list of formats too X-Git-Tag: 3.0.dev0~167 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=36b9fb7c1fbe3a594fa0de7ad8ae36f7e6b536e2;p=thirdparty%2Fpsycopg.git Transformer.dump_sequence returns a list of formats too --- diff --git a/psycopg3/psycopg3/_queries.py b/psycopg3/psycopg3/_queries.py index 76ccb1ac1..97155f69d 100644 --- a/psycopg3/psycopg3/_queries.py +++ b/psycopg3/psycopg3/_queries.py @@ -31,7 +31,7 @@ class PostgresQuery: __slots__ = """ params types formats - _tx _unknown_oid _parts query _encoding _order + _tx _want_formats _parts query _encoding _order """.split() def __init__(self, transformer: "Transformer"): @@ -40,7 +40,10 @@ class PostgresQuery: self.params: Optional[List[Optional[bytes]]] = None # these are tuples so they can be used as keys e.g. in prepared stmts self.types: Tuple[int, ...] = () - self.formats: Optional[List[Format]] = None + + # The format requested by the user and the ones to really pass Postgres + self._want_formats: Optional[List[Format]] = None + self.formats: Optional[Sequence[Format]] = None self._parts: List[QueryPart] self.query = b"" @@ -62,20 +65,23 @@ class PostgresQuery: query = query.as_bytes(self._tx) if vars is not None: - self.query, self.formats, self._order, self._parts = _query2pg( - query, self._encoding - ) + ( + self.query, + self._want_formats, + self._order, + self._parts, + ) = _query2pg(query, self._encoding) else: if isinstance(query, str): query = query.encode(self._encoding) self.query = query - self.formats = self._order = None + self._want_formats = self._order = None self.dump(vars) def dump(self, vars: Optional[Params]) -> None: """ - Process a new set of variables on the same query as before. + Process a new set of variables on the query processed by `convert()`. This method updates `params` and `types`. """ @@ -83,13 +89,14 @@ class PostgresQuery: params = _validate_and_reorder_params( self._parts, vars, self._order ) - assert self.formats is not None - self.params, self.types = self._tx.dump_sequence( - params, self.formats + assert self._want_formats is not None + self.params, self.types, self.formats = self._tx.dump_sequence( + params, self._want_formats ) else: self.params = None self.types = () + self.formats = None @lru_cache() diff --git a/psycopg3/psycopg3/_transform.py b/psycopg3/psycopg3/_transform.py index 4a0b11f5d..27c53c0b4 100644 --- a/psycopg3/psycopg3/_transform.py +++ b/psycopg3/psycopg3/_transform.py @@ -104,7 +104,7 @@ class Transformer(AdaptContext): def dump_sequence( self, params: Sequence[Any], formats: Sequence[Format] - ) -> Tuple[List[Any], Tuple[int, ...]]: + ) -> Tuple[List[Any], Tuple[int, ...], Sequence[Format]]: ps: List[Optional[bytes]] = [None] * len(params) ts = [INVALID_OID] * len(params) @@ -121,7 +121,7 @@ class Transformer(AdaptContext): ps[i] = dumper.dump(param) ts[i] = dumper.oid - return ps, tuple(ts) + return ps, tuple(ts), formats def get_dumper(self, obj: Any, format: Format) -> "Dumper": # Fast path: return a Dumper class already instantiated from the same type diff --git a/psycopg3/psycopg3/proto.py b/psycopg3/psycopg3/proto.py index fbaa6eea4..2cc5ab665 100644 --- a/psycopg3/psycopg3/proto.py +++ b/psycopg3/psycopg3/proto.py @@ -93,7 +93,7 @@ class Transformer(Protocol): def dump_sequence( self, params: Sequence[Any], formats: Sequence[Format] - ) -> Tuple[List[Any], Tuple[int, ...]]: + ) -> Tuple[List[Any], Tuple[int, ...], Sequence[Format]]: ... def get_dumper(self, obj: Any, format: Format) -> "Dumper": diff --git a/psycopg3_c/psycopg3_c/_psycopg3.pyi b/psycopg3_c/psycopg3_c/_psycopg3.pyi index dad01fabb..50a49a877 100644 --- a/psycopg3_c/psycopg3_c/_psycopg3.pyi +++ b/psycopg3_c/psycopg3_c/_psycopg3.pyi @@ -30,7 +30,7 @@ class Transformer(proto.AdaptContext): ) -> None: ... def dump_sequence( self, params: Sequence[Any], formats: Sequence[Format] - ) -> Tuple[List[Any], Tuple[int, ...]]: ... + ) -> Tuple[List[Any], Tuple[int, ...], Sequence[Format]]: ... def get_dumper(self, obj: Any, format: Format) -> Dumper: ... def load_rows(self, row0: int, row1: int) -> List[Tuple[Any, ...]]: ... def load_row(self, row: int) -> Optional[Tuple[Any, ...]]: ... diff --git a/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx b/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx index 9c8b18fd0..033230648 100644 --- a/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx +++ b/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx @@ -258,7 +258,7 @@ cdef class Transformer: Py_INCREF(oid) PyTuple_SET_ITEM(ts, i, oid) - return ps, ts + return ps, ts, formats cdef RowDumper _get_row_dumper(self, object param, object fmt): cdef RowDumper row_dumper = RowDumper()