o
    "4g'                     @  s0  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 ddlmZmZmZmZ erhd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 ded Z*G dd deZ+G dd de+Z,G dd de,Z-dS )z7
Objects to support the COPY protocol (async version).
    )annotations)ABCabstractmethod)TracebackType)AnyAsyncIteratorSequenceTYPE_CHECKING   )pq)errors)Self)BaseCopyMAX_BUFFER_SIZE
QUEUE_SIZEPREFER_FLUSH)copy_tocopy_end)aspawnagatherAQueueAWorker)Buffer)AsyncCursor)AsyncConnectionc                      s   e Zd ZU dZdZded< dddd2 fddZd3ddZd4ddZd5ddZd6dd Z	d7d"d#Z
d8d%d&Zd9d)d*Zd:d-d.Zd;d0d1Z  ZS )<	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                  s.   t  j||d |st|}|| _|j| _d S )N)r   )super__init__AsyncLibpqWriterr   write_write)selfr    r   r   	__class__ \/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/_copy_async.pyr%   2   s
   zAsyncCopy.__init__returnr   c                   s   |    | S N)_enterr)   r,   r,   r-   
__aenter__@   s   zAsyncCopy.__aenter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | NoneNonec                   s   |  |I d H  d S r/   )finish)r)   r3   r5   r7   r,   r,   r-   	__aexit__D   s   zAsyncCopy.__aexit__AsyncIterator[Buffer]c                 C s"   	 |   I dH }|sdS |V  q)z5Implement block-by-block iteration on :sql:`COPY TO`.TN)readr)   datar,   r,   r-   	__aiter__N   s   zAsyncCopy.__aiter__r   c                      | j |  I dH 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_genr1   r,   r,   r-   r=   V   s   zAsyncCopy.readAsyncIterator[tuple[Any, ...]]c                 C s&   	 |   I dH }|du rdS |V  q)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()`.
        TN)read_row)r)   recordr,   r,   r-   rows^   s   zAsyncCopy.rowstuple[Any, ...] | Nonec                   rA   )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)rB   rC   _read_row_genr1   r,   r,   r-   rF   k   s   	zAsyncCopy.read_rowbufferBuffer | strc                   *   | j |}|r| |I dH  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)   rK   r?   r,   r,   r-   r'   v   s
   zAsyncCopy.writerowSequence[Any]c                   rM   )z=Write a record to a table after a :sql:`COPY FROM` operation.N)rN   	write_rowr(   )r)   rO   r?   r,   r,   r-   rQ      s
   zAsyncCopy.write_rowexcc                   s   | j tkr#| j }|r| |I dH  | j|I dH  d| _dS |s'dS | jj	t
kr/dS | j I dH  | j|  I dH  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_INrN   endr(   r   r:   	_finished_pgconntransaction_statusACTIVErB   _try_cancelrC   _end_copy_out_gen)r)   rR   r?   r,   r,   r-   r:      s   


zAsyncCopy.finish)r    r!   r   r"   r   r#   )r.   r   )r3   r4   r5   r6   r7   r8   r.   r9   )r.   r<   )r.   r   )r.   rE   )r.   rI   )rK   rL   r.   r9   )rO   rP   r.   r9   rR   r6   r.   r9   )__name__
__module____qualname____doc____annotations__r%   r2   r;   r@   r=   rH   rF   r'   rQ   r:   __classcell__r,   r,   r*   r-   r      s    
 








r   zAsyncConnection[Any]c                   @  s*   e Zd ZdZedddZddddZdS )r   zG
    A class to write copy data somewhere (for async connections).
    r?   r   r.   r9   c                      dS )zWrite some data to destination.Nr,   r>   r,   r,   r-   r'      s   zAsyncWriter.writeNrR   r6   c                   rc   )z
        Called when write operations are finished.

        If operations finished with an error, it will be passed to ``exc``.
        Nr,   r)   rR   r,   r,   r-   r:      s   zAsyncWriter.finishr?   r   r.   r9   r/   r\   )r]   r^   r_   r`   r   r'   r:   r,   r,   r,   r-   r      s
    r   c                   @  s4   e Zd ZdZdZdddZdddZddddZdS )r&   zE
    An `AsyncWriter` to write copy data to a Postgres database.
    psycopg.copyr    r!   c                 C  s   || _ |j| _| jj| _d S r/   )r    rB   pgconnrW   r)   r    r,   r,   r-   r%      s   zAsyncLibpqWriter.__init__r?   r   r.   r9   c              	     st   t |tkr| jt| j|tdI d H  d S tdt |tD ]}| jt| j|||t  tdI d H  q d S )Nflushr   )lenr   rB   rC   r   rW   r   ranger)   r?   ir,   r,   r-   r'      s   "zAsyncLibpqWriter.writeNrR   r6   c                   s|   |rdt |j d| }|| jjd}nd }z| jt| j|I d H }W n tj	y6   |s3 Y d S w |g| j
_d S )Nzerror from Python: z - replace)typer_   encoderW   	_encodingrB   rC   r   eQueryCanceledr    _results)r)   rR   msgbmsgresr,   r,   r-   r:      s   zAsyncLibpqWriter.finishr    r!   re   r/   r\   )r]   r^   r_   r`   r%   r'   r:   r,   r,   r,   r-   r&      s    

r&   c                      sJ   e Zd ZdZdZd fddZdd	d
ZdddZdd fddZ  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.
    rf   r    r!   c                   s(   t  | ttd| _d | _d | _d S )N)maxsize)r$   r%   r   r   _queue_worker_worker_errorrh   r*   r,   r-   r%      s   
zAsyncQueuedLibpqWriter.__init__r.   r9   c              
     sl   z	 | j  I dH }|sW dS | jt| j|tdI dH  q ty5 } z
|| _W Y d}~dS d}~w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.
        TNri   )	r|   getrB   rC   r   rW   r   BaseExceptionr~   )r)   r?   exr,   r,   r-   worker   s   
zAsyncQueuedLibpqWriter.workerr?   r   c                   sz   | j s
t| j| _ | jr| jt|tkr!| j|I d H  d S tdt|tD ]}| j|||t  I d H  q)d S )Nr   )	r}   r   r   r~   rk   r   r|   putrl   rm   r,   r,   r-   r'     s    zAsyncQueuedLibpqWriter.writeNrR   r6   c                   sR   | j dI d H  | jrt| jI d H  d | _| jr| jt |I d H  d S )N    )r|   r   r}   r   r~   r$   r:   rd   r*   r,   r-   r:      s   zAsyncQueuedLibpqWriter.finishry   )r.   r9   re   r/   r\   )	r]   r^   r_   r`   r%   r   r'   r:   rb   r,   r,   r*   r-   rz      s    

rz   N).r`   
__future__r   abcr   r   typesr   typingr   r   r   r	    r   r   rs   _compatr   
_copy_baser   r   r   r   
generatorsr   r   _acompatr   r   r   r   r   cursor_asyncr   connection_asyncr   
ExecStatusrT   COPY_OUTTransactionStatusrY   r   r   r&   rz   r,   r,   r,   r-   <module>   s.     /