
    2i,                    $   d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZmZ ddlmZmZmZ ddlmZmZ ddl m!Z! ddl"m#Z# erddl$m%Z% ejL                  jN                  Z' G d de!def         Z(y)z
Psycopg AsyncCursor object.
    )annotations)TracebackType)TYPE_CHECKINGAnyoverload)asynccontextmanager)AsyncIteratorIterable   )errors)pq)ParamsQueryQueryNoTemplate)	AsyncCopyAsyncWriter)AsyncRowFactoryRowRowMaker)SelfTemplate)
BaseCursor)AsyncPipeline)AsyncConnectionc                      e Zd ZdZdZed#d       Ze	 	 	 	 d$d       Zdd	 	 	 d% fdZd&dZ	 	 	 	 	 	 	 	 d'd	Zd(d
Ze	d)d       Z
e
j                  d*d       Z
d+dZe	 d,ddd	 	 	 	 	 	 	 	 	 d-d       Zeddd	 	 	 	 	 	 	 d.d       Z	 d,ddd	 	 	 	 	 	 	 	 	 d/dZdd	 	 	 	 	 	 	 d0dZ	 d,ddd	 	 	 	 	 	 	 	 	 d1dZd2dZd3dZd4dZd5d6dZd7dZd&dZd8dZd9d:dZe	 d,dd 	 	 	 	 	 	 	 d;d!       Zd(d"Z xZS )<AsyncCursorpsycopg c                     y Nr   )self
connections     S/var/www/html/secretsanta/venv/lib/python3.12/site-packages/psycopg/cursor_async.py__init__zAsyncCursor.__init__!   s    :=    c                    y r    r   )r!   r"   row_factorys      r#   r$   zAsyncCursor.__init__$   s     r%   N)r'   c               N    t         |   |       |xs |j                  | _        y r    )superr$   r'   _row_factory)r!   r"   r'   	__class__s      r#   r$   zAsyncCursor.__init__)   s%     	$'A:+A+Ar%   c                   K   | S wr    r   r!   s    r#   
__aenter__zAsyncCursor.__aenter__2   s     s   c                @   K   | j                          d {    y 7 wr    )close)r!   exc_typeexc_valexc_tbs       r#   	__aexit__zAsyncCursor.__aexit__5   s      jjls   c                ,   K   | j                          yw)zI
        Close the current cursor and free associated resources.
        N)_closer-   s    r#   r0   zAsyncCursor.close=   s      	s   c                    | j                   S )z9Writable attribute to control how result rows are formed.r*   r-   s    r#   r'   zAsyncCursor.row_factoryC   s        r%   c                F    || _         | j                  r ||       | _        y y r    )r*   pgresult	_make_row)r!   r'   s     r#   r'   zAsyncCursor.row_factoryH   s"    '==(.DN r%   c                $    | j                  |       S r    r8   r-   s    r#   _make_row_makerzAsyncCursor._make_row_makerN   s      &&r%   preparebinaryc                  K   y wr    r   )r!   queryparamsr?   r@   s        r#   executezAsyncCursor.executeQ   s         c                  K   y wr    r   )r!   rB   r?   r@   s       r#   rD   zAsyncCursor.execute[   s      rE   c          	     j  K   	 | j                   j                  4 d{    | j                   j                  | j                  ||||             d{    ddd      d{    | S 7 L7 7 
# 1 d{  7  sw Y   | S xY w# t        j
                  $ r}|j                  d      d}~ww xY ww)z=
        Execute a query or command to the database.
        Nr>   )_connlockwait_execute_gene_NO_TRACEBACKwith_traceback)r!   rB   rC   r?   r@   exs         r#   rD   zAsyncCursor.executed   s     	*zz  jjoo%%eVWV%T          	*##D))	*s   B3B A+B 2A1A-A1B $A/%B )B3+B -A1/B 1B7A:8B?B B3B B0B++B00B3F)	returningc          	     ,  K   	 | j                   j                  4 d{    t        j                         r| j                   j                  r5| j                   j                  | j                  |||             d{    n| j                   j                         4 d{    | j                   j                  | j                  |||             d{    ddd      d{    n4| j                   j                  | j                  |||             d{    ddd      d{    y7 7 7 7 a7 S# 1 d{  7  sw Y   .xY w7 57 '# 1 d{  7  sw Y   yxY w# t        j                  $ r}|j                  d      d}~ww xY ww)zI
        Execute the same command with a sequence of input data.
        N)rH   rI   r   is_supported	_pipelinerJ   _executemany_gen_pipeline_pipeline_nolock_executemany_gen_no_pipelinerL   rM   rN   )r!   rB   
params_seqrP   rO   s        r#   executemanyzAsyncCursor.executemanyx   sh    	*zz   --/ zz++"jjoo ::5*iX  
 $(::#>#>#@  "&**// $ > >$):y!"#     **//99%YW  #  

   #   (  	*##D))	*s   FE( D/E( AE<D2=#E D4!E$0D:D6D:E$D8%4EEEE( )E*E( .F/E( 2E4E6D:8E:E	 EE	EE( E%EE%!E( $F%E( (F;FFFr   r@   sizec          	    :  K   | j                   j                  rt        j                  d      | j                  j
                  4 d{    	 | j                  j                  | j                  ||||             d{    d}| j                  j                  | j                  |             d{   x}rzt        |j                        D ]+  }| j                  j                  || j                         - d}| j                  j                  | j                  |             d{   x}rz| j                   j                   t"        k(  r| j                  j%                          d{    	 | j                  j                  | j                  d             d{   r5	 | j                  j                  | j                  d             d{   r5	 | j                  j                  | j                  d             d{    ddd      d{    y7 7 7 t7 # t        j                  $ r}|j                  d      d}~ww xY w7 7 7 # t&        $ r Y w xY w7 a# t&        $ r Y jw xY w# | j                   j                   t"        k(  r| j                  j%                          d{  7   	 | j                  j                  | j                  d             d{  7  r6	 | j                  j                  | j                  d             d{  7  r6n# t&        $ r Y nw xY w	 | j                  j                  | j                  d             d{  7   w # t&        $ r Y w w xY ww xY w7 h# 1 d{  7  sw Y   yxY ww)a  
        Iterate row-by-row on a result from the database.

        :param size: if greater than 1, results will be retrieved by chunks of
            this size from the server (but still yielded row-by-row); this is only
            available from version 17 of the libpq.
        z(stream() cannot be used in pipeline modeNrY   TF)first)_pgconnpipeline_statusrL   ProgrammingErrorrH   rI   rJ   _stream_send_gen_stream_fetchone_genrangentuples_txload_rowr;   rM   rN   transaction_statusACTIVE_try_cancel	Exception)	r!   rB   rC   r@   rZ   r\   resposrO   s	            r#   streamzAsyncCursor.stream   s4     <<''$$%OPP::?? 	 	jjoo))%T)R   #'::??43L3LU3S#TTTcT$S[[1 E"hh//T^^DDE!E $(::??43L3LU3S#TTTcT <<22f< **00222$(JJOO 55E5B%   ! %)JJOO 55E5B%  "jjood.G.Ge.G.TUUU9	 	 	 UT ?? .''--. 3  %  V$ ! <<22f< **00222$(JJOO 55E5B%   ! %)JJOO 55E5B%   % 
"jjood.G.Ge.G.TUUU$ ! =	 	 	 	s  ANH!	NN1H,?H$ 3H,3H'4A8H,,H*-H,4:N.I/N4.I"I#3III.I/I-I/NNN$H,'H,*H,,I?III>NII	I*'N)I**N-I//	I;8N:I;;N>;N 9J<
:N  .L+.K1
/4L+#L&
$L+*N +	L7	4N 6L7	7N ;.M0)M,
*M0/N 0	M<	9N ;M<	<N  NNNNNNc               T   K   | j                   r	 |  | j                         syyw)z
        Iterate across multiple record sets received by the cursor.

        Multiple record sets are received after using `executemany()` with
        `!returning=True` or using `execute()` with more than one query in the
        command.
        N)r:   nextsetr-   s    r#   resultszAsyncCursor.results   s-      ==
||~	  s   &(c                  K   t        | j                         |cxk  rt        | j                        k  s'n t        d| dt        | j                         d      |dk  rt        | j                        |z   }| j                  |       | S w)a  
        Move to a specific result set.

        :arg index: index of the result to go to
        :type index: `!int`

        More than one result will be available after executing calling
        `executemany()` or `execute()` with more than one query.

        `!index` is 0-based and supports negative values, counting from the end,
        the same way you can index items in a list.

        The function returns self, so that the result may be followed by a
        fetch operation. See `results()` for details.
        zindex z out of range: z result(s) availabler   )len_results
IndexError_select_current_result)r!   indexs     r#   
set_resultzAsyncCursor.set_result   s       DMM""e@c$--.@@s4==/A.BBVW  19&.E##E*s   B
Bc                   K   | j                          d{    | j                         }| j                  |j                  k  rG| j                  j                  | j                  | j                        }| xj                  dz  c_        |S y7 uw)z
        Return the next record from the current result set.

        Return `!None` the result set is finished.

        :rtype: Row | None, with Row defined by `row_factory`
        Nr   )_fetch_pipeline_check_result_for_fetch_posrc   rd   re   r;   r!   rj   records      r#   fetchonezAsyncCursor.fetchone   sp      ""$$$**,99s{{"XX&&tyy$..AFIINIM 	%s   BBA6Bc                ^  K   | j                          d{    | j                         }|s| j                  }| j                  j	                  | j
                  t        | j
                  |z   |j                        | j                        }| xj
                  t        |      z  c_        |S 7 w)z
        Return the next `!size` records from the current result set.

        `!size` default to `!self.arraysize` if not specified.

        :rtype: Sequence[Row], with Row defined by `row_factory`
        N)
rx   ry   	arraysizerd   	load_rowsrz   minrc   r;   rq   )r!   rZ   rj   recordss       r#   	fetchmanyzAsyncCursor.fetchmany  s      ""$$$**,>>D(($$IIs499t+S[[94>>
 			S\!	 	%s   B-B+BB-c                   K   | j                          d{    | j                         }| j                  j                  | j                  |j
                  | j                        }|j
                  | _        |S 7 bw)z
        Return all the remaining records from the current result set.

        :rtype: Sequence[Row], with Row defined by `row_factory`
        N)rx   ry   rd   r   rz   rc   r;   )r!   rj   r   s      r#   fetchallzAsyncCursor.fetchall  sa      ""$$$**,(($$TYYT^^LKK		 	%s   A;A9A#A;c                    | S r    r   r-   s    r#   	__aiter__zAsyncCursor.__aiter__   s    r%   c                4  K   | j                          d {    | j                         }| j                  |j                  k  rG| j                  j                  | j                  | j                        }| xj                  dz  c_        |S t        d      7 w)Nr   zno more records to return)rx   ry   rz   rc   rd   re   r;   StopAsyncIterationr{   s      r#   	__anext__zAsyncCursor.__anext__#  sw     ""$$$**,99s{{"XX&&tyy$..AFIINIM !<== 	%s   BBB Bc                d   K   | j                          d{    | j                  ||       y7 w)a  
        Move the cursor in the result set to a new position according to mode.

        If `!mode` is ``'relative'`` (default), `!value` is taken as offset to
        the current position in the result set; if set to ``'absolute'``,
        `!value` states an absolute target position.

        Raise `!IndexError` in case a scroll operation would leave the result
        set. In this case the position will not change.
        N)rx   _scroll)r!   valuemodes      r#   scrollzAsyncCursor.scroll,  s-      ""$$$UD! 	%s   0.0writerc              
  K   	 | j                   j                  4 d{    | j                   j                  | j                  ||             d{    t	        | |      4 d{   }| ddd      d{    ddd      d{    | j                  d       y7 7 S7 ?7 ,# 1 d{  7  sw Y   <xY w7 3# 1 d{  7  sw Y   CxY w# t
        j                  $ r}|j                  d      d}~ww xY ww)zU
        Initiate a :sql:`COPY` operation and return an object to manage it.
        Nr   r   )	rH   rI   rJ   _start_copy_genr   rL   rM   rN   rt   )r!   	statementrC   r   copyrO   s         r#   r   zAsyncCursor.copy:  s     	*zz  jjood&:&:9f&MNNN$T&9  TJ   	##A&N       
  	*##D))	*s   DC B#C /CB%C'B'(C+B+1C<B)=CC C C D#C %C'C)C+B=	1B42B=	9C C CC	CC D *C;;D  Dc                  K   | j                   dur| j                  s| j                  j                  rw| j                  j                  4 d {    | j                  j                  | j                  j                  j                  d             d {    d d d       d {    y y y y 7 _7 7 # 1 d {  7  sw Y   y xY ww)NFT)flush)_execmany_returningr:   rH   rS   rI   rJ   
_fetch_genr-   s    r#   rx   zAsyncCursor._fetch_pipelineR  s     $$E1MM

$$zz S Sjjoodjj&:&:&E&ED&E&QRRRS S S % " 2SRS S S Ss[   ACB-CAB3B/B3C$B1%	C/B31C3C9B<:CC)r"   zAsyncConnection[Row])r"   AsyncConnection[Any]r'   AsyncRowFactory[Row])r"   r   r'   zAsyncRowFactory[Row] | None)returnr   )r1   ztype[BaseException] | Noner2   zBaseException | Noner3   zTracebackType | Noner   None)r   r   )r   r   )r'   r   r   r   )r   zRowMaker[Row]r    )
rB   r   rC   Params | Noner?   bool | Noner@   r   r   r   )rB   r   r?   r   r@   r   r   r   )
rB   r   rC   r   r?   r   r@   r   r   r   )rB   r   rW   zIterable[Params]rP   boolr   r   )
rB   r   rC   r   r@   r   rZ   intr   zAsyncIterator[Row])r   zAsyncIterator[Self])ru   r   r   r   )r   z
Row | None)r   )rZ   r   r   	list[Row])r   r   )r   r   )relative)r   r   r   strr   r   )r   r   rC   r   r   zAsyncWriter | Noner   zAsyncIterator[AsyncCopy])__name__
__module____qualname__	__slots__r   r$   r.   r4   r0   propertyr'   setterr=   rD   rX   rl   ro   rv   r}   r   r   r   r   r   r   r   rx   __classcell__)r+   s   @r#   r   r      s   JI= =.@T  48	B(B 1	B, & %	
 
 ! ! / /
'  !%
  $" 
   
  
  $" 	
  
  !%
  $" 
   
* PU**(8*HL*	*B !%0
 #00 0
 0 0 
0d4 &
>"  !%'
 &*'' '
 #' 
"' '.Sr%   r   r   N))__doc__
__future__r   typesr   typingr   r   r   
contextlibr   collections.abcr	   r
    r   rL   r   abcr   r   r   r   r   r   rowsr   r   r   _compatr   r   _cursor_baser   _pipeline_asyncr   connection_asyncr   TransactionStatusrg   r   r   r%   r#   <module>r      sn    #  / / * 3   / / ( 0 0 # $ *1				$	$|S*3S89 |Sr%   