]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Don't require a Transformer roundtrip to choose the specific int dumper
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 20 Jan 2021 12:23:06 +0000 (13:23 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 22 Jan 2021 03:11:22 +0000 (04:11 +0100)
They are static object, they require no customisation with the
connection. I don't expect anyone wanting to subclass them; if they
do they will deal with the whole 5 objects.

psycopg3/psycopg3/types/__init__.py
psycopg3/psycopg3/types/numeric.py

index 1bc7fed4aa1d45d9f0da18a709b4464358e6a237..7c9dc2ee9dd1778d45d16fe5ae591c7fb2efc15a 100644 (file)
@@ -46,7 +46,6 @@ from .numeric import (
     Int2BinaryDumper,
     Int4BinaryDumper,
     Int8BinaryDumper,
-    IntNumericBinaryDumper,
     OidBinaryDumper,
     IntLoader,
     Int2BinaryLoader,
@@ -163,7 +162,6 @@ def register_default_globals(ctx: AdaptContext) -> None:
     Int2BinaryDumper.register(Int2, ctx)
     Int4BinaryDumper.register(Int4, ctx)
     Int8BinaryDumper.register(Int8, ctx)
-    IntNumericBinaryDumper.register(IntNumeric, ctx)
     OidBinaryDumper.register(Oid, ctx)
     IntLoader.register("int2", ctx)
     IntLoader.register("int4", ctx)
index c5e969a613cda59e8a8055425fe96287ecdc0246..803173d670dce1fc8eaf8dc813d605731dca5817 100644 (file)
@@ -5,13 +5,12 @@ Adapers for numeric types.
 # Copyright (C) 2020-2021 The Psycopg Team
 
 import struct
-from typing import Any, Callable, Dict, Optional, Tuple, cast
+from typing import Any, Callable, Dict, Tuple, cast
 from decimal import Decimal
 
-from .. import proto
 from ..pq import Format
 from ..oids import builtins
-from ..adapt import Buffer, Dumper, Loader, Transformer
+from ..adapt import Buffer, Dumper, Loader
 from ..adapt import Format as Pg3Format
 
 _PackInt = Callable[[int], bytes]
@@ -60,59 +59,6 @@ class Oid(int):
         return super().__new__(cls, arg)  # type: ignore
 
 
-class IntDumper(Dumper):
-
-    format = Format.TEXT
-
-    def __init__(
-        self, cls: type, context: Optional[proto.AdaptContext] = None
-    ):
-        super().__init__(cls, context)
-        self._tx = Transformer(context)
-
-    def dump(self, obj: Any) -> bytes:
-        raise TypeError(
-            "dispatcher to find the int subclass: not supposed to be called"
-        )
-
-    def get_key(cls, obj: int, format: Pg3Format) -> type:
-        if -(2 ** 31) <= obj < 2 ** 31:
-            if -(2 ** 15) <= obj < 2 ** 15:
-                return Int2
-            else:
-                return Int4
-        else:
-            if -(2 ** 63) <= obj < 2 ** 63:
-                return Int8
-            else:
-                return IntNumeric
-
-    def upgrade(self, obj: int, format: Pg3Format) -> Dumper:
-        sample: Any
-        if -(2 ** 31) <= obj < 2 ** 31:
-            if -(2 ** 15) <= obj < 2 ** 15:
-                sample = INT2_SAMPLE
-            else:
-                sample = INT4_SAMPLE
-        else:
-            if -(2 ** 63) <= obj < 2 ** 63:
-                sample = INT8_SAMPLE
-            else:
-                sample = INTNUMERIC_SAMPLE
-
-        return self._tx.get_dumper(sample, format)
-
-
-class IntBinaryDumper(IntDumper):
-    format = Format.BINARY
-
-
-INT2_SAMPLE = Int2(0)
-INT4_SAMPLE = Int4(0)
-INT8_SAMPLE = Int8(0)
-INTNUMERIC_SAMPLE = IntNumeric(0)
-
-
 class NumberDumper(Dumper):
 
     format = Format.TEXT
@@ -190,6 +136,45 @@ class OidDumper(NumberDumper):
     _oid = builtins["oid"].oid
 
 
+class IntDumper(Dumper):
+
+    format = Format.TEXT
+
+    def dump(self, obj: Any) -> bytes:
+        raise TypeError(
+            "dispatcher to find the int subclass: not supposed to be called"
+        )
+
+    def get_key(cls, obj: int, format: Pg3Format) -> type:
+        if -(2 ** 31) <= obj < 2 ** 31:
+            if -(2 ** 15) <= obj < 2 ** 15:
+                return Int2
+            else:
+                return Int4
+        else:
+            if -(2 ** 63) <= obj < 2 ** 63:
+                return Int8
+            else:
+                return IntNumeric
+
+    _int2_dumper = Int2Dumper(Int2)
+    _int4_dumper = Int4Dumper(Int4)
+    _int8_dumper = Int8Dumper(Int8)
+    _int_numeric_dumper = IntNumericDumper(IntNumeric)
+
+    def upgrade(self, obj: int, format: Pg3Format) -> Dumper:
+        if -(2 ** 31) <= obj < 2 ** 31:
+            if -(2 ** 15) <= obj < 2 ** 15:
+                return self._int2_dumper
+            else:
+                return self._int4_dumper
+        else:
+            if -(2 ** 63) <= obj < 2 ** 63:
+                return self._int8_dumper
+            else:
+                return self._int_numeric_dumper
+
+
 class Int2BinaryDumper(Int2Dumper):
 
     format = Format.BINARY
@@ -219,7 +204,7 @@ class IntNumericBinaryDumper(IntNumericDumper):
     format = Format.BINARY
 
     def dump(self, obj: int) -> bytes:
-        raise NotImplementedError
+        raise NotImplementedError("binary decimal dump not implemented yet")
 
 
 class OidBinaryDumper(OidDumper):
@@ -230,6 +215,16 @@ class OidBinaryDumper(OidDumper):
         return _pack_uint4(obj)
 
 
+class IntBinaryDumper(IntDumper):
+
+    format = Format.BINARY
+
+    _int2_dumper = Int2BinaryDumper(Int2)
+    _int4_dumper = Int4BinaryDumper(Int4)
+    _int8_dumper = Int8BinaryDumper(Int8)
+    _int_numeric_dumper = IntNumericBinaryDumper(IntNumeric)
+
+
 class IntLoader(Loader):
 
     format = Format.TEXT