
    $g9                       d Z ddlmZ ddlmZ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 dd	lmZmZ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 ddl m!Z! ddl"m#Z# erddl$m%Z% ddl&m'Z' dZ(ej)        j*        Z*ej)        j+        Z+ej,        j-        Z-ej,        j.        Z.ej/        j0        Z0ej/        j1        Z1 G d de!eef                   Z2 G d de2def         ee                   Z3 G d de2def         e#e                   Z4dS )z%
psycopg server-side cursor objects.
    )annotations)AnyAsyncIteratorIterableIteratorTYPE_CHECKINGoverload)warn   )pq)sql)errors)ConnectionTypeQueryParamsPQGen)Row
RowFactoryAsyncRowFactory)Cursor)Self)execute)
BaseCursor)AsyncCursor)
Connection)AsyncConnectiond   c                       e Zd ZdZd                                Zd$d	Zd% fdZed%d            Z	ed&d            Z
ed'd            Zed(d            Z	 	 d)d*dZd+dZd+dZd,dZd-d!Zd.d#Z xZS )/ServerCursorMixinzDMixin to add ServerCursor behaviour and implementation a BaseCursor.z7_name _scrollable _withhold _described itersize _formatnamestr
scrollablebool | Nonewithholdboolc                n    || _         || _        || _        d| _        t          | _        t          | _        d S )NF)_name_scrollable	_withhold
_describedDEFAULT_ITERSIZEitersizeTEXT_format)selfr    r"   r$   s       U/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/psycopg/server_cursor.py__init__zServerCursorMixin.__init__,   s3     
%!-    returnc                    t                                                                          d d          }|                    d| j                   d                    |          S )Nr    )super__repr__splitinsertr'   join)r/   parts	__class__s     r0   r7   zServerCursorMixin.__repr__9   sQ      ""((q11Q4:)***xxr2   c                    | j         S )zThe name of the cursor.)r'   r/   s    r0   r    zServerCursorMixin.name?   s     zr2   c                    | j         S )z
        Whether the cursor is scrollable or not.

        If `!None` leave the choice to the server. Use `!True` if you want to
        use `scroll()` on the cursor.
        )r(   r>   s    r0   r"   zServerCursorMixin.scrollableD   s     r2   c                    | j         S )zY
        If the cursor can be used after the creating transaction has committed.
        )r)   r>   s    r0   r$   zServerCursorMixin.withholdN   s    
 ~r2   
int | Nonec                `    | j         }|o|j        t          k    p
|j        dk    }|r| j        ndS )zoIndex of the next row to fetch in the current result.

        `!None` if there is no result to fetch.
        r2   N)pgresultstatus	TUPLES_OKcommand_status_pos)r/   restupless      r0   	rownumberzServerCursorMixin.rownumberU   s<     m O#*	1NS5G35N",tyy,r2   Nqueryr   paramsParams | NonebinaryPQGen[None]c              #  0  K   |                      |          }| j        r!|                                 E d{V  d| _        |                     |          E d{V  |                     ||          }|                     |d           t          | j        j                  E d{V }|d         j	        t          k    r|                     |d                    || j        | _        n|rt          nt          | _        |                                 E d{V  dS )z0Generator implementing `ServerCursor.execute()`.NFT)force_extended)_make_declare_statementr*   
_close_gen_start_query_convert_query_execute_sendr   _connpgconnrD   
COMMAND_OK_raise_for_resultformatr.   BINARYr-   _describe_gen)r/   rK   rL   rN   pgqresultss         r0   _declare_genzServerCursorMixin._declare_genb   s@      ,,U33 ? 	$(((((((((#DO$$U+++++++++!!%003t444$TZ%6777777772;++""72;/// >;DLL%+566DL %%'''''''''''r2   c              #  *  K   | j                             | j                            | j                             t          | j                   E d {V }|                     |           || _        |                     d| j	                   d| _
        d S )Nr   )r\   T)_pgconnsend_describe_portalr'   encode	_encodingr   _check_results_results_select_current_resultr.   r*   )r/   r`   s     r0   r^   zServerCursorMixin._describe_gen   s      ))$**;*;DN*K*KLLL$T\22222222G$$$##Adl#;;;r2   c              #  ,  K   | j         j        j        }|t          k    r|t          k    rd S | j        s|t          k    rd S | j        sot          j        d          	                    t          j
        | j                            }| j                             |          E d {V }|J |j        dk    rd S t          j        d          	                    t          j        | j                            }| j                             |          E d {V  d S )Nz3SELECT 1 FROM pg_catalog.pg_cursors WHERE name = {}r   zCLOSE {})rX   rY   transaction_statusIDLEINTRANSr)   r*   r   SQLr\   Literalr'   _exec_commandntuples
Identifier)r/   tsrK   rH   s       r0   rT   zServerCursorMixin._close_gen   s     Z1 ::"--F ~ 	"**F  	GE fS[,,--  "Z55e<<<<<<<<C???{a
##**3>$*+E+EFF:++E22222222222r2   numPQGen[list[Row]]c              #  h  K   | j         rt          j        d          | j        s4|                                 E d {V  |                                 E d {V  t          j        d                              |t          j        d          nt          j	        |          t          j
        | j                            }| j                            || j                  E d {V }|J || _        | j                            |d           | j                            d|j        | j                  S )Nzthe cursor is closedzFETCH FORWARD {} FROM {}ALL)result_formatF)set_loadersr   )closedeInterfaceErrorr*   rU   r^   r   rn   r\   ro   rr   r'   rX   rp   r.   rC   _txset_pgresult	load_rowsrq   	_make_row)r/   rt   rK   rH   s       r0   
_fetch_genzServerCursorMixin._fetch_gen   s9     ; 	;"#9::: 	,((*********))+++++++++233::!kCGENNNs{3/?/?N4:&&
 
 11%t|1TTTTTTTTcu555x!!!S[$.AAAr2   valueintmodec              #  P  K   |dvrt          d| d          t          j        d                              t          j        |dk    rdnd          t          j        |          t          j        | j                            }| j                            |          E d {V  d S )N)relativeabsolutez
bad mode: z'. It should be 'relative' or 'absolute'zMOVE{} {} FROM {}r   z	 ABSOLUTE )	
ValueErrorr   rn   r\   ro   rr   r'   rX   rp   )r/   r   r   rK   s       r0   _scroll_genzServerCursorMixin._scroll_gen   s      ///W$WWWXXX+,,33G4:#5#5KK2>>KN4:&&
 

 :++E22222222222r2   sql.Composedc                   t          |t                    r|                    | j                  }t          |t          j                  st	          j        |          }t	          j        d          t	          j        | j                  g}| j	        0|
                    t	          j        | j	        rdnd                     |
                    t	          j        d                     | j        r'|
                    t	          j        d                     |
                    t	          j        d                     |
                    |           t	          j        d                              |          S )NDECLARESCROLLz	NO SCROLLCURSORz	WITH HOLDFORr5   )
isinstancebytesdecoderf   r   
Composablern   rr   r'   r(   appendr)   r:   )r/   rK   r;   s      r0   rS   z)ServerCursorMixin._make_declare_statement   s*   eU## 	1LL00E%00 	#GENNE GIN4:&&
 'LLT-=!N;OOPPPSWX&&'''> 	/LL--...SWU^^$$$Uws||  '''r2   )r    r!   r"   r#   r$   r%   )r3   r!   )r3   r#   )r3   r%   )r3   rA   )NN)rK   r   rL   rM   rN   r#   r3   rO   )r3   rO   )rt   rA   r3   ru   )r   r   r   r!   r3   rO   )rK   r   r3   r   )__name__
__module____qualname____doc__r8   	__slots__r1   r7   propertyr    r"   r$   rJ   ra   r^   rT   r   r   rS   __classcell__r<   s   @r0   r   r   '   sq       NNIOOQQI            X       X     X 
- 
- 
- X
- !%"	( ( ( ( (>   3 3 3 34B B B B(3 3 3 3( ( ( ( ( ( ( (r2   r   c                       e Zd ZdZdZedddd8d            Zedddd9d            Zddddd:dZd;dZd; fdZ	 d<ddd=d#Zd$d%d>d)Z	d?d+Z
d@dAd0ZdBd1ZdCd3ZdDdEd7Z xZS )FServerCursorpsycopg NFr"   r$   
connectionConnection[Row]r    r!   r"   r#   r$   r%   c                   d S Nr   r/   r   r    r"   r$   s        r0   r1   zServerCursor.__init__   	     sr2   Connection[Any]row_factoryRowFactory[Row]c                   d S r   r   r/   r   r    r   r"   r$   s         r0   r1   zServerCursor.__init__   	     sr2   r   r"   r$   RowFactory[Row] | Nonec               |    t          j        | ||p|j                   t                              | |||           d S N)r   )r   r1   r   r   r   s         r0   r1   zServerCursor.__init__   sN     	*+*O9O	
 	
 	
 	
 	""4z8DDDDDr2   r3   Nonec                J    | j         st          d|  dt                     d S d S Nzthe server-side cursor z[ was deleted while still open. Please use 'with' or '.close()' to close the cursor properlyrz   r
   ResourceWarningr>   s    r0   __del__zServerCursor.__del__   O    { 	P$ P P P    	 	r2   c                ,   | j         j        5  | j        r	 ddd           dS | j         j        s,| j                             |                                            t                                                       ddd           dS # 1 swxY w Y   dS )zI
        Close the current cursor and free associated resources.
        NrX   lockrz   waitrT   r6   closer/   r<   s    r0   r   zServerCursor.close  s     Z_ 	 	{ 	 	 	 	 	 	 	 	 :$ 3
 1 1222GGMMOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   	B	AB		BBrN   rK   r   rL   rM   rN   kwargsr   r   c                  |r%t          dt          |          d                    | j        j        rt	          j        d          	 | j        j        5  | j                            | 	                    |||                     ddd           n# 1 swxY w Y   n,# t          j
        $ r}|                    d          d}~ww xY w| S )zC
        Open a cursor to execute a query to the database.
        keyword not supported: r   2server-side cursors not supported in pipeline modeN	TypeErrorlistrc   pipeline_statusr{   NotSupportedErrorrX   r   r   ra   _NO_TRACEBACKwith_tracebackr/   rK   rL   rN   r   exs         r0   r   zServerCursor.execute  s+     	IGd6ll1oGGHHH<' 	%D  	* J J
 1 1% H HIIIJ J J J J J J J J J J J J J J 	* 	* 	*##D)))	* s<   	B 0BB BB BB C,CCT	returning
params_seqIterable[Params]r   c               *    t          j        d          )z/Method not implemented for server-side cursors.0executemany not supported on server-side cursorsr{   r   r/   rK   r   r   s       r0   executemanyzServerCursor.executemany*  s     !"TUUUr2   
Row | Nonec                    | j         j        5  | j                             |                     d                    }d d d            n# 1 swxY w Y   |r| xj        dz  c_        |d         S d S Nr   r   rX   r   r   r   rG   r/   recss     r0   fetchonezServerCursor.fetchone4  s    Z_ 	7 	7:??4??1#5#566D	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	IINII7N4   .AAAr   sizer   	list[Row]c                    |s| j         }| j        j        5  | j                            |                     |                    }d d d            n# 1 swxY w Y   | xj        t          |          z  c_        |S r   	arraysizerX   r   r   r   rG   lenr/   r   r   s      r0   	fetchmanyzServerCursor.fetchmany=  s     	">DZ_ 	: 	::??4??4#8#899D	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:		SYY		s   .AAAc                    | j         j        5  | j                             |                     d                     }d d d            n# 1 swxY w Y   | xj        t          |          z  c_        |S r   rX   r   r   r   rG   r   r   s     r0   fetchallzServerCursor.fetchallE  s    Z_ 	: 	::??4??4#8#899D	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:		SYY		r   Iterator[Row]c              #    K   	 | j         j        5  | j                             |                     | j                            }d d d            n# 1 swxY w Y   |D ]}| xj        dz  c_        |V  t          |          | j        k     rd S NTr   rX   r   r   r   r,   rG   r   r/   r   recs      r0   __iter__zServerCursor.__iter__K  s      	 G Gztt}'E'EFFG G G G G G G G G G G G G G G  		Q						4yy4=((	s   3AAAr   r   r   c                    | j         j        5  | j                             |                     ||                     d d d            n# 1 swxY w Y   |dk    r| xj        |z  c_        d S || _        d S )Nr   )rX   r   r   r   rG   r/   r   r   s      r0   scrollzServerCursor.scrollU  s    Z_ 	; 	;JOOD,,UD99:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; :IIIIIIDIIIs   /AAA)r   r   r    r!   r"   r#   r$   r%   )
r   r   r    r!   r   r   r"   r#   r$   r%   )
r   r   r    r!   r   r   r"   r#   r$   r%   r3   r   r   
rK   r   rL   rM   rN   r#   r   r   r3   r   rK   r   r   r   r   r%   r3   r   r3   r   r   r   r   r3   r   r3   r   )r3   r   r   r   r   r   r!   r3   r   )r   r   r   r   r	   r1   r   r   r   r   r   r   r   r   r   r   r   s   @r0   r   r      s       JI #'     X  #'     X /3"&E E E E E E   	 	 	 	 	 	 !%
 #     > V V V V V V                     r2   r   r   c                       e Zd ZdZdZedddd8d            Zedddd9d            Zddddd:dZd;dZd; fdZ	 d<ddd=d#Zd$d%d>d)Z	d?d+Z
d@dAd0ZdBd1ZdCd3ZdDdEd7Z xZS )FAsyncServerCursorr   r   NFr   r   AsyncConnection[Row]r    r!   r"   r#   r$   r%   c                   d S r   r   r   s        r0   r1   zAsyncServerCursor.__init__e  r   r2   AsyncConnection[Any]r   AsyncRowFactory[Row]c                   d S r   r   r   s         r0   r1   zAsyncServerCursor.__init__o  r   r2   r   AsyncRowFactory[Row] | Nonec               |    t          j        | ||p|j                   t                              | |||           d S r   )r   r1   r   r   r   s         r0   r1   zAsyncServerCursor.__init__z  sO     	*+*O9O	
 	
 	
 	
 	""4z8DDDDDr2   r3   r   c                J    | j         st          d|  dt                     d S d S r   r   r>   s    r0   r   zAsyncServerCursor.__del__  r   r2   c                x  K   | j         j        4 d {V  | j        r	 d d d           d {V  d S | j         j        s2| j                             |                                            d {V  t                                                       d {V  d d d           d {V  d S # 1 d {V swxY w Y   d S r   r   r   s    r0   r   zAsyncServerCursor.close  s     :? 	" 	" 	" 	" 	" 	" 	" 	"{ 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" :$ 9joodoo&7&7888888888''--//!!!!!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   	B)A$B))
B36B3r   rK   r   rL   rM   rN   r   r   r   c                 K   |r%t          dt          |          d                    | j        j        rt	          j        d          	 | 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 )Nr   r   r   r   r   s         r0   r   zAsyncServerCursor.execute  s       	IGd6ll1oGGHHH<' 	%D  	*z P P P P P P P Pjood&7&7vv&N&NOOOOOOOOOP P P P P P P P P P P P P P P P P P P P P P P P P P P 	* 	* 	*##D)))	* s<   B7 6B%B7 %
B//B7 2B/3B7 7C CC Tr   r   r   r   c               .   K   t          j        d          )Nr   r   r   s       r0   r   zAsyncServerCursor.executemany  s       !"TUUUr2   r   c                  K   | j         j        4 d {V  | j                             |                     d                     d {V }d d d           d {V  n# 1 d {V swxY w Y   |r| xj        dz  c_        |d         S d S r   r   r   s     r0   r   zAsyncServerCursor.fetchone  s     :? 	= 	= 	= 	= 	= 	= 	= 	=););<<<<<<<<D	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	IINII7N4   4A
A%(A%r   r   r   r   c                (  K   |s| j         }| j        j        4 d {V  | j                            |                     |                     d {V }d d d           d {V  n# 1 d {V swxY w Y   | xj        t          |          z  c_        |S r   r   r   s      r0   r   zAsyncServerCursor.fetchmany  s=      	">D:? 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@)>)>????????D	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@		SYY		s   4A$$
A.1A.c                  K   | j         j        4 d {V  | j                             |                     d                      d {V }d d d           d {V  n# 1 d {V swxY w Y   | xj        t          |          z  c_        |S r   r   r   s     r0   r   zAsyncServerCursor.fetchall  s.     :? 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@)>)>????????D	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@		SYY		r  AsyncIterator[Row]c               N  K   	 | j         j        4 d {V  | j                             |                     | j                             d {V }d d d           d {V  n# 1 d {V swxY w Y   |D ]}| xj        dz  c_        |W V  t          |          | j        k     rd S r   r   r   s      r0   	__aiter__zAsyncServerCursor.__aiter__  sb     	z M M M M M M M M!Z__T__T]-K-KLLLLLLLLM M M M M M M M M M M M M M M M M M M M M M M M M M M  		Q							4yy4=((	s   9A!!
A+.A+r   r   r   c                   K   | 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 r   )rX   r   r   r   r   s      r0   r   zAsyncServerCursor.scroll  s*     :? 	A 	A 	A 	A 	A 	A 	A 	A*//$"2"25$"?"?@@@@@@@@@	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	As   5A
A'*A')r   r   r    r!   r"   r#   r$   r%   )
r   r  r    r!   r   r  r"   r#   r$   r%   )
r   r  r    r!   r   r  r"   r#   r$   r%   r   r   r   r   r   r   r   r   )r3   r  r   r   )r   r   r   r   r	   r1   r   r   r   r   r   r   r   r  r   r   r   s   @r0   r   r   _  s        JI #'     X  #'     X 48"&E E E E E E   " " " " " " !%
 #     8 V V V V V V             A A A A A A A A Ar2   r   r  N)5r   
__future__r   typingr   r   r   r   r   r	   warningsr
   r   r   r   r   r{   abcr   r   r   r   rowsr   r   r   cursorr   _compatr   
generatorsr   _cursor_baser   cursor_asyncr   r   r   connection_asyncr   r+   Formatr-   r]   
ExecStatusrZ   rE   TransactionStatusrl   rm   r   r   r   r   r2   r0   <module>r      s    # " " " " " R R R R R R R R R R R R R R R R                         5 5 5 5 5 5 5 5 5 5 5 5 2 2 2 2 2 2 2 2 2 2                   $ $ $ $ $ $ % % % % % % 2&&&&&&111111 	y~		]%
M#		 


&l( l( l( l( l(
>3#67 l( l( l(^F F F F F$%6%;<fSk F F FR|A |A |A |A |A,c12K4D|A |A |A |A |Ar2   