From f4bf807cf4dccda983052247336562d034b6a1d4 Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Wed, 25 Nov 2020 18:02:33 -0800 Subject: [PATCH 1/6] Add copy_ wrappers to Pool The `copy_to_table()` and friends are currently missing from the `Pool` interface, add them in. Fixes: #641. --- asyncpg/pool.py | 196 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 182 insertions(+), 14 deletions(-) diff --git a/asyncpg/pool.py b/asyncpg/pool.py index c4321a2f..585ff1eb 100644 --- a/asyncpg/pool.py +++ b/asyncpg/pool.py @@ -522,7 +522,7 @@ async def execute(self, query: str, *args, timeout: float=None) -> str: Pool performs this operation using one of its connections. Other than that, it behaves identically to - :meth:`Connection.execute() `. + :meth:`Connection.execute() `. .. versionadded:: 0.10.0 """ @@ -534,7 +534,8 @@ async def executemany(self, command: str, args, *, timeout: float=None): Pool performs this operation using one of its connections. Other than that, it behaves identically to - :meth:`Connection.executemany() `. + :meth:`Connection.executemany() + `. .. versionadded:: 0.10.0 """ @@ -546,7 +547,7 @@ async def fetch(self, query, *args, timeout=None) -> list: Pool performs this operation using one of its connections. Other than that, it behaves identically to - :meth:`Connection.fetch() `. + :meth:`Connection.fetch() `. .. versionadded:: 0.10.0 """ @@ -558,7 +559,8 @@ async def fetchval(self, query, *args, column=0, timeout=None): Pool performs this operation using one of its connections. Other than that, it behaves identically to - :meth:`Connection.fetchval() `. + :meth:`Connection.fetchval() + `. .. versionadded:: 0.10.0 """ @@ -571,13 +573,178 @@ async def fetchrow(self, query, *args, timeout=None): Pool performs this operation using one of its connections. Other than that, it behaves identically to - :meth:`Connection.fetchrow() `. + :meth:`Connection.fetchrow() `. .. versionadded:: 0.10.0 """ async with self.acquire() as con: return await con.fetchrow(query, *args, timeout=timeout) + async def copy_from_table( + self, + table_name, + *, + output, + columns=None, + schema_name=None, + timeout=None, + format=None, + oids=None, + delimiter=None, + null=None, + header=None, + quote=None, + escape=None, + force_quote=None, + encoding=None + ): + """Copy table contents to a file or file-like object. + + Pool performs this operation using one of its connections. Other than + that, it behaves identically to + :meth:`Connection.copy_from_table() + `. + + .. versionadded:: 0.22.0 + """ + async with self.acquire() as con: + return await con.copy_from_table( + table_name, + output=output, + columns=columns, + schema_name=schema_name, + timeout=timeout, + format=format, + oids=oids, + delimiter=delimiter, + null=null, + header=header, + quote=quote, + escape=escape, + force_quote=force_quote, + encoding=encoding + ) + + async def copy_from_query( + self, + query, + *args, + output, + timeout=None, + format=None, + oids=None, + delimiter=None, + null=None, + header=None, + quote=None, + escape=None, + force_quote=None, + encoding=None + ): + """Copy the results of a query to a file or file-like object. + + Pool performs this operation using one of its connections. Other than + that, it behaves identically to + :meth:`Connection.copy_from_query() + `. + + .. versionadded:: 0.22.0 + """ + async with self.acquire() as con: + return await con.copy_from_query( + query, + *args, + output=output, + timeout=timeout, + format=format, + oids=oids, + delimiter=delimiter, + null=null, + header=header, + quote=quote, + escape=escape, + force_quote=force_quote, + encoding=encoding + ) + + async def copy_to_table( + self, + table_name, + *, + source, + columns=None, + schema_name=None, + timeout=None, + format=None, + oids=None, + freeze=None, + delimiter=None, + null=None, + header=None, + quote=None, + escape=None, + force_quote=None, + force_not_null=None, + force_null=None, + encoding=None + ): + """Copy data to the specified table. + + Pool performs this operation using one of its connections. Other than + that, it behaves identically to + :meth:`Connection.copy_to_table() + `. + + .. versionadded:: 0.22.0 + """ + async with self.acquire() as con: + return await con.copy_to_table( + table_name, + source=source, + columns=columns, + schema_name=schema_name, + timeout=timeout, + format=format, + oids=oids, + freeze=freeze, + delimiter=delimiter, + null=null, + header=header, + quote=quote, + escape=escape, + force_quote=force_quote, + force_not_null=force_not_null, + force_null=force_null, + encoding=encoding + ) + + async def copy_records_to_table( + self, + table_name, + *, + records, + columns=None, + schema_name=None, + timeout=None + ): + """Copy a list of records to the specified table using binary COPY. + + Pool performs this operation using one of its connections. Other than + that, it behaves identically to + :meth:`Connection.copy_records_to_table() + `. + + .. versionadded:: 0.22.0 + """ + async with self.acquire() as con: + return await con.copy_to_table( + table_name, + records=records, + columns=columns, + schema_name=schema_name, + timeout=timeout + ) + def acquire(self, *, timeout=None): """Acquire a database connection from the pool. @@ -844,12 +1011,12 @@ def create_pool(dsn=None, *, .. warning:: Prepared statements and cursors returned by - :meth:`Connection.prepare() ` and - :meth:`Connection.cursor() ` become - invalid once the connection is released. Likewise, all notification - and log listeners are removed, and ``asyncpg`` will issue a warning - if there are any listener callbacks registered on a connection that - is being released to the pool. + :meth:`Connection.prepare() ` + and :meth:`Connection.cursor() ` + become invalid once the connection is released. Likewise, all + notification and log listeners are removed, and ``asyncpg`` will + issue a warning if there are any listener callbacks registered on a + connection that is being released to the pool. :param str dsn: Connection arguments specified using as a single string in @@ -915,10 +1082,11 @@ def create_pool(dsn=None, *, .. versionchanged:: 0.13.0 An :exc:`~asyncpg.exceptions.InterfaceWarning` will be produced if there are any active listeners (added via - :meth:`Connection.add_listener() ` + :meth:`Connection.add_listener() + ` or :meth:`Connection.add_log_listener() - `) present on the connection - at the moment of its release to the pool. + `) present on the + connection at the moment of its release to the pool. .. versionchanged:: 0.22.0 Added the *record_class* parameter. From 6fcd145727d7b033b5748ece785f777f4bd07d95 Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Mon, 9 Aug 2021 16:45:24 -0700 Subject: [PATCH 2/6] Update asyncpg/pool.py Co-authored-by: Fantix King --- asyncpg/pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncpg/pool.py b/asyncpg/pool.py index 585ff1eb..899d5aea 100644 --- a/asyncpg/pool.py +++ b/asyncpg/pool.py @@ -737,7 +737,7 @@ async def copy_records_to_table( .. versionadded:: 0.22.0 """ async with self.acquire() as con: - return await con.copy_to_table( + return await con.copy_records_to_table( table_name, records=records, columns=columns, From 34013b849dbe086651b0476c825db47d79859acf Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Mon, 9 Aug 2021 16:45:29 -0700 Subject: [PATCH 3/6] Update asyncpg/pool.py Co-authored-by: Fantix King --- asyncpg/pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncpg/pool.py b/asyncpg/pool.py index 899d5aea..48b3b4a0 100644 --- a/asyncpg/pool.py +++ b/asyncpg/pool.py @@ -605,7 +605,7 @@ async def copy_from_table( :meth:`Connection.copy_from_table() `. - .. versionadded:: 0.22.0 + .. versionadded:: 0.24.0 """ async with self.acquire() as con: return await con.copy_from_table( From 08d38290db41043dd5fe1beec69f8c51705ee34d Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Mon, 9 Aug 2021 16:45:33 -0700 Subject: [PATCH 4/6] Update asyncpg/pool.py Co-authored-by: Fantix King --- asyncpg/pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncpg/pool.py b/asyncpg/pool.py index 48b3b4a0..ed578573 100644 --- a/asyncpg/pool.py +++ b/asyncpg/pool.py @@ -648,7 +648,7 @@ async def copy_from_query( :meth:`Connection.copy_from_query() `. - .. versionadded:: 0.22.0 + .. versionadded:: 0.24.0 """ async with self.acquire() as con: return await con.copy_from_query( From 69e9f4593c71478a04c0979f1c09db0afea48783 Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Mon, 9 Aug 2021 16:45:38 -0700 Subject: [PATCH 5/6] Update asyncpg/pool.py Co-authored-by: Fantix King --- asyncpg/pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncpg/pool.py b/asyncpg/pool.py index ed578573..7b3fae5a 100644 --- a/asyncpg/pool.py +++ b/asyncpg/pool.py @@ -695,7 +695,7 @@ async def copy_to_table( :meth:`Connection.copy_to_table() `. - .. versionadded:: 0.22.0 + .. versionadded:: 0.24.0 """ async with self.acquire() as con: return await con.copy_to_table( From 4a86dca3f8275282f54b5c1672cf1569b8b82a3c Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Mon, 9 Aug 2021 16:45:42 -0700 Subject: [PATCH 6/6] Update asyncpg/pool.py Co-authored-by: Fantix King --- asyncpg/pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncpg/pool.py b/asyncpg/pool.py index 7b3fae5a..c868097c 100644 --- a/asyncpg/pool.py +++ b/asyncpg/pool.py @@ -734,7 +734,7 @@ async def copy_records_to_table( :meth:`Connection.copy_records_to_table() `. - .. versionadded:: 0.22.0 + .. versionadded:: 0.24.0 """ async with self.acquire() as con: return await con.copy_records_to_table(