
    R
h&                       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 )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       X/var/www/html/01_SiteInternet/03_Maps/venv/lib/python3.11/site-packages/psycopg/_copy.pyr(   zCopy.__init__5   sK     	/// 	) ((Fl    returnr   c                .    |                                   | S N)_enterr,   s    r.   	__enter__zCopy.__enter__C   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__G   s     	Gr/   Iterator[Buffer]c              #  B   K   	 |                                  }|sdS |V  )z5Implement block-by-block iteration on :sql:`COPY TO`.TN)readr,   datas     r.   __iter__zCopy.__iter__Q   s3      	99;;D JJJ		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.readY   s$     ##DNN$4$4555r/   Iterator[tuple[Any, ...]]c              #  B   K   	 |                                  }|dS |V  )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,   records     r.   rowsz	Copy.rowsa   s0      	]]__F~LLL		r/   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_rown   s&     ##D$6$6$8$8999r/   bufferBuffer | strc                l    | 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,   rR   rD   s      r.   r*   z
Copy.writey   sC     ~##F++ 	KK	 	r/   rowSequence[Any]c                l    | j                             |          }|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    ~'',, 	KK	 	r/   excc                   | j         t          k    rS| 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_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%%>%%''D "D!!!Ks###!DNNN |.&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NNN # $# # # # # # # #         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    t99'' O  t<!P!P!PQQQQQ 1c$ii99  $$d1q?/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H3HHC::dl4i@@DDD		)/&&xd'C'CDDC %(5DK   	  	 	 	   	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                                         }|sdS | j                            t	          | j        |t                               R# 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.
        Trw   N)	r   getrG   rH   r   r`   r   BaseExceptionr   )r,   rD   exs      r.   workerzQueuedLibpqWriter.worker   s    	$V{(( E$$WT\4|%T%T%TUUU	V
  	$ 	$ 	$!#D	$s   A 5A 
A1A,,A1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    | 	. --DL  	%$$t99'' KOOD!!!!! 1c$ii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<   DL  	%$$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   r	   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      s0    # " " " " " # # # # # # # #       9 9 9 9 9 9 9 9 9 9 9 9                   2 2 2 2 2 2 2 2 2 2 2 2 K K K K K K K K K K K K ) ) ) ) ) ) ) ) '&&&&&&
-
=!			$A; A; A; A; A;8%& A; A; A;H    S   &,) ,) ,) ,) ,)& ,) ,) ,)^B B B B B B B B B Br/   