Jan 30, 2016, 2:55:18 PM (3 years ago)

Port type cache and typecasting from pgdb to pg

So far, the typecasting in the classic module was been only done by
the C extension module and was not extensible through typecasting
functions in Python. This has now been made extensible by adding
a cast hook to the C extension module which has been hooked up to
a new type cache object that holds information on the types and the
associated typecast functions. All of this works very similar to the
pgdb module now, except that the basic types are still handled by
the C extension module and the Python typecast functions are only
called via the hook for types which are not supported internally.

Also added tests and a chapter on the type cache in the documentation,
and cleaned up the error messages in the C extension module.

1 edited


  • trunk/docs/contents/pg/module.rst

    r781 r798  
    453453.. versionadded:: 5.0
     455get/set_typecast -- custom typecasting
     458PyGreSQL uses typecast functions to cast the raw data coming from the
     459database to Python objects suitable for the particular database type.
     460These functions take a single string argument that represents the data
     461to be casted and must return the casted value.
     463PyGreSQL provides through its C extension module basic typecast functions
     464for the common database types, but if you want to add more typecast functions,
     465you can set these using the following functions.
     467.. method:: get_typecast(typ)
     469    Get the global cast function for the given database type
     471    :param str typ: PostgreSQL type name
     472    :returns: the typecast function for the specified type
     473    :rtype: function or None
     475.. versionadded:: 5.0
     477.. method:: set_typecast(typ, cast)
     479    Set a global typecast function for the given database type(s)
     481    :param typ: PostgreSQL type name or list of type names
     482    :type typ: str or list
     483    :param cast: the typecast function to be set for the specified type(s)
     484    :type typ: str or int
     486.. versionadded:: 5.0
     488Note that database connections cache types and their cast functions using
     489connection specific :class:`DbTypes` objects.  You can also get, set and
     490reset typecast functions on the connection level using the methods
     491:meth:`DbTypes.get_typecast`, :meth:`DbTypes.set_typecast` and
     492:meth:`DbTypes.reset_typecast` of the :attr:`DB.dbtypes` object.  This will
     493not affect other connections or future connections.  In order to be sure
     494a global change is picked up by a running connection, you must reopen it or
     495call :meth:`DbTypes.reset_typecast` on the :attr:`DB.dbtypes` object.
     497Also note that the typecasting for all of the basic types happens already
     498in the C extension module.  The typecast functions that can be set with
     499the above methods are only called for the types that are not already
     500supported by the C extension module.
    456503Module constants
Note: See TracChangeset for help on using the changeset viewer.