Skip to content

Commit e4bef17

Browse files
committed
Expand usage documentation, add FAQ.
1 parent ccc5f7a commit e4bef17

File tree

6 files changed

+391
-61
lines changed

6 files changed

+391
-61
lines changed

asyncpg/connection.py

+62-5
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,25 @@ async def copy_from_table(self, table_name, *, output,
378378
379379
:return: The status string of the COPY command.
380380
381-
.. versionadded:: 0.11.0
381+
Example:
382+
383+
.. code-block:: pycon
384+
385+
>>> import asyncpg
386+
>>> import asyncio
387+
>>> async def run():
388+
... con = await asyncpg.connect(user='postgres')
389+
... result = await con.copy_from_table(
390+
... 'mytable', columns=('foo', 'bar'),
391+
... output='file.csv', format='csv')
392+
... print(result)
393+
>>> asyncio.get_event_loop().run_until_complete(run())
394+
'COPY 100'
382395
383396
.. _`COPY statement documentation`: https://www.postgresql.org/docs/\
384397
current/static/sql-copy.html
385398
399+
.. versionadded:: 0.11.0
386400
"""
387401
tabname = utils._quote_ident(table_name)
388402
if schema_name:
@@ -415,7 +429,7 @@ async def copy_from_query(self, query, *args, output,
415429
:param str query:
416430
The query to copy the results of.
417431
418-
:param *args:
432+
:param \*args:
419433
Query arguments.
420434
421435
:param output:
@@ -432,11 +446,25 @@ async def copy_from_query(self, query, *args, output,
432446
433447
:return: The status string of the COPY command.
434448
435-
.. versionadded:: 0.11.0
449+
Example:
450+
451+
.. code-block:: pycon
452+
453+
>>> import asyncpg
454+
>>> import asyncio
455+
>>> async def run():
456+
... con = await asyncpg.connect(user='postgres')
457+
... result = await con.copy_from_query(
458+
... 'SELECT foo, bar FROM mytable WHERE foo > $1', 10,
459+
... output='file.csv', format='csv')
460+
... print(result)
461+
>>> asyncio.get_event_loop().run_until_complete(run())
462+
'COPY 10'
436463
437464
.. _`COPY statement documentation`: https://www.postgresql.org/docs/\
438465
current/static/sql-copy.html
439466
467+
.. versionadded:: 0.11.0
440468
"""
441469
opts = self._format_copy_opts(
442470
format=format, oids=oids, delimiter=delimiter,
@@ -469,7 +497,7 @@ async def copy_to_table(self, table_name, *, source,
469497
or a :term:`file-like object <python:file-like object>`, or
470498
an :term:`asynchronous iterable <python:asynchronous iterable>`
471499
that returns ``bytes``, or an object supporting the
472-
:term:`buffer protocol <python:buffer protocol>`.
500+
:ref:`buffer protocol <python:bufferobjects>`.
473501
474502
:param list columns:
475503
An optional list of column names to copy.
@@ -485,11 +513,24 @@ async def copy_to_table(self, table_name, *, source,
485513
486514
:return: The status string of the COPY command.
487515
488-
.. versionadded:: 0.11.0
516+
Example:
517+
518+
.. code-block:: pycon
519+
520+
>>> import asyncpg
521+
>>> import asyncio
522+
>>> async def run():
523+
... con = await asyncpg.connect(user='postgres')
524+
... result = await con.copy_to_table(
525+
... 'mytable', source='datafile.tbl')
526+
.... print(result)
527+
>>> asyncio.get_event_loop().run_until_complete(run())
528+
'COPY 140000'
489529
490530
.. _`COPY statement documentation`: https://www.postgresql.org/docs/\
491531
current/static/sql-copy.html
492532
533+
.. versionadded:: 0.11.0
493534
"""
494535
tabname = utils._quote_ident(table_name)
495536
if schema_name:
@@ -535,6 +576,22 @@ async def copy_records_to_table(self, table_name, *, records,
535576
536577
:return: The status string of the COPY command.
537578
579+
Example:
580+
581+
.. code-block:: pycon
582+
583+
>>> import asyncpg
584+
>>> import asyncio
585+
>>> async def run():
586+
... con = await asyncpg.connect(user='postgres')
587+
... result = await con.copy_records_to_table(
588+
... 'mytable', records=[
589+
... (1, 'foo', 'bar'),
590+
... (2, 'ham', 'spam')])
591+
.... print(result)
592+
>>> asyncio.get_event_loop().run_until_complete(run())
593+
'COPY 2'
594+
538595
.. versionadded:: 0.11.0
539596
"""
540597
tabname = utils._quote_ident(table_name)

docs/api/index.rst

+5-8
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ It's also possible to create cursors from prepared statements:
233233

234234
.. _asyncpg-api-pool:
235235

236-
Connection Pool
237-
===============
236+
Connection Pools
237+
================
238238

239239
.. autofunction:: asyncpg.pool.create_pool
240240

@@ -326,11 +326,8 @@ items either by a numeric index or by a field name:
326326
'UTF8'
327327
328328
329-
Introspection
330-
=============
331-
332-
.. autoclass:: asyncpg.types.Type()
333-
:members:
329+
Data Types
330+
==========
334331

335-
.. autoclass:: asyncpg.types.Attribute()
332+
.. automodule:: asyncpg.types
336333
:members:

docs/examples.rst

-47
This file was deleted.

docs/faq.rst

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. _asyncpg-faq:
2+
3+
4+
Frequently Asked Questions
5+
==========================
6+
7+
Does asyncpg support DB-API?
8+
No. DB-API is a synchronous API, while asyncpg is based
9+
around an asynchronous I/O model. Thus, full drop-in compatibility
10+
with DB-API is not possible and we decided to design asyncpg API
11+
in a way that is better aligned with PostgreSQL architecture and
12+
terminology. We will release a synchronous DB-API-compatible version
13+
of asyncpg at some point in the future.
14+
15+
Can I use asyncpg with SQLAlchemy ORM?
16+
Short answer: no. asyncpg uses asynchronous execution model
17+
and API, which is fundamentally incompatible with asyncpg.
18+
However, it is possible to use asyncpg and SQLAlchemy Core
19+
with the help of a third-party adapter, such as asyncpgsa_.
20+
21+
Can I use dot-notation with :class:`asyncpg.Record`? It looks cleaner.
22+
We decided against making :class:`asyncpg.Record` a named tuple
23+
because we want to keep the ``Record`` method namespace separate
24+
from the column namespace.
25+
26+
Why can't I use a :ref:`cursor <asyncpg-api-cursor>` outside of a transaction?
27+
Cursors created by a call to
28+
:meth:`Connection.cursor() <asyncpg.connection.Connection.cursor>` or
29+
:meth:`PreparedStatement.cursor() \
30+
<asyncpg.prepared_stmt.PreparedStatement.cursor>`
31+
cannot be used outside of a transaction. Any such attempt will result in
32+
``InterfaceError``.
33+
To create a cursor usable outside of a transaction, use the
34+
``DECLARE ... CURSOR WITH HOLD`` SQL statement directly.
35+
36+
Why do I get ``PostgresSyntaxError`` when using ``expression IN $1``?
37+
``expression IN $1`` is not a valid PostgreSQL syntax. To check
38+
a value against a sequence use ``expression = any($1::mytype[])``,
39+
where ``mytype`` is the array element type.
40+
41+
.. _asyncpgsa: https://github.com/CanopyTax/asyncpgsa

docs/index.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ Contents
2424
:maxdepth: 2
2525

2626
installation
27-
examples
27+
usage
2828
api/index
29+
faq

0 commit comments

Comments
 (0)