
    gi&                       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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       c/var/www/html/Pagina-Ingenieria-Requisitos/venv/lib/python3.11/site-packages/psycopg/_copy_async.pyr(   zAsyncCopy.__init__2   sK     	/// 	.%f--Fl    returnr   c                2   K   |                                   | S N)_enterr,   s    r.   
__aenter__zAsyncCopy.__aenter__@   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__D   s2       kk'"""""""""""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__N   su      !YY[[(((((()d 	JJJJ "YY[[(((((()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.readS   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       !%//////0v=LLLL !%//////0v=====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_rowe   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.writep   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_rowz   sW      >++C0004 	$++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)))))))))!DNNN |.&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      t99'' /&&wt|T'V'V'VWWWWWWWWWWW 1c$ii99  o**d1q?/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H3HHC::dl4i@@DDD		),,XdlD-I-IJJJJJJJJC %(5DK   	  	 	 	   	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)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.
        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**DL$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      | 	/!$+..DL  	%$$t99'' +//$''''''''''' 1c$ii99 E Ekood1q?/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      kooc"""""""""< 	 $,'''''''''DL  	%$$ggnnS!!!!!!!!!!!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   r	   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      s9    # " " " " " # # # # # # # #       > > > > > > > > > > > >                   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/   