
    (ph'                       S r SSKJr  SSKJrJr  SSKJr  SSKJ	r	J
r
JrJr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJrJr  SSKJrJr  SSKJrJrJrJr  \(       a  SSKJ r   SSK!J"r"  SSK#J$r$  \RJ                  RL                  r&\RJ                  RN                  r'\RP                  RR                  r) " S S\S   5      r* " S S\5      r+ " S S\+5      r, " S S\,5      r-g)z7
Objects to support the COPY protocol (async version).
    )annotations)ABCabstractmethod)TracebackType)AnyAsyncIteratorSequenceTYPE_CHECKING   )pq)errors)Self)BaseCopyMAX_BUFFER_SIZE
QUEUE_SIZEPREFER_FLUSH)copy_tocopy_end)aspawnagatherAQueueAWorker)Buffer)AsyncCursor)AsyncConnectionc                     ^  \ rS rSr% SrSrS\S'   SSS.     SU 4S jjjrSS	 jr        SS
 jrSS jr	SS jr
SS jrSS jrSS jrSS jrSS jrSrU =r$ )	AsyncCopy   aB  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 `!binary` is not necessary if the cursor has executed a
:sql:`COPY` operation, because the operation result describes the format
too. The parameter is useful when a `!Copy` object is created manually and
no operation is performed on the cursor, such as when using ``writer=``\
`~psycopg.copy.FileWriter`.
psycopgAsyncWriterwriterN)binaryr!   c               r   > [         TU ]  XS9  U(       d  [        U5      nX0l        UR                  U l        g )N)r"   )super__init__AsyncLibpqWriterr!   write_write)selfcursorr"   r!   	__class__s       F/var/www/html/venv/lib/python3.13/site-packages/psycopg/_copy_async.pyr%   AsyncCopy.__init__2   s2     	/%f-Fll    c                .   #    U R                  5         U $ 7fN)_enterr)   s    r,   
__aenter__AsyncCopy.__aenter__@   s     s   c                B   #    U R                  U5      I S h  vN   g  N7fr0   )finish)r)   exc_typeexc_valexc_tbs       r,   	__aexit__AsyncCopy.__aexit__D   s      kk'"""s   c               ^   #     U R                  5       I Sh  vN nU(       d  gU7v   M'   N7f)z5Implement block-by-block iteration on :sql:`COPY TO`.N)readr)   datas     r,   	__aiter__AsyncCopy.__aiter__N   s)     $DJ	 $s   -+-c                p   #    U R                   R                  U R                  5       5      I Sh  vN $  N7f)zk
Read an unparsed row after a :sql:`COPY TO` operation.

Return an empty string when the data is finished.
N)
connectionwait	_read_genr2   s    r,   r=   AsyncCopy.readV   s)      __))$..*:;;;;   -646c               V   #     U R                  5       I Sh  vN nUc  gU7v   M#   N7f)z
Iterate on the result of a :sql:`COPY TO` operation record by record.

Note that the records returned will be tuples of unparsed strings or
bytes, unless data types are specified using `set_types()`.
N)read_row)r)   records     r,   rowsAsyncCopy.rows^   s-      ==?*F~L	 *s   )')c                p   #    U R                   R                  U R                  5       5      I Sh  vN $  N7f)z
Read a parsed row of data from a table after a :sql:`COPY TO` operation.

Return `!None` when the data is finished.

Note that the records returned will be tuples of unparsed strings or
bytes, unless data types are specified using `set_types()`.
N)rC   rD   _read_row_genr2   s    r,   rI   AsyncCopy.read_rowk   s+      __))$*<*<*>????rG   c                   #    U R                   R                  U5      nU(       a  U R                  U5      I Sh  vN   gg N7f)z
Write a block of data to a table after a :sql:`COPY FROM` operation.

If the :sql:`COPY` is in binary format `!buffer` must be `!bytes`. In
text mode it can be either `!bytes` or `!str`.
N)	formatterr'   r(   )r)   bufferr?   s      r,   r'   AsyncCopy.writev   s8      ~~##F+++d### #   7AA Ac                   #    U R                   R                  U5      nU(       a  U R                  U5      I Sh  vN   gg N7f)z=Write a record to a table after a :sql:`COPY FROM` operation.N)rQ   	write_rowr(   )r)   rowr?   s      r,   rV   AsyncCopy.write_row   s6     ~~'',++d### #rT   c                  #    U R                   [        :X  ae  U R                  R                  5       nU(       a  U R	                  U5      I Sh  vN   U R
                  R                  U5      I Sh  vN   SU l        gU(       d  gU R                  R                  [        :w  a  gU R                  R                  5       I Sh  vN   U R                  R                  U R                  5       5      I Sh  vN   g N N N: N7f)zTerminate the copy operation and free the resources allocated.

You shouldn't need to call this function yourself: it is usually called
by exit. It is available if, despite what is documented, you end up
using the `Copy` object outside a block.
NT)
_directionCOPY_INrQ   endr(   r!   r6   	_finished_pgconntransaction_statusACTIVErC   _try_cancelrD   _end_copy_out_gen)r)   excr?   s      r,   r6   AsyncCopy.finish   s      ??g%>>%%'Dkk$'''++$$S)))!DN||..&8  //--/////&&t'='='?@@@# () 0@sI   A
C?C7"C?/C90AC? C;0C?1C=2C?9C?;C?=C?)r]   r(   r!   )r*   AsyncCursor[Any]r"   zbool | Noner!   zAsyncWriter | None)returnr   )r7   ztype[BaseException] | Noner8   BaseException | Noner9   zTracebackType | Nonerf   None)rf   zAsyncIterator[Buffer])rf   r   )rf   zAsyncIterator[tuple[Any, ...]])rf   ztuple[Any, ...] | None)rR   zBuffer | strrf   rh   )rW   zSequence[Any]rf   rh   rc   rg   rf   rh   )__name__
__module____qualname____firstlineno____doc____annotations__r%   r3   r:   r@   r=   rK   rI   r'   rV   r6   __static_attributes____classcell__r+   s   @r,   r   r      s     J #%)# # 	#
 ## ##,# &# %	#
 
#<	@	$$A Ar.   r   zAsyncConnection[Any]c                  :    \ rS rSrSr\SS j5       rSS	S jjrSrg)
r       z?
A class to write copy data somewhere (for async connections).
c                   #    g7f)zWrite some data to destination.N r>   s     r,   r'   AsyncWriter.write   s
      	   Nc                   #    g7f)zq
Called when write operations are finished.

If operations finished with an error, it will be passed to ``exc``.
Nrv   )r)   rc   s     r,   r6   AsyncWriter.finish   s
      	rx   rv   r?   r   rf   rh   r0   ri   )	rj   rk   rl   rm   rn   r   r'   r6   rp   rv   r.   r,   r    r       s%       r.   r    c                  >    \ rS rSrSrSrS	S jrS
S jrSSS jjrSrg)r&      z=
An `AsyncWriter` to write copy data to a Postgres database.
psycopg.copyc                h    Xl         UR                  U l        U R                  R                  U l        g r0   )r*   rC   pgconnr^   )r)   r*   s     r,   r%   AsyncLibpqWriter.__init__   s%     ++--r.   c           
     x  #    [        U5      [        ::  a;  U R                  R                  [	        U R
                  U[        S95      I S h  vN   g [        S[        U5      [        5       HF  nU R                  R                  [	        U R
                  XU[        -    [        S95      I S h  vN   MH     g  Nj N7f)Nflushr   )lenr   rC   rD   r   r^   r   ranger)   r?   is      r,   r'   AsyncLibpqWriter.write   s     t9' //&&wt||T'VWWW 1c$i9oo**dq?/B&C<   :	 X
s%   A	B:B6A B:,B8-
B:8B:Nc                ~  #    U(       aB  S[        U5      R                   SU 3nUR                  U R                  R                  S5      nOS n U R
                  R                  [        U R                  U5      5      I S h  vN nU/U R                  l	        g  N! [        R                   a    U(       d  e  g f = f7f)Nzerror from Python: z - replace)typerl   encoder^   	_encodingrC   rD   r   r*   _resultseQueryCanceled)r)   rc   msgbmsgress        r,   r6   AsyncLibpqWriter.finish   s     'S	(>(>'?s3%HC::dll44i@DD		),,XdllD-IJJC %(5DKK  K  	 	s<   AB=2B BB B=B B:7B=9B::B=)r^   rC   r*   r*   re   r{   r0   ri   )	rj   rk   rl   rm   rn   r%   r'   r6   rp   rv   r.   r,   r&   r&      s"      J.
) )r.   r&   c                  \   ^  \ rS rSrSrSrS	U 4S jjrS
S jrSS jrSSU 4S jjjrSr	U =r
$ )AsyncQueuedLibpqWriter   z
`AsyncWriter` using a buffer to queue data to write.

`write()` returns immediately, so that the main thread can be CPU-bound
formatting messages, while a worker thread can be IO-bound waiting to write
on the connection.
r~   c                d   > [         TU ]  U5        [        [        S9U l        S U l        S U l        g )N)maxsize)r$   r%   r   r   _queue_worker_worker_error)r)   r*   r+   s     r,   r%   AsyncQueuedLibpqWriter.__init__   s+     &,Z&@'+37r.   c                  #      U R                   R                  5       I Sh  vN nU(       d  gU R                  R                  [	        U R
                  U[        S95      I Sh  vN   Mf   NH N! [         a  nX l         SnAgSnAff = f7f)zPush data to the server when available from the copy queue.

Terminate reading when the queue receives a false-y value, or in case
of error.

The function is designed to be run in a separate task.
Nr   )	r   getrC   rD   r   r^   r   BaseExceptionr   )r)   r?   exs      r,   workerAsyncQueuedLibpqWriter.worker   sy     
	$![[__..oo**DLL$lC  	 .  	$!#	$sN   BA/ A+A/ B5A/ $A-%A/ -A/ /
B	9B?BB		Bc                  #    U R                   (       d  [        U R                  5      U l         U R                  (       a  U R                  e[	        U5      [
        ::  a$  U R                  R                  U5      I S h  vN   g [        S[	        U5      [
        5       H/  nU R                  R                  XU[
        -    5      I S h  vN   M1     g  NS N7f)Nr   )	r   r   r   r   r   r   r   putr   r   s      r,   r'   AsyncQueuedLibpqWriter.write  s     ||!$++.DL $$$t9' ++//$''' 1c$i9kkoodq?/B&CDDD :	 (
 Es%   A:C<C=A	CC
CCc                2  >#    U R                   R                  S5      I S h  vN   U R                  (       a$  [        U R                  5      I S h  vN   S U l        U R                  (       a  U R                  e[
        TU ]  U5      I S h  vN   g  Nn NB N	7f)Nr.   )r   r   r   r   r   r$   r6   )r)   rc   r+   s     r,   r6   AsyncQueuedLibpqWriter.finish   st     kkooc"""<<$,,'''DL $$$gnS!!! 	# ( 	"s3   BB-BB:BBBBB)r   r   r   r   )rf   rh   r{   r0   ri   )rj   rk   rl   rm   rn   r%   r   r'   r6   rp   rq   rr   s   @r,   r   r      s)      J8$(E&" "r.   r   N).rn   
__future__r   abcr   r   typesr   typingr   r   r	   r
    r   r   r   _compatr   
_copy_baser   r   r   r   
generatorsr   r   _acompatr   r   r   r   r   cursor_asyncr   connection_asyncr   
ExecStatusr[   COPY_OUTTransactionStatusr`   r   r    r&   r   rv   r.   r,   <module>r      s    # #  > >    K K ) 6 6)1
--

==!!				$	$CA/0 CAL# &,){ ,)^D"- D"r.   