o
    "4g%                     @  s   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	rbddl!m"Z" ej#j$Z$G d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                      s"  e Zd ZdZdZedWddZedXd
dZdddY fddZdZddZd[ddZd\ddZe	d]ddZ
e
jd^d dZ
d_d"d#Z	d`ddd$dad,d-Zd.d/dbd4d5Z	d`dd6d7dcd;d<Zddd>d?ZdedfdBdCZdgdDdEZdhdFdGZdidjdLdMZe	d`ddNdkdSdTZd\dUdVZ  ZS )lAsyncCursorpsycopg 
connectionAsyncConnection[Row]c                 C     d S Nr   )selfr   r   r   ]/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/cursor_async.py__init__    s   zAsyncCursor.__init__AsyncConnection[Any]row_factoryAsyncRowFactory[Row]c                C  r   r   r   r   r   r#   r   r   r    r!   #   s   N)r#   AsyncRowFactory[Row] | Nonec                  s   t  | |p
|j| _d S r   )superr!   r#   _row_factoryr%   	__class__r   r    r!   (   s   returnr   c                   s   | S r   r   r   r   r   r    
__aenter__1   s   zAsyncCursor.__aenter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | NoneNonec                   s   |   I d H  d S r   )close)r   r.   r0   r2   r   r   r    	__aexit__4   s   zAsyncCursor.__aexit__c                   s   |    dS )zI
        Close the current cursor and free associated resources.
        N)_closer,   r   r   r    r5   <   s   zAsyncCursor.closec                 C  s   | j S )z9Writable attribute to control how result rows are formed.r(   r,   r   r   r    r#   B   s   zAsyncCursor.row_factoryc                 C  s   || _ | jr|| | _d S d S r   )r(   pgresult	_make_row)r   r#   r   r   r    r#   G   s   RowMaker[Row]c                 C  s
   |  | S r   r8   r,   r   r   r    _make_row_makerM   s   
zAsyncCursor._make_row_makerpreparebinaryqueryr   paramsParams | Noner>   bool | Noner?   c             
     s   z3| j j4 I dH  | j | j||||dI dH  W d  I dH  W | S 1 I dH s-w   Y  W | S  tjyF } z|dd}~ww )z=
        Execute a query or command to the database.
        Nr=   )_connlockwait_execute_gene_NO_TRACEBACKwith_traceback)r   r@   rA   r>   r?   exr   r   r    executeP   s   
zAsyncCursor.executeF)	returning
params_seqIterable[Params]rM   boolc             
     s  zt  r| jj4 I dH  | jj}|r#| j| |||I dH  W d  I dH  n1 I dH s3w   Y  |s| j 4 I dH 7 | jj4 I dH  | j| |||I dH  W d  I dH  n1 I dH skw   Y  W d  I dH  n1 I dH sw   Y  W dS W dS W dS | jj4 I dH  | j| |||I dH  W d  I dH  W dS 1 I dH sw   Y  W dS  t	j
y } z|dd}~ww )zI
        Execute the same command with a sequence of input data.
        N)r   is_supportedrD   rE   	_pipelinerF   _executemany_gen_pipelinepipeline_executemany_gen_no_pipelinerH   rI   rJ   )r   r@   rN   rM   prK   r   r   r    executemanyd   s8   

(&X2
zAsyncCursor.executemanyr
   r?   sizerY   intAsyncIterator[Row]c          	      C s|  | j jr
td| jj4 I dH  zzF| j| j||||dI dH  d}| j| |I dH rZt	|D ]}| j
|| j}|du rH n|V  q8d}| j| |I dH s4W n tjym } z|dd}~ww W | j jtkr| j I dH  z| j| jddI dH r	 | j| jddI dH sW n	 ty   Y nw z| j| jddI dH  W nf ty   Y niw nQ| j jtkr| j I dH  z| j| jddI dH r	 | j| jddI dH sW n	 ty   Y nw z| j| jddI dH  W w  ty   Y w w w W d  I dH  dS W d  I dH  dS W d  I dH  dS 1 I dH s7w   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 modeNrX   TF)first)_pgconnpipeline_statusrH   ProgrammingErrorrD   rE   rF   _stream_send_gen_stream_fetchone_genrange_txload_rowr:   rI   rJ   transaction_statusACTIVE_try_cancel	Exception)	r   r@   rA   r?   rY   r\   posrecrK   r   r   r    stream   s   










!#0zAsyncCursor.stream
Row | Nonec                   sD   |   I dH  |   | j| j| j}|dur |  jd7  _|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_fetchrc   rd   _posr:   )r   recordr   r   r    fetchone   s   zAsyncCursor.fetchoner   	list[Row]c                   sf   |   I dH  |   | jsJ |s| j}| j| jt| j| | jj| j	}|  jt
|7  _|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)rm   rn   r9   	arraysizerc   	load_rowsro   minntuplesr:   len)r   rY   recordsr   r   r    	fetchmany   s   
zAsyncCursor.fetchmanyc                   sH   |   I dH  |   | 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)rm   rn   r9   rc   rt   ro   rv   r:   )r   rx   r   r   r    fetchall   s   

zAsyncCursor.fetchallc                  sT      I d H     d	 fdd}	 | j}|d u rd S   jd7  _|V  q)
Nri   rZ   r+   rl   c                   s    j |  jS r   )rc   rd   r:   )ri   r,   r   r    load   s   z#AsyncCursor.__aiter__.<locals>.loadTr
   )ri   rZ   r+   rl   )rm   rn   ro   )r   r{   rowr   r,   r    	__aiter__   s   
zAsyncCursor.__aiter__relativevaluemodestrc                   s    |   I dH  | || 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)rm   _scroll)r   r   r   r   r   r    scroll   s   zAsyncCursor.scrollwriter	statementr   AsyncWriter | NoneAsyncIterator[AsyncCopy]c             
   C s   zP| j j4 I dH  | j | ||I dH  W d  I dH  n1 I dH s(w   Y  t| |d4 I dH }|V  W d  I dH  n1 I dH sKw   Y  W n tjyc } z|dd}~ww | d dS )zU
        Initiate a :sql:`COPY` operation and return an object to manage it.
        Nr   r   )	rD   rE   rF   _start_copy_genr   rH   rI   rJ   _select_current_result)r   r   rA   r   copyrK   r   r   r    r     s   ((
zAsyncCursor.copyc              	     s   | j dur=| js?| jjrA| jj4 I d H  | j| jjjddI d H  W d   I d H  d S 1 I d H s6w   Y  d S d S d S d S )NFT)flush)_execmany_returningr9   rD   rR   rE   rF   
_fetch_genr,   r   r   r    rm   #  s   
 .zAsyncCursor._fetch_pipeline)r   r   )r   r"   r#   r$   )r   r"   r#   r&   )r+   r   )r.   r/   r0   r1   r2   r3   r+   r4   )r+   r4   )r+   r$   )r#   r$   r+   r4   )r+   r;   r   )
r@   r   rA   rB   r>   rC   r?   rC   r+   r   )r@   r   rN   rO   rM   rP   r+   r4   )
r@   r   rA   rB   r?   rC   rY   rZ   r+   r[   )r+   rl   )r   )rY   rZ   r+   rr   )r+   rr   )r+   r[   )r~   )r   rZ   r   r   r+   r4   )r   r   rA   rB   r   r   r+   r   )__name__
__module____qualname__	__slots__r   r!   r-   r6   r5   propertyr#   setterr<   rL   rW   rk   rq   ry   rz   r}   r   r	   r   rm   __classcell__r   r   r)   r    r      sP    
	


%
7

r   r"   N)&__doc__
__future__r   typesr   typingr   r   r   r   r   
contextlibr	    r   r   rH   abcr   r   r   r   r   rowsr   r   r   _compatr   rR   r   _cursor_baser   connection_asyncr   TransactionStatusrf   r   r   r   r   r    <module>   s"    