2010-02-26 03:17:52 +03:00
|
|
|
`psycopg2.extras` -- Miscellaneous goodies for Psycopg 2
|
2010-02-09 07:58:28 +03:00
|
|
|
=============================================================
|
|
|
|
|
|
|
|
.. sectionauthor:: Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
|
|
|
|
|
|
|
.. module:: psycopg2.extras
|
|
|
|
|
2010-02-13 19:06:39 +03:00
|
|
|
.. testsetup::
|
|
|
|
|
|
|
|
import psycopg2.extras
|
2010-02-14 23:32:35 +03:00
|
|
|
from psycopg2.extras import Inet
|
2010-02-13 19:06:39 +03:00
|
|
|
|
|
|
|
create_test_table()
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
This module is a generic place used to hold little helper functions and
|
|
|
|
classes until a better place in the distribution is found.
|
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
|
2013-04-07 05:30:12 +04:00
|
|
|
.. _cursor-subclasses:
|
2010-11-06 04:39:43 +03:00
|
|
|
|
|
|
|
Connection and cursor subclasses
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
A few objects that change the way the results are returned by the cursor or
|
2013-04-07 05:30:12 +04:00
|
|
|
modify the object behavior in some other way. Typically `!cursor` subclasses
|
|
|
|
are passed as *cursor_factory* argument to `~psycopg2.connect()` so that the
|
|
|
|
connection's `~connection.cursor()` method will generate objects of this
|
|
|
|
class. Alternatively a `!cursor` subclass can be used one-off by passing it
|
|
|
|
as the *cursor_factory* argument to the `!cursor()` method.
|
|
|
|
|
|
|
|
If you want to use a `!connection` subclass you can pass it as the
|
|
|
|
*connection_factory* argument of the `!connect()` function.
|
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: Cursor; Dictionary
|
|
|
|
|
|
|
|
.. _dict-cursor:
|
|
|
|
|
2010-11-06 04:39:43 +03:00
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
Dictionary-like cursor
|
2010-11-06 04:39:43 +03:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^
|
2010-02-13 05:10:51 +03:00
|
|
|
|
2013-04-26 12:21:56 +04:00
|
|
|
The dict cursors allow to access to the retrieved records using an interface
|
2010-11-06 04:39:43 +03:00
|
|
|
similar to the Python dictionaries instead of the tuples.
|
2010-02-13 05:10:51 +03:00
|
|
|
|
2010-02-13 19:06:39 +03:00
|
|
|
>>> dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
|
|
|
>>> dict_cur.execute("INSERT INTO test (num, data) VALUES(%s, %s)",
|
|
|
|
... (100, "abc'def"))
|
|
|
|
>>> dict_cur.execute("SELECT * FROM test")
|
|
|
|
>>> rec = dict_cur.fetchone()
|
2010-02-13 05:10:51 +03:00
|
|
|
>>> rec['id']
|
|
|
|
1
|
|
|
|
>>> rec['num']
|
|
|
|
100
|
|
|
|
>>> rec['data']
|
|
|
|
"abc'def"
|
|
|
|
|
2010-02-14 08:02:09 +03:00
|
|
|
The records still support indexing as the original tuple:
|
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
>>> rec[2]
|
|
|
|
"abc'def"
|
|
|
|
|
|
|
|
|
|
|
|
.. autoclass:: DictCursor
|
|
|
|
|
|
|
|
.. autoclass:: DictConnection
|
|
|
|
|
2013-04-07 05:30:12 +04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Not very useful since Psycopg 2.5: you can use `psycopg2.connect`\
|
|
|
|
``(dsn, cursor_factory=DictCursor)`` instead of `!DictConnection`.
|
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
.. autoclass:: DictRow
|
|
|
|
|
|
|
|
|
|
|
|
Real dictionary cursor
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. autoclass:: RealDictCursor
|
|
|
|
|
|
|
|
.. autoclass:: RealDictConnection
|
|
|
|
|
2013-04-07 05:30:12 +04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Not very useful since Psycopg 2.5: you can use `psycopg2.connect`\
|
|
|
|
``(dsn, cursor_factory=RealDictCursor)`` instead of
|
|
|
|
`!RealDictConnection`.
|
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
.. autoclass:: RealDictRow
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-06 04:39:43 +03:00
|
|
|
.. index::
|
|
|
|
pair: Cursor; namedtuple
|
|
|
|
|
|
|
|
`namedtuple` cursor
|
|
|
|
^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. versionadded:: 2.3
|
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
These objects require :py:func:`collections.namedtuple` to be found, so it is
|
2010-11-06 04:39:43 +03:00
|
|
|
available out-of-the-box only from Python 2.6. Anyway, the namedtuple
|
|
|
|
implementation is compatible with previous Python versions, so all you
|
2010-11-06 07:40:47 +03:00
|
|
|
have to do is to `download it`__ and make it available where we
|
2010-11-06 04:39:43 +03:00
|
|
|
expect it to be... ::
|
|
|
|
|
|
|
|
from somewhere import namedtuple
|
|
|
|
import collections
|
|
|
|
collections.namedtuple = namedtuple
|
|
|
|
from psycopg.extras import NamedTupleConnection
|
|
|
|
# ...
|
|
|
|
|
|
|
|
.. __: http://code.activestate.com/recipes/500261-named-tuples/
|
|
|
|
|
|
|
|
.. autoclass:: NamedTupleCursor
|
|
|
|
|
|
|
|
.. autoclass:: NamedTupleConnection
|
|
|
|
|
2013-04-07 05:30:12 +04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Not very useful since Psycopg 2.5: you can use `psycopg2.connect`\
|
|
|
|
``(dsn, cursor_factory=NamedTupleCursor)`` instead of
|
|
|
|
`!NamedTupleConnection`.
|
|
|
|
|
2010-11-06 04:39:43 +03:00
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
.. index::
|
|
|
|
pair: Cursor; Logging
|
|
|
|
|
|
|
|
Logging cursor
|
2010-11-06 04:39:43 +03:00
|
|
|
^^^^^^^^^^^^^^
|
2010-02-13 05:10:51 +03:00
|
|
|
|
|
|
|
.. autoclass:: LoggingConnection
|
|
|
|
:members: initialize,filter
|
|
|
|
|
|
|
|
.. autoclass:: LoggingCursor
|
|
|
|
|
|
|
|
|
|
|
|
.. autoclass:: MinTimeLoggingConnection
|
|
|
|
:members: initialize,filter
|
|
|
|
|
|
|
|
.. autoclass:: MinTimeLoggingCursor
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-06 04:39:43 +03:00
|
|
|
.. index::
|
|
|
|
single: Data types; Additional
|
|
|
|
|
|
|
|
Additional data types
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
2012-09-19 07:12:20 +04:00
|
|
|
.. index::
|
|
|
|
pair: JSON; Data types
|
|
|
|
pair: JSON; Adaptation
|
|
|
|
|
2013-03-18 23:22:31 +04:00
|
|
|
.. _adapt-json:
|
|
|
|
|
2012-09-24 14:51:35 +04:00
|
|
|
JSON_ adaptation
|
|
|
|
^^^^^^^^^^^^^^^^
|
2012-09-19 07:12:20 +04:00
|
|
|
|
2012-12-02 16:04:24 +04:00
|
|
|
.. versionadded:: 2.5
|
2012-09-19 07:12:20 +04:00
|
|
|
|
2012-09-24 14:51:35 +04:00
|
|
|
Psycopg can adapt Python objects to and from the PostgreSQL |pgjson|_ type.
|
|
|
|
With PostgreSQL 9.2 adaptation is available out-of-the-box. To use JSON data
|
|
|
|
with previous database versions (either with the `9.1 json extension`__, but
|
|
|
|
even if you want to convert text fields to JSON) you can use
|
|
|
|
`register_json()`.
|
|
|
|
|
|
|
|
.. __: http://people.planetpostgresql.org/andrew/index.php?/archives/255-JSON-for-PG-9.2-...-and-now-for-9.1!.html
|
|
|
|
|
|
|
|
The Python library used to convert Python objects to JSON depends on the
|
|
|
|
language version: with Python 2.6 and following the :py:mod:`json` module from
|
|
|
|
the standard library is used; with previous versions the `simplejson`_ module
|
|
|
|
is used if available. Note that the last `!simplejson` version supporting
|
|
|
|
Python 2.4 is the 2.0.9.
|
2012-09-19 07:12:20 +04:00
|
|
|
|
|
|
|
.. _JSON: http://www.json.org/
|
|
|
|
.. |pgjson| replace:: :sql:`json`
|
|
|
|
.. _pgjson: http://www.postgresql.org/docs/current/static/datatype-json.html
|
|
|
|
.. _simplejson: http://pypi.python.org/pypi/simplejson/
|
|
|
|
|
2012-09-24 14:51:35 +04:00
|
|
|
In order to pass a Python object to the database as query argument you can use
|
|
|
|
the `Json` adapter::
|
|
|
|
|
|
|
|
curs.execute("insert into mytable (jsondata) values (%s)",
|
|
|
|
[Json({'a': 100})])
|
|
|
|
|
|
|
|
Reading from the database, |pgjson| values will be automatically converted to
|
|
|
|
Python objects.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
You can use `~psycopg2.extensions.register_adapter()` to adapt any Python
|
|
|
|
dictionary to JSON, either registering `Json` or any subclass or factory
|
|
|
|
creating a compatible adapter::
|
|
|
|
|
|
|
|
psycopg2.extensions.register_adapter(dict, psycopg2.extras.Json)
|
|
|
|
|
|
|
|
This setting is global though, so it is not compatible with similar
|
|
|
|
adapters such as the one registered by `register_hstore()`. Any other
|
|
|
|
object supported by JSON can be registered the same way, but this will
|
|
|
|
clobber the default adaptation rule, so be careful to unwanted side
|
|
|
|
effects.
|
|
|
|
|
|
|
|
If you want to customize the adaptation from Python to PostgreSQL you can
|
|
|
|
either provide a custom `!dumps()` function to `!Json`::
|
|
|
|
|
|
|
|
curs.execute("insert into mytable (jsondata) values (%s)",
|
|
|
|
[Json({'a': 100}, dumps=simplejson.dumps)])
|
|
|
|
|
|
|
|
or you can subclass it overriding the `~Json.dumps()` method::
|
|
|
|
|
|
|
|
class MyJson(Json):
|
|
|
|
def dumps(self, obj):
|
|
|
|
return simplejson.dumps(obj)
|
|
|
|
|
|
|
|
curs.execute("insert into mytable (jsondata) values (%s)",
|
|
|
|
[MyJson({'a': 100})])
|
|
|
|
|
|
|
|
Customizing the conversion from PostgreSQL to Python can be done passing a
|
|
|
|
custom `!loads()` function to `register_json()` (or `register_default_json()`
|
|
|
|
for PostgreSQL 9.2). For example, if you want to convert the float values
|
|
|
|
from :sql:`json` into :py:class:`~decimal.Decimal` you can use::
|
|
|
|
|
|
|
|
loads = lambda x: json.loads(x, parse_float=Decimal)
|
|
|
|
psycopg2.extras.register_json(conn, loads=loads)
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-19 07:12:20 +04:00
|
|
|
.. autoclass:: Json
|
2012-09-24 14:51:35 +04:00
|
|
|
|
|
|
|
.. automethod:: dumps
|
2012-09-19 07:12:20 +04:00
|
|
|
|
2012-09-19 07:26:35 +04:00
|
|
|
.. autofunction:: register_json
|
|
|
|
|
2012-09-19 18:49:00 +04:00
|
|
|
.. autofunction:: register_default_json
|
|
|
|
|
2012-09-19 07:12:20 +04:00
|
|
|
|
|
|
|
|
2010-09-27 05:25:09 +04:00
|
|
|
.. index::
|
|
|
|
pair: hstore; Data types
|
|
|
|
pair: dict; Adaptation
|
|
|
|
|
2013-03-18 23:22:31 +04:00
|
|
|
.. _adapt-hstore:
|
|
|
|
|
2010-09-27 05:25:09 +04:00
|
|
|
Hstore data type
|
2010-11-06 04:39:43 +03:00
|
|
|
^^^^^^^^^^^^^^^^
|
2010-09-27 05:25:09 +04:00
|
|
|
|
2010-11-05 15:38:49 +03:00
|
|
|
.. versionadded:: 2.3
|
2010-09-27 05:25:09 +04:00
|
|
|
|
2010-09-28 03:33:34 +04:00
|
|
|
The |hstore|_ data type is a key-value store embedded in PostgreSQL. It has
|
|
|
|
been available for several server versions but with the release 9.0 it has
|
2013-03-06 13:36:32 +04:00
|
|
|
been greatly improved in capacity and usefulness with the addition of many
|
2010-09-28 03:33:34 +04:00
|
|
|
functions. It supports GiST or GIN indexes allowing search by keys or
|
|
|
|
key/value pairs as well as regular BTree indexes for equality, uniqueness etc.
|
2010-09-27 05:25:09 +04:00
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
Psycopg can convert Python `!dict` objects to and from |hstore| structures.
|
|
|
|
Only dictionaries with string/unicode keys and values are supported. `!None`
|
2011-02-25 03:19:49 +03:00
|
|
|
is also allowed as value but not as a key. Psycopg uses a more efficient |hstore|
|
2010-09-28 03:33:34 +04:00
|
|
|
representation when dealing with PostgreSQL 9.0 but previous server versions
|
2011-02-25 03:19:49 +03:00
|
|
|
are supported as well. By default the adapter/typecaster are disabled: they
|
2010-09-28 03:33:34 +04:00
|
|
|
can be enabled using the `register_hstore()` function.
|
|
|
|
|
|
|
|
.. autofunction:: register_hstore
|
2010-09-27 05:25:09 +04:00
|
|
|
|
2012-09-22 05:10:58 +04:00
|
|
|
.. versionchanged:: 2.4
|
|
|
|
added the *oid* parameter. If not specified, the typecaster is
|
|
|
|
installed also if |hstore| is not installed in the :sql:`public`
|
|
|
|
schema.
|
|
|
|
|
|
|
|
.. versionchanged:: 2.4.3
|
|
|
|
added support for |hstore| array.
|
|
|
|
|
|
|
|
|
2010-09-27 05:25:09 +04:00
|
|
|
.. |hstore| replace:: :sql:`hstore`
|
2012-02-28 20:28:07 +04:00
|
|
|
.. _hstore: http://www.postgresql.org/docs/current/static/hstore.html
|
2010-09-27 05:25:09 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
.. index::
|
|
|
|
pair: Composite types; Data types
|
|
|
|
pair: tuple; Adaptation
|
|
|
|
pair: namedtuple; Adaptation
|
|
|
|
|
2013-03-18 23:22:31 +04:00
|
|
|
.. _adapt-composite:
|
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
Composite types casting
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
2011-02-15 14:00:08 +03:00
|
|
|
.. versionadded:: 2.4
|
2011-01-02 02:34:13 +03:00
|
|
|
|
|
|
|
Using `register_composite()` it is possible to cast a PostgreSQL composite
|
2011-12-16 00:11:17 +04:00
|
|
|
type (either created with the |CREATE TYPE|_ command or implicitly defined
|
|
|
|
after a table row type) into a Python named tuple, or into a regular tuple if
|
|
|
|
:py:func:`collections.namedtuple` is not found.
|
2011-01-02 02:34:13 +03:00
|
|
|
|
|
|
|
.. |CREATE TYPE| replace:: :sql:`CREATE TYPE`
|
2012-02-28 20:28:07 +04:00
|
|
|
.. _CREATE TYPE: http://www.postgresql.org/docs/current/static/sql-createtype.html
|
2011-01-02 02:34:13 +03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> cur.execute("CREATE TYPE card AS (value int, suit text);")
|
|
|
|
>>> psycopg2.extras.register_composite('card', cur)
|
|
|
|
<psycopg2.extras.CompositeCaster object at 0x...>
|
|
|
|
|
|
|
|
>>> cur.execute("select (8, 'hearts')::card")
|
|
|
|
>>> cur.fetchone()[0]
|
|
|
|
card(value=8, suit='hearts')
|
|
|
|
|
2012-09-22 05:01:04 +04:00
|
|
|
Nested composite types are handled as expected, provided that the type of the
|
|
|
|
composite components are registered as well.
|
2011-01-02 02:34:13 +03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> cur.execute("CREATE TYPE card_back AS (face card, back text);")
|
|
|
|
>>> psycopg2.extras.register_composite('card_back', cur)
|
|
|
|
<psycopg2.extras.CompositeCaster object at 0x...>
|
|
|
|
|
|
|
|
>>> cur.execute("select ((8, 'hearts'), 'blue')::card_back")
|
|
|
|
>>> cur.fetchone()[0]
|
|
|
|
card_back(face=card(value=8, suit='hearts'), back='blue')
|
|
|
|
|
|
|
|
Adaptation from Python tuples to composite types is automatic instead and
|
|
|
|
requires no adapter registration.
|
|
|
|
|
2012-09-22 05:01:04 +04:00
|
|
|
|
|
|
|
.. _custom-composite:
|
|
|
|
|
|
|
|
.. Note::
|
|
|
|
|
|
|
|
If you want to convert PostgreSQL composite types into something different
|
|
|
|
than a `!namedtuple` you can subclass the `CompositeCaster` overriding
|
|
|
|
`~CompositeCaster.make()`. For example, if you want to convert your type
|
|
|
|
into a Python dictionary you can use::
|
|
|
|
|
|
|
|
>>> class DictComposite(psycopg2.extras.CompositeCaster):
|
|
|
|
... def make(self, values):
|
|
|
|
... return dict(zip(self.attnames, values))
|
|
|
|
|
|
|
|
>>> psycopg2.extras.register_composite('card', cur,
|
|
|
|
... factory=DictComposite)
|
|
|
|
|
|
|
|
>>> cur.execute("select (8, 'hearts')::card")
|
|
|
|
>>> cur.fetchone()[0]
|
|
|
|
{'suit': 'hearts', 'value': 8}
|
|
|
|
|
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
.. autofunction:: register_composite
|
|
|
|
|
2012-09-22 05:08:21 +04:00
|
|
|
.. versionchanged:: 2.4.3
|
|
|
|
added support for array of composite types
|
2012-12-02 16:04:24 +04:00
|
|
|
.. versionchanged:: 2.5
|
2012-09-22 05:08:21 +04:00
|
|
|
added the *factory* parameter
|
|
|
|
|
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
.. autoclass:: CompositeCaster
|
|
|
|
|
2012-09-22 05:01:04 +04:00
|
|
|
.. automethod:: make
|
|
|
|
|
2012-12-02 16:04:24 +04:00
|
|
|
.. versionadded:: 2.5
|
2012-09-22 05:01:04 +04:00
|
|
|
|
|
|
|
Object attributes:
|
|
|
|
|
|
|
|
.. attribute:: name
|
|
|
|
|
|
|
|
The name of the PostgreSQL type.
|
|
|
|
|
2012-09-22 18:10:40 +04:00
|
|
|
.. attribute:: schema
|
|
|
|
|
|
|
|
The schema where the type is defined.
|
|
|
|
|
2012-12-02 16:04:24 +04:00
|
|
|
.. versionadded:: 2.5
|
2012-09-22 18:10:40 +04:00
|
|
|
|
2012-09-22 05:01:04 +04:00
|
|
|
.. attribute:: oid
|
|
|
|
|
|
|
|
The oid of the PostgreSQL type.
|
|
|
|
|
|
|
|
.. attribute:: array_oid
|
|
|
|
|
|
|
|
The oid of the PostgreSQL array type, if available.
|
|
|
|
|
|
|
|
.. attribute:: type
|
|
|
|
|
|
|
|
The type of the Python objects returned. If :py:func:`collections.namedtuple()`
|
|
|
|
is available, it is a named tuple with attributes equal to the type
|
|
|
|
components. Otherwise it is just the `!tuple` object.
|
|
|
|
|
|
|
|
.. attribute:: attnames
|
|
|
|
|
|
|
|
List of component names of the type to be casted.
|
|
|
|
|
|
|
|
.. attribute:: atttypes
|
|
|
|
|
|
|
|
List of component type oids of the type to be casted.
|
2011-01-02 02:34:13 +03:00
|
|
|
|
|
|
|
|
2012-09-24 03:49:44 +04:00
|
|
|
.. index::
|
|
|
|
pair: range; Data types
|
|
|
|
|
2013-03-18 23:22:31 +04:00
|
|
|
.. _adapt-range:
|
|
|
|
|
2012-09-24 03:49:44 +04:00
|
|
|
Range data types
|
|
|
|
^^^^^^^^^^^^^^^^
|
|
|
|
|
2012-12-02 16:04:24 +04:00
|
|
|
.. versionadded:: 2.5
|
2012-09-24 03:49:44 +04:00
|
|
|
|
|
|
|
Psycopg offers a `Range` Python type and supports adaptation between them and
|
|
|
|
PostgreSQL |range|_ types. Builtin |range| types are supported out-of-the-box;
|
|
|
|
user-defined |range| types can be adapted using `register_range()`.
|
|
|
|
|
|
|
|
.. |range| replace:: :sql:`range`
|
|
|
|
.. _range: http://www.postgresql.org/docs/current/static/rangetypes.html
|
|
|
|
|
|
|
|
.. autoclass:: Range
|
|
|
|
|
|
|
|
This Python type is only used to pass and retrieve range values to and
|
|
|
|
from PostgreSQL and doesn't attempt to replicate the PostgreSQL range
|
|
|
|
features: it doesn't perform normalization and doesn't implement all the
|
|
|
|
operators__ supported by the database.
|
|
|
|
|
|
|
|
.. __: http://www.postgresql.org/docs/current/static/functions-range.html#RANGE-OPERATORS-TABLE
|
|
|
|
|
|
|
|
`!Range` objects are immutable, hashable, and support the ``in`` operator
|
|
|
|
(checking if an element is within the range). They can be tested for
|
|
|
|
equivalence but not for ordering. Empty ranges evaluate to `!False` in
|
|
|
|
boolean context, nonempty evaluate to `!True`.
|
|
|
|
|
|
|
|
Although it is possible to instantiate `!Range` objects, the class doesn't
|
|
|
|
have an adapter registered, so you cannot normally pass these instances as
|
|
|
|
query arguments. To use range objects as query arguments you can either
|
|
|
|
use one of the provided subclasses, such as `NumericRange` or create a
|
|
|
|
custom subclass using `register_range()`.
|
|
|
|
|
|
|
|
Object attributes:
|
|
|
|
|
|
|
|
.. autoattribute:: isempty
|
|
|
|
.. autoattribute:: lower
|
|
|
|
.. autoattribute:: upper
|
|
|
|
.. autoattribute:: lower_inc
|
|
|
|
.. autoattribute:: upper_inc
|
|
|
|
.. autoattribute:: lower_inf
|
|
|
|
.. autoattribute:: upper_inf
|
|
|
|
|
|
|
|
|
|
|
|
The following `Range` subclasses map builtin PostgreSQL |range| types to
|
|
|
|
Python objects: they have an adapter registered so their instances can be
|
|
|
|
passed as query arguments. |range| values read from database queries are
|
|
|
|
automatically casted into instances of these classes.
|
|
|
|
|
|
|
|
.. autoclass:: NumericRange
|
|
|
|
.. autoclass:: DateRange
|
|
|
|
.. autoclass:: DateTimeRange
|
|
|
|
.. autoclass:: DateTimeTZRange
|
|
|
|
|
2013-06-18 18:00:30 +04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Python lacks a representation for :sql:`infinity` date so Psycopg converts
|
|
|
|
the value to `date.max` and such. When written into the database these
|
|
|
|
dates will assume their literal value (e.g. :sql:`9999-12-31` instead of
|
|
|
|
:sql:`infinity`). Check :ref:`infinite-dates-handling` for an example of
|
|
|
|
an alternative adapter to map `date.max` to :sql:`infinity`. An
|
|
|
|
alternative dates adapter will be used automatically by the `DateRange`
|
|
|
|
adapter and so on.
|
|
|
|
|
|
|
|
|
2012-09-24 03:49:44 +04:00
|
|
|
Custom |range| types (created with |CREATE TYPE|_ :sql:`... AS RANGE`) can be
|
|
|
|
adapted to a custom `Range` subclass:
|
|
|
|
|
|
|
|
.. autofunction:: register_range
|
|
|
|
|
|
|
|
.. autoclass:: RangeCaster
|
|
|
|
|
|
|
|
Object attributes:
|
|
|
|
|
|
|
|
.. attribute:: range
|
|
|
|
|
|
|
|
The `!Range` subclass adapted.
|
|
|
|
|
|
|
|
.. attribute:: adapter
|
|
|
|
|
|
|
|
The `~psycopg2.extensions.ISQLQuote` responsible to adapt `!range`.
|
|
|
|
|
|
|
|
.. attribute:: typecaster
|
|
|
|
|
|
|
|
The object responsible for casting.
|
|
|
|
|
|
|
|
.. attribute:: array_typecaster
|
|
|
|
|
2012-09-24 15:13:46 +04:00
|
|
|
The object responsible to cast arrays, if available, else `!None`.
|
2012-09-24 03:49:44 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
.. index::
|
|
|
|
pair: UUID; Data types
|
|
|
|
|
2013-03-18 23:22:31 +04:00
|
|
|
.. _adapt-uuid:
|
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
UUID data type
|
2010-11-06 04:39:43 +03:00
|
|
|
^^^^^^^^^^^^^^
|
2010-02-13 05:10:51 +03:00
|
|
|
|
|
|
|
.. versionadded:: 2.0.9
|
|
|
|
.. versionchanged:: 2.0.13 added UUID array support.
|
|
|
|
|
2010-02-14 08:02:09 +03:00
|
|
|
.. doctest::
|
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
>>> psycopg2.extras.register_uuid()
|
2010-02-13 19:06:39 +03:00
|
|
|
<psycopg2._psycopg.type object at 0x...>
|
2010-02-14 08:02:09 +03:00
|
|
|
|
2010-02-13 19:06:39 +03:00
|
|
|
>>> # Python UUID can be used in SQL queries
|
|
|
|
>>> import uuid
|
2010-02-14 08:02:09 +03:00
|
|
|
>>> my_uuid = uuid.UUID('{12345678-1234-5678-1234-567812345678}')
|
|
|
|
>>> psycopg2.extensions.adapt(my_uuid).getquoted()
|
|
|
|
"'12345678-1234-5678-1234-567812345678'::uuid"
|
|
|
|
|
2010-02-13 19:06:39 +03:00
|
|
|
>>> # PostgreSQL UUID are transformed into Python UUID objects.
|
2010-02-13 05:10:51 +03:00
|
|
|
>>> cur.execute("SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid")
|
|
|
|
>>> cur.fetchone()[0]
|
|
|
|
UUID('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11')
|
|
|
|
|
|
|
|
|
|
|
|
.. autofunction:: register_uuid
|
|
|
|
|
|
|
|
.. autoclass:: UUID_adapter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: INET; Data types
|
|
|
|
|
2010-02-14 23:32:35 +03:00
|
|
|
:sql:`inet` data type
|
2010-11-06 04:39:43 +03:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^
|
2010-02-14 23:32:35 +03:00
|
|
|
|
|
|
|
.. versionadded:: 2.0.9
|
2012-02-24 03:56:55 +04:00
|
|
|
.. versionchanged:: 2.4.5 added inet array support.
|
2010-02-14 23:32:35 +03:00
|
|
|
|
|
|
|
.. doctest::
|
2010-02-13 05:10:51 +03:00
|
|
|
|
|
|
|
>>> psycopg2.extras.register_inet()
|
2010-02-13 19:06:39 +03:00
|
|
|
<psycopg2._psycopg.type object at 0x...>
|
2010-02-14 23:32:35 +03:00
|
|
|
|
|
|
|
>>> cur.mogrify("SELECT %s", (Inet('127.0.0.1/32'),))
|
|
|
|
"SELECT E'127.0.0.1/32'::inet"
|
|
|
|
|
|
|
|
>>> cur.execute("SELECT '192.168.0.1/24'::inet")
|
2010-02-13 05:10:51 +03:00
|
|
|
>>> cur.fetchone()[0].addr
|
2010-02-14 23:32:35 +03:00
|
|
|
'192.168.0.1/24'
|
|
|
|
|
2010-02-13 05:10:51 +03:00
|
|
|
|
2012-02-24 03:56:55 +04:00
|
|
|
.. autofunction:: register_inet
|
2010-02-13 05:10:51 +03:00
|
|
|
|
|
|
|
.. autoclass:: Inet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: Time zones; Fractional
|
|
|
|
|
|
|
|
Fractional time zones
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
.. autofunction:: register_tstz_w_secs
|
|
|
|
|
2010-02-13 08:49:34 +03:00
|
|
|
.. versionadded:: 2.0.9
|
|
|
|
|
2010-11-01 02:57:40 +03:00
|
|
|
.. versionchanged:: 2.2.2
|
2010-05-20 05:10:33 +04:00
|
|
|
function is no-op: see :ref:`tz-handling`.
|
|
|
|
|
2010-04-04 06:10:18 +04:00
|
|
|
.. index::
|
|
|
|
pair: Example; Coroutine;
|
|
|
|
|
2010-11-06 04:39:43 +03:00
|
|
|
|
|
|
|
|
2010-04-04 06:10:18 +04:00
|
|
|
Coroutine support
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
.. autofunction:: wait_select(conn)
|
|
|
|
|