Skip to content

Commit 63b6595

Browse files
committed
docs: Document get_parameters, get_attributes etc
1 parent 8edc203 commit 63b6595

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

asyncpg/prepared_stmt.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,67 @@ def __init__(self, connection, query, state):
2424
self._last_status = None
2525

2626
def get_query(self) -> str:
27-
"""Return the text of the query for this prepared statement."""
27+
"""Return the text of the query for this prepared statement.
28+
29+
Example::
30+
31+
stmt = await connection.prepare('SELECT $1::int')
32+
assert stmt.get_query() == "SELECT $1::int"
33+
"""
2834
return self._query
2935

3036
def get_statusmsg(self) -> str:
31-
"""Return the status of the executed command."""
37+
"""Return the status of the executed command.
38+
39+
Example::
40+
41+
stmt = await connection.prepare('CREATE TABLE mytab (a int)')
42+
await stmt.fetch()
43+
assert stmt.get_statusmsg() == "CREATE TABLE"
44+
"""
3245
if self._last_status is None:
3346
return self._last_status
3447
return self._last_status.decode()
3548

3649
def get_parameters(self):
50+
"""Return a description of statement parameters types.
51+
52+
:return: A tuple of :class:`asyncpg.types.Type`.
53+
54+
Example::
55+
56+
stmt = await connection.prepare('SELECT ($1::int, $2::text)')
57+
print(stmt.get_parameters())
58+
59+
# Will print:
60+
# (Type(oid=23, name='int4', kind='scalar', schema='pg_catalog'),
61+
# Type(oid=25, name='text', kind='scalar', schema='pg_catalog'))
62+
"""
3763
self.__check_open()
3864
return self._state._get_parameters()
3965

4066
def get_attributes(self):
67+
"""Return a description of relation attributes (columns).
68+
69+
:return: A tuple of :class:`asyncpg.types.Attribute`.
70+
71+
Example::
72+
73+
st = await self.con.prepare('''
74+
SELECT typname, typnamespace FROM pg_type
75+
''')
76+
print(st.get_attributes())
77+
78+
# Will print:
79+
# (Attribute(
80+
# name='typname',
81+
# type=Type(oid=19, name='name', kind='scalar',
82+
# schema='pg_catalog')),
83+
# Attribute(
84+
# name='typnamespace',
85+
# type=Type(oid=26, name='oid', kind='scalar',
86+
# schema='pg_catalog')))
87+
"""
4188
self.__check_open()
4289
return self._state._get_attributes()
4390

asyncpg/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@
1515

1616

1717
Type = collections.namedtuple('Type', ['oid', 'name', 'kind', 'schema'])
18+
Type.__doc__ = 'Database data type.'
19+
Type.oid.__doc__ = 'OID of the type.'
20+
Type.name.__doc__ = 'Type name. For example "int2".'
21+
Type.kind.__doc__ = \
22+
'Type kind. Can be "scalar", "array", "composite" or "range".'
23+
Type.schema.__doc__ = 'Name of the database schema that defines the type.'
1824

1925

2026
Attribute = collections.namedtuple('Attribute', ['name', 'type'])
27+
Attribute.__doc__ = 'Database relation attribute.'
28+
Attribute.name.__doc__ = 'Attribute name.'
29+
Attribute.type.__doc__ = 'Attribute data type :class:`asyncpg.types.Type`.'
2130

2231

2332
class Range:

docs/api/index.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,12 @@ of values either by a numeric index or by a field name:
282282
.. method:: items()
283283

284284
Return an iterator over ``(field, value)`` pairs.
285+
286+
Introspection
287+
=============
288+
289+
.. autoclass:: asyncpg.types.Type()
290+
:members:
291+
292+
.. autoclass:: asyncpg.types.Attribute()
293+
:members:

0 commit comments

Comments
 (0)