From fe90f7289acc12362f533fa7e04c60ecbf47a5bf Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 14 Jan 2021 17:40:05 +0100 Subject: [PATCH] Less work to create a C Transformer --- psycopg3_c/psycopg3_c/_psycopg3/transform.pyx | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx b/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx index 8cb0c31ab..9d09bc3c6 100644 --- a/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx +++ b/psycopg3_c/psycopg3_c/_psycopg3/transform.pyx @@ -66,11 +66,16 @@ cdef class Transformer: cdef readonly object connection cdef readonly object adapters + + # mapping class -> Dumper instance (auto, text, binary) cdef dict _auto_dumpers cdef dict _text_dumpers cdef dict _binary_dumpers + + # mapping oid -> Loader instance (text, binary) cdef dict _text_loaders cdef dict _binary_loaders + cdef pq.PGresult _pgresult cdef int _nfields, _ntuples cdef list _row_dumpers @@ -85,19 +90,6 @@ cdef class Transformer: self.adapters = global_adapters self.connection = None - # mapping class -> Dumper instance (auto, text, binary) - self._auto_dumpers = None - self._text_dumpers = None - self._binary_dumpers = None - - # mapping oid -> Loader instance (text, binary) - self._text_loaders = {} - self._binary_loaders = {} - - self.pgresult = None - self._row_dumpers = None - self._row_loaders = [] - @property def pgresult(self) -> Optional[PGresult]: return self._pgresult @@ -448,8 +440,18 @@ cdef class Transformer: cdef PyObject *ptr cdef PyObject *cache - cache = ( - self._binary_loaders if fmt == 0 else self._text_loaders) + if fmt == PQ_TEXT: + if self._text_loaders is None: + self._text_loaders = {} + cache = self._text_loaders + elif fmt == PQ_BINARY: + if self._binary_loaders is None: + self._binary_loaders = {} + cache = self._binary_loaders + else: + raise ValueError( + f"format should be a psycopg3.pq.Format, not {format}") + ptr = PyDict_GetItem(cache, oid) if ptr != NULL: return ptr -- 2.47.2