MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / copy_from_query

Method copy_from_query

asyncpg/connection.py:901–961  ·  view source on GitHub ↗

Copy the results of a query to a file or file-like object. :param str query: The query to copy the results of. :param args: Query arguments. :param output: A :term:`path-like object `, or a :term:`fil

(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)

Source from the content-addressed store, hash-verified

899 return await self._copy_out(copy_stmt, output, timeout)
900
901 async def copy_from_query(self, query, *args, output,
902 timeout=None, format=None, oids=None,
903 delimiter=None, null=None, header=None,
904 quote=None, escape=None, force_quote=None,
905 encoding=None):
906 """Copy the results of a query to a file or file-like object.
907
908 :param str query:
909 The query to copy the results of.
910
911 :param args:
912 Query arguments.
913
914 :param output:
915 A :term:`path-like object <python:path-like object>`,
916 or a :term:`file-like object <python:file-like object>`, or
917 a :term:`coroutine function <python:coroutine function>`
918 that takes a ``bytes`` instance as a sole argument.
919
920 :param float timeout:
921 Optional timeout value in seconds.
922
923 The remaining keyword arguments are ``COPY`` statement options,
924 see `COPY statement documentation`_ for details.
925
926 :return: The status string of the COPY command.
927
928 Example:
929
930 .. code-block:: pycon
931
932 >>> import asyncpg
933 >>> import asyncio
934 >>> async def run():
935 ... con = await asyncpg.connect(user='postgres')
936 ... result = await con.copy_from_query(
937 ... 'SELECT foo, bar FROM mytable WHERE foo > $1', 10,
938 ... output='file.csv', format='csv')
939 ... print(result)
940 ...
941 >>> asyncio.run(run())
942 'COPY 10'
943
944 .. _`COPY statement documentation`:
945 https://www.postgresql.org/docs/current/static/sql-copy.html
946
947 .. versionadded:: 0.11.0
948 """
949 opts = self._format_copy_opts(
950 format=format, oids=oids, delimiter=delimiter,
951 null=null, header=header, quote=quote, escape=escape,
952 force_quote=force_quote, encoding=encoding
953 )
954
955 if args:
956 query = await utils._mogrify(self, query, args)
957
958 copy_stmt = 'COPY ({query}) TO STDOUT {opts}'.format(

Callers

nothing calls this directly

Calls 2

_format_copy_optsMethod · 0.95
_copy_outMethod · 0.95

Tested by

no test coverage detected