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
|
.. _psycopg.rows:
`rows` -- row factory implementations
=====================================
.. module:: psycopg.rows
The module exposes a few generic `~psycopg.RowFactory` implementation, which
can be used to retrieve data from the database in more complex structures than
the basic tuples.
Check out :ref:`row-factory-create` for information about how to use these objects.
.. autofunction:: tuple_row
Example::
>>> cur = conn.cursor(row_factory=tuple_row)
>>> cur.execute("SELECT 10 AS foo, 'hello' AS bar").fetchone()
(10, 'hello')
.. autofunction:: dict_row
Example::
>>> cur = conn.cursor(row_factory=dict_row)
>>> cur.execute("SELECT 10 AS foo, 'hello' AS bar").fetchone()
{'foo': 10, 'bar': 'hello'}
.. autofunction:: namedtuple_row
Example::
>>> cur = conn.cursor(row_factory=namedtuple_row)
>>> cur.execute("SELECT 10 AS foo, 'hello' AS bar").fetchone()
Row(foo=10, bar='hello')
.. autofunction:: scalar_row
Example::
>>> cur = conn.cursor(row_factory=scalar_row)
>>> cur.execute("SELECT 10 AS foo, 'hello' AS bar").fetchone()
10
.. versionadded:: 3.2
.. autofunction:: class_row
This is not a row factory, but rather a factory of row factories.
Specifying `!row_factory=class_row(MyClass)` will create connections and
cursors returning `!MyClass` objects on fetch.
Example::
from dataclasses import dataclass
import psycopg
from psycopg.rows import class_row
@dataclass
class Person:
first_name: str
last_name: str
age: int = None
conn = psycopg.connect()
cur = conn.cursor(row_factory=class_row(Person))
cur.execute("select 'John' as first_name, 'Smith' as last_name").fetchone()
# Person(first_name='John', last_name='Smith', age=None)
.. autofunction:: args_row
.. autofunction:: kwargs_row
Formal rows protocols
---------------------
These objects can be used to describe your own rows adapter for static typing
checks, such as mypy_.
.. _mypy: https://mypy.readthedocs.io/
.. autoclass:: psycopg.rows.RowMaker()
.. method:: __call__(values: Sequence[Any]) -> Row
Convert a sequence of values from the database to a finished object.
.. autoclass:: psycopg.rows.RowFactory()
.. method:: __call__(cursor: Cursor[Row]) -> RowMaker[Row]
Inspect the result on a cursor and return a `RowMaker` to convert rows.
.. autoclass:: psycopg.rows.AsyncRowFactory()
.. autoclass:: psycopg.rows.BaseRowFactory()
Note that it's easy to implement an object implementing both `!RowFactory` and
`!AsyncRowFactory`: usually, everything you need to implement a row factory is
to access the cursor's `~psycopg.Cursor.description`, which is provided by
both the cursor flavours.
|