Skip to content

Commit a6b0f28

Browse files
authored
Add copy_ wrappers to Pool (#661)
The `copy_to_table()` and friends are currently missing from the `Pool` interface, add them in. Fixes: #641.
1 parent 67ebbc9 commit a6b0f28

File tree

1 file changed

+182
-14
lines changed

1 file changed

+182
-14
lines changed

asyncpg/pool.py

+182-14
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ async def execute(self, query: str, *args, timeout: float=None) -> str:
522522
523523
Pool performs this operation using one of its connections. Other than
524524
that, it behaves identically to
525-
:meth:`Connection.execute() <connection.Connection.execute>`.
525+
:meth:`Connection.execute() <asyncpg.connection.Connection.execute>`.
526526
527527
.. versionadded:: 0.10.0
528528
"""
@@ -534,7 +534,8 @@ async def executemany(self, command: str, args, *, timeout: float=None):
534534
535535
Pool performs this operation using one of its connections. Other than
536536
that, it behaves identically to
537-
:meth:`Connection.executemany() <connection.Connection.executemany>`.
537+
:meth:`Connection.executemany()
538+
<asyncpg.connection.Connection.executemany>`.
538539
539540
.. versionadded:: 0.10.0
540541
"""
@@ -546,7 +547,7 @@ async def fetch(self, query, *args, timeout=None) -> list:
546547
547548
Pool performs this operation using one of its connections. Other than
548549
that, it behaves identically to
549-
:meth:`Connection.fetch() <connection.Connection.fetch>`.
550+
:meth:`Connection.fetch() <asyncpg.connection.Connection.fetch>`.
550551
551552
.. versionadded:: 0.10.0
552553
"""
@@ -558,7 +559,8 @@ async def fetchval(self, query, *args, column=0, timeout=None):
558559
559560
Pool performs this operation using one of its connections. Other than
560561
that, it behaves identically to
561-
:meth:`Connection.fetchval() <connection.Connection.fetchval>`.
562+
:meth:`Connection.fetchval()
563+
<asyncpg.connection.Connection.fetchval>`.
562564
563565
.. versionadded:: 0.10.0
564566
"""
@@ -571,13 +573,178 @@ async def fetchrow(self, query, *args, timeout=None):
571573
572574
Pool performs this operation using one of its connections. Other than
573575
that, it behaves identically to
574-
:meth:`Connection.fetchrow() <connection.Connection.fetchrow>`.
576+
:meth:`Connection.fetchrow() <asyncpg.connection.Connection.fetchrow>`.
575577
576578
.. versionadded:: 0.10.0
577579
"""
578580
async with self.acquire() as con:
579581
return await con.fetchrow(query, *args, timeout=timeout)
580582

583+
async def copy_from_table(
584+
self,
585+
table_name,
586+
*,
587+
output,
588+
columns=None,
589+
schema_name=None,
590+
timeout=None,
591+
format=None,
592+
oids=None,
593+
delimiter=None,
594+
null=None,
595+
header=None,
596+
quote=None,
597+
escape=None,
598+
force_quote=None,
599+
encoding=None
600+
):
601+
"""Copy table contents to a file or file-like object.
602+
603+
Pool performs this operation using one of its connections. Other than
604+
that, it behaves identically to
605+
:meth:`Connection.copy_from_table()
606+
<asyncpg.connection.Connection.copy_from_table>`.
607+
608+
.. versionadded:: 0.24.0
609+
"""
610+
async with self.acquire() as con:
611+
return await con.copy_from_table(
612+
table_name,
613+
output=output,
614+
columns=columns,
615+
schema_name=schema_name,
616+
timeout=timeout,
617+
format=format,
618+
oids=oids,
619+
delimiter=delimiter,
620+
null=null,
621+
header=header,
622+
quote=quote,
623+
escape=escape,
624+
force_quote=force_quote,
625+
encoding=encoding
626+
)
627+
628+
async def copy_from_query(
629+
self,
630+
query,
631+
*args,
632+
output,
633+
timeout=None,
634+
format=None,
635+
oids=None,
636+
delimiter=None,
637+
null=None,
638+
header=None,
639+
quote=None,
640+
escape=None,
641+
force_quote=None,
642+
encoding=None
643+
):
644+
"""Copy the results of a query to a file or file-like object.
645+
646+
Pool performs this operation using one of its connections. Other than
647+
that, it behaves identically to
648+
:meth:`Connection.copy_from_query()
649+
<asyncpg.connection.Connection.copy_from_query>`.
650+
651+
.. versionadded:: 0.24.0
652+
"""
653+
async with self.acquire() as con:
654+
return await con.copy_from_query(
655+
query,
656+
*args,
657+
output=output,
658+
timeout=timeout,
659+
format=format,
660+
oids=oids,
661+
delimiter=delimiter,
662+
null=null,
663+
header=header,
664+
quote=quote,
665+
escape=escape,
666+
force_quote=force_quote,
667+
encoding=encoding
668+
)
669+
670+
async def copy_to_table(
671+
self,
672+
table_name,
673+
*,
674+
source,
675+
columns=None,
676+
schema_name=None,
677+
timeout=None,
678+
format=None,
679+
oids=None,
680+
freeze=None,
681+
delimiter=None,
682+
null=None,
683+
header=None,
684+
quote=None,
685+
escape=None,
686+
force_quote=None,
687+
force_not_null=None,
688+
force_null=None,
689+
encoding=None
690+
):
691+
"""Copy data to the specified table.
692+
693+
Pool performs this operation using one of its connections. Other than
694+
that, it behaves identically to
695+
:meth:`Connection.copy_to_table()
696+
<asyncpg.connection.Connection.copy_to_table>`.
697+
698+
.. versionadded:: 0.24.0
699+
"""
700+
async with self.acquire() as con:
701+
return await con.copy_to_table(
702+
table_name,
703+
source=source,
704+
columns=columns,
705+
schema_name=schema_name,
706+
timeout=timeout,
707+
format=format,
708+
oids=oids,
709+
freeze=freeze,
710+
delimiter=delimiter,
711+
null=null,
712+
header=header,
713+
quote=quote,
714+
escape=escape,
715+
force_quote=force_quote,
716+
force_not_null=force_not_null,
717+
force_null=force_null,
718+
encoding=encoding
719+
)
720+
721+
async def copy_records_to_table(
722+
self,
723+
table_name,
724+
*,
725+
records,
726+
columns=None,
727+
schema_name=None,
728+
timeout=None
729+
):
730+
"""Copy a list of records to the specified table using binary COPY.
731+
732+
Pool performs this operation using one of its connections. Other than
733+
that, it behaves identically to
734+
:meth:`Connection.copy_records_to_table()
735+
<asyncpg.connection.Connection.copy_records_to_table>`.
736+
737+
.. versionadded:: 0.24.0
738+
"""
739+
async with self.acquire() as con:
740+
return await con.copy_records_to_table(
741+
table_name,
742+
records=records,
743+
columns=columns,
744+
schema_name=schema_name,
745+
timeout=timeout
746+
)
747+
581748
def acquire(self, *, timeout=None):
582749
"""Acquire a database connection from the pool.
583750
@@ -844,12 +1011,12 @@ def create_pool(dsn=None, *,
8441011
8451012
.. warning::
8461013
Prepared statements and cursors returned by
847-
:meth:`Connection.prepare() <connection.Connection.prepare>` and
848-
:meth:`Connection.cursor() <connection.Connection.cursor>` become
849-
invalid once the connection is released. Likewise, all notification
850-
and log listeners are removed, and ``asyncpg`` will issue a warning
851-
if there are any listener callbacks registered on a connection that
852-
is being released to the pool.
1014+
:meth:`Connection.prepare() <asyncpg.connection.Connection.prepare>`
1015+
and :meth:`Connection.cursor() <asyncpg.connection.Connection.cursor>`
1016+
become invalid once the connection is released. Likewise, all
1017+
notification and log listeners are removed, and ``asyncpg`` will
1018+
issue a warning if there are any listener callbacks registered on a
1019+
connection that is being released to the pool.
8531020
8541021
:param str dsn:
8551022
Connection arguments specified using as a single string in
@@ -915,10 +1082,11 @@ def create_pool(dsn=None, *,
9151082
.. versionchanged:: 0.13.0
9161083
An :exc:`~asyncpg.exceptions.InterfaceWarning` will be produced
9171084
if there are any active listeners (added via
918-
:meth:`Connection.add_listener() <connection.Connection.add_listener>`
1085+
:meth:`Connection.add_listener()
1086+
<asyncpg.connection.Connection.add_listener>`
9191087
or :meth:`Connection.add_log_listener()
920-
<connection.Connection.add_log_listener>`) present on the connection
921-
at the moment of its release to the pool.
1088+
<asyncpg.connection.Connection.add_log_listener>`) present on the
1089+
connection at the moment of its release to the pool.
9221090
9231091
.. versionchanged:: 0.22.0
9241092
Added the *record_class* parameter.

0 commit comments

Comments
 (0)