
    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)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ddZddZ x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    c               r    t         |   ||       |st        |      }|| _        |j                  | _        y )N)r!   )super__init__AsyncLibpqWriterr    write_write)selfcursorr!   r    	__class__s       s/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/psycopg/_copy_async.pyr$   zAsyncCopy.__init__2   s6     	/%f-Fll    c                .   K   | j                          | S wN)_enterr(   s    r+   
__aenter__zAsyncCopy.__aenter__@   s     s   c                B   K   | j                  |       d {    y 7 wr.   )finish)r(   exc_typeexc_valexc_tbs       r+   	__aexit__zAsyncCopy.__aexit__D   s      kk'"""s   c                  K   | j                          d{   x}r!| | j                          d{   x}r yy7 (7 w)z5Implement block-by-block iteration on :sql:`COPY TO`.N)readr(   datas     r+   	__aiter__zAsyncCopy.__aiter__N   s<     !YY[()d)J "YY[()d)((#   A?AAAAAc                p   K   | j                   j                  | j                                d{   S 7 w)z
        Read an unparsed row after a :sql:`COPY TO` operation.

        Return an empty string when the data is finished.
        N)
connectionwait	_read_genr0   s    r+   r9   zAsyncCopy.readS   s*      __))$..*:;;;;   -646c                  K   | j                          d{   x}!| | j                          d{   x} yy7 (7 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AsyncCopy.rows[   s>      !%/0v=L !%/0v=//r=   c                p   K   | j                   j                  | j                                d{   S 7 w)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)r?   r@   _read_row_genr0   s    r+   rD   zAsyncCopy.read_rowe   s,      __))$*<*<*>????rB   c                ~   K   | j                   j                  |      x}r| j                  |       d{    yy7 w)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AsyncCopy.writep   s<      >>''//4/++d### 0#   2=;=c                ~   K   | j                   j                  |      x}r| j                  |       d{    yy7 w)z=Write a record to a table after a :sql:`COPY FROM` operation.N)rJ   	write_rowr'   )r(   rowr;   s      r+   rN   zAsyncCopy.write_rowz   s:     >>++C0040++d### 1#rL   c                  K   | j                   t        k(  r`| j                  j                         x}r| j	                  |       d{    | j
                  j                  |       d{    d| _        y|sy| j                  j                  t        k7  ry| j                  j                          d{    | j                  j                  | j                                d{    y7 7 7 :7 w)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_INrJ   endr'   r    r3   	_finished_pgconntransaction_statusACTIVEr?   _try_cancelr@   _end_copy_out_gen)r(   excr;   s      r+   r3   zAsyncCopy.finish   s      ??g%~~))++t+kk$'''++$$S)))!DN||..&8  //--/////&&t'='='?@@@# () 0@sI   AC3C+"C3)C-*A
C34C/50C3%C1&C3-C3/C31C3)r)   AsyncCursor[Any]r!   zbool | Noner    zAsyncWriter | None)returnr   )r4   ztype[BaseException] | Noner5   BaseException | Noner6   zTracebackType | Noner\   None)r\   zAsyncIterator[Buffer])r\   r   )r\   zAsyncIterator[tuple[Any, ...]])r\   ztuple[Any, ...] | None)rK   zBuffer | strr\   r^   )rO   zSequence[Any]r\   r^   rZ   r]   r\   r^   )__name__
__module____qualname____doc____annotations__r$   r1   r7   r<   r9   rF   rD   r&   rN   r3   __classcell__r*   s   @r+   r   r      s     J #%)# # 	#
 ###,# &# %	#
 
#
<	@$$
Ar,   r   zAsyncConnection[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                   K   yw)zWrite some data to destination.N r:   s     r+   r&   zAsyncWriter.write   s      	   Nc                   K   yw)z
        Called when write operations are finished.

        If operations finished with an error, it will be passed to ``exc``.
        Nri   )r(   rZ   s     r+   r3   zAsyncWriter.finish   s      	rj   r;   r   r\   r^   r.   r_   )r`   ra   rb   rc   r   r&   r3   ri   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%   zE
    An `AsyncWriter` to write copy data to a Postgres database.
    psycopg.copyc                j    || _         |j                  | _        | j                  j                  | _        y r.   )r)   r?   pgconnrU   )r(   r)   s     r+   r$   zAsyncLibpqWriter.__init__   s'     ++--r,   c           
       K   t        |      t        k  r>| j                  j                  t	        | j
                  |t                     d {    y t        dt        |      t              D ]I  }| j                  j                  t	        | j
                  |||t        z    t                     d {    K y 7 l7 	w)Nflushr   )lenr   r?   r@   r   rU   r   ranger(   r;   is      r+   r&   zAsyncLibpqWriter.write   s     t9' //&&wt||T'VWWW 1c$i9 oo**d1q?/B&C<  	 X
s%   AB>B:A$B>2B<3B><B>Nc                h  K   |rBdt        |      j                   d| }|j                  | j                  j                  d      }nd }	 | j
                  j                  t        | j                  |             d {   }|g| j                  _	        y 7 # t        j                  $ r |s Y y w xY ww)Nzerror from Python: z - replace)typerb   encoderU   	_encodingr?   r@   r   r)   _resultseQueryCanceled)r(   rZ   msgbmsgress        r+   r3   zAsyncLibpqWriter.finish   s     'S	(>(>'?s3%HC::dll44i@DD		),,XdllD-IJJC %(5DKK  K  	 	s<   AB2
2B <B=B B2B B/,B2.B//B2r)   r[   rl   r.   r_   )r`   ra   rb   rc   r$   r&   r3   ri   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 )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.
    rn   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AsyncQueuedLibpqWriter.__init__   s+     &,Z&@'+37r,   c                b  K   	 | j                   j                          d{   x}rc| j                  j                  t	        | j
                  |t                     d{    | j                   j                          d{   x}rbyy7 j7 -7 # t        $ r}|| _        Y d}~yd}~ww xY 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.
        Nrr   )	r   getr?   r@   r   rU   r   BaseExceptionr   )r(   r;   exs      r+   workerzAsyncQueuedLibpqWriter.worker   s     	$!%!223$3oo**DLL$lC   "&!223$32 3  	$!#D	$sb   B/B B>B  B!!B BB 
B/B B B 	B,B'"B/'B,,B/c                  K   | j                   st        | j                        | _         | j                  r| j                  t	        |      t
        k  r$| j                  j                  |       d {    y t        dt	        |      t
              D ]/  }| j                  j                  |||t
        z           d {    1 y 7 R7 	w)Nr   )	r   r   r   r   rt   r   r   putru   rv   s      r+   r&   zAsyncQueuedLibpqWriter.write  s     ||!$++.DL $$$t9' ++//$''' 1c$i9 Ekkood1q?/B&CDDDE	 (
 Es%   A/C1C2A
C<C=CCc                  K   | j                   j                  d       d {    | j                  r$t        | j                         d {    d | _        | j                  r| j                  t
        |   |       d {    y 7 d7 =7 	w)Nr,   )r   r   r   r   r   r#   r3   )r(   rZ   r*   s     r+   r3   zAsyncQueuedLibpqWriter.finish  sw     kkooc"""<<$,,'''DL $$$gnS!!! 	# ( 	"s3   BB(BB	5BBB	BBr   )r\   r^   rl   r.   r_   )	r`   ra   rb   rc   r$   r   r&   r3   re   rf   s   @r+   r   r      s)      J8$"E&" "r,   r   N).rc   
__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   
ExecStatusrR   COPY_OUTTransactionStatusrW   r   r   r%   r   ri   r,   r+   <module>r      s    # #  > >    6 6 K K ))1
--

==!!				$	$zA/0 zAz# &,){ ,)^A"- A"r,   