File: query.rst

package info (click to toggle)
pygresql 1%3A5.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,468 kB
  • sloc: python: 14,213; ansic: 5,231; makefile: 164
file content (382 lines) | stat: -rw-r--r-- 13,290 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
Query methods
=============

.. py:currentmodule:: pg

.. class:: Query

The :class:`Query` object returned by :meth:`Connection.query` and
:meth:`DB.query` can be used as an iterable returning rows as tuples.
You can also directly access row tuples using their index, and get
the number of rows with the :func:`len` function.
The :class:`Query` class also provides the following methods for accessing
the results of the query:

getresult -- get query values as list of tuples
-----------------------------------------------

.. method:: Query.getresult()

    Get query values as list of tuples

    :returns: result values as a list of tuples
    :rtype: list
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

This method returns query results as a list of tuples.
More information about this result may be accessed using
:meth:`Query.listfields`, :meth:`Query.fieldname`
and :meth:`Query.fieldnum` methods.

Note that since PyGreSQL 5.0 this method will return the values of array
type columns as Python lists.

Since PyGreSQL 5.1 the :class:`Query` can be also used directly as
an iterable sequence, i.e. you can iterate over the :class:`Query`
object to get the same tuples as returned by :meth:`Query.getresult`.
This is slightly more efficient than getting the full list of results,
but note that the full result is always fetched from the server anyway
when the query is executed.

You can also call :func:`len` on a query to find the number of rows
in the result, and access row tuples using their index directly on
the :class:`Query` object.

dictresult/dictiter -- get query values as dictionaries
-------------------------------------------------------

.. method:: Query.dictresult()

    Get query values as list of dictionaries

    :returns: result values as a list of dictionaries
    :rtype: list
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

This method returns query results as a list of dictionaries which have
the field names as keys.

If the query has duplicate field names, you will get the value for the
field with the highest index in the query.

Note that since PyGreSQL 5.0 this method will return the values of array
type columns as Python lists.

.. method:: Query.dictiter()

    Get query values as iterable of dictionaries

    :returns: result values as an iterable of dictionaries
    :rtype: iterable
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

This method returns query results as an iterable of dictionaries which have
the field names as keys. This is slightly more efficient than getting the full
list of results as dictionaries, but note that the full result is always
fetched from the server anyway when the query is executed.

If the query has duplicate field names, you will get the value for the
field with the highest index in the query.

.. versionadded:: 5.1

namedresult/namediter -- get query values as named tuples
---------------------------------------------------------

.. method:: Query.namedresult()

    Get query values as list of named tuples

    :returns: result values as a list of named tuples
    :rtype: list
    :raises TypeError: too many (any) parameters
    :raises TypeError: named tuples not supported
    :raises MemoryError: internal memory error

This method returns query results as a list of named tuples with
proper field names.

Column names in the database that are not valid as field names for
named tuples (particularly, names starting with an underscore) are
automatically renamed to valid positional names.

Note that since PyGreSQL 5.0 this method will return the values of array
type columns as Python lists.

.. versionadded:: 4.1

.. method:: Query.namediter()

    Get query values as iterable of named tuples

    :returns: result values as an iterable of named tuples
    :rtype: iterable
    :raises TypeError: too many (any) parameters
    :raises TypeError: named tuples not supported
    :raises MemoryError: internal memory error

This method returns query results as an iterable of named tuples with
proper field names. This is slightly more efficient than getting the full
list of results as named tuples, but note that the full result is always
fetched from the server anyway when the query is executed.

Column names in the database that are not valid as field names for
named tuples (particularly, names starting with an underscore) are
automatically renamed to valid positional names.

.. versionadded:: 5.1

scalarresult/scalariter -- get query values as scalars
------------------------------------------------------

.. method:: Query.scalarresult()

    Get first fields from query result as list of scalar values

    :returns: first fields from result as a list of scalar values
    :rtype: list
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

This method returns the first fields from the query results as a list of
scalar values in the order returned by the server.

.. versionadded:: 5.1

.. method:: Query.scalariter()

    Get first fields from query result as iterable of scalar values

    :returns: first fields from result as an iterable of scalar values
    :rtype: list
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

This method returns the first fields from the query results as an iterable
of scalar values in the order returned by the server. This is slightly more
efficient than getting the full list of results as rows or scalar values,
but note that the full result is always fetched from the server anyway when
the query is executed.

.. versionadded:: 5.1

one/onedict/onenamed/onescalar -- get one result of a query
-----------------------------------------------------------

.. method:: Query.one()

    Get one row from the result of a query as a tuple

    :returns: next row from the query results as a tuple of fields
    :rtype: tuple or None
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

Returns only one row from the result as a tuple of fields.

This method can be called multiple times to return more rows.
It returns None if the result does not contain one more row.

.. versionadded:: 5.1

.. method:: Query.onedict()

    Get one row from the result of a query as a dictionary

    :returns: next row from the query results as a dictionary
    :rtype: dict or None
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

Returns only one row from the result as a dictionary with the field names
used as the keys.

This method can be called multiple times to return more rows.
It returns None if the result does not contain one more row.

.. versionadded:: 5.1

.. method:: Query.onenamed()

    Get one row from the result of a query as named tuple

    :returns: next row from the query results as a named tuple
    :rtype: named tuple or None
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

Returns only one row from the result as a named tuple with proper field names.

Column names in the database that are not valid as field names for
named tuples (particularly, names starting with an underscore) are
automatically renamed to valid positional names.

This method can be called multiple times to return more rows.
It returns None if the result does not contain one more row.

.. versionadded:: 5.1

.. method:: Query.onescalar()

    Get one row from the result of a query as scalar value

    :returns: next row from the query results as a scalar value
    :rtype: type of first field or None
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

Returns the first field of the next row from the result as a scalar value.

This method can be called multiple times to return more rows as scalars.
It returns None if the result does not contain one more row.

.. versionadded:: 5.1

single/singledict/singlenamed/singlescalar -- get single result of a query
--------------------------------------------------------------------------

.. method:: Query.single()

    Get single row from the result of a query as a tuple

    :returns: single row from the query results as a tuple of fields
    :rtype: tuple
	:raises InvalidResultError: result does not have exactly one row
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

Returns a single row from the result as a tuple of fields.

This method returns the same single row when called multiple times.
It raises an :exc:`pg.InvalidResultError` if the result does not have exactly
one row. More specifically, this will be of type :exc:`pg.NoResultError` if it
is empty and of type :exc:`pg.MultipleResultsError` if it has multiple rows.

.. versionadded:: 5.1

.. method:: Query.singledict()

    Get single row from the result of a query as a dictionary

    :returns: single row from the query results as a dictionary
    :rtype: dict
	:raises InvalidResultError: result does not have exactly one row
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

Returns a single row from the result as a dictionary with the field names
used as the keys.

This method returns the same single row when called multiple times.
It raises an :exc:`pg.InvalidResultError` if the result does not have exactly
one row. More specifically, this will be of type :exc:`pg.NoResultError` if it
is empty and of type :exc:`pg.MultipleResultsError` if it has multiple rows.

.. versionadded:: 5.1

.. method:: Query.singlenamed()

    Get single row from the result of a query as named tuple

    :returns: single row from the query results as a named tuple
    :rtype: named tuple
	:raises InvalidResultError: result does not have exactly one row
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

Returns single row from the result as a named tuple with proper field names.

Column names in the database that are not valid as field names for
named tuples (particularly, names starting with an underscore) are
automatically renamed to valid positional names.

This method returns the same single row when called multiple times.
It raises an :exc:`pg.InvalidResultError` if the result does not have exactly
one row. More specifically, this will be of type :exc:`pg.NoResultError` if it
is empty and of type :exc:`pg.MultipleResultsError` if it has multiple rows.

.. versionadded:: 5.1

.. method:: Query.singlescalar()

    Get single row from the result of a query as scalar value

    :returns: single row from the query results as a scalar value
    :rtype: type of first field
	:raises InvalidResultError: result does not have exactly one row
    :raises TypeError: too many (any) parameters
    :raises MemoryError: internal memory error

Returns the first field of a single row from the result as a scalar value.

This method returns the same single row as scalar when called multiple times.
It raises an :exc:`pg.InvalidResultError` if the result does not have exactly
one row. More specifically, this will be of type :exc:`pg.NoResultError` if it
is empty and of type :exc:`pg.MultipleResultsError` if it has multiple rows.

.. versionadded:: 5.1

listfields -- list fields names of previous query result
--------------------------------------------------------

.. method:: Query.listfields()

    List fields names of previous query result

    :returns: field names
    :rtype: list
    :raises TypeError: too many parameters

This method returns the list of field names defined for the
query result. The fields are in the same order as the result values.

fieldname, fieldnum -- field name/number conversion
---------------------------------------------------

.. method:: Query.fieldname(num)

    Get field name from its number

    :param int num: field number
    :returns: field name
    :rtype: str
    :raises TypeError: invalid connection, bad parameter type, or too many parameters
    :raises ValueError: invalid field number

This method allows to find a field name from its rank number. It can be
useful for displaying a result. The fields are in the same order as the
result values.

.. method:: Query.fieldnum(name)

    Get field number from its name

    :param str name: field name
    :returns: field number
    :rtype: int
    :raises TypeError: invalid connection, bad parameter type, or too many parameters
    :raises ValueError: unknown field name

This method returns a field number given its name. It can be used to
build a function that converts result list strings to their correct
type, using a hardcoded table definition. The number returned is the
field rank in the query result.

ntuples -- return number of tuples in query object
--------------------------------------------------

.. method:: Query.ntuples()

    Return number of tuples in query object

    :returns: number of tuples in :class:`Query`
    :rtype: int
    :raises TypeError: Too many arguments.

This method returns the number of tuples in the query result.

.. deprecated:: 5.1
   You can use the normal :func:`len` function instead.