:type: `~psycopg.types.TypesRegistry`
.. automethod:: get_dumper
+ .. automethod:: get_dumper_by_oid
.. automethod:: get_loader
Note that PostgreSQL is particularly finicky when loading data in binary mode
and will apply *no cast rule*. This means that e.g. passing the value 100 to
an `integer` column will fail because Psycopg will pass it as a `smallint`
-value. You can work around the problem by registering the right binary
-`~adapt.Dumper` on the cursor (see :ref:`adaptation`) or using the right data
-wrapper (e.g. `~psycopg.types.numeric.Int4`).
+value. You can work around the problem using the `~Copy.set_types()` method of
+the `!Copy` object and specify carefully the types to dump.
.. _copy-out-row:
will be possible to register it without importing it before. In this
case it should be the fully qualified name of the object (e.g.
``"uuid.UUID"``).
+
+ If *cls* is None, only use the dumper when looking up using
+ `get_dumper_by_oid()`, which happens when we know the Postgres type to
+ adapt to, but not the Python type that will be adapted (e.g. in COPY
+ after using `~psycopg.Copy.set_types()`).
+
"""
if not (cls is None or isinstance(cls, (str, type))):
raise TypeError(
"""
Return the dumper class for the given type and format.
- Raise ProgrammingError if a class is not available.
+ Raise `~psycopg.ProgrammingError` if a class is not available.
+
+ :param cls: The class to adapt.
+ :param format: The format to dump to. If `~psycopg.adapt.PyFormat.AUTO`,
+ use the last one of the dumpers registered on *cls*.
"""
try:
dmap = self._dumpers[format]
"""
Return the dumper class for the given oid and format.
- Raise ProgrammingError if a class is not available.
+ Raise `~psycopg.ProgrammingError` if a class is not available.
+
+ :param oid: The oid of the type to dump to.
+ :param format: The format to dump to.
"""
try:
dmap = self._dumpers_by_oid[format]
"""
Return the loader class for the given oid and format.
- Return None if not found.
+ Return `!None` if not found.
+
+ :param oid: The oid of the type to load.
+ :param format: The format to load from.
"""
return self._loaders[format].get(oid)
def set_types(self, types: Sequence[Union[int, str]]) -> None:
"""
- Set the types expected out of a :sql:`COPY TO` operation.
-
- Without setting the types, the data from :sql:`COPY TO` will be
- returned as unparsed strings or bytes.
+ Set the types expected in a COPY operation.
The types must be specified as a sequence of oid or PostgreSQL type
names (e.g. ``int4``, ``timestamptz[]``).
+
+ This operation overcomes the lack of metadata returned by PostgreSQL
+ when a COPY operation begins:
+
+ - On :sql:`COPY TO`, `!set_types()` allows to specify what types the
+ operation returns. If `!set_types()` is not used, the data will be
+ reurned as unparsed strings or bytes instead of Python objects.
+
+ - On :sql:`COPY FROM`, `!set_types()` allows to choose what type the
+ database expects. This is especially useful in binary copy, because
+ PostgreSQL will apply no cast rule.
+
"""
registry = self.cursor.adapters.types
oids = [