
    :"ri#                         d dl Z ddlmZ ddlmZ  G d dej                        Z G d dej                        Z G d	 d
e      Z G d de      Zy)    N   )connresource)
exceptionsc                   t     e Zd ZdZdZ fdZej                  d        Zej                  d        Z	d Z
 x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                     t         |   |       || _        || _        || _        || _        || _        || _        ||j                          y y N)	super__init__r	   r
   r   r   r   r   attach)	self
connectionquerystateargsprefetchtimeoutrecord_class	__class__s	           V/var/www/fortnox.pascalinesoft.com/venv/lib/python3.12/site-packages/asyncpg/cursor.pyr   zCursorFactory.__init__   sQ     	$
!)LLN     c           	          | j                   dn| j                   }t        | j                  | j                  | j                  | j
                  | j                  || j                        S )N2   )r
   CursorIterator_connectionr   r   r	   r   r   )r   r   s     r   	__aiter__zCursorFactory.__aiter__2   sT    /2T^^KKKKJJMM
 	
r   c                    | j                   t        j                  d      t        | j                  | j
                  | 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   cursors     r   r'   zCursorFactory.__await__?   ss    >>%++MO OKKKKJJ
 ||DMM*4466r   c                     | j                   @| j                   j                          | j                  j                  | j                          y y r   r   detachr!   _maybe_gc_stmtr   s    r   __del__zCursorFactory.__del__M   9    ;;"KK ++DKK8 #r   )__name__
__module____qualname____doc__	__slots__r   r   guardedr"   r'   r.   __classcell__r   s   @r   r   r      sM    I( 

 

 7 79r   r   c                   L     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Z xZS )

BaseCursor)r   r	   _portal_name
_exhaustedr   r   c                     t         |   |       || _        || _        ||j	                          d | _        d| _        || _        || _        y )NF)	r   r   r	   r   r   r:   r;   r   r   )r   r   r   r   r   r   r   s         r   r   zBaseCursor.__init__^   sK    $
LLN )r   c                     | j                   t        j                  d      | j                   j                  rt        j                  d      | j                  j
                  st        j                  d      y )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-   s    r   _check_readyzBaseCursor._check_readyi   su    ;;++:< < ;;++:< < ))88CE E *r   c                 Z  K   | j                          | j                  rt        j                  d      | j                  }|j
                  }|j                  d      | _        |j                  | j                  | j                  | j                  |d|       d {   \  }}| _
        |S 7 w)N!cursor already has an open portalportalT)rA   r:   r   r$   r!   	_protocol_get_unique_idbind_executer   r	   r;   )r   nr   conprotocolbuffer_s          r   
_bind_execzBaseCursor._bind_execv   s     ++35 5 ==..x8+3+@+@KKT%6%64,J &J"4?&Js   BB+B)B+c                 D  K   | j                          | j                  rt        j                  d      | j                  }|j
                  }|j                  d      | _        |j                  | j                  | j                  | j                  |       d {   }|S 7 w)NrC   rD   )
rA   r:   r   r$   r!   rE   rF   bindr   r	   )r   r   rI   rJ   rK   s        r   _bindzBaseCursor._bind   s     ++35 5 ==..x8}}T[[$**%)%6%6%,. . .s   BB BB c                   K   | j                          | j                  st        j                  d      | j                  j
                  }|j                  | j                  | j                  |d|       d {   \  }}| _        |S 7 w)N#cursor does not have an open portalT)	rA   r:   r   r$   r!   rE   executer   r;   )r   rH   r   rJ   rK   rL   s         r   _execzBaseCursor._exec   s       ++57 7 ##--+3+;+;KK**AtW,> &>"4?&>s   A4B6B7Bc                    K   | j                          | j                  st        j                  d      | j                  j
                  }|j                  | j                  |       d {    d | _        y 7 w)NrR   )rA   r:   r   r$   r!   rE   close_portal)r   r   rJ   s      r   _close_portalzBaseCursor._close_portal   sn       ++57 7 ##--##D$5$5w???  	@s   A'A8)A6*A8c           	         g }| j                   r|j                  d       |j                  d       | j                  j                  j	                  d      rd}n| j                  j                  }dj                  || j                  j                  | j                  j                  dj                  |      t        |             S )N	exhausted zasyncpg.asyncpgz<{}.{} "{!s:.30}" {}{:#x}> )r;   appendr   r1   
startswithformatr0   r   r   joinid)r   attrsmods      r   __repr__zBaseCursor.__repr__   s    ??LL%R>>$$//
;C..++C+22((KKHHUORX' 	'r   c                     | j                   @| j                   j                          | j                  j                  | j                          y y r   r*   r-   s    r   r.   zBaseCursor.__del__   r/   r   )r0   r1   r2   r4   r   rA   rM   rP   rT   rW   rd   r.   r6   r7   s   @r   r9   r9   S   s3    I	*E 
	!' 9r   r9   c                   j     e Zd ZdZ fdZej                  d        Zej                  d        Z xZ	S )r    )_bufferr
   r   c                     t         |   |||||       |dk  rt        j                  d      t	        j
                         | _        || _        || _        y )Nr   z+prefetch argument must be greater than zero)	r   r   r   r$   collectionsdequerg   r
   r   )	r   r   r   r   r   r   r   r   r   s	           r   r   zCursorIterator.__init__   sW     	UE4Fq=++=? ? #((*!r   c                     | S r    r-   s    r   r"   zCursorIterator.__aiter__   s    r   c                 N  K   | j                   d| j                  j                  | j                  | j                  d| j
                         d {   | _         | j                   j                          | j                  sU| j                  sI| j                  | j                  | j                         d {   }| j                  j                  |       | j                  sU| j                  sI| j                  | j                  | j                         d {   }| j                  j                  |       | j                  r/| j                  r#| j                  | j                         d {    | j                  r| j                  j                         S t         7 M7 7 7 7wNT)namedr   )r   r!   _get_statementr   r   r   r   r:   r;   rM   r
   rg   extendrT   rW   popleftStopAsyncIteration)r   rK   s     r   	__anext__zCursorIterator.__anext__   s5    ;; $ 0 0 ? ?!//	 !@ ! DK KK   ??4>>4==IIFLL'||DOO::dnndmmDDFLL'$$T]]333<<<<''))  - J E 4sK   AF%FA$F%4F5A F%F!AF%+F#,1F%F%!F%#F%)
r0   r1   r2   r4   r   r   r5   r"   rt   r6   r7   s   @r   r    r       sA    4I (   ! !r   r    c                       e Zd ZdZdZd Zej                  ddd       Zej                  ddd       Z	ej                  ddde
fd	       Zy)
r%   z-An open *portal* into the results of a query.rl   c                 4  K   | j                   Z| j                  j                  | j                  |d| j                         d {   | _         | j                   j                          | j                          | j                  |       d {    | S 7 N7 wrn   )r   r!   rp   r   r   r   rA   rP   )r   r   s     r   r&   zCursor._init   s     ;; $ 0 0 ? ?!//	 !@ ! DK KK jj!!! 	"s%   ABBABBBBN)r   c                   K   | j                          |dk  rt        j                  d      | j                  rg S | j	                  ||       d{   }t        |      |k  rd| _        |S 7 w)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)rA   r   r$   r;   rT   len)r   rH   r   recss       r   fetchzCursor.fetch  sh      	6++,IJJ??IZZ7++t9q="DO ,s   AA.A,A.c                   K   | j                          | j                  ry| j                  d|       d{   }t        |      dk  rd| _        y|d   S 7 w)zReturn the next row.

        :param float timeout: Optional timeout value in seconds.

        :return: A :class:`Record` instance.
        Nr   Tr   )rA   r;   rT   ry   )r   r   rz   s      r   fetchrowzCursor.fetchrow  sT      	??ZZ7++t9q="DOAw	 ,s   3AA Areturnc                J  K   | j                          |dk  rt        j                  d      | j                  j                  }|j                  dj                  || j                        |       d{   }t        |j                         d         }||k  rd| _
        |S 7 .w)zSkip over the next *n* rows.

        :param float timeout: Optional timeout value in seconds.

        :return: A number of rows actually skipped over (<= *n*).
        r   rx   zMOVE FORWARD {:d} {}Nr   T)rA   r   r$   r!   rE   r   r_   r:   intsplitr;   )r   rH   r   rJ   statusadvanceds         r   forwardzCursor.forward/  s      	6++,IJJ##--~~&<&C&Ct  '"#*, , v||~a()a<"DO,s   A0B#2B!3/B#)r0   r1   r2   r3   r4   r&   r   r5   r{   r}   r   r   rl   r   r   r%   r%      sm    7I (,  " (,    *. 3  r   r%   )	ri   rZ   r   r   ConnectionResourcer   r9   r    r%   rl   r   r   <module>r      sU      B9L33 B9Jl900 l9^5!Z 5!pIZ Ir   