.. autoclass:: Dumper(src, context=None)
+ This is an abstract base class: subclasses *must* implement the `dump()`
+ method. They *may* implement `oid` (as attribute or property) in order to
+ override the oid type oid; if not PostgreSQL will try to infer the type
+ from the context, but this may fail in some contexts and may require a
+ cast.
+
:param src: The type that will be managed by this dumper.
:type src: type
:param context: The context where the transformation is performed. If not
.. autoattribute:: oid
:annotation: int
+ .. admonition:: todo
+
+ Document how to find type OIDs in a database.
+
.. automethod:: register(src, context=None, format=Format.TEXT)
You should call this method on the `Dumper` subclass you create,
.. autoclass:: Loader(oid, context=None)
+ This is an abstract base class: subclasses *must* implement the `load()`
+ method.
+
:param oid: The type that will be managed by this dumper.
:type oid: int
:param context: The context where the transformation is performed. If not
# Copyright (C) 2020 The Psycopg Team
+from abc import ABC, abstractmethod
from typing import Any, cast, Callable, Optional, Type, Union
from . import pq
TEXT_OID = builtins["text"].oid
-class Dumper:
+class Dumper(ABC):
"""
Convert Python object of the type *src* to PostgreSQL representation.
"""
self.context = context
self.connection = _connection_from_context(context)
+ @abstractmethod
def dump(self, obj: Any) -> bytes:
"""Convert the object *obj* to PostgreSQL representation."""
- raise NotImplementedError()
+ ...
def quote(self, obj: Any) -> bytes:
"""Convert the object *obj* to escaped representation."""
return binary_
-class Loader:
+class Loader(ABC):
"""
Convert PostgreSQL objects with OID *oid* to Python objects.
"""
self.context = context
self.connection = _connection_from_context(context)
+ @abstractmethod
def load(self, data: bytes) -> Any:
"""Convert a PostgreSQL value to a Python object."""
- raise NotImplementedError()
+ ...
@classmethod
def register(
quote(), so it can be used in sql composition.
"""
+ def dump(self, obj: None) -> bytes:
+ raise NotImplementedError("NULL is passed to Postgres in other ways")
+
def quote(self, obj: None) -> bytes:
return b"NULL"