o
    h#                     @   sh   d dl Z ddlmZ ddlmZ G dd dejZG dd dejZG d	d
 d
eZG dd deZdS )    N   )connresource)
exceptionsc                       sH   e Zd ZdZdZ fddZejdd Zejdd Z	d	d
 Z
  ZS )CursorFactoryzA cursor interface for the results of a query.

    A cursor interface can be used to initiate efficient traversal of the
    results of a large query.
    )_state_args	_prefetch_query_timeout_record_classc                    sH   t  | || _|| _|| _|| _|| _|| _|d ur"|  d S d S N)	super__init__r   r   r	   r
   r   r   attach)self
connectionquerystateargsprefetchtimeoutrecord_class	__class__ b/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/asyncpg/cursor.pyr      s   
zCursorFactory.__init__c                 C   s4   | j d u rdn| j }t| j| j| j| j| j|| jS )N2   )r   CursorIterator_connectionr	   r   r   r   r
   )r   r   r   r   r   	__aiter__2   s   zCursorFactory.__aiter__c                 C   s>   | j d ur
tdt| j| j| j| j| j}|	| j
 S )Nz;prefetch argument can only be specified for iterable cursor)r   r   InterfaceErrorCursorr   r	   r   r   r   _initr
   	__await__)r   cursorr   r   r   r#   ?   s   
zCursorFactory.__await__c                 C   *   | j d ur| j   | j| j  d S d S r   r   detachr   _maybe_gc_stmtr   r   r   r   __del__M      

zCursorFactory.__del__)__name__
__module____qualname____doc__	__slots__r   r   guardedr   r#   r*   __classcell__r   r   r   r   r      s    	

r   c                       sX   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Z  ZS )
BaseCursor)r   r   _portal_name
_exhaustedr	   r   c                    sD   t  | || _|| _|d ur|  d | _d| _|| _|| _d S )NF)	r   r   r   r   r   r4   r5   r	   r   )r   r   r   r   r   r   r   r   r   r   ^   s   
zBaseCursor.__init__c                 C   s<   | j d u r
td| j jrtd| jjstdd S )Nz(cursor: no associated prepared statementz(cursor: the prepared statement is closedz1cursor cannot be created outside of a transaction)r   r   r    closedr   	_top_xactNoActiveSQLTransactionErrorr)   r   r   r   _check_readyi   s   
zBaseCursor._check_readyc                    s^   |    | jrtd| j}|j}|d| _|| j| j	| j|d|I d H \}}| _
|S )N!cursor already has an open portalportalT)r9   r4   r   r    r   	_protocol_get_unique_idbind_executer   r   r5   )r   nr   conprotocolbuffer_r   r   r   
_bind_execv   s   zBaseCursor._bind_execc                    sR   |    | jrtd| j}|j}|d| _|| j| j	| j|I d H }|S )Nr:   r;   )
r9   r4   r   r    r   r<   r=   bindr   r   )r   r   r@   rA   rB   r   r   r   _bind   s   
zBaseCursor._bindc                    sJ   |    | jstd| jj}|| j| j|d|I d H \}}| _|S )N#cursor does not have an open portalT)	r9   r4   r   r    r   r<   executer   r5   )r   r?   r   rA   rB   rC   r   r   r   _exec   s   zBaseCursor._execc                    s@   |    | jstd| jj}|| j|I d H  d | _d S )NrG   )r9   r4   r   r    r   r<   close_portal)r   r   rA   r   r   r   _close_portal   s   
zBaseCursor._close_portalc                 C   s^   g }| j r
|d |d | jjdrd}n| jj}d|| jj| jjd	|t
| S )N	exhausted zasyncpg.asyncpgz<{}.{} "{!s:.30}" {}{:#x}> )r5   appendr   r-   
startswithformatr,   r   r   joinid)r   attrsmodr   r   r   __repr__   s   

zBaseCursor.__repr__c                 C   r%   r   r&   r)   r   r   r   r*      r+   zBaseCursor.__del__)r,   r-   r.   r0   r   r9   rD   rF   rI   rK   rW   r*   r2   r   r   r   r   r3   S   s    	r3   c                       s<   e Zd ZdZ fddZejdd Zejdd Z  Z	S )r   )_bufferr   r
   c                    s@   t  ||||| |dkrtdt | _|| _|| _d S )Nr   z+prefetch argument must be greater than zero)	r   r   r   r    collectionsdequerX   r   r
   )r   r   r   r   r   r   r   r   r   r   r   r      s   


zCursorIterator.__init__c                 C   s   | S r   r   r)   r   r   r   r      s   zCursorIterator.__aiter__c                    s   | j d u r| jj| j| jd| jdI d H | _ | j   | js3| js3| 	| j
| jI d H }| j| | jsJ| jsJ| | j
| jI d H }| j| | jrY| jrY| | jI d H  | jra| j S tNT)namedr   )r   r   _get_statementr	   r
   r   r   r4   r5   rD   r   rX   extendrI   rK   popleftStopAsyncIteration)r   rB   r   r   r   	__anext__   s(   


zCursorIterator.__anext__)
r,   r-   r.   r0   r   r   r1   r   ra   r2   r   r   r   r   r      s    
r   c                   @   s^   e Zd ZdZdZdd ZejddddZejddd	d
Z	ejddde
fddZdS )r!   z-An open *portal* into the results of a query.r   c                    sR   | j d u r| jj| j|d| jdI d H | _ | j   |   | |I d H  | S r[   )r   r   r]   r	   r   r   r9   rF   )r   r   r   r   r   r"      s   

zCursor._initN)r   c                   sN   |    |dkrtd| jrg S | ||I dH }t||k r%d| _|S )zReturn the next *n* rows as a list of :class:`Record` objects.

        :param float timeout: Optional timeout value in seconds.

        :return: A list of :class:`Record` instances.
        r   n must be greater than zeroNT)r9   r   r    r5   rI   len)r   r?   r   recsr   r   r   fetch  s   
zCursor.fetchc                   sD   |    | jr
dS | d|I dH }t|dk rd| _dS |d S )zReturn the next row.

        :param float timeout: Optional timeout value in seconds.

        :return: A :class:`Record` instance.
        Nr   Tr   )r9   r5   rI   rc   )r   r   rd   r   r   r   fetchrow  s   zCursor.fetchrowreturnc                   sb   |    |dkrtd| jj}|d|| j|I dH }t|	 d }||k r/d| _
|S )zSkip over the next *n* rows.

        :param float timeout: Optional timeout value in seconds.

        :return: A number of rows actually skipped over (<= *n*).
        r   rb   zMOVE FORWARD {:d} {}Nr   T)r9   r   r    r   r<   r   rR   r4   intsplitr5   )r   r?   r   rA   statusadvancedr   r   r   forward/  s   

zCursor.forward)r,   r-   r.   r/   r0   r"   r   r1   re   rf   rh   rl   r   r   r   r   r!      s    r!   )	rY   rM   r   r   ConnectionResourcer   r3   r   r!   r   r   r   r   <module>   s   Eo8