Skip to content

Commit 16a0a4f

Browse files
committed
Document more of asyncpg public API
1 parent 685279f commit 16a0a4f

File tree

7 files changed

+499
-33
lines changed

7 files changed

+499
-33
lines changed

asyncpg/connection.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222

2323
class Connection:
24+
"""A representation of a database session.
25+
26+
Connections are created by calling :func:`~asyncpg.connect`.
27+
"""
2428

2529
__slots__ = ('_protocol', '_transport', '_loop', '_types_stmt',
2630
'_type_by_name_stmt', '_top_xact', '_uid', '_aborted',
@@ -52,7 +56,23 @@ def get_settings(self):
5256

5357
def transaction(self, *, isolation='read_committed', readonly=False,
5458
deferrable=False):
59+
"""Create a :class:`~transaction.Transaction` object.
60+
61+
Refer to `PostgreSQL documentation`_ on the meaning of transaction
62+
parameters.
63+
64+
:param isolation: Transaction isolation mode, can be one of:
65+
`'serializable'`, `'repeatable_read'`,
66+
`'read_committed'`.
67+
68+
:param readonly: Specifies whether or not this transaction is
69+
read-only.
70+
71+
:param deferrable: Specifies whether or not this transaction is
72+
deferrable.
5573
74+
.. _`PostgreSQL documentation`: https://www.postgresql.org/docs/current/static/sql-set-transaction.html
75+
"""
5676
return transaction.Transaction(self, isolation, readonly, deferrable)
5777

5878
async def execute(self, script: str, *, timeout: float=None) -> str:
@@ -110,32 +130,36 @@ async def prepare(self, query, *, timeout=None):
110130
:param str query: Text of the query to create a prepared statement for.
111131
:param int timeout: Optional timeout value in seconds.
112132
113-
:return: A :class:`PreparedStatement` instance.
133+
:return: A :class:`~prepared_stmt.PreparedStatement` instance.
114134
"""
115135
stmt = await self._get_statement(query, timeout)
116136
return prepared_stmt.PreparedStatement(self, query, stmt)
117137

118-
async def fetch(self, query, *args, timeout=None):
138+
async def fetch(self, query, *args, timeout=None) -> list:
119139
"""Run a query and return the results as a list of :class:`Record`.
120140
121-
:param str query: Query text
122-
:param args: Query arguments
141+
:param str query: Query text.
142+
:param args: Query arguments.
123143
:param int timeout: Optional timeout value in seconds.
124144
125-
:return: A list of :class:`Record` instances.
145+
:return list: A list of :class:`Record` instances.
126146
"""
127147
stmt = await self._get_statement(query, timeout)
128148
protocol = self._protocol
129149
data = await protocol.bind_execute(stmt, args, '', 0, False, timeout)
130150
return data
131151

132152
async def fetchval(self, query, *args, column=0, timeout=None):
133-
"""Run a query and return the value of the specified column of the first row.
153+
"""Run a query and return a value in the first row.
134154
135-
:param str query: Query text
136-
:param args: Query arguments
137-
:param int timeout: Optional column index (defaults to 0).
155+
:param str query: Query text.
156+
:param args: Query arguments.
157+
:param int column: Numeric index within the record of the value to
158+
return (defaults to 0).
138159
:param int timeout: Optional timeout value in seconds.
160+
If not specified, defaults to the value of
161+
``command_timeout`` argument to the ``Connection``
162+
instance constructor.
139163
140164
:return: The value of the specified column of the first record.
141165
"""

asyncpg/cursor.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313

1414
class CursorInterface:
15+
"""A cursor interface for the results of a query.
16+
17+
A cursor interface can be used to initiate efficient traversal of the
18+
results of a large query.
19+
"""
1520

1621
__slots__ = ('_state', '_connection', '_args', '_prefetch',
1722
'_query', '_timeout')
@@ -181,6 +186,7 @@ async def __anext__(self):
181186

182187

183188
class Cursor(BaseCursor):
189+
"""An open *portal* into the results of a query."""
184190

185191
__slots__ = ()
186192

@@ -194,6 +200,12 @@ async def _init(self, timeout):
194200
return self
195201

196202
async def fetch(self, n, *, timeout=None):
203+
r"""Return the next *n* rows as a list of :class:`Record` objects.
204+
205+
:param float timeout: Optional timeout value in seconds.
206+
207+
:return: A list of :class:`Record` instances.
208+
"""
197209
self._check_ready()
198210
if n <= 0:
199211
raise exceptions.InterfaceError('n must be greater than zero')
@@ -205,6 +217,12 @@ async def fetch(self, n, *, timeout=None):
205217
return recs
206218

207219
async def fetchrow(self, *, timeout=None):
220+
r"""Return the next row.
221+
222+
:param float timeout: Optional timeout value in seconds.
223+
224+
:return: A :class:`Record` instance.
225+
"""
208226
self._check_ready()
209227
if self._exhausted:
210228
return None
@@ -214,7 +232,13 @@ async def fetchrow(self, *, timeout=None):
214232
return None
215233
return recs[0]
216234

217-
async def forward(self, n, *, timeout=None):
235+
async def forward(self, n, *, timeout=None) -> int:
236+
r"""Skip over the next *n* rows.
237+
238+
:param float timeout: Optional timeout value in seconds.
239+
240+
:return: A number of rows actually skipped over (<= *n*).
241+
"""
218242
self._check_ready()
219243
if n <= 0:
220244
raise exceptions.InterfaceError('n must be greater than zero')

asyncpg/prepared_stmt.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313

1414
class PreparedStatement:
15+
"""A representation of a prepared statement."""
1516

1617
__slots__ = ('_connection', '_state', '_query', '_last_status')
1718

@@ -22,11 +23,11 @@ def __init__(self, connection, query, state):
2223
state.attach()
2324
self._last_status = None
2425

25-
def get_query(self):
26+
def get_query(self) -> str:
2627
"""Return the text of the query for this prepared statement."""
2728
return self._query
2829

29-
def get_statusmsg(self):
30+
def get_statusmsg(self) -> str:
3031
"""Return the status of the executed command."""
3132
if self._last_status is None:
3233
return self._last_status
@@ -40,13 +41,33 @@ def get_attributes(self):
4041
self.__check_open()
4142
return self._state._get_attributes()
4243

43-
def cursor(self, *args, prefetch=None, timeout=None):
44+
def cursor(self, *args, prefetch=50,
45+
timeout=None) -> cursor.CursorInterface:
46+
"""Return a *cursor interface* for the prepared statement.
47+
48+
:param args: Query arguments.
49+
:param int prefetch: The number of rows the *cursor iterator*
50+
will prefetch (defaults to ``50``.)
51+
:param float timeout: Optional timeout in seconds.
52+
53+
:return: A :class:`~cursor.CursorInterface` object.
54+
"""
4455
self.__check_open()
4556
return cursor.CursorInterface(self._connection, self._query,
4657
self._state, args, prefetch,
4758
timeout)
4859

4960
async def explain(self, *args, analyze=False):
61+
"""Return the execution plan of the statement.
62+
63+
:param args: Query arguments.
64+
:param analyze: If ``True``, the statement will be executed and
65+
the run time statitics added to the return value.
66+
67+
:return: An object representing the execution plan. This value
68+
is actually a deserialized JSON output of the SQL
69+
``EXPLAIN`` command.
70+
"""
5071
query = 'EXPLAIN (FORMAT JSON, VERBOSE'
5172
if analyze:
5273
query += ', ANALYZE) '
@@ -78,7 +99,8 @@ async def explain(self, *args, analyze=False):
7899
return json.loads(data)
79100

80101
async def fetch(self, *args, timeout=None):
81-
"""Execute the statement and return the results as a list of :class:`Record`.
102+
r"""Execute the statement and return the results as a list \
103+
of :class:`Record` objects.
82104
83105
:param str query: Query text
84106
:param args: Query arguments
@@ -94,12 +116,15 @@ async def fetch(self, *args, timeout=None):
94116
return data
95117

96118
async def fetchval(self, *args, column=0, timeout=None):
97-
r"""Execute the statement and return the value of the specified \
98-
column of the first row.
119+
"""Execute the statement and return a value in the first row.
99120
100-
:param args: Query arguments
101-
:param int timeout: Optional column index (defaults to 0).
121+
:param args: Query arguments.
122+
:param int column: Numeric index within the record of the value to
123+
return (defaults to 0).
102124
:param int timeout: Optional timeout value in seconds.
125+
If not specified, defaults to the value of
126+
``command_timeout`` argument to the ``Connection``
127+
instance constructor.
103128
104129
:return: The value of the specified column of the first record.
105130
"""

asyncpg/transaction.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class TransactionState(enum.Enum):
2222

2323

2424
class Transaction:
25+
"""Represents a transaction or savepoint block.
26+
27+
Transactions are created by calling
28+
:meth:`Connection.transaction`.
29+
"""
2530

2631
__slots__ = ('_connection', '_isolation', '_readonly', '_deferrable',
2732
'_state', '_nested', '_id', '_managed')
@@ -69,6 +74,7 @@ async def __aexit__(self, extype, ex, tb):
6974
self._managed = False
7075

7176
async def start(self):
77+
"""Enter the transaction or savepoint block."""
7278
self.__check_state_base('start')
7379
if self._state is TransactionState.STARTED:
7480
raise apg_errors.InterfaceError(
@@ -173,12 +179,14 @@ async def __rollback(self):
173179
self._state = TransactionState.ROLLEDBACK
174180

175181
async def commit(self):
182+
"""Exit the transaction or savepoint block and commit changes."""
176183
if self._managed:
177184
raise apg_errors.InterfaceError(
178185
'cannot manually commit from within an `async with` block')
179186
await self.__commit()
180187

181188
async def rollback(self):
189+
"""Exit the transaction or savepoint block and rollback changes."""
182190
if self._managed:
183191
raise apg_errors.InterfaceError(
184192
'cannot manually rollback from within an `async with` block')

0 commit comments

Comments
 (0)