
    pj%                       d Z ddlmZ ddlmZ ddlmZ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 dd
lmZmZ ddlmZmZmZ ddlmZ ddlmZ ddlm Z  e	rddl!m"Z" ej#        j$        Z$ G d de def                   Z%dS )z
Psycopg AsyncCursor object.
    )annotations)TracebackType)AnyAsyncIteratorIterableTYPE_CHECKINGoverload)asynccontextmanager   )pq)errors)QueryParams)	AsyncCopyAsyncWriter)RowRowMakerAsyncRowFactory)Self)Pipeline)
BaseCursor)AsyncConnectionc                  D    e Zd ZdZdZedGd            ZedHd	            Zd
ddI fdZdJdZdKdZdLdZe	dMd            Z
e
j        dNd            Z
dOdZ	 dPd
d
ddQd&Zd'd(dRd-Z	 dPd
d.d/dSd3ZdTd5ZdUdVd8ZdWd9ZdXd:ZdYdZd?Ze	 dPd
d@d[dE            ZdLdFZ xZS )\AsyncCursorpsycopg 
connectionAsyncConnection[Row]c                    d S Nr   )selfr   s     d/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/psycopg/cursor_async.py__init__zAsyncCursor.__init__    s    :=#    AsyncConnection[Any]row_factoryAsyncRowFactory[Row]c                   d S r    r   )r!   r   r&   s      r"   r#   zAsyncCursor.__init__#   s	     sr$   N)r&   AsyncRowFactory[Row] | Nonec               f    t                                          |           |p|j        | _        d S r    )superr#   r&   _row_factory)r!   r   r&   	__class__s      r"   r#   zAsyncCursor.__init__(   s3     	$$$'A:+Ar$   returnr   c                
   K   | S r    r   r!   s    r"   
__aenter__zAsyncCursor.__aenter__1   s      r$   exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | NoneNonec                >   K   |                                   d {V  d S r    )close)r!   r2   r4   r6   s       r"   	__aexit__zAsyncCursor.__aexit__4   s.       jjllr$   c                2   K   |                                   dS )zI
        Close the current cursor and free associated resources.
        N)_closer0   s    r"   r:   zAsyncCursor.close<   s       	r$   c                    | j         S )z9Writable attribute to control how result rows are formed.r,   r0   s    r"   r&   zAsyncCursor.row_factoryB   s       r$   c                F    || _         | j        r ||           | _        d S d S r    )r,   pgresult	_make_row)r!   r&   s     r"   r&   zAsyncCursor.row_factoryG   s3    '= 	/([..DNNN	/ 	/r$   RowMaker[Row]c                ,    |                      |           S r    r?   r0   s    r"   _make_row_makerzAsyncCursor._make_row_makerM   s      &&&r$   preparebinaryqueryr   paramsParams | NonerG   bool | NonerH   c          	     @  K   	 | j         j        4 d{V  | j                             |                     ||||                     d{V  ddd          d{V  n# 1 d{V swxY w Y   n,# t          j        $ r}|                    d          d}~ww xY w| S )z=
        Execute a query or command to the database.
        NrF   )_connlockwait_execute_gene_NO_TRACEBACKwith_traceback)r!   rI   rJ   rG   rH   exs         r"   executezAsyncCursor.executeP   sQ     	*z        joo%%eVWV%TT                                    	* 	* 	*##D)))	*s:   A2 8A A2  
A**A2 -A*.A2 2BBBF)	returning
params_seqIterable[Params]rW   boolc          	       K   	 t          j                    r/| j        j        4 d{V  | j        j        }|r5| j                            |                     |||                     d{V  ddd          d{V  n# 1 d{V swxY w Y   |s| j                                        4 d{V  | j        j        4 d{V  | j                            |                     |||                     d{V  ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  n# 1 d{V swxY w Y   dS dS dS | j        j        4 d{V  | j                            |                     |||                     d{V  ddd          d{V  dS # 1 d{V swxY w Y   dS # t          j
        $ r}|                    d          d}~ww xY w)zI
        Execute the same command with a sequence of input data.
        N)r   is_supportedrN   rO   	_pipelinerP   _executemany_gen_pipelinepipeline_executemany_gen_no_pipelinerR   rS   rT   )r!   rI   rX   rW   prU   s         r"   executemanyzAsyncCursor.executemanyd   sn     	*$&&   :?        
,A "joo ::5*iXX                                    #z2244        djo        "joo ::5*iXX                                                                 
  :?        *//99%YWW                                       	* 	* 	*##D)))	*s   &F5 AB .F5  
B

F5 B
$F5 2D06D;D0
D	D0D	D0F5 0
D::F5 =D:>F5 F5 6F"F5 "
F,,F5 /F,0F5 5GGGr   rH   sizerd   intAsyncIterator[Row]c          	      K   | j         j        rt          j        d          | j        j        4 d{V  	 | j                            |                     ||||                     d{V  d}| j                            |                     |                     d{V rpt          |          D ]+}| j
                            || j                  }| n|W V  ,d}| j                            |                     |                     d{V pn,# t          j        $ r}|                    d          d}~ww xY w| j         j        t           k    r| j                                         d{V  	 | j                            |                     d                     d{V r5	 | j                            |                     d                     d{V 5n# t$          $ r Y nw xY w	 | j                            |                     d                     d{V  n# t$          $ r Y nw xY wn# | j         j        t           k    r| j                                         d{V  	 | j                            |                     d                     d{V r5	 | j                            |                     d                     d{V 5n# t$          $ r Y nw xY w	 | j                            |                     d                     d{V  w # t$          $ r Y w w xY ww xY wddd          d{V  dS # 1 d{V swxY w Y   dS )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 modeNrc   TF)first)_pgconnpipeline_statusrR   ProgrammingErrorrN   rO   rP   _stream_send_gen_stream_fetchone_genrange_txload_rowrB   rS   rT   transaction_statusACTIVE_try_cancel	Exception)	r!   rI   rJ   rH   rd   rh   posrecrU   s	            r"   streamzAsyncCursor.stream   s      <' 	Q$%OPPP:? #	 #	 #	 #	 #	 #	 #	 #	"joo))%T)RR          JOOD,E,Ee,L,LMMMMMMMM "$T{{ " ""h//T^DD;!E!					!E !JOOD,E,Ee,L,LMMMMMMMM " ? . . .''---. <2f<< *00222222222$(JOO 55E5BB% %       ! ! %)JOO 55E5BB% %       ! %   
"jood.G.Ge.G.T.TUUUUUUUUUU$   ! =4<2f<< *00222222222$(JOO 55E5BB% %       ! ! %)JOO 55E5BB% %       ! %   
"jood.G.Ge.G.T.TUUUUUUUUUU$   ! =%#	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	s   MCDH7D=#D88D==H7 4M5A)GM
G,)M+G,,M04H&$M&
H30M2H33M75L.-A)KL.
K$	!L.#K$	$L.(4LL.
L*	'L.)L*	*L..M
MM
Row | Nonec                   K   |                                   d{V  |                                  | j                            | j        | j                  }|| xj        dz  c_        |S )z
        Return the next record from the current recordset.

        Return `!None` the recordset is finished.

        :rtype: Row | None, with Row defined by `row_factory`
        Nr   )_fetch_pipeline_check_result_for_fetchro   rp   _posrB   )r!   records     r"   fetchonezAsyncCursor.fetchone   ss       ""$$$$$$$$$$$&&&""49dn==IINIIr$   r   	list[Row]c                P  K   |                                   d{V  |                                  | j        sJ |s| j        }| j                            | j        t          | j        |z   | j        j                  | j	                  }| xj        t          |          z  c_        |S )z
        Return the next `!size` records from the current recordset.

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

        :rtype: Sequence[Row], with Row defined by `row_factory`
        N)rz   r{   rA   	arraysizero   	load_rowsr|   minntuplesrB   len)r!   rd   recordss      r"   	fetchmanyzAsyncCursor.fetchmany   s       ""$$$$$$$$$$$&&&}} 	">D($$I	D $-"788N
 

 			S\\!		r$   c                   K   |                                   d{V  |                                  | j        sJ | j                            | j        | j        j        | j                  }| j        j        | _        |S )z
        Return all the remaining records from the current recordset.

        :rtype: Sequence[Row], with Row defined by `row_factory`
        N)rz   r{   rA   ro   r   r|   r   rB   )r!   r   s     r"   fetchallzAsyncCursor.fetchall   s~       ""$$$$$$$$$$$&&&}}($$TY0Et~VVM)	r$   c                   K                                      d {V                                    d fd}	  | j                  }|d S  xj        dz  c_        |W V  *)	Nru   re   r.   rx   c                D    j                             | j                  S r    )ro   rp   rB   )ru   r!   s    r"   loadz#AsyncCursor.__aiter__.<locals>.load   s    8$$S$.999r$   Tr   )ru   re   r.   rx   )rz   r{   r|   )r!   r   rows   `  r"   	__aiter__zAsyncCursor.__aiter__   s      ""$$$$$$$$$$$&&&	: 	: 	: 	: 	: 	:	$ty//C{IINIIIIII	r$   relativevaluemodestrc                j   K   |                                   d{V  |                     ||           dS )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)rz   _scroll)r!   r   r   s      r"   scrollzAsyncCursor.scroll   sF       ""$$$$$$$$$UD!!!!!r$   writer	statementr   AsyncWriter | NoneAsyncIterator[AsyncCopy]c                K   	 | j         j        4 d{V  | j                             |                     ||                     d{V  ddd          d{V  n# 1 d{V swxY w Y   t	          | |          4 d{V }|W V  ddd          d{V  n# 1 d{V swxY w Y   n,# t
          j        $ r}|                    d          d}~ww xY w|                     d           dS )zU
        Initiate a :sql:`COPY` operation and return an object to manage it.
        Nr   r   )	rN   rO   rP   _start_copy_genr   rR   rS   rT   _select_current_result)r!   r   rJ   r   copyrU   s         r"   r   zAsyncCursor.copy  s;     	*z O O O O O O O Ojood&:&:9f&M&MNNNNNNNNNO O O O O O O O O O O O O O O O O O O O O O O O O O O !f555       



                           	* 	* 	*##D)))	*
 	##A&&&&&s^   B/ 5AB/ 
A''B/ *A'+B/ BB/ 
B''B/ *B'+B/ /C>CCc                8  K   | j         dur| j        s| j        j        r{| j        j        4 d {V  | j                            | j        j                            d                     d {V  d d d           d {V  d S # 1 d {V swxY w Y   d S d S d S d S )NFT)flush)_execmany_returningrA   rN   r]   rO   rP   
_fetch_genr0   s    r"   rz   zAsyncCursor._fetch_pipeline#  sk     $E11M 2
$ 2 z S S S S S S S Sjoodj&:&E&ED&E&Q&QRRRRRRRRRS S S S S S S S S S S S S S S S S S S S S S S S S S S S S S	 211111s   ?B
BB)r   r   )r   r%   r&   r'   )r   r%   r&   r)   )r.   r   )r2   r3   r4   r5   r6   r7   r.   r8   )r.   r8   )r.   r'   )r&   r'   r.   r8   )r.   rC   r    )
rI   r   rJ   rK   rG   rL   rH   rL   r.   r   )rI   r   rX   rY   rW   rZ   r.   r8   )
rI   r   rJ   rK   rH   rL   rd   re   r.   rf   )r.   rx   )r   )rd   re   r.   r   )r.   r   )r.   rf   )r   )r   re   r   r   r.   r8   )r   r   rJ   rK   r   r   r.   r   )__name__
__module____qualname__	__slots__r	   r#   r1   r;   r:   propertyr&   setterrE   rV   rb   rw   r~   r   r   r   r   r
   r   rz   __classcell__)r-   s   @r"   r   r      so       JI=== X=   X 48	B B B B B B B B          ! ! ! X! / / / /
' ' ' ' !%
  $"     2   *  *  *  *  *  *J !%5
 #5 5 5 5 5 5n       ,      " " " " "  !%'
 &*' ' ' ' ' '.S S S S S S S Sr$   r   r%   N)&__doc__
__future__r   typesr   typingr   r   r   r   r	   
contextlibr
    r   r   rR   abcr   r   r   r   r   rowsr   r   r   _compatr   r]   r   _cursor_baser   connection_asyncr   TransactionStatusrr   r   r   r$   r"   <module>r      s    # " " " " "       H H H H H H H H H H H H H H * * * * * *                     ( ( ( ( ( ( ( ( 0 0 0 0 0 0 0 0 0 0             $ $ $ $ $ $ 2111111			$NS NS NS NS NS*3S89 NS NS NS NS NSr$   