From: Daniele Varrazzo Date: Thu, 6 Aug 2020 00:08:59 +0000 (+0100) Subject: Added oid property on Dumper object X-Git-Tag: 3.0.dev0~458^2~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=092b5fd593d353cdfbdcdc97763afb6108e4ee69;p=thirdparty%2Fpsycopg.git Added oid property on Dumper object --- diff --git a/psycopg3/psycopg3/adapt.py b/psycopg3/psycopg3/adapt.py index ea70aba53..2b43b1265 100644 --- a/psycopg3/psycopg3/adapt.py +++ b/psycopg3/psycopg3/adapt.py @@ -9,11 +9,13 @@ from typing import Any, Callable, Optional, Tuple, Type, Union from . import pq from . import proto from .pq import Format as Format -from .proto import AdaptContext, DumpersMap, DumperType -from .proto import LoadersMap, LoaderType +from .types import builtins +from .proto import AdaptContext, DumpersMap, DumperType, LoadersMap, LoaderType from .cursor import BaseCursor from .connection import BaseConnection +TEXT_OID = builtins["text"].oid + class Dumper: globals: DumpersMap = {} @@ -27,6 +29,10 @@ class Dumper: def dump(self, obj: Any) -> Union[bytes, Tuple[bytes, int]]: raise NotImplementedError() + @property + def oid(self) -> int: + return TEXT_OID + @classmethod def register( cls, diff --git a/psycopg3/psycopg3/types/array.py b/psycopg3/psycopg3/types/array.py index 02bd13835..edc1edaf3 100644 --- a/psycopg3/psycopg3/types/array.py +++ b/psycopg3/psycopg3/types/array.py @@ -21,6 +21,7 @@ class BaseListDumper(Dumper): def __init__(self, src: type, context: AdaptContext = None): super().__init__(src, context) self._tx = Transformer(context) + self._oid = 0 def _array_oid(self, base_oid: int) -> int: """ @@ -62,11 +63,7 @@ class TextListDumper(BaseListDumper): def dump(self, obj: List[Any]) -> Tuple[bytes, int]: tokens: List[bytes] = [] - oid = 0 - def dump_list(obj: List[Any]) -> None: - nonlocal oid - if not obj: tokens.append(b"{}") return @@ -80,10 +77,10 @@ class TextListDumper(BaseListDumper): else: ad = self._tx.dump(item) if isinstance(ad, tuple): - if oid == 0: - oid = ad[1] + if not self._oid: + self._oid = ad[1] got_type = type(item) - elif oid != ad[1]: + elif self._oid != ad[1]: raise e.DataError( f"array contains different types," f" at least {got_type} and {type(item)}" @@ -105,7 +102,11 @@ class TextListDumper(BaseListDumper): dump_list(obj) - return b"".join(tokens), self._array_oid(oid) + return b"".join(tokens), self._array_oid(self._oid) + + @property + def oid(self) -> int: + return self._array_oid(self._oid) if self._oid else TEXT_ARRAY_OID @Dumper.binary(list) @@ -117,7 +118,6 @@ class BinaryListDumper(BaseListDumper): data: List[bytes] = [b"", b""] # placeholders to avoid a resize dims: List[int] = [] hasnull = 0 - oid = 0 def calc_dims(L: List[Any]) -> None: if isinstance(L, self.src): @@ -129,7 +129,7 @@ class BinaryListDumper(BaseListDumper): calc_dims(obj) def dump_list(L: List[Any], dim: int) -> None: - nonlocal oid, hasnull + nonlocal hasnull if len(L) != dims[dim]: raise e.DataError("nested lists have inconsistent lengths") @@ -137,10 +137,10 @@ class BinaryListDumper(BaseListDumper): for item in L: ad = self._tx.dump(item, Format.BINARY) if isinstance(ad, tuple): - if oid == 0: - oid = ad[1] + if not self._oid: + self._oid = ad[1] got_type = type(item) - elif oid != ad[1]: + elif self._oid != ad[1]: raise e.DataError( f"array contains different types," f" at least {got_type} and {type(item)}" @@ -162,12 +162,16 @@ class BinaryListDumper(BaseListDumper): dump_list(obj, 0) - if oid == 0: - oid = TEXT_OID + if not self._oid: + self._oid = TEXT_OID - data[0] = _struct_head.pack(len(dims), hasnull, oid or TEXT_OID) + data[0] = _struct_head.pack(len(dims), hasnull, self._oid) data[1] = b"".join(_struct_dim.pack(dim, 1) for dim in dims) - return b"".join(data), self._array_oid(oid) + return b"".join(data), self._array_oid(self._oid) + + @property + def oid(self) -> int: + return self._array_oid(self._oid) if self._oid else TEXT_ARRAY_OID class BaseArrayLoader(Loader): diff --git a/psycopg3/psycopg3/types/numeric.py b/psycopg3/psycopg3/types/numeric.py index 5c22561d5..82bf98186 100644 --- a/psycopg3/psycopg3/types/numeric.py +++ b/psycopg3/psycopg3/types/numeric.py @@ -33,6 +33,10 @@ class TextIntDumper(Dumper): # We don't know the size of it, so we have to return a type big enough return _encode(str(obj))[0], NUMERIC_OID + @property + def oid(self) -> int: + return NUMERIC_OID + @Dumper.text(float) class TextFloatDumper(Dumper): @@ -40,6 +44,10 @@ class TextFloatDumper(Dumper): # Float can't be bigger than this instead return _encode(str(obj))[0], FLOAT8_OID + @property + def oid(self) -> int: + return FLOAT8_OID + @Dumper.text(Decimal) class TextDecimalDumper(Dumper): diff --git a/psycopg3/psycopg3/types/text.py b/psycopg3/psycopg3/types/text.py index 11a147eca..5773db38a 100644 --- a/psycopg3/psycopg3/types/text.py +++ b/psycopg3/psycopg3/types/text.py @@ -93,12 +93,20 @@ class BytesDumper(Dumper): def dump(self, obj: bytes) -> Tuple[bytes, int]: return self.esc.escape_bytea(obj), BYTEA_OID + @property + def oid(self) -> int: + return BYTEA_OID + @Dumper.binary(bytes) class BinaryBytesDumper(Dumper): def dump(self, b: bytes) -> Tuple[bytes, int]: return b, BYTEA_OID + @property + def oid(self) -> int: + return BYTEA_OID + @Loader.text(builtins["bytea"].oid) def load_bytea_text(data: bytes) -> bytes: