Ë
    êëjì%  ã                  ó¶  — d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZ ddlmZmZmZmZ ddlmZmZ e	rddlm Z  ddl!m"Z" ddl#m$Z$ ejJ                  jL                  Z&ejJ                  jN                  Z'ejP                  jR                  Z) G d„ ded   «      Z* G d„ de«      Z+ G d„ de+«      Z, G d„ de,«      Z-y)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                  óž   ‡ — e Zd ZU dZdZded<   dddœ	 	 	 	 	 dˆ fd„Zdd„Z	 	 	 	 	 	 	 	 dd	„Zdd
„Zdd„Z	dd„Z
dd„Zdd„Zdd„Zdd„Zˆ xZS )ÚCopyaj  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         ‰| �  ||¬«       |st        |«      }|| _        |j                  | _        y )N)r!   )ÚsuperÚ__init__ÚLibpqWriterr    ÚwriteÚ_write)ÚselfÚcursorr!   r    Ú	__class__s       €úT/var/www/html/kelly/kelly-backend/venv/lib/python3.12/site-packages/psycopg/_copy.pyr$   zCopy.__init__5   s6   ø€ ô 	‰Ñ˜¨ÐÔ/ÙÜ  Ó(ˆFàˆŒØ—l‘lˆ�ó    c                ó&   — | j                  «        | S ©N)Ú_enter©r(   s    r+   Ú	__enter__zCopy.__enter__C   s   € Ø�‰ŒØˆr,   c                ó&   — | j                  |«       y r.   )Úfinish)r(   Úexc_typeÚexc_valÚexc_tbs       r+   Ú__exit__zCopy.__exit__G   s   € ð 	�‰�GÕr,   c              #  ó`   K  — | j                  «       x}r|–— | j                  «       x}rŒyy­w)z5Implement block-by-block iteration on :sql:`COPY TO`.N)Úread©r(   Údatas     r+   Ú__iter__zCopy.__iter__Q   s/   è ø€ à—i‘i“kÐ!ˆdÐ!ØŠJð —i‘i“kÐ!ˆdÓ!ùó   ‚).¬.c                óT   — | j                   j                  | j                  «       «      S )zƒ
        Read an unparsed row after a :sql:`COPY TO` operation.

        Return an empty string when the data is finished.
        )Ú
connectionÚwaitÚ	_read_genr0   s    r+   r9   z	Copy.readV   s    € ð �‰×#Ñ# D§N¡NÓ$4Ó5Ð5r,   c              #  ó`   K  — | j                  «       x}�|–— | j                  «       x}�Œyy­w)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+   Úrowsz	Copy.rows^   s1   è ø€ ð Ÿ™›Ð(ˆvÐ5ØŠLð Ÿ™›Ð(ˆvÓ5ùr=   c                óT   — | j                   j                  | j                  «       «      S )a  
        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()`.
        )r?   r@   Ú_read_row_genr0   s    r+   rC   zCopy.read_rowh   s"   € ð �‰×#Ñ# D×$6Ñ$6Ó$8Ó9Ð9r,   c                ób   — | j                   j                  |«      x}r| j                  |«       yy)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&   z
Copy.writes   s/   € ð —>‘>×'Ñ'¨Ó/Ð/ˆ4Ð/Ø�K‰K˜Õð 0r,   c                ób   — | j                   j                  |«      x}r| j                  |«       yy)z=Write a record to a table after a :sql:`COPY FROM` operation.N)rI   Ú	write_rowr'   )r(   Úrowr;   s      r+   rL   zCopy.write_row}   s-   € à—>‘>×+Ñ+¨CÓ0Ð0ˆ4Ð0Ø�K‰K˜Õð 1r,   c                ó–  — | j                   t        k(  rR|s-| j                  j                  «       x}r| j	                  |«       | j
                  j                  |«       d| _        y|sy| j                  j                  t        k7  ry| j                  j                  «        | j                  j                  | j                  «       «       y)a  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_INrI   Úendr'   r    r3   Ú	_finishedÚ_pgconnÚtransaction_statusÚACTIVEr?   Ú_try_cancelr@   Ú_end_copy_out_gen)r(   Úexcr;   s      r+   r3   zCopy.finish‚   sš   € ð �?‰?œgÒ%ÙØŸ>™>×-Ñ-Ó/Ð/�4Ð/Ø—K‘K Ô%Ø�K‰K×Ñ˜sÔ#Ø!ˆD�NáØØ�|‰|×.Ñ.´&Ò8ð ð
 �O‰O×'Ñ'Ô)Ø�O‰O× Ñ  ×!7Ñ!7Ó!9Õ:r,   )r)   úCursor[Any]r!   zbool | Noner    zWriter | None)Úreturnr   )r4   ztype[BaseException] | Noner5   úBaseException | Noner6   zTracebackType | NonerZ   ÚNone)rZ   zIterator[Buffer])rZ   r   )rZ   zIterator[tuple[Any, ...]])rZ   ztuple[Any, ...] | None)rJ   zBuffer | strrZ   r\   )rM   zSequence[Any]rZ   r\   ©rX   r[   rZ   r\   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r$   r1   r7   r<   r9   rE   rC   r&   rL   r3   Ú__classcell__©r*   s   @r+   r   r   "   s�   ø… ñð €JàƒNð #Ø $ñ#àð#ð ð	#ð
 õ#óðà,ðð &ðð %ð	ð
 
óóó
6óó	:óó÷
;r,   r   zConnection[Any]c                  ó,   — e Zd ZdZedd„«       Zddd„Zy)r   zG
    A class to write copy data somewhere (for async connections).
    c                 ó   — y)zWrite some data to destination.N© r:   s     r+   r&   zWriter.write£   s   € ð 	r,   Nc                 ó   — y)z‰
        Called when write operations are finished.

        If operations finished with an error, it will be passed to ``exc``.
        Nrg   )r(   rX   s     r+   r3   zWriter.finish¨   s   € ð 	r,   ©r;   r   rZ   r\   r.   r]   )r^   r_   r`   ra   r   r&   r3   rg   r,   r+   r   r   ž   s    „ ñð òó ðõr,   r   c                  ó.   — e Zd ZdZdZdd„Zdd„Zd	d
d„Zy)r%   z@
    An `Writer` to write copy data to a Postgres database.
    úpsycopg.copyc                ój   — || _         |j                  | _        | j                  j                  | _        y r.   )r)   r?   ÚpgconnrS   )r(   r)   s     r+   r$   zLibpqWriter.__init__¸   s'   € ØˆŒØ ×+Ñ+ˆŒØ—‘×-Ñ-ˆ�r,   c           
     óP  — t        |«      t        k  r6| j                  j                  t	        | j
                  |t        ¬«      «       y t        dt        |«      t        «      D ]A  }| j                  j                  t	        | j
                  |||t        z    t        ¬«      «       ŒC y )N©Úflushr   )Úlenr   r?   r@   r   rS   r   Úrange©r(   r;   Úis      r+   r&   zLibpqWriter.write½   sy   € Üˆt‹9œÒ'ð �O‰O× Ñ ¤¨¯©°tÄ<Ô!PÕQô ˜1œc $›i¬Ö9�Ø—‘×$Ñ$ÜØŸ™ d¨1¨q´?Ñ/BÐ&CÌ<ôõñ :r,   Nc                óL  — |rBdt        |«      j                  › d|› �}|j                  | j                  j                  d«      }nd }	 | j
                  j                  t        | j                  |«      «      }|g| j                  _	        y # t        j                  $ r |s‚ Y y w xY w)Nzerror from Python: z - Úreplace)Útyper`   ÚencoderS   Ú	_encodingr?   r@   r   r)   Ú_resultsÚeÚQueryCanceled)r(   rX   ÚmsgÚbmsgÚress        r+   r3   z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)   rY   ri   r.   r]   )r^   r_   r`   ra   r$   r&   r3   rg   r,   r+   r%   r%   ±   s   „ ñð  €Jó.ó
õ)r,   r%   c                  óF   ‡ — e Zd ZdZdZdˆ fd„Zdd„Zd	d„Zd
dˆ fd„Zˆ xZS )ÚQueuedLibpqWriterzí
    `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.
    rk   c                ój   •— t         ‰| �  |«       t        t        ¬«      | _        d | _        d | _        y )N)Úmaxsize)r#   r$   r   r   Ú_queueÚ_workerÚ_worker_error)r(   r)   r*   s     €r+   r$   zQueuedLibpqWriter.__init__ë   s+   ø€ Ü‰Ñ˜Ô ä%*´:Ô%>ˆŒØ&*ˆŒØ37ˆÕr,   c                ó  — 	 | j                   j                  «       x}rS| j                  j                  t	        | j
                  |t        ¬«      «       | j                   j                  «       x}rŒRyy# t        $ r}|| _        Y d}~yd}~ww xY w)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.
        ro   N)	r…   Úgetr?   r@   r   rS   r   ÚBaseExceptionr‡   )r(   r;   Úexs      r+   ÚworkerzQueuedLibpqWriter.workerò   so   € ð	$ØŸ+™+Ÿ/™/Ó+Ð+�$Ð+Ø—‘×$Ñ$¤W¨T¯\©\¸4Ä|Ô%TÔUð Ÿ+™+Ÿ/™/Ó+Ð+�$Ó+øäò 	$à!#ˆD×Ñûð	$ús   ‚A-A2 Á2	BÁ;BÂBc                ód  — | j                   st        | j                  «      | _         | j                  r| j                  ‚t	        |«      t
        k  r| j                  j                  |«       y t        dt	        |«      t
        «      D ]'  }| j                  j                  |||t
        z    «       Œ) y )Nr   )	r†   r   rŒ   r‡   rq   r   r…   Úputrr   rs   s      r+   r&   zQueuedLibpqWriter.write  sƒ   € Ø�|Š|ä  §¡Ó-ˆDŒLð ×ÒØ×$Ñ$Ð$äˆt‹9œÒ'ð �K‰K�O‰O˜DÕ!ô ˜1œc $›i¬Ö9�Ø—‘—‘  Q¨¬_Ñ)<Ð =Õ>ñ :r,   c                óÚ   •— | j                   j                  d«       | j                  rt        | j                  «       d | _        | j                  r| j                  ‚t
        ‰| �  |«       y )Nr,   )r…   rŽ   r†   r   r‡   r#   r3   )r(   rX   r*   s     €r+   r3   zQueuedLibpqWriter.finish  sP   ø€ Ø�‰�‰˜Ôà�<Š<Ü�4—<‘<Ô ØˆDŒLð ×ÒØ×$Ñ$Ð$ä‰‰�sÕr,   r€   )rZ   r\   ri   r.   r]   )	r^   r_   r`   ra   r$   rŒ   r&   r3   rc   rd   s   @r+   r‚   r‚   à   s(   ø„ ñð  €Jõ8ó$ó?÷&ò r,   r‚   N).ra   Ú
__future__r   Úabcr   r   Útypesr   Útypingr   r   r	   r
   Ú r   r{   r   Ú_compatr   Ú_acompatr   r   r   r   Ú
_copy_baser   r   r   r   Ú
generatorsr   r   r   r)   r   r?   r   Ú
ExecStatusrP   ÚCOPY_OUTÚTransactionStatusrU   r   r   r%   r‚   rg   r,   r+   Ú<module>rœ      s¨   ðñõ #ç #Ý ß 9Ó 9å Ý Ý ß 2Ó 2ß KÓ Kß )áÝÝÝ&à
�-‰-×
Ñ
€Ø�=‰=×!Ñ!€à	×	Ñ	×	$Ñ	$€ôy;ˆ8Ð%Ñ&ô y;ôxˆSô ô&,)�&ô ,)ô^?˜õ ?r,   