~james-w/ubuntu/lucid/psycopg2/precise-backport

« back to all changes in this revision

Viewing changes to doc/html/_sources/extras.txt

  • Committer: Mikhail Turov
  • Date: 2010-07-28 20:30:01 UTC
  • mfrom: (5.1.9 sid)
  • Revision ID: groldster@gmail.com-20100728203001-u1dv46tnd3s02ejg
Tags: 2.2.1-1ubuntu1
releasing version 2.2.1-1ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
`psycopg2.extras` -- Miscellaneous goodies for Psycopg 2
 
2
=============================================================
 
3
 
 
4
.. sectionauthor:: Daniele Varrazzo <daniele.varrazzo@gmail.com>
 
5
 
 
6
.. module:: psycopg2.extras
 
7
 
 
8
.. testsetup::
 
9
 
 
10
    import psycopg2.extras
 
11
    from psycopg2.extras import Inet
 
12
 
 
13
    create_test_table()
 
14
 
 
15
This module is a generic place used to hold little helper functions and
 
16
classes until a better place in the distribution is found.
 
17
 
 
18
 
 
19
.. index::
 
20
    pair: Cursor; Dictionary
 
21
 
 
22
.. _dict-cursor:
 
23
 
 
24
Dictionary-like cursor
 
25
----------------------
 
26
 
 
27
The dict cursors allow to access to the retrieved records using an iterface
 
28
similar to the Python dictionaries instead of the tuples. You can use it
 
29
either passing `DictConnection` as `connection_factory` argument
 
30
to the `~psycopg2.connect()` function or passing `DictCursor` as
 
31
the `!cursor_factory` argument to the `~connection.cursor()` method
 
32
of a regular `connection`.
 
33
 
 
34
    >>> dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
 
35
    >>> dict_cur.execute("INSERT INTO test (num, data) VALUES(%s, %s)",
 
36
    ...                  (100, "abc'def"))
 
37
    >>> dict_cur.execute("SELECT * FROM test")
 
38
    >>> rec = dict_cur.fetchone()
 
39
    >>> rec['id']
 
40
    1
 
41
    >>> rec['num']
 
42
    100
 
43
    >>> rec['data']
 
44
    "abc'def"
 
45
 
 
46
The records still support indexing as the original tuple:
 
47
 
 
48
    >>> rec[2]
 
49
    "abc'def"
 
50
 
 
51
 
 
52
.. autoclass:: DictCursor
 
53
 
 
54
.. autoclass:: DictConnection
 
55
 
 
56
.. autoclass:: DictRow
 
57
 
 
58
 
 
59
Real dictionary cursor
 
60
^^^^^^^^^^^^^^^^^^^^^^
 
61
 
 
62
.. autoclass:: RealDictCursor
 
63
 
 
64
.. autoclass:: RealDictConnection
 
65
 
 
66
.. autoclass:: RealDictRow
 
67
 
 
68
 
 
69
 
 
70
.. index::
 
71
    pair: Cursor; Logging
 
72
 
 
73
Logging cursor
 
74
--------------
 
75
 
 
76
.. autoclass:: LoggingConnection
 
77
    :members: initialize,filter
 
78
 
 
79
.. autoclass:: LoggingCursor
 
80
 
 
81
 
 
82
.. autoclass:: MinTimeLoggingConnection
 
83
    :members: initialize,filter
 
84
 
 
85
.. autoclass:: MinTimeLoggingCursor
 
86
 
 
87
 
 
88
 
 
89
.. index::
 
90
    pair: UUID; Data types
 
91
 
 
92
UUID data type
 
93
--------------
 
94
 
 
95
.. versionadded:: 2.0.9
 
96
.. versionchanged:: 2.0.13 added UUID array support.
 
97
 
 
98
.. doctest::
 
99
 
 
100
    >>> psycopg2.extras.register_uuid()
 
101
    <psycopg2._psycopg.type object at 0x...>
 
102
 
 
103
    >>> # Python UUID can be used in SQL queries
 
104
    >>> import uuid
 
105
    >>> my_uuid = uuid.UUID('{12345678-1234-5678-1234-567812345678}')
 
106
    >>> psycopg2.extensions.adapt(my_uuid).getquoted()
 
107
    "'12345678-1234-5678-1234-567812345678'::uuid"
 
108
 
 
109
    >>> # PostgreSQL UUID are transformed into Python UUID objects.
 
110
    >>> cur.execute("SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid")
 
111
    >>> cur.fetchone()[0]
 
112
    UUID('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11')
 
113
 
 
114
 
 
115
.. autofunction:: register_uuid
 
116
 
 
117
.. autoclass:: UUID_adapter
 
118
 
 
119
 
 
120
 
 
121
.. index::
 
122
    pair: INET; Data types
 
123
 
 
124
:sql:`inet` data type
 
125
----------------------
 
126
 
 
127
.. versionadded:: 2.0.9
 
128
 
 
129
.. doctest::
 
130
 
 
131
    >>> psycopg2.extras.register_inet()
 
132
    <psycopg2._psycopg.type object at 0x...>
 
133
 
 
134
    >>> cur.mogrify("SELECT %s", (Inet('127.0.0.1/32'),))
 
135
    "SELECT E'127.0.0.1/32'::inet"
 
136
 
 
137
    >>> cur.execute("SELECT '192.168.0.1/24'::inet")
 
138
    >>> cur.fetchone()[0].addr
 
139
    '192.168.0.1/24'
 
140
 
 
141
 
 
142
.. autofunction:: register_inet()
 
143
 
 
144
.. autoclass:: Inet
 
145
 
 
146
 
 
147
 
 
148
.. index::
 
149
    single: Time zones; Fractional
 
150
 
 
151
Fractional time zones
 
152
---------------------
 
153
 
 
154
.. autofunction:: register_tstz_w_secs
 
155
 
 
156
    .. versionadded:: 2.0.9
 
157
 
 
158
.. index::
 
159
   pair: Example; Coroutine;
 
160
 
 
161
Coroutine support
 
162
-----------------
 
163
 
 
164
.. autofunction:: wait_select(conn)
 
165