o
    "4g                     @  s   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	rId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 deZG dd dZdS )z!
Support for prepared statements
    )annotations)IntEnumauto)AnySequenceTYPE_CHECKING)OrderedDict   )pq)PQGen)Deque	TypeAlias)PostgresQuery)PGresult)BaseConnectionztuple[bytes, tuple[int, ...]]r   Keyc                   @  s   e Zd Ze Ze Ze ZdS )PrepareN)__name__
__module____qualname__r   NOYESSHOULD r   r   [/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/_preparing.pyr      s    
r   c                   @  s   e Zd ZU dZded< dZded< d0d	d
Zed1ddZ	d2d3ddZ	d4ddZ
ed5ddZd0dd Zd6d$d%Zd7d'd(Zd8d)d*Zd9d.d/ZdS ):PrepareManager   z
int | Noneprepare_thresholdd   intprepared_maxreturnNonec                 C  s&   t  | _t  | _d| _td  | _d S )Nr   zbytes | None)r   _counts_names_prepared_idxr   	_to_flushselfr   r   r   __init__)   s   zPrepareManager.__init__queryr   r   c                 C  s   | j | jfS N)r*   types)r*   r   r   r   key5   s   zPrepareManager.keyNpreparebool | Nonetuple[Prepare, bytes]c                 C  s   |du s	| j du rtjdfS | |}| j|}|r tj|fS | j|d}|| j ks.|rBd| j 	 }|  jd7  _tj
|fS tjdfS )zP
        Check if a query is prepared, tell back whether to prepare it.
        FN    r   _pg3_r	   )r   r   r   r-   r$   getr   r#   r%   encoder   )r(   r*   r.   r-   namecountr   r   r   r3   9   s   




zPrepareManager.getprepr   resultsSequence[PGresult]boolc                 C  sR   | j s|tjkr'|D ]}|jtkrq
|j}|r&|ds |dkr&|    S q
d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(   r7   r8   resultcmdstatr   r   r   _should_discardS   s   
zPrepareManager._should_discardc                 C  s:   t | dkrdS | d j}t|  krtkrdS  dS dS )zCReturn False if 'results' are invalid for prepared statement cache.r	   Fr   T)lenr;   r<   	TUPLES_OK)r8   r;   r   r   r   _check_resultsa   s   
zPrepareManager._check_resultsc                 C  sT   t | j| jkr| jjdd t | j| jkr(| jjd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)rC   r#   r    popitemr$   r&   append)r(   r5   r   r   r   _rotateo   s   zPrepareManager._rotater5   bytes
Key | Nonec                 C  s   | j du rdS | |}|| jv r2|tju r!| j|= || j|< dS | j|  d7  < | j| dS || jv r?| j| dS |tju rK|| j|< |S 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   r-   r#   r   r   r$   move_to_end)r(   r*   r7   r5   r-   r   r   r   maybe_add_to_cache|   s&   
	







z!PrepareManager.maybe_add_to_cacher-   c                 C  sF   |  ||rdS | |s| 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)rB   rE   r$   popr#   rI   )r(   r-   r7   r5   r8   r   r   r   validate   s   
zPrepareManager.validatec                 C  s8   | j   | jr| 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#   r?   r$   r&   rH   r'   r   r   r   r?      s   


zPrepareManager.clearconnBaseConnection[Any]PQGen[None]c                 c  s0    | j r| j  }||E dH  | j s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(   rP   r5   r   r   r   maintain_gen   s
   
zPrepareManager.maintain_gen)r!   r"   )r*   r   r!   r   r+   )r*   r   r.   r/   r!   r0   )r7   r   r8   r9   r!   r:   )r8   r9   r!   r:   )r*   r   r7   r   r5   rJ   r!   rK   )
r-   r   r7   r   r5   rJ   r8   r9   r!   r"   )r!   r:   )rP   rQ   r!   rR   )r   r   r   r   __annotations__r    r)   staticmethodr-   r3   rB   rE   rI   rM   rO   r?   rU   r   r   r   r   r   "   s    
 




!
r   N) __doc__
__future__r   enumr   r   typingr   r   r   collectionsr    r
   abcr   _compatr   r   _queriesr   pq.abcr   _connection_baser   r   rV   
ExecStatusr<   rD   r   r   r   r   r   r   <module>   s"    