o
    DDi,                     @  s   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rjd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)TYPE_CHECKINGAnyoverload)asynccontextmanager)AsyncIteratorIterable   )errors)pq)ParamsQueryQueryNoTemplate)	AsyncCopyAsyncWriter)AsyncRowFactoryRowRowMaker)SelfTemplate)
BaseCursor)AsyncPipeline)AsyncConnectionc                      sp  e Zd ZdZdZeddddZeded
dZdddf fddZdgddZdhddZdiddZe	djddZ
e
jdkd dZ
dld"d#Ze	dmddd$dnd,d-Zeddd$dod/d-Z	dmddd$dpd1d-Zd2d3dqd8d9Z	dmdd:d;drd?d@ZdsdBdCZdtdEdFZdudHdIZdvdwdLdMZdxdNdOZdgdPdQZdydSdTZdzd{dYdZZe	dmdd[d|d`daZdidbdcZ  ZS )}AsyncCursorpsycopg 
connectionAsyncConnection[Row]c                 C     d S Nr   )selfr   r   r   M/var/www/Datamplify/venv/lib/python3.10/site-packages/psycopg/cursor_async.py__init__!      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__2   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!   r1   r3   r5   r   r   r"   	__aexit__5   s   zAsyncCursor.__aexit__c                   s   |    dS )zI
        Close the current cursor and free associated resources.
        N)_closer/   r   r   r"   r8   =   s   zAsyncCursor.closec                 C  s   | j S )z9Writable attribute to control how result rows are formed.r+   r/   r   r   r"   r&   C   s   zAsyncCursor.row_factoryc                 C  s   || _ | jr|| | _d S d S r    )r+   pgresult	_make_row)r!   r&   r   r   r"   r&   H   s   RowMaker[Row]c                 C  s
   |  | S r    r;   r/   r   r   r"   _make_row_makerN   s   
zAsyncCursor._make_row_makerpreparebinaryqueryr   paramsParams | NonerA   bool | NonerB   c                     d S r    r   )r!   rC   rD   rA   rB   r   r   r"   executeQ   s   zAsyncCursor.executer   c                  rG   r    r   )r!   rC   rA   rB   r   r   r"   rH   [   s   r   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!   rC   rD   rA   rB   exr   r   r"   rH   d   s   
F)	returning
params_seqIterable[Params]rQ   boolc             
     sD  z| j j4 I dH x t rP| j jr"| j | |||I dH  nT| j  4 I dH  | j | |||I dH  W d  I dH  n1 I dH sJw   Y  n| j | |||I dH  W d  I dH  W dS W d  I dH  W dS 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)rI   rJ   r   is_supported	_pipelinerK   _executemany_gen_pipeline_pipeline_nolock_executemany_gen_no_pipelinerM   rN   rO   )r!   rC   rR   rQ   rP   r   r   r"   executemanyx   s8   ( 2
zAsyncCursor.executemanyr
   rB   sizer\   intAsyncIterator[Row]c          	      C sv  | j jr
td| jj4 I dH  zzC| j| j||||dI dH  d}| j| |I dH  }rWt	|j
D ]}| j|| jV  q;d}| j| |I dH  }s6W n tjyj } 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 s4w   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 modeNr[   TF)first)_pgconnpipeline_statusrM   ProgrammingErrorrI   rJ   rK   _stream_send_gen_stream_fetchone_genrangentuples_txload_rowr=   rN   rO   transaction_statusACTIVE_try_cancel	Exception)	r!   rC   rD   rB   r\   r_   resposrP   r   r   r"   stream   s   










0zAsyncCursor.streamAsyncIterator[Self]c                 C s"   | j r	 | V  |  sdS qdS )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.
        TN)r<   nextsetr/   r   r   r"   results   s   zAsyncCursor.resultsindexc                   sf   t | j |  krt | jk s!n td| dt | j d|dk r,t | j| }| | | S )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!   rs   r   r   r"   
set_result   s   $
zAsyncCursor.set_result
Row | Nonec                   sL   |   I dH  |  }| j|jk r$| j| j| j}|  jd7  _|S dS )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_posrf   rg   rh   r=   r!   rm   recordr   r   r"   fetchone   s   zAsyncCursor.fetchoner   	list[Row]c                   sZ   |   I dH  |  }|s| j}| j| jt| j| |j| j}|  jt	|7  _|S )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)
rz   r{   	arraysizerg   	load_rowsr|   minrf   r=   rt   )r!   r\   rm   recordsr   r   r"   	fetchmany  s   zAsyncCursor.fetchmanyc                   s:   |   I dH  |  }| j| j|j| j}|j| _|S )z
        Return all the remaining records from the current result set.

        :rtype: Sequence[Row], with Row defined by `row_factory`
        N)rz   r{   rg   r   r|   rf   r=   )r!   rm   r   r   r   r"   fetchall  s   zAsyncCursor.fetchallc                 C  s   | S r    r   r/   r   r   r"   	__aiter__   r$   zAsyncCursor.__aiter__r   c                   sP   |   I d H  |  }| j|jk r$| j| j| j}|  jd7  _|S td)Nr
   zno more records to return)rz   r{   r|   rf   rg   rh   r=   StopAsyncIterationr}   r   r   r"   	__anext__#  s   zAsyncCursor.__anext__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)rz   _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  t| |d4 I dH }|V  W d  I dH  n1 I dH s6w   Y  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   )	rI   rJ   rK   _start_copy_genr   rM   rN   rO   rw   )r!   r   rD   r   copyrP   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_returningr<   rI   rV   rJ   rK   
_fetch_genr/   r   r   r"   rz   R  s   
 .zAsyncCursor._fetch_pipeline)r   r   )r   r%   r&   r'   )r   r%   r&   r)   )r.   r   )r1   r2   r3   r4   r5   r6   r.   r7   )r.   r7   )r.   r'   )r&   r'   r.   r7   )r.   r>   r    )
rC   r   rD   rE   rA   rF   rB   rF   r.   r   )rC   r   rA   rF   rB   rF   r.   r   )
rC   r   rD   rE   rA   rF   rB   rF   r.   r   )rC   r   rR   rS   rQ   rT   r.   r7   )
rC   r   rD   rE   rB   rF   r\   r]   r.   r^   )r.   rp   )rs   r]   r.   r   )r.   ry   )r   )r\   r]   r.   r   )r.   r   )r.   r   )r   )r   r]   r   r   r.   r7   )r   r   rD   rE   r   r   r.   r   )__name__
__module____qualname__	__slots__r   r#   r0   r9   r8   propertyr&   setterr?   rH   rZ   ro   rr   rx   r   r   r   r   r   r   r   r   rz   __classcell__r   r   r,   r"   r      sj    
	


	!
2




	r   r%   N))__doc__
__future__r   typesr   typingr   r   r   
contextlibr   collections.abcr   r	    r   rM   r   abcr   r   r   r   r   r   rowsr   r   r   _compatr   r   _cursor_baser   _pipeline_asyncr   connection_asyncr   TransactionStatusrj   r   r   r   r   r"   <module>   s$    