MCPcopy
hub / github.com/psycopg/psycopg / AsyncCopy

Class AsyncCopy

psycopg/psycopg/_copy_async.py:32–155  ·  view source on GitHub ↗

Manage an asynchronous :sql:`COPY` operation. :param cursor: the cursor where the operation is performed. :param binary: if `!True`, write binary format. :param writer: the object to write to destination. If not specified, write to the `!cursor` connection. Choosing `!binar

Source from the content-addressed store, hash-verified

30
31
32class AsyncCopy(BaseCopy["AsyncConnection[Any]"]):
33 """Manage an asynchronous :sql:`COPY` operation.
34
35 :param cursor: the cursor where the operation is performed.
36 :param binary: if `!True`, write binary format.
37 :param writer: the object to write to destination. If not specified, write
38 to the `!cursor` connection.
39
40 Choosing `!binary` is not necessary if the cursor has executed a
41 :sql:`COPY` operation, because the operation result describes the format
42 too. The parameter is useful when a `!Copy` object is created manually and
43 no operation is performed on the cursor, such as when using ``writer=``\\
44 `~psycopg.copy.FileWriter`.
45 """
46
47 __module__ = "psycopg"
48
49 writer: AsyncWriter
50
51 def __init__(
52 self,
53 cursor: AsyncCursor[Any],
54 *,
55 binary: bool | None = None,
56 writer: AsyncWriter | None = None,
57 ):
58 super().__init__(cursor, binary=binary)
59 if not writer:
60 writer = AsyncLibpqWriter(cursor)
61
62 self.writer = writer
63 self._write = writer.write
64
65 async def __aenter__(self) -> Self:
66 self._enter()
67 return self
68
69 async def __aexit__(
70 self,
71 exc_type: type[BaseException] | None,
72 exc_val: BaseException | None,
73 exc_tb: TracebackType | None,
74 ) -> None:
75 await self.finish(exc_val)
76
77 # End user sync interface
78
79 async def __aiter__(self) -> AsyncIterator[Buffer]:
80 """Implement block-by-block iteration on :sql:`COPY TO`."""
81 while data := (await self.read()):
82 yield data
83
84 async def read(self) -> Buffer:
85 """
86 Read an unparsed row after a :sql:`COPY TO` operation.
87
88 Return an empty string when the data is finished.
89 """

Callers 3

test_copy_in_formatFunction · 0.90
test_file_writerFunction · 0.90
copyMethod · 0.85

Calls

no outgoing calls

Tested by 2

test_copy_in_formatFunction · 0.72
test_file_writerFunction · 0.72