ó
    Ð]j&  ã                  óÈ  • 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
  SSK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JrJr  SSKJrJ r   \	(       a  SSKJ!r!  SSK"J#r#  SSK$J%r%  \RL                  RN                  r'\RL                  RP                  r(\RR                  RT                  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)ÚTYPE_CHECKINGÚAny)ÚIteratorÚSequenceé   )Úerrors)Úpq)ÚSelf)ÚQueueÚWorkerÚgatherÚspawn)ÚMAX_BUFFER_SIZEÚPREFER_FLUSHÚ
QUEUE_SIZEÚBaseCopy)Úcopy_endÚcopy_to)Ú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       €ÚJ/home/mande/repo/quber/.venv/lib/python3.13/site-packages/psycopg/_copy.pyr%   ÚCopy.__init__6   s2   ø€ ô 	‰Ñ˜ÐÑ/ÞÜ  Ó(ˆFàŒØ—l‘lˆ�ó    c                ó&   • U R                  5         U $ ©N)Ú_enter©r)   s    r,   Ú	__enter__ÚCopy.__enter__D   s   € Ø�‰ŒØˆr.   c                ó&   • U R                  U5        g r0   )Úfinish)r)   Úexc_typeÚexc_valÚexc_tbs       r,   Ú__exit__ÚCopy.__exit__H   s   € ð 	�‰�GÕr.   c              #  óv   #   • U R                  5       =n(       a  Uv •  U R                  5       =n(       a  M  gg7f)z5Implement block-by-block iteration on :sql:`COPY TO`.N)Úread©r)   Údatas     r,   Ú__iter__ÚCopy.__iter__R   s1   é € à—i‘i“kÐ!ˆdÕ!ØŠJð —i‘i“kÐ!ˆd×!Ñ!ùs   ‚39·9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.readW   s    € ð �‰×#Ñ# D§N¡NÓ$4Ó5Ð5r.   c              #  óf   #   • U R                  5       =nb  Uv •  U R                  5       =nb  M  gg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.rows_   s0   é € ð Ÿ™›Ð(ˆvÑ5ØŠLð Ÿ™›Ð(ˆvÕ5ùs   ‚+1¯1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_rowi   s"   € ð �‰×#Ñ# D×$6Ñ$6Ó$8Ó9Ð9r.   c                ól   • U R                   R                  U5      =n(       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.writet   s/   € ð —>‘>×'Ñ'¨Ó/Ð/ˆ4Õ/Ø�K‰K˜Õð 0r.   c                ól   • U R                   R                  U5      =n(       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-   € à—>‘>×+Ñ+¨CÓ0Ð0ˆ4Õ0Ø�K‰K˜Õð 1r.   c                ó¸  • U R                   [        :X  a\  U(       d2  U R                  R                  5       =n(       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)zõTerminate 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Ó%ÞØŸ>™>×-Ñ-Ó/Ð/�4Õ/Ø—K‘K Ô%Ø�K‰K×Ñ˜sÔ#Ø!ˆD�NæØØ�|‰|×.Ñ.´&Ó8ð ð
 �O‰O×'Ñ'Ô)Ø�O‰O× Ñ  ×!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   € Üˆt‹9œÓ'ð �O‰O× Ñ ¤¨¯©°tÄ<Ñ!PÕQô ˜1œc $›i¬Ö9�Ø—‘×$Ñ$ÜØŸ™ d¨q´?Ñ/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«	×(>Ñ(>Ð'?¸sÀ3À%ÐHˆCØ—:‘:˜dŸl™l×4Ñ4°iÓ@‰DàˆDð		)Ø—/‘/×&Ñ&¤x°·±¸dÓ'CÓDˆCð %( 5ˆD�K‰KÕ øô	 �‰ó 	ÞØñ ð	ú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(       aV  U R                  R                  [	        U R
                  U[        S95        U R                   R                  5       =n(       a  MU  gg! [         a  nX l         SnAgSnAff = f)zÅPush 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ó   sp   € ð	$ØŸ+™+Ÿ/™/Ó+Ð+�$Õ+Ø—‘×$Ñ$¤W¨T¯\©\¸4Ä|Ñ%TÔUð Ÿ+™+Ÿ/™/Ó+Ð+�$×+Ñ+øäó 	$à!#×Ñûð	$ús   ‚A4A: Á:
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        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�   € Ø�|�|ä  §¡Ó-ˆDŒLð ××Ø×$Ñ$Ð$äˆt‹9œÓ'ð �K‰K�O‰O˜DÕ!ô ˜1œc $›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—<‘<Ô ØˆDŒLð ××Ø×$Ñ$Ð$ä‰‰�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   Úcollections.abcr	   r
   Ú r   r�   r   Ú_compatr   Ú_acompatr   r   r   r   Ú
_copy_baser   r   r   r   Ú
generatorsr   r   r   r*   r   rC   r   Ú
ExecStatusrY   ÚCOPY_OUTÚTransactionStatusr^   r   r    r&   r•   rt   r.   r,   Ú<module>rµ      s¨   ðñõ #ç #Ý ß %ß .å Ý Ý ß 2Ó 2ß KÓ Kß )æÝÝÝ&à
�-‰-×
Ñ
€Ø�=‰=×!Ñ!€à	×	Ñ	×	$Ñ	$€ôy;ˆ8Ð%Ñ&ô y;ôxˆSô ô&,)�&ô ,)ô^?˜õ ?r.   