§
    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 )z7
Objects to support the COPY protocol (async version).
é    )Úannotations)ÚABCÚabstractmethod)ÚTracebackType)ÚTYPE_CHECKINGÚAny)ÚAsyncIteratorÚSequenceé   )Úerrors)Úpq)ÚSelf)ÚAQueueÚAWorkerÚagatherÚaspawn)ÚMAX_BUFFER_SIZEÚPREFER_FLUSHÚ
QUEUE_SIZEÚBaseCopy)Úcopy_endÚcopy_to)ÚBuffer)ÚAsyncCursor)ÚAsyncConnectionc                  ó„   ‡ — 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Ú	AsyncCopyaj  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    ÚcursorúAsyncCursor[Any]r!   úbool | NoneúAsyncWriter | Nonec               ó–   •— t          ¦   «                              ||¬¦  «         |st          |¦  «        }|| _        |j        | _        d S )N)r!   )ÚsuperÚ__init__ÚAsyncLibpqWriterr    ÚwriteÚ_write)Úselfr"   r!   r    Ú	__class__s       €úQ/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/psycopg/_copy_async.pyr(   zAsyncCopy.__init__3   sK   ø€ õ 	‰Œ×Ò˜¨ÐÑ/Ô/Ð/Øð 	.Ý% fÑ-Ô-ˆFàˆŒØ”lˆŒˆˆó    Úreturnr   c              ƒ  ó2   K  — |                       ¦   «          | S ©N)Ú_enter©r,   s    r.   Ú
__aenter__zAsyncCopy.__aenter__A   s   è è € Ø�Š‰ŒˆØˆr/   Úexc_typeútype[BaseException] | NoneÚexc_valúBaseException | NoneÚexc_tbúTracebackType | NoneÚNonec              ƒ  ó@   K  — |                       |¦  «        ƒ d {V —† d S r2   )Úfinish)r,   r6   r8   r:   s       r.   Ú	__aexit__zAsyncCopy.__aexit__E   s2   è è € ð �kŠk˜'Ñ"Ô"Ð"Ð"Ð"Ð"Ð"Ð"Ð"Ð"Ð"r/   úAsyncIterator[Buffer]c               óˆ   K  — |                       ¦   «         ƒ d{V —†x}r#|W V — |                       ¦   «         ƒ d{V —†x}°!dS dS )z5Implement block-by-block iteration on :sql:`COPY TO`.N)Úread©r,   Údatas     r.   Ú	__aiter__zAsyncCopy.__aiter__O   su   è è € à!ŸYšY™[œ[Ð(Ð(Ð(Ð(Ð(Ð(Ð)ˆdð 	ØˆJˆJˆJˆJð "ŸYšY™[œ[Ð(Ð(Ð(Ð(Ð(Ð(Ð)ˆdð 	ð 	ð 	ð 	ð 	r/   r   c              ƒ  ój   K  — | j                              |                      ¦   «         ¦  «        ƒ d{V —†S )zƒ
        Read an unparsed row after a :sql:`COPY TO` operation.

        Return an empty string when the data is finished.
        N)Ú
connectionÚwaitÚ	_read_genr4   s    r.   rB   zAsyncCopy.readT   s:   è è € ð ”_×)Ò)¨$¯.ª.Ñ*:Ô*:Ñ;Ô;Ð;Ð;Ð;Ð;Ð;Ð;Ð;r/   úAsyncIterator[tuple[Any, ...]]c               óˆ   K  — |                       ¦   «         ƒ d{V —†x}�#|W V — |                       ¦   «         ƒ d{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AsyncCopy.rows\   sk   è è € ð !%§¢¡¤Ð/Ð/Ð/Ð/Ð/Ð/Ð0ˆvÐ=ØˆLˆLˆLˆLð !%§¢¡¤Ð/Ð/Ð/Ð/Ð/Ð/Ð0ˆvÐ=Ð=Ð=Ð=Ð=r/   útuple[Any, ...] | Nonec              ƒ  ój   K  — | j                              |                      ¦   «         ¦  «        ƒ d{V —†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()`.
        N)rG   rH   Ú_read_row_genr4   s    r.   rL   zAsyncCopy.read_rowf   s<   è è € ð ”_×)Ò)¨$×*<Ò*<Ñ*>Ô*>Ñ?Ô?Ð?Ð?Ð?Ð?Ð?Ð?Ð?r/   ÚbufferúBuffer | strc              ƒ  ó|   K  — | j                              |¦  «        x}r|                      |¦  «        ƒ d{V —† 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AsyncCopy.writeq   sY   è è € ð ”>×'Ò'¨Ñ/Ô/Ð/ˆ4ð 	$Ø—+’+˜dÑ#Ô#Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#ð	$ð 	$r/   ÚrowúSequence[Any]c              ƒ  ó|   K  — | j                              |¦  «        x}r|                      |¦  «        ƒ d{V —† 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AsyncCopy.write_row{   sW   è è € à”>×+Ò+¨CÑ0Ô0Ð0ˆ4ð 	$Ø—+’+˜dÑ#Ô#Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#ð	$ð 	$r/   Úexcc              ƒ  óÄ  K  — | j         t          k    ra|s6| j                             ¦   «         x}r|                      |¦  «        ƒ d{V —† | j                             |¦  «        ƒ d{V —† d| _        dS |sdS | j        j	        t          k    rdS | j                             ¦   «         ƒ d{V —† | j                             |                      ¦   «         ¦  «        ƒ d{V —† 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.
        NT)Ú
_directionÚCOPY_INrU   Úendr+   r    r>   Ú	_finishedÚ_pgconnÚtransaction_statusÚACTIVErG   Ú_try_cancelrH   Ú_end_copy_out_gen)r,   rZ   rD   s      r.   r>   zAsyncCopy.finish€   s  è è € ð Œ?�gÒ%Ð%Øð ,Øœ>×-Ò-Ñ/Ô/Ð/�4ð ,ØŸ+š+ dÑ+Ô+Ð+Ð+Ð+Ð+Ð+Ð+Ð+Ø”+×$Ò$ SÑ)Ô)Ð)Ð)Ð)Ð)Ð)Ð)Ð)Ø!ˆDŒNˆNˆNàð Ø�àŒ|Ô.µ&Ò8Ð8ð �ð ”/×-Ò-Ñ/Ô/Ð/Ð/Ð/Ð/Ð/Ð/Ð/Ø”/×&Ò& t×'=Ò'=Ñ'?Ô'?Ñ@Ô@Ð@Ð@Ð@Ð@Ð@Ð@Ð@Ð@Ð@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àÐÐÑð #Ø%)ð#ð #ð #ð #ð #ð #ð #ð #ðð ð ð ð#ð #ð #ð #ðð ð ð ð
<ð <ð <ð <ðð ð ð ð	@ð 	@ð 	@ð 	@ð$ð $ð $ð $ð$ð $ð $ð $ð
Að Að Að Að Að Að Að Ar/   r   zAsyncConnection[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              ƒ  ó
   K  — dS )zWrite some data to destination.N© rC   s     r.   r*   zAsyncWriter.write£   s   è è € ð 	ˆr/   NrZ   r9   c              ƒ  ó
   K  — 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AsyncWriter.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)   zE
    An `AsyncWriter` 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AsyncLibpqWriter.__init__¸   s#   € ØˆŒØ Ô+ˆŒØ”Ô-ˆŒˆˆr/   rD   r   r0   r<   c           
   ƒ  ó’  K  — t          |¦  «        t          k    r<| j                             t	          | j        |t          ¬¦  «        ¦  «        ƒ d {V —† d S t          dt          |¦  «        t          ¦  «        D ]L}| j                             t	          | j        |||t          z   …         t          ¬¦  «        ¦  «        ƒ d {V —† ŒMd S )N©Úflushr   )Úlenr   rG   rH   r   r`   r   Úrange©r,   rD   Úis      r.   r*   zAsyncLibpqWriter.write½   sì   è è € Ýˆt‰9Œ9�Ò'Ð'ð ”/×&Ò&¥w¨t¬|¸TÍÐ'VÑ'VÔ'VÑWÔWÐWÐWÐWÐWÐWÐWÐWÐWÐWõ ˜1�c $™iœi­Ñ9Ô9ð ð �Ø”o×*Ò*ÝØœ d¨1¨qµ?Ñ/BÐ+BÔ&CÍ<ðñ ô ñô ð ð ð ð ð ð ð ð ðð r/   NrZ   r9   c              ƒ  ó<  K  — |r;dt          |¦  «        j        › d|› �}|                     | j        j        d¦  «        }nd }	 | j                             t          | j        |¦  «        ¦  «        ƒ d {V —†}|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AsyncLibpqWriter.finishÌ   sÐ   è è € àð 	ØH­¨S©	¬	Ô(>ÐHÐHÀ3ÐHÐHˆCØ—:’:˜dœlÔ4°iÑ@Ô@ˆDˆDàˆDð		)Øœ×,Ò,­X°d´lÀDÑ-IÔ-IÑJÔJÐJÐJÐJÐJÐJÐJˆCð %( 5ˆDŒKÔ Ð Ð øõ	 Œð 	ð 	ð 	Øð Øðð ð ð	øøøs   Á3B Â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 )ÚAsyncQueuedLibpqWriterzò
    `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.
    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AsyncQueuedLibpqWriter.__init__ë   sB   ø€ Ý‰Œ×Ò˜Ñ Ô Ð å&,µZÐ&@Ñ&@Ô&@ˆŒØ'+ˆŒØ37ˆÔÐÐr/   r0   r<   c              ƒ  óF  K  — 	 | j                              ¦   «         ƒ d{V —†x}r]| j                             t	          | j        |t          ¬¦  «        ¦  «        ƒ d{V —† | j                              ¦   «         ƒ d{V —†x}°[d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.
        Nrw   )	r�   ÚgetrG   rH   r   r`   r   ÚBaseExceptionr�   )r,   rD   Úexs      r.   ÚworkerzAsyncQueuedLibpqWriter.workerò   sþ   è è € ð	$Ø!%¤§¢Ñ!2Ô!2Ð2Ð2Ð2Ð2Ð2Ð2Ð3�$ð Ø”o×*Ò*Ý˜DœL¨$µlÐCÑCÔCñô ð ð ð ð ð ð ð ð "&¤§¢Ñ!2Ô!2Ð2Ð2Ð2Ð2Ð2Ð2Ð3�$ð ð ð ð ð øõ ð 	$ð 	$ð 	$à!#ˆDÔÐÐÐÐÐÐøøøøð	$øøøs   „A<B Â
B ÂBÂB rD   r   c              ƒ  ó†  K  — | j         st          | j        ¦  «        | _         | j        r| j        ‚t	          |¦  «        t
          k    r"| j                             |¦  «        ƒ d {V —† d S t          dt	          |¦  «        t
          ¦  «        D ]2}| j                             |||t
          z   …         ¦  «        ƒ d {V —† Œ3d S )Nr   )	rŽ   r   r”   r�   ry   r   r�   Úputrz   r{   s      r.   r*   zAsyncQueuedLibpqWriter.write  sß   è è € ØŒ|ð 	/å! $¤+Ñ.Ô.ˆDŒLð Ôð 	%ØÔ$Ð$åˆt‰9Œ9�Ò'Ð'ð ”+—/’/ $Ñ'Ô'Ð'Ð'Ð'Ð'Ð'Ð'Ð'Ð'Ð'õ ˜1�c $™iœi­Ñ9Ô9ð Eð E�Ø”k—o’o d¨1¨qµ?Ñ/BÐ+BÔ&CÑDÔDÐDÐDÐDÐDÐDÐDÐDÐDðEð Er/   NrZ   r9   c              ƒ  ó  •K  — | j                              d¦  «        ƒ d {V —† | j        r!t          | j        ¦  «        ƒ d {V —† d | _        | j        r| j        ‚t          ¦   «                              |¦  «        ƒ d {V —† d S )Nr/   )r�   r–   rŽ   r   r�   r'   r>   )r,   rZ   r-   s     €r.   r>   zAsyncQueuedLibpqWriter.finish  s¤   øè è € ØŒk�oŠo˜cÑ"Ô"Ð"Ð"Ð"Ð"Ð"Ð"Ð"àŒ<ð 	 Ý˜$œ,Ñ'Ô'Ð'Ð'Ð'Ð'Ð'Ð'Ð'ØˆDŒLð Ôð 	%ØÔ$Ð$å‰gŒg�nŠn˜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ð$ð $ð $ð $ð"Eð Eð Eð Eð&"ð "ð "ð "ð "ð "ð "ð "ð "ð "ð "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   Úcursor_asyncr   Úconnection_asyncr   Ú
ExecStatusr]   ÚCOPY_OUTÚTransactionStatusrb   r   r   r)   rŠ   ro   r/   r.   ú<module>r§      sE  ððð ð #Ð "Ð "Ð "Ð "Ð "à #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %Ð %Ð %Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KØ )Ð )Ð )Ð )Ð )Ð )Ð )Ð )àð 2ØÐÐÐÐÐØ)Ð)Ð)Ð)Ð)Ð)Ø1Ð1Ð1Ð1Ð1Ð1à
Œ-Ô
€ØŒ=Ô!€à	Ô	Ô	$€ð{Að {Að {Að {Að {A�Ð/Ô0ñ {Aô {Að {Að|ð ð ð ð �#ñ ô ð ð&,)ð ,)ð ,)ð ,)ð ,)�{ñ ,)ô ,)ð ,)ð^A"ð A"ð A"ð A"ð A"Ð-ñ A"ô A"ð A"ð A"ð A"r/   