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 )z6
Objects to support the COPY protocol (sync version).
    )annotations)ABCabstractmethod)TracebackType)AnyIteratorSequenceTYPE_CHECKING   )pq)errors)Self)BaseCopyMAX_BUFFER_SIZE
QUEUE_SIZEPREFER_FLUSH)copy_tocopy_end)spawngatherQueueWorker)Buffer)Cursor)
Connectionc                      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 )<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                  s.   t  j||d |st|}|| _|j| _d S )N)r   )super__init__LibpqWriterr   write_write)selfr    r   r   	__class__ V/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/_copy.pyr%   5   s
   zCopy.__init__returnr   c                 C  s   |    | S N)_enterr)   r,   r,   r-   	__enter__C   s   zCopy.__enter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | NoneNonec                 C  s   |  | d S r/   )finish)r)   r3   r5   r7   r,   r,   r-   __exit__G   s   zCopy.__exit__Iterator[Buffer]c                 c  s    	 |   }|s
dS |V  q)z5Implement block-by-block iteration on :sql:`COPY TO`.TN)readr)   datar,   r,   r-   __iter__Q   s   zCopy.__iter__r   c                 C     | 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_genr1   r,   r,   r-   r=   Y   s   z	Copy.readIterator[tuple[Any, ...]]c                 c  s     	 |   }|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-   rowsa   s   z	Copy.rowstuple[Any, ...] | Nonec                 C  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()`.
        )rB   rC   _read_row_genr1   r,   r,   r-   rF   n   s   	zCopy.read_rowbufferBuffer | strc                 C  "   | j |}|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)   rK   r?   r,   r,   r-   r'   y   s   z
Copy.writerowSequence[Any]c                 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Copy.write_rowexcc                 C  sn   | j tkr| j }|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_INrN   endr(   r   r:   	_finished_pgconntransaction_statusACTIVErB   _try_cancelrC   _end_copy_out_gen)r)   rR   r?   r,   r,   r-   r:      s   




zCopy.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Connection[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                 C     dS )zWrite some data to destination.Nr,   r>   r,   r,   r-   r'      s   zWriter.writeNrR   r6   c                 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Writer.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&   z@
    An `Writer` 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LibpqWriter.__init__r?   r   r.   r9   c              	   C  sf   t |tkr| jt| j|td d S tdt |tD ]}| jt| j|||t  td qd S )Nflushr   )lenr   rB   rC   r   rW   r   ranger)   r?   ir,   r,   r-   r'      s   zLibpqWriter.writeNrR   r6   c                 C  st   |rdt |j d| }|| jjd}nd }z| jt| j|}W n tj	y2   |s/ 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LibpqWriter.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 )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.
    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QueuedLibpqWriter.__init__r.   r9   c              
   C  s^   z	 | j  }|sW dS | jt| j|td q ty. } 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.
        Tri   N)	r|   getrB   rC   r   rW   r   BaseExceptionr~   )r)   r?   exr,   r,   r-   worker   s   
zQueuedLibpqWriter.workerr?   r   c                 C  sl   | j s	t| j| _ | jr| jt|tkr| j| d S tdt|tD ]}| j|||t   q%d S )Nr   )	r}   r   r   r~   rk   r   r|   putrl   rm   r,   r,   r-   r'     s   zQueuedLibpqWriter.writeNrR   r6   c                   s>   | j d | jrt| j d | _| jr| jt | d S )N    )r|   r   r}   r   r~   r$   r:   rd   r*   r,   r-   r:     s   
zQueuedLibpqWriter.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   r    r   rB   r   
ExecStatusrT   COPY_OUTTransactionStatusrY   r   r   r&   rz   r,   r,   r,   r-   <module>   s.    /