§
    YŠtj&  ã                  ó¢  — 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
 ddl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j&        j'        Z'ej&        j(        Z(ej)        j*        Z* G d„ ded         ¦  «        Z+ G d„ de¦  «        Z, G d„ de,¦  «        Z- G d„ de-¦  «        Z.dS )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d0d%„Zd1d'„Zˆ xZS )2Ú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    ÚcursorúCursor[Any]r!   úbool | NoneúWriter | Nonec               ó–   •— t          ¦   «                              ||¬¦  «         |st          |¦  «        }|| _        |j        | _        d S )N)r!   )ÚsuperÚ__init__ÚLibpqWriterr    ÚwriteÚ_write)Úselfr"   r!   r    Ú	__class__s       €úK/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/psycopg/_copy.pyr(   zCopy.__init__6   sK   ø€ õ 	‰Œ×Ò˜¨ÐÑ/Ô/Ð/Øð 	)Ý  Ñ(Ô(ˆFàˆŒØ”lˆŒˆˆó    Úreturnr   c                ó.   — |                       ¦   «          | S ©N)Ú_enter©r,   s    r.   Ú	__enter__zCopy.__enter__D   s   € Ø�Š‰ŒˆØˆr/   Úexc_typeútype[BaseException] | NoneÚexc_valúBaseException | NoneÚexc_tbúTracebackType | NoneÚNonec                ó0   — |                       |¦  «         d S r2   )Úfinish)r,   r6   r8   r:   s       r.   Ú__exit__zCopy.__exit__H   s   € ð 	�Š�GÑÔÐÐÐr/   úIterator[Buffer]c              #  ón   K  — |                       ¦   «         x}r|V — |                       ¦   «         x}°dS dS )z5Implement block-by-block iteration on :sql:`COPY TO`.N)Úread©r,   Údatas     r.   Ú__iter__zCopy.__iter__R   sO   è è € à—i’i‘k”kÐ!ˆdð 	ØˆJˆJˆJð —i’i‘k”kÐ!ˆdð 	ð 	ð 	ð 	ð 	r/   r   c                óZ   — | 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_genr4   s    r.   rB   z	Copy.readW   s$   € ð Œ×#Ò# D§N¢NÑ$4Ô$4Ñ5Ô5Ð5r/   úIterator[tuple[Any, ...]]c              #  ón   K  — |                       ¦   «         x}�|V — |                       ¦   «         x}­dS dS )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_   sE   è è € ð Ÿš™œÐ(ˆvÐ5ØˆLˆLˆLð Ÿš™œÐ(ˆvÐ5Ð5Ð5Ð5Ð5r/   útuple[Any, ...] | Nonec                óZ   — | 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()`.
        )rG   rH   Ú_read_row_genr4   s    r.   rL   zCopy.read_rowi   s&   € ð Œ×#Ò# D×$6Ò$6Ñ$8Ô$8Ñ9Ô9Ð9r/   ÚbufferúBuffer | strc                ól   — | j                              |¦  «        x}r|                      |¦  «         dS dS )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,   rR   rD   s      r.   r*   z
Copy.writet   sC   € ð ”>×'Ò'¨Ñ/Ô/Ð/ˆ4ð 	Ø�KŠK˜ÑÔÐÐÐð	ð 	r/   ÚrowúSequence[Any]c                ól   — | j                              |¦  «        x}r|                      |¦  «         dS dS )z=Write a record to a table after a :sql:`COPY FROM` operation.N)rU   Ú	write_rowr+   )r,   rV   rD   s      r.   rY   zCopy.write_row~   sA   € à”>×+Ò+¨CÑ0Ô0Ð0ˆ4ð 	Ø�KŠK˜ÑÔÐÐÐð	ð 	r/   Úexcc                ó�  — | j         t          k    rU|s0| j                             ¦   «         x}r|                      |¦  «         | j                             |¦  «         d| _        dS |sdS | j        j	        t          k    rdS | j                             ¦   «          | j                             |                      ¦   «         ¦  «         dS )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_INrU   Úendr+   r    r>   Ú	_finishedÚ_pgconnÚtransaction_statusÚACTIVErG   Ú_try_cancelrH   Ú_end_copy_out_gen)r,   rZ   rD   s      r.   r>   zCopy.finishƒ   sÉ   € ð Œ?�gÒ%Ð%Øð &Øœ>×-Ò-Ñ/Ô/Ð/�4ð &Ø—K’K Ñ%Ô%Ð%ØŒK×Ò˜sÑ#Ô#Ð#Ø!ˆDŒNˆNˆNàð Ø�ØŒ|Ô.µ&Ò8Ð8ð �ð
 ŒO×'Ò'Ñ)Ô)Ð)ØŒO× Ò  ×!7Ò!7Ñ!9Ô!9Ñ:Ô:Ð:Ð:Ð:r/   )r"   r#   r!   r$   r    r%   )r0   r   )r6   r7   r8   r9   r:   r;   r0   r<   )r0   r@   )r0   r   )r0   rJ   )r0   rO   )rR   rS   r0   r<   )rV   rW   r0   r<   ©rZ   r9   r0   r<   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r(   r5   r?   rE   rB   rN   rL   r*   rY   r>   Ú__classcell__©r-   s   @r.   r   r   #   s  ø€ € € € € € ðð ð €Jà€N€N�Nð #Ø $ð#ð #ð #ð #ð #ð #ð #ð #ðð ð ð ðð ð ð ðð ð ð ð
6ð 6ð 6ð 6ðð ð ð ð	:ð 	:ð 	:ð 	:ðð ð ð ðð ð ð ð
;ð ;ð ;ð ;ð ;ð ;ð ;ð ;r/   r   zConnection[Any]c                  ó4   — e Zd ZdZedd„¦   «         Zddd
„ZdS )r   zG
    A class to write copy data somewhere (for async connections).
    rD   r   r0   r<   c                ó   — dS )zWrite some data to destination.N© rC   s     r.   r*   zWriter.write¤   s	   € ð 	ˆr/   NrZ   r9   c                ó   — dS )z‰
        Called when write operations are finished.

        If operations finished with an error, it will be passed to ``exc``.
        Nro   )r,   rZ   s     r.   r>   zWriter.finish©   s	   € ð 	ˆr/   ©rD   r   r0   r<   r2   re   )rf   rg   rh   ri   r   r*   r>   ro   r/   r.   r   r   Ÿ   sW   € € € € € ðð ð ðð ð ñ „^ððð ð ð ð ð ð r/   r   c                  ó0   — e Zd ZdZdZdd„Zdd
„Zddd„ZdS )r)   z@
    An `Writer` to write copy data to a Postgres database.
    úpsycopg.copyr"   r#   c                óN   — || _         |j        | _        | j        j        | _        d S r2   )r"   rG   Úpgconnr`   )r,   r"   s     r.   r(   zLibpqWriter.__init__¹   s#   € ØˆŒØ Ô+ˆŒØ”Ô-ˆŒˆˆr/   rD   r   r0   r<   c           
     óv  — t          |¦  «        t          k    r6| j                             t	          | j        |t          ¬¦  «        ¦  «         d S t          dt          |¦  «        t          ¦  «        D ]F}| j                             t	          | j        |||t          z   …         t          ¬¦  «        ¦  «         ŒGd S )N©Úflushr   )Úlenr   rG   rH   r   r`   r   Úrange©r,   rD   Úis      r.   r*   zLibpqWriter.write¾   s¸   € Ýˆt‰9Œ9�Ò'Ð'ð ŒO× Ò ¥¨¬°tÅ<Ð!PÑ!PÔ!PÑQÔQÐQÐQÐQõ ˜1�c $™iœi­Ñ9Ô9ð ð �Ø”×$Ò$ÝØœ d¨1¨qµ?Ñ/BÐ+BÔ&CÍ<ðñ ô ñô ð ð ðð r/   NrZ   r9   c                ó,  — |r;dt          |¦  «        j        › d|› �}|                     | j        j        d¦  «        }nd }	 | j                             t          | j        |¦  «        ¦  «        }|g| j        _	        d S # t          j        $ r |s‚ Y d S w xY w)Nzerror from Python: z - Úreplace)Útyperh   Úencoder`   Ú	_encodingrG   rH   r   r"   Ú_resultsÚeÚQueryCanceled)r,   rZ   ÚmsgÚbmsgÚress        r.   r>   zLibpqWriter.finishÍ   sº   € àð 	ØH­¨S©	¬	Ô(>ÐHÐHÀ3ÐHÐHˆCØ—:’:˜dœlÔ4°iÑ@Ô@ˆDˆDàˆDð		)Ø”/×&Ò&¥x°´¸dÑ'CÔ'CÑDÔDˆCð %( 5ˆDŒKÔ Ð Ð øõ	 Œð 	ð 	ð 	Øð Øðð ð ð	øøøs   Á-A= Á=BÂB©r"   r#   rq   r2   re   )rf   rg   rh   ri   r(   r*   r>   ro   r/   r.   r)   r)   ²   sf   € € € € € ðð ð  €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.
    rs   r"   r#   c                óš   •— t          ¦   «                              |¦  «         t          t          ¬¦  «        | _        d | _        d | _        d S )N)Úmaxsize)r'   r(   r   r   Ú_queueÚ_workerÚ_worker_error)r,   r"   r-   s     €r.   r(   zQueuedLibpqWriter.__init__ì   sB   ø€ Ý‰Œ×Ò˜Ñ Ô Ð å%*µ:Ð%>Ñ%>Ô%>ˆŒØ&*ˆŒØ37ˆÔÐÐr/   r0   r<   c                ó  — 	 | j                              ¦   «         x}rQ| j                             t	          | j        |t          ¬¦  «        ¦  «         | j                              ¦   «         x}°OdS dS # t          $ r}|| _        Y d}~dS 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.
        rw   N)	r�   ÚgetrG   rH   r   r`   r   ÚBaseExceptionr�   )r,   rD   Úexs      r.   ÚworkerzQueuedLibpqWriter.workeró   sµ   € ð	$Øœ+Ÿ/š/Ñ+Ô+Ð+�$ð VØ”×$Ò$¥W¨T¬\¸4Å|Ð%TÑ%TÔ%TÑUÔUÐUð œ+Ÿ/š/Ñ+Ô+Ð+�$ð Vð Vð Vð Vð Vøåð 	$ð 	$ð 	$à!#ˆDÔÐÐÐÐÐÐøøøøð	$øøøs   ‚A*A0 Á0
BÁ:BÂBrD   r   c                ój  — | j         st          | j        ¦  «        | _         | j        r| j        ‚t	          |¦  «        t
          k    r| j                             |¦  «         d S t          dt	          |¦  «        t
          ¦  «        D ],}| j                             |||t
          z   …         ¦  «         Œ-d S )Nr   )	rŽ   r   r”   r�   ry   r   r�   Úputrz   r{   s      r.   r*   zQueuedLibpqWriter.write  s³   € ØŒ|ð 	.å  ¤Ñ-Ô-ˆDŒLð Ôð 	%ØÔ$Ð$åˆt‰9Œ9�Ò'Ð'ð ŒK�OŠO˜DÑ!Ô!Ð!Ð!Ð!õ ˜1�c $™iœi­Ñ9Ô9ð ?ð ?�Ø”—’  Q¨­_Ñ)<Ð%<Ô =Ñ>Ô>Ð>Ð>ð?ð ?r/   NrZ   r9   c                óÞ   •— | j                              d¦  «         | j        rt          | j        ¦  «         d | _        | j        r| j        ‚t          ¦   «                              |¦  «         d S )Nr/   )r�   r–   rŽ   r   r�   r'   r>   )r,   rZ   r-   s     €r.   r>   zQueuedLibpqWriter.finish  sj   ø€ ØŒ�Š˜ÑÔÐàŒ<ð 	 Ý�4”<Ñ Ô Ð ØˆDŒLð Ôð 	%ØÔ$Ð$å‰Œ�Š�sÑÔÐÐÐr/   rˆ   )r0   r<   rq   r2   re   )	rf   rg   rh   ri   r(   r”   r*   r>   rk   rl   s   @r.   rŠ   rŠ   á   s™   ø€ € € € € ðð ð  €Jð8ð 8ð 8ð 8ð 8ð 8ð$ð $ð $ð $ð?ð ?ð ?ð ?ð&ð ð ð ð ð ð ð ð ð ð r/   rŠ   N)/ri   Ú
__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   rG   r   Ú
ExecStatusr]   ÚCOPY_OUTÚTransactionStatusrb   r   r   r)   rŠ   ro   r/   r.   ú<module>r¥      s2  ððð ð #Ð "Ð "Ð "Ð "Ð "à #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %Ð %Ð %Ø .Ð .Ð .Ð .Ð .Ð .Ð .Ð .à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KØ )Ð )Ð )Ð )Ð )Ð )Ð )Ð )àð 'ØÐÐÐÐÐØÐÐÐÐÐØ&Ð&Ð&Ð&Ð&Ð&à
Œ-Ô
€ØŒ=Ô!€à	Ô	Ô	$€ðy;ð y;ð y;ð y;ð y;ˆ8Ð%Ô&ñ y;ô y;ð y;ðxð ð ð ð ˆSñ ô ð ð&,)ð ,)ð ,)ð ,)ð ,)�&ñ ,)ô ,)ð ,)ð^?ð ?ð ?ð ?ð ?˜ñ ?ô ?ð ?ð ?ð ?r/   