This commit is contained in:
@@ -231,8 +231,9 @@ class Connection(metaclass=ConnectionMeta):
|
||||
|
||||
:param callable callback:
|
||||
A callable or a coroutine function receiving one argument:
|
||||
**record**, a LoggedQuery containing `query`, `args`, `timeout`,
|
||||
`elapsed`, `exception`, `conn_addr`, and `conn_params`.
|
||||
**record**: a LoggedQuery containing `query`, `args`, `timeout`,
|
||||
`elapsed`, `exception`, `conn_addr`, and
|
||||
`conn_params`.
|
||||
|
||||
.. versionadded:: 0.29.0
|
||||
"""
|
||||
@@ -756,44 +757,6 @@ class Connection(metaclass=ConnectionMeta):
|
||||
return None
|
||||
return data[0]
|
||||
|
||||
async def fetchmany(
|
||||
self, query, args, *, timeout: float=None, record_class=None
|
||||
):
|
||||
"""Run a query for each sequence of arguments in *args*
|
||||
and return the results as a list of :class:`Record`.
|
||||
|
||||
:param query:
|
||||
Query to execute.
|
||||
:param args:
|
||||
An iterable containing sequences of arguments for the query.
|
||||
:param float timeout:
|
||||
Optional timeout value in seconds.
|
||||
:param type record_class:
|
||||
If specified, the class to use for records returned by this method.
|
||||
Must be a subclass of :class:`~asyncpg.Record`. If not specified,
|
||||
a per-connection *record_class* is used.
|
||||
|
||||
:return list:
|
||||
A list of :class:`~asyncpg.Record` instances. If specified, the
|
||||
actual type of list elements would be *record_class*.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> rows = await con.fetchmany('''
|
||||
... INSERT INTO mytab (a, b) VALUES ($1, $2) RETURNING a;
|
||||
... ''', [('x', 1), ('y', 2), ('z', 3)])
|
||||
>>> rows
|
||||
[<Record row=('x',)>, <Record row=('y',)>, <Record row=('z',)>]
|
||||
|
||||
.. versionadded:: 0.30.0
|
||||
"""
|
||||
self._check_open()
|
||||
return await self._executemany(
|
||||
query, args, timeout, return_rows=True, record_class=record_class
|
||||
)
|
||||
|
||||
async def copy_from_table(self, table_name, *, output,
|
||||
columns=None, schema_name=None, timeout=None,
|
||||
format=None, oids=None, delimiter=None,
|
||||
@@ -837,7 +800,7 @@ class Connection(metaclass=ConnectionMeta):
|
||||
... output='file.csv', format='csv')
|
||||
... print(result)
|
||||
...
|
||||
>>> asyncio.run(run())
|
||||
>>> asyncio.get_event_loop().run_until_complete(run())
|
||||
'COPY 100'
|
||||
|
||||
.. _`COPY statement documentation`:
|
||||
@@ -906,7 +869,7 @@ class Connection(metaclass=ConnectionMeta):
|
||||
... output='file.csv', format='csv')
|
||||
... print(result)
|
||||
...
|
||||
>>> asyncio.run(run())
|
||||
>>> asyncio.get_event_loop().run_until_complete(run())
|
||||
'COPY 10'
|
||||
|
||||
.. _`COPY statement documentation`:
|
||||
@@ -982,7 +945,7 @@ class Connection(metaclass=ConnectionMeta):
|
||||
... 'mytable', source='datafile.tbl')
|
||||
... print(result)
|
||||
...
|
||||
>>> asyncio.run(run())
|
||||
>>> asyncio.get_event_loop().run_until_complete(run())
|
||||
'COPY 140000'
|
||||
|
||||
.. _`COPY statement documentation`:
|
||||
@@ -1064,7 +1027,7 @@ class Connection(metaclass=ConnectionMeta):
|
||||
... (2, 'ham', 'spam')])
|
||||
... print(result)
|
||||
...
|
||||
>>> asyncio.run(run())
|
||||
>>> asyncio.get_event_loop().run_until_complete(run())
|
||||
'COPY 2'
|
||||
|
||||
Asynchronous record iterables are also supported:
|
||||
@@ -1082,7 +1045,7 @@ class Connection(metaclass=ConnectionMeta):
|
||||
... 'mytable', records=record_gen(100))
|
||||
... print(result)
|
||||
...
|
||||
>>> asyncio.run(run())
|
||||
>>> asyncio.get_event_loop().run_until_complete(run())
|
||||
'COPY 100'
|
||||
|
||||
.. versionadded:: 0.11.0
|
||||
@@ -1342,7 +1305,7 @@ class Connection(metaclass=ConnectionMeta):
|
||||
... print(result)
|
||||
... print(datetime.datetime(2002, 1, 1) + result)
|
||||
...
|
||||
>>> asyncio.run(run())
|
||||
>>> asyncio.get_event_loop().run_until_complete(run())
|
||||
relativedelta(years=+2, months=+3, days=+1)
|
||||
2004-04-02 00:00:00
|
||||
|
||||
@@ -1515,10 +1478,11 @@ class Connection(metaclass=ConnectionMeta):
|
||||
self._abort()
|
||||
self._cleanup()
|
||||
|
||||
async def _reset(self):
|
||||
async def reset(self, *, timeout=None):
|
||||
self._check_open()
|
||||
self._listeners.clear()
|
||||
self._log_listeners.clear()
|
||||
reset_query = self._get_reset_query()
|
||||
|
||||
if self._protocol.is_in_transaction() or self._top_xact is not None:
|
||||
if self._top_xact is None or not self._top_xact._managed:
|
||||
@@ -1530,36 +1494,10 @@ class Connection(metaclass=ConnectionMeta):
|
||||
})
|
||||
|
||||
self._top_xact = None
|
||||
await self.execute("ROLLBACK")
|
||||
reset_query = 'ROLLBACK;\n' + reset_query
|
||||
|
||||
async def reset(self, *, timeout=None):
|
||||
"""Reset the connection state.
|
||||
|
||||
Calling this will reset the connection session state to a state
|
||||
resembling that of a newly obtained connection. Namely, an open
|
||||
transaction (if any) is rolled back, open cursors are closed,
|
||||
all `LISTEN <https://www.postgresql.org/docs/current/sql-listen.html>`_
|
||||
registrations are removed, all session configuration
|
||||
variables are reset to their default values, and all advisory locks
|
||||
are released.
|
||||
|
||||
Note that the above describes the default query returned by
|
||||
:meth:`Connection.get_reset_query`. If one overloads the method
|
||||
by subclassing ``Connection``, then this method will do whatever
|
||||
the overloaded method returns, except open transactions are always
|
||||
terminated and any callbacks registered by
|
||||
:meth:`Connection.add_listener` or :meth:`Connection.add_log_listener`
|
||||
are removed.
|
||||
|
||||
:param float timeout:
|
||||
A timeout for resetting the connection. If not specified, defaults
|
||||
to no timeout.
|
||||
"""
|
||||
async with compat.timeout(timeout):
|
||||
await self._reset()
|
||||
reset_query = self.get_reset_query()
|
||||
if reset_query:
|
||||
await self.execute(reset_query)
|
||||
if reset_query:
|
||||
await self.execute(reset_query, timeout=timeout)
|
||||
|
||||
def _abort(self):
|
||||
# Put the connection into the aborted state.
|
||||
@@ -1720,15 +1658,7 @@ class Connection(metaclass=ConnectionMeta):
|
||||
con_ref = self._proxy
|
||||
return con_ref
|
||||
|
||||
def get_reset_query(self):
|
||||
"""Return the query sent to server on connection release.
|
||||
|
||||
The query returned by this method is used by :meth:`Connection.reset`,
|
||||
which is, in turn, used by :class:`~asyncpg.pool.Pool` before making
|
||||
the connection available to another acquirer.
|
||||
|
||||
.. versionadded:: 0.30.0
|
||||
"""
|
||||
def _get_reset_query(self):
|
||||
if self._reset_query is not None:
|
||||
return self._reset_query
|
||||
|
||||
@@ -1842,7 +1772,7 @@ class Connection(metaclass=ConnectionMeta):
|
||||
... await con.execute('LOCK TABLE tbl')
|
||||
... await change_type(con)
|
||||
...
|
||||
>>> asyncio.run(run())
|
||||
>>> asyncio.get_event_loop().run_until_complete(run())
|
||||
|
||||
.. versionadded:: 0.14.0
|
||||
"""
|
||||
@@ -1879,8 +1809,9 @@ class Connection(metaclass=ConnectionMeta):
|
||||
|
||||
:param callable callback:
|
||||
A callable or a coroutine function receiving one argument:
|
||||
**record**, a LoggedQuery containing `query`, `args`, `timeout`,
|
||||
`elapsed`, `exception`, `conn_addr`, and `conn_params`.
|
||||
**record**: a LoggedQuery containing `query`, `args`, `timeout`,
|
||||
`elapsed`, `exception`, `conn_addr`, and
|
||||
`conn_params`.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -1967,27 +1898,17 @@ class Connection(metaclass=ConnectionMeta):
|
||||
)
|
||||
return result, stmt
|
||||
|
||||
async def _executemany(
|
||||
self,
|
||||
query,
|
||||
args,
|
||||
timeout,
|
||||
return_rows=False,
|
||||
record_class=None,
|
||||
):
|
||||
async def _executemany(self, query, args, timeout):
|
||||
executor = lambda stmt, timeout: self._protocol.bind_execute_many(
|
||||
state=stmt,
|
||||
args=args,
|
||||
portal_name='',
|
||||
timeout=timeout,
|
||||
return_rows=return_rows,
|
||||
)
|
||||
timeout = self._protocol._get_timeout(timeout)
|
||||
with self._stmt_exclusive_section:
|
||||
with self._time_and_log(query, args, timeout):
|
||||
result, _ = await self._do_execute(
|
||||
query, executor, timeout, record_class=record_class
|
||||
)
|
||||
result, _ = await self._do_execute(query, executor, timeout)
|
||||
return result
|
||||
|
||||
async def _do_execute(
|
||||
@@ -2082,13 +2003,11 @@ async def connect(dsn=None, *,
|
||||
max_cacheable_statement_size=1024 * 15,
|
||||
command_timeout=None,
|
||||
ssl=None,
|
||||
direct_tls=None,
|
||||
direct_tls=False,
|
||||
connection_class=Connection,
|
||||
record_class=protocol.Record,
|
||||
server_settings=None,
|
||||
target_session_attrs=None,
|
||||
krbsrvname=None,
|
||||
gsslib=None):
|
||||
target_session_attrs=None):
|
||||
r"""A coroutine to establish a connection to a PostgreSQL server.
|
||||
|
||||
The connection parameters may be specified either as a connection
|
||||
@@ -2113,7 +2032,7 @@ async def connect(dsn=None, *,
|
||||
.. note::
|
||||
|
||||
The URI must be *valid*, which means that all components must
|
||||
be properly quoted with :py:func:`urllib.parse.quote_plus`, and
|
||||
be properly quoted with :py:func:`urllib.parse.quote`, and
|
||||
any literal IPv6 addresses must be enclosed in square brackets.
|
||||
For example:
|
||||
|
||||
@@ -2316,14 +2235,6 @@ async def connect(dsn=None, *,
|
||||
or the value of the ``PGTARGETSESSIONATTRS`` environment variable,
|
||||
or ``"any"`` if neither is specified.
|
||||
|
||||
:param str krbsrvname:
|
||||
Kerberos service name to use when authenticating with GSSAPI. This
|
||||
must match the server configuration. Defaults to 'postgres'.
|
||||
|
||||
:param str gsslib:
|
||||
GSS library to use for GSSAPI/SSPI authentication. Can be 'gssapi'
|
||||
or 'sspi'. Defaults to 'sspi' on Windows and 'gssapi' otherwise.
|
||||
|
||||
:return: A :class:`~asyncpg.connection.Connection` instance.
|
||||
|
||||
Example:
|
||||
@@ -2337,7 +2248,7 @@ async def connect(dsn=None, *,
|
||||
... types = await con.fetch('SELECT * FROM pg_type')
|
||||
... print(types)
|
||||
...
|
||||
>>> asyncio.run(run())
|
||||
>>> asyncio.get_event_loop().run_until_complete(run())
|
||||
[<Record typname='bool' typnamespace=11 ...
|
||||
|
||||
.. versionadded:: 0.10.0
|
||||
@@ -2392,9 +2303,6 @@ async def connect(dsn=None, *,
|
||||
.. versionchanged:: 0.28.0
|
||||
Added the *target_session_attrs* parameter.
|
||||
|
||||
.. versionchanged:: 0.30.0
|
||||
Added the *krbsrvname* and *gsslib* parameters.
|
||||
|
||||
.. _SSLContext: https://docs.python.org/3/library/ssl.html#ssl.SSLContext
|
||||
.. _create_default_context:
|
||||
https://docs.python.org/3/library/ssl.html#ssl.create_default_context
|
||||
@@ -2436,9 +2344,7 @@ async def connect(dsn=None, *,
|
||||
statement_cache_size=statement_cache_size,
|
||||
max_cached_statement_lifetime=max_cached_statement_lifetime,
|
||||
max_cacheable_statement_size=max_cacheable_statement_size,
|
||||
target_session_attrs=target_session_attrs,
|
||||
krbsrvname=krbsrvname,
|
||||
gsslib=gsslib,
|
||||
target_session_attrs=target_session_attrs
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user