
    *Jf;                        U d Z ddlmZ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 dd	lmZ e	rdd
lmZ eeeedf         f         Zeed<   ej        j        Zej        j        Z G d de          Z G d d          ZdS )z!
Support for prepared statements
    )IntEnumauto)IteratorOptionalSequenceTupleTYPE_CHECKING)OrderedDict)	TypeAlias   )pq)Deque)PostgresQuery)PGresult.Keyc                   J    e Zd Z e            Z e            Z e            ZdS )PrepareN)__name__
__module____qualname__r   NOYESSHOULD     U/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/psycopg/_preparing.pyr   r      s/        	B
$&&CTVVFFFr   r   c            
       X   e Zd ZU dZee         ed<   dZeed<   ddZe	de
defd	            Z	 dde
d
ee         deeef         fdZdeded         defdZe	ded         defd            ZddZde
dededee         fdZdedededed         ddf
dZdefdZdee         fdZdS )PrepareManager   prepare_thresholdd   prepared_maxreturnNc                     t                      | _        t                      | _        d| _        t	          t
                               | _        d S )Nr   )r
   _counts_names_prepared_idxr   bytes_maint_commandsselfs    r   __init__zPrepareManager.__init__&   s:    .9mm 0;}} $U|~~r   queryc                     | j         | j        fS N)r-   types)r-   s    r   keyzPrepareManager.key2   s    U[))r   preparec                    |du s| j         t          j        dfS |                     |          }| j                            |          }|rt          j        |fS | j                            |d          }|| j         k    s|r:d| j         	                                }| xj        dz  c_        t          j
        |fS t          j        dfS )zP
        Check if a query is prepared, tell back whether to prepare it.
        FNr   r   _pg3_r   )r    r   r   r1   r&   getr   r%   r'   encoder   )r+   r-   r2   r1   namecounts         r   r5   zPrepareManager.get6   s     et5=:s?"hhuoo{s## 	%;$$  a((D***g*/4-//6688D!#>4'' :s?"r   prepresultsr   c                     | j         s|t          j        k    rP|D ]M}|j        t          k    r|j        }|r1|                    d          s|dk    r|                                 c S NdS )zCheck if we need to discard our entire state: it should happen on
        rollback or on dropping objects, because the same object may get
        recreated and postgres would fail internal lookups.
        s   DROP s   ROLLBACKF)r&   r   r   status
COMMAND_OKcommand_status
startswithclear)r+   r9   r:   resultcmdstats        r   _should_discardzPrepareManager._should_discardP   s    
 ; 	($'.00! ( (=J.. / ( 2 28 < < (;@V@V::<<'''ur   c                     t          |           dk    rdS | d         j        }t          |cxk    rt          k    rn ndS dS )zCReturn False if 'results' are invalid for prepared statement cache.r   Fr   T)lenr<   r=   	TUPLES_OK)r:   r<   s     r   _check_resultszPrepareManager._check_results^   sR     w<<15",,,,9,,,,,5tr   c                 0   t          | j                  | j        k    r| j                            d           t          | j                  | j        k    r@| j                            d          d         }| j                            d|z              dS dS )zEvict an old value from the cache.

        If it was prepared, deallocate it. Do it only once: if the cache was
        resized, deallocate gradually.
        F)lastr   s   DEALLOCATE N)rE   r%   r"   popitemr&   r)   append)r+   r7   s     r   _rotatezPrepareManager._rotatel   s     t|t000L  e ,,,t{d///;&&E&2215D ''(=>>>>> 0/r   r7   c                    | j         dS |                     |          }|| j        v rR|t          j        u r| j        |= || j        |<   n/| j        |xx         dz  cc<   | j                            |           dS || j        v r| j                            |           dS |t          j        u r|| j        |<   n
d| j        |<   |S )zHandle 'query' for possible addition to the cache.

        If a new entry has been added, return its key. Return None otherwise
        (meaning the query is already in cache or cache is not enabled).
        Nr   )r    r1   r%   r   r   r&   move_to_end)r+   r-   r9   r7   r1   s        r   maybe_add_to_cachez!PrepareManager.maybe_add_to_cachey   s     !)4hhuoo$,w~%%L%#'C  S!!!Q&!!!((---4DKK##C(((4 w~%%#'C  $%S!Jr   r1   c                     |                      ||          rdS |                     |          s8| j                            |d           | j                            |d           dS |                                  dS )zValidate cached entry with 'key' by checking query 'results'.

        Possibly record a command to perform maintenance on database side.
        N)rC   rG   r&   popr%   rL   )r+   r1   r9   r7   r:   s        r   validatezPrepareManager.validate   s|     g.. 	F""7++ 	KOOC&&&LS$'''''LLNNNNNr   c                     | j                                          | j        rN| j                                         | j                                         | j                            d           dS dS )zClear the cache of the maintenance commands.

        Clear the internal state and prepare a command to clear the state of
        the server.
        s   DEALLOCATE ALLTF)r%   r@   r&   r)   rK   r*   s    r   r@   zPrepareManager.clear   sm     	; 	K &&((( ''(9:::45r   c              #   `   K   | j         r$| j                                         V  | j         "dS dS )zY
        Iterate over the commands needed to align the server state to our state
        N)r)   popleftr*   s    r   get_maintenance_commandsz'PrepareManager.get_maintenance_commands   sO       " 	1&..00000 " 	1 	1 	1 	1 	1r   )r#   Nr/   )r   r   r   r    r   int__annotations__r"   r,   staticmethodr   r   r1   boolr   r   r(   r5   r   rC   rG   rL   rO   rR   r@   r   rV   r   r   r   r   r      s        '(x}((( L#
. 
. 
. 
. *= *S * * * \* ?C# #"#-5d^#	w~	# # # #4G hz6J t      4     \? ? ? ?"*19>	#   B  	
 *% 
   (t    1(5/ 1 1 1 1 1 1r   r   N)__doc__enumr   r   typingr   r   r   r   r	   collectionsr
   typing_extensionsr    r   _compatr   _queriesr   pq.abcr   r(   rW   r   rX   
ExecStatusr=   rF   r   r   r   r   r   <module>re      sh             E E E E E E E E E E E E E E # # # # # # ' ' ' ' ' '             # # # # # # !      ueCHo-.Y . . .]%
M#	    g   c1 c1 c1 c1 c1 c1 c1 c1 c1 c1r   