]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added oid property on Dumper object
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 6 Aug 2020 00:08:59 +0000 (01:08 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 23 Aug 2020 18:24:01 +0000 (19:24 +0100)
psycopg3/psycopg3/adapt.py
psycopg3/psycopg3/types/array.py
psycopg3/psycopg3/types/numeric.py
psycopg3/psycopg3/types/text.py

index ea70aba53205dfff46e92183c9f9cd7e8fb40d03..2b43b1265851a3f5a1f0224efaa1ea2494883f6c 100644 (file)
@@ -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,
index 02bd13835012b2c1ee15cb374c4d7dfb67f7c346..edc1edaf3cda6525776b5974561d29b3c81af674 100644 (file)
@@ -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):
index 5c22561d5233906724363606a12651b8b83a5b4d..82bf981869e9f56454ac03755803ecaa0fd89de6 100644 (file)
@@ -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):
index 11a147eca995105a24f3f52c1607766e0a37cf8b..5773db38a251920ac3040e5033cdbb2cbf82280a 100644 (file)
@@ -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: