
    (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)z6
Objects to support the COPY protocol (sync version).
    )annotations)ABCabstractmethod)TracebackType)AnyIteratorSequenceTYPE_CHECKING   )pq)errors)Self)BaseCopyMAX_BUFFER_SIZE
QUEUE_SIZEPREFER_FLUSH)copy_tocopy_end)spawngatherQueueWorker)Buffer)Cursor)
Connectionc                     ^  \ 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$ )Copy"   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WriterwriterN)binaryr!   c               r   > [         TU ]  XS9  U(       d  [        U5      nX0l        UR                  U l        g )N)r"   )super__init__LibpqWriterr!   write_write)selfcursorr"   r!   	__class__s       @/var/www/html/venv/lib/python3.13/site-packages/psycopg/_copy.pyr%   Copy.__init__5   s2     	/ (Fll    c                &    U R                  5         U $ N)_enterr)   s    r,   	__enter__Copy.__enter__C   s    r.   c                &    U R                  U5        g r0   )finish)r)   exc_typeexc_valexc_tbs       r,   __exit__Copy.__exit__G   s     	Gr.   c              #  H   #     U R                  5       nU(       d  gUv   M  7f)z5Implement block-by-block iteration on :sql:`COPY TO`.N)readr)   datas     r,   __iter__Copy.__iter__Q   s#     99;DJ	 s    "c                T    U R                   R                  U R                  5       5      $ )zk
Read an unparsed row after a :sql:`COPY TO` operation.

Return an empty string when the data is finished.
)
connectionwait	_read_genr2   s    r,   r=   	Copy.readY   s      ##DNN$455r.   c              #  @   #     U R                  5       nUc  gUv   M  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	Copy.rowsa   s'      ]]_F~L	 s   c                T    U R                   R                  U R                  5       5      $ )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()`.
)rC   rD   _read_row_genr2   s    r,   rH   Copy.read_rown   s"     ##D$6$6$899r.   c                l    U R                   R                  U5      nU(       a  U R                  U5        gg)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'   
Copy.writey   s,     ~~##F+KK r.   c                l    U R                   R                  U5      nU(       a  U R                  U5        gg)z=Write a record to a table after a :sql:`COPY FROM` operation.N)rP   	write_rowr(   )r)   rowr?   s      r,   rT   Copy.write_row   s*    ~~'',KK r.   c                   U R                   [        :X  aU  U R                  R                  5       nU(       a  U R	                  U5        U R
                  R                  U5        SU l        gU(       d  gU R                  R                  [        :w  a  gU R                  R                  5         U R                  R                  U R                  5       5        g)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.
TN)
_directionCOPY_INrP   endr(   r!   r6   	_finished_pgconntransaction_statusACTIVErC   _try_cancelrD   _end_copy_out_gen)r)   excr?   s      r,   r6   Copy.finish   s     ??g%>>%%'DD!KKs#!DN||..&8 
 OO'')OO  !7!7!9:r.   )r[   r(   r!   )r*   Cursor[Any]r"   zbool | Noner!   zWriter | None)returnr   )r7   ztype[BaseException] | Noner8   BaseException | Noner9   zTracebackType | Nonerd   None)rd   zIterator[Buffer])rd   r   )rd   zIterator[tuple[Any, ...]])rd   ztuple[Any, ...] | None)rQ   zBuffer | strrd   rf   )rU   zSequence[Any]rd   rf   ra   re   rd   rf   )__name__
__module____qualname____firstlineno____doc____annotations__r%   r3   r:   r@   r=   rJ   rH   r'   rT   r6   __static_attributes____classcell__r+   s   @r,   r   r   "   s     JN # $## 	#
 # #, & %	
 
6	:	; ;r.   r   zConnection[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)zWrite some data to destination.N r>   s     r,   r'   Writer.write   s     	r.   Nc                    g)zq
Called when write operations are finished.

If operations finished with an error, it will be passed to ``exc``.
Nrt   )r)   ra   s     r,   r6   Writer.finish   s     	r.   rt   r?   r   rd   rf   r0   rg   )	rh   ri   rj   rk   rl   r   r'   r6   rn   rt   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&      z8
An `Writer` 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%   LibpqWriter.__init__   s%     ++--r.   c           
     H   [        U5      [        ::  a3  U R                  R                  [	        U R
                  U[        S95        g [        S[        U5      [        5       H>  nU R                  R                  [	        U R
                  XU[        -    [        S95        M@     g )Nflushr   )lenr   rC   rD   r   r\   r   ranger)   r?   is      r,   r'   LibpqWriter.write   sw    t9' OO  t<!PQ 1c$i9$$dq?/B&C< :r.   Nc                b   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      nU/U R                  l	        g ! [        R                   a    U(       d  e  g f = f)Nzerror from Python: z - replace)typerj   encoder\   	_encodingrC   rD   r   r*   _resultseQueryCanceled)r)   ra   msgbmsgress        r,   r6   LibpqWriter.finish   s    'S	(>(>'?s3%HC::dll44i@DD		)//&&xd'CDC %(5DKK 	  	 	s   /B B.-B.)r\   rC   r*   r*   rc   rx   r0   rg   )	rh   ri   rj   rk   rl   r%   r'   r6   rn   rt   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
$ )QueuedLibpqWriter   z
`Writer` 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%   QueuedLibpqWriter.__init__   s+     %*:%>&*37r.   c                      U R                   R                  5       nU(       d  gU R                  R                  [	        U R
                  U[        S95        MV  ! [         a  nX l         SnAgSnAff = 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.
r   N)	r   getrC   rD   r   r\   r   BaseExceptionr   )r)   r?   exs      r,   workerQueuedLibpqWriter.worker   s_    	${{($$WT\\4|%TU	 
  	$!#	$s   "A 4A 
A3#A..A3c                |   U R                   (       d  [        U R                  5      U l         U R                  (       a  U R                  e[	        U5      [
        ::  a  U R                  R                  U5        g [        S[	        U5      [
        5       H'  nU R                  R                  XU[
        -    5        M)     g )Nr   )	r   r   r   r   r   r   r   putr   r   s      r,   r'   QueuedLibpqWriter.write  s    || -DL $$$t9' KKOOD! 1c$i9_)< => :r.   c                   > U R                   R                  S5        U R                  (       a  [        U R                  5        S U l        U R                  (       a  U R                  e[
        TU ]  U5        g )Nr.   )r   r   r   r   r   r$   r6   )r)   ra   r+   s     r,   r6   QueuedLibpqWriter.finish  sP    <<4<< DL $$$sr.   )r   r   r   r   )rd   rf   rx   r0   rg   )rh   ri   rj   rk   rl   r%   r   r'   r6   rn   ro   rp   s   @r,   r   r      s(      J8$$?& r.   r   N).rl   
__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   r*   r   rC   r   
ExecStatusrY   COPY_OUTTransactionStatusr^   r   r    r&   r   rt   r.   r,   <module>r      s    # #  9 9    K K ) 2 2&
--

==!!				$	$A;8%& A;HS &,)& ,)^B Br.   