
    R
h                       U d Z ddlmZ ddl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mZ dd
lmZ erddlmZ ddlmZ dZd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
    )annotations)IntEnumauto)TYPE_CHECKINGAnySequence)OrderedDict   )pq)PQGen)Deque	TypeAlias)PostgresQuery)PGresult)BaseConnectionztuple[bytes, tuple[int, ...]]r   Keyc                  J    e Zd Z e            Z e            Z e            ZdS )PrepareN)__name__
__module____qualname__r   NOYESSHOULD     ]/var/www/html/01_SiteInternet/03_Maps/venv/lib/python3.11/site-packages/psycopg/_preparing.pyr   r      s/        	B
$&&CTVVFFFr   r   c                      e Zd ZU dZded<   dZded<   d&d	Zed'd            Z	 d(d)dZ	d*dZ
ed+d            Zd&dZd,dZd-d Zd.d!Zd/d%ZdS )0PrepareManager   z
int | Noneprepare_thresholdd   intprepared_maxreturnNonec                    t                      | _        t                      | _        d| _        t	          d                     | _        d S )Nr   zbytes | None)r	   _counts_names_prepared_idxr   	_to_flushselfs    r   __init__zPrepareManager.__init__)   s:    .9mm 0;}} ~.00r   queryr   r   c                    | j         | j        fS N)r/   types)r/   s    r   keyzPrepareManager.key5   s    U[))r   Npreparebool | Nonetuple[Prepare, bytes]c                   |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   r3   r)   getr   r(   r*   encoder   )r-   r/   r4   r3   namecounts         r   r9   zPrepareManager.get9   s     et5=:s?"hhuoo{s## 	%;$$  a((D***g*/4-//6688D!#>4'' :s?"r   prepr   resultsSequence[PGresult]boolc                    | 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-   r=   r>   resultcmdstats        r   _should_discardzPrepareManager._should_discardS   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)lenrB   rC   	TUPLES_OK)r>   rB   s     r   _check_resultszPrepareManager._check_resultsa   sR     w<<15",,,,9,,,,,5tr   c                *   t          | j                  | j        k    r| j                            d           t          | j                  | j        k    r=| j                            d          d         }| j                            |           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
   N)rK   r(   r$   popitemr)   r+   append)r-   r;   s     r   _rotatezPrepareManager._rotateo   s     t|t000L  e ,,,t{d///;&&E&2215DN!!$''''' 0/r   r;   bytes
Key | Nonec                   | 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!   r3   r(   r   r   r)   move_to_end)r-   r/   r=   r;   r3   s        r   maybe_add_to_cachez!PrepareManager.maybe_add_to_cache|   s     !)4hhuoo$,w~%%L%#'C  S!!!Q&!!!((---4DKK##C(((4 w~%%#'C  $%S!Jr   r3   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)rI   rM   r)   popr(   rR   )r-   r3   r=   r;   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.
        NTF)r(   rF   r)   r+   rQ   r,   s    r   rF   zPrepareManager.clear   sj     	; 	KN  """N!!$'''45r   connBaseConnection[Any]PQGen[None]c              #     K   | j         r=| j                                         }|                    |          E d{V  | j         ;dS dS )z
        Generator to send the commands to perform periodic maintenance

        Deallocate unneeded command in the server, or flush the prepared
        statements server state entirely if necessary.
        N)r+   popleft_deallocate)r-   r\   r;   s      r   maintain_genzPrepareManager.maintain_gen   sk       n 	.>))++D''--------- n 	. 	. 	. 	. 	.r   )r%   r&   )r/   r   r%   r   r1   )r/   r   r4   r5   r%   r6   )r=   r   r>   r?   r%   r@   )r>   r?   r%   r@   )r/   r   r=   r   r;   rS   r%   rT   )
r3   r   r=   r   r;   rS   r>   r?   r%   r&   )r%   r@   )r\   r]   r%   r^   )r   r   r   r!   __annotations__r$   r.   staticmethodr3   r9   rI   rM   rR   rW   rZ   rF   rb   r   r   r   r   r   "   s'        $%%%%% L
1 
1 
1 
1 * * * \* <@# # # # #4       \( ( ( (   B   (   	. 	. 	. 	. 	. 	.r   r   N) __doc__
__future__r   enumr   r   typingr   r   r   collectionsr	    r   abcr   _compatr   r   _queriesr   pq.abcr   _connection_baser   r   rc   
ExecStatusrC   rL   r   r   r   r   r   <module>rq      su     # " " " " "         / / / / / / / / / / # # # # # #             % % % % % % % % # # # # # # 1      0000000 0 0 0 0]%
M#	    g   g. g. g. g. g. g. g. g. g. g.r   