
    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jJ                  jL                  Z&ejJ                  jN                  Z'ejP                  jR                  Z) G d ded         Z* G d de      Z+ G d de+      Z, G d de,      Z-y)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ddZddZ x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    c               r    t         |   ||       |st        |      }|| _        |j                  | _        y )N)r!   )super__init__LibpqWriterr    write_write)selfcursorr!   r    	__class__s       m/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/psycopg/_copy.pyr$   zCopy.__init__5   s6     	/ (Fll    c                &    | j                          | S N)_enterr(   s    r+   	__enter__zCopy.__enter__C   s    r,   c                &    | j                  |       y r.   )finish)r(   exc_typeexc_valexc_tbs       r+   __exit__zCopy.__exit__G   s     	Gr,   c              #  `   K   | j                         x}r| | j                         x}ryyw)z5Implement block-by-block iteration on :sql:`COPY TO`.N)readr(   datas     r+   __iter__zCopy.__iter__Q   s/     iik!d!J iik!d!   )..c                T    | j                   j                  | 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_genr0   s    r+   r9   z	Copy.readV   s      ##DNN$455r,   c              #  `   K   | j                         x}| | j                         x}yyw)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	Copy.rows^   s1      (v5L (v5r=   c                T    | j                   j                  | 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()`.
        )r?   r@   _read_row_genr0   s    r+   rC   zCopy.read_rowh   s"     ##D$6$6$899r,   c                b    | j                   j                  |      x}r| j                  |       yy)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(   bufferr;   s      r+   r&   z
Copy.writes   s/     >>''//4/KK 0r,   c                b    | j                   j                  |      x}r| j                  |       yy)z=Write a record to a table after a :sql:`COPY FROM` operation.N)rI   	write_rowr'   )r(   rowr;   s      r+   rL   zCopy.write_row}   s-    >>++C0040KK 1r,   c                   | j                   t        k(  rP| j                  j                         x}r| j	                  |       | j
                  j                  |       d| _        y|sy| j                  j                  t        k7  ry| j                  j                          | j                  j                  | j                                y)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_INrI   endr'   r    r3   	_finished_pgconntransaction_statusACTIVEr?   _try_cancelr@   _end_copy_out_gen)r(   excr;   s      r+   r3   zCopy.finish   s     ??g%~~))++t+D!KKs#!DN||..&8 
 OO'')OO  !7!7!9:r,   )r)   Cursor[Any]r!   zbool | Noner    zWriter | None)returnr   )r4   ztype[BaseException] | Noner5   BaseException | Noner6   zTracebackType | NonerZ   None)rZ   zIterator[Buffer])rZ   r   )rZ   zIterator[tuple[Any, ...]])rZ   ztuple[Any, ...] | None)rJ   zBuffer | strrZ   r\   )rM   zSequence[Any]rZ   r\   rX   r[   rZ   r\   )__name__
__module____qualname____doc____annotations__r$   r1   r7   r<   r9   rE   rC   r&   rL   r3   __classcell__r*   s   @r+   r   r   "   s     JN # $## 	#
 #, & %	
 

6	:
;r,   r   zConnection[Any]c                  ,    e Zd ZdZedd       ZdddZy)r   zG
    A class to write copy data somewhere (for async connections).
    c                     y)zWrite some data to destination.N r:   s     r+   r&   zWriter.write   s     	r,   Nc                     y)z
        Called when write operations are finished.

        If operations finished with an error, it will be passed to ``exc``.
        Nrg   )r(   rX   s     r+   r3   zWriter.finish   s     	r,   r;   r   rZ   r\   r.   r]   )r^   r_   r`   ra   r   r&   r3   rg   r,   r+   r   r      s       r,   r   c                  .    e Zd ZdZdZddZddZd	d
dZy)r%   z@
    An `Writer` to write copy data to a Postgres database.
    psycopg.copyc                j    || _         |j                  | _        | j                  j                  | _        y r.   )r)   r?   pgconnrS   )r(   r)   s     r+   r$   zLibpqWriter.__init__   s'     ++--r,   c           
     P   t        |      t        k  r6| j                  j                  t	        | j
                  |t                     y t        dt        |      t              D ]A  }| j                  j                  t	        | j
                  |||t        z    t                     C y )Nflushr   )lenr   r?   r@   r   rS   r   ranger(   r;   is      r+   r&   zLibpqWriter.write   s~    t9' OO  t<!PQ 1c$i9 $$d1q?/B&C<r,   Nc                L   |rBdt        |      j                   d| }|j                  | j                  j                  d      }nd }	 | j
                  j                  t        | j                  |            }|g| j                  _	        y # t        j                  $ r |s Y y w xY w)Nzerror from Python: z - replace)typer`   encoderS   	_encodingr?   r@   r   r)   _resultseQueryCanceled)r(   rX   msgbmsgress        r+   r3   zLibpqWriter.finish   s    'S	(>(>'?s3%HC::dll44i@DD		)//&&xd'CDC %(5DKK 	  	 	s   /B
 
B#"B#r)   rY   ri   r.   r]   )r^   r_   r`   ra   r$   r&   r3   rg   r,   r+   r%   r%      s      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.
    rk   c                j    t         |   |       t        t              | _        d | _        d | _        y )N)maxsize)r#   r$   r   r   _queue_worker_worker_error)r(   r)   r*   s     r+   r$   zQueuedLibpqWriter.__init__   s+     %*:%>&*37r,   c                   	 | j                   j                         x}rS| j                  j                  t	        | j
                  |t                     | j                   j                         x}rRyy# t        $ r}|| _        Y d}~y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.
        ro   N)	r   getr?   r@   r   rS   r   BaseExceptionr   )r(   r;   exs      r+   workerzQueuedLibpqWriter.worker   so    	$++//++$+$$WT\\4|%TU ++//++$+ 	$!#D	$s   A-A2 2	B;BBc                d   | j                   st        | j                        | _         | j                  r| j                  t	        |      t
        k  r| j                  j                  |       y t        dt	        |      t
              D ]'  }| j                  j                  |||t
        z           ) y )Nr   )	r   r   r   r   rq   r   r   putrr   rs   s      r+   r&   zQueuedLibpqWriter.write   s    || -DL $$$t9' KKOOD! 1c$i9 ?Q_)< =>?r,   c                    | j                   j                  d       | j                  rt        | j                         d | _        | j                  r| j                  t
        |   |       y )Nr,   )r   r   r   r   r   r#   r3   )r(   rX   r*   s     r+   r3   zQueuedLibpqWriter.finish  sP    <<4<< DL $$$sr,   r   )rZ   r\   ri   r.   r]   )	r^   r_   r`   ra   r$   r   r&   r3   rc   rd   s   @r+   r   r      s(      J8$?& r,   r   N).ra   
__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   r?   r   
ExecStatusrP   COPY_OUTTransactionStatusrU   r   r   r%   r   rg   r,   r+   <module>r      s    # #  9 9    2 2 K K )&
--

==!!				$	$x;8%& x;vS &,)& ,)^? ?r,   