-Advanced topics
-===============
+.. _advanced:
+
+More advanced topics
+====================
+
+Once you have familiarised yourself with the :ref:`Psycopg basic operations
+<basic>`, you can take a look at the chapter of this section for more advanced
+usages.
.. toctree::
:maxdepth: 1
Psycopg 3 API
=============
+.. _api:
+
+This sections is a reference for all the public objects exposed by the
+`psycopg` module. For a more conceptual description you can take a look at
+:ref:`basic` and :ref:`advanced`.
+
.. toctree::
:maxdepth: 2
:caption: Contents:
- :ref:`transactions`.
+Shortcuts
+---------
+
+The pattern above is familiar to `!psycopg2` users. However, Psycopg 3 also
+exposes a few simple extensions which make the above pattern leaner:
+
+- the `Connection` objects exposes a `~Connection.execute()` method,
+ equivalent to creating a cursor, calling its `~Cursor.execute()` method, and
+ returning it.
+
+ .. code::
+
+ # This
+ cur = conn.execute(...)
+
+ # is equivalent to:
+ cur = conn.cursor()
+ cur.execute(...)
+
+- The `Cursor.execute()` method returns `!self`. This means that you can chain
+ a fetch operation, such as `~Cursor.fetchone()`, to the `!execute()` call:
+
+ .. code::
+
+ # This
+ cur.execute(...)
+ record = cur.fetchone()
+
+ cur.execute(...)
+ for record in cur:
+ ...
+
+ # is equivalent to:
+ record = cur.execute(...).fetchone()
+ for record in cur.execute(...):
+ ...
+
+Using them together, in simple cases, you can go from creating a connection to
+using a result in a single expression:
+
+.. code::
+
+ print(psycopg.connect(DSN).execute("select now()").fetchone()[0])
+ # 2042-07-12 18:15:10.706497+01:00
+
+
.. index::
pair: Connection; ``with``
import sys
from pathlib import Path
+import psycopg
+
docs_dir = Path(__file__).parent
sys.path.append(str(docs_dir / "lib"))
project = "psycopg"
copyright = "2020-2021, Daniele Varrazzo and The Psycopg Team"
author = "Daniele Varrazzo"
-release = "UNRELEASED"
+release = psycopg.__version__
# -- General configuration ---------------------------------------------------