o
    DDi                     @  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mZ ddlmZ ddlmZ dd	lmZ dd
lmZ erIddlmZ ddlmZ eeeedf f 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)TYPE_CHECKINGAny	TypeAlias)OrderedDictdeque)Sequence   )pq)PQGen)PostgresQuery)PGresult)BaseConnection.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   K/var/www/Datamplify/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td B   | _d S )Nr   )r   _counts_names_prepared_idxr	   bytes	_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$   getr.   r   r#   r%   encoder   )r)   r+   r/   r.   namecountr   r   r   r4   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)   r8   r9   resultcmdstatr   r   r   _should_discardQ   s   
zPrepareManager._should_discardc                 C  s6   t | dkrdS t| d j  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)r9   r   r   r   _check_results_   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)rD   r#   r    popitemr$   r'   append)r)   r6   r   r   r   _rotatel   s   zPrepareManager._rotater6   r&   
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+   r8   r6   r.   r   r   r   maybe_add_to_cachey   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)rC   rF   r$   popr#   rJ   )r)   r.   r8   r6   r9   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'   rI   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   r6   r   r   r   maintain_gen   s
   
zPrepareManager.maintain_gen)r!   r"   )r+   r   r!   r   r,   )r+   r   r/   r0   r!   r1   )r8   r   r9   r:   r!   r;   )r9   r:   r!   r;   )r+   r   r8   r   r6   r&   r!   rK   )
r.   r   r8   r   r6   r&   r9   r:   r!   r"   )r!   r;   )rP   rQ   r!   rR   )r   r   r   r   __annotations__r    r*   staticmethodr.   r4   rC   rF   rJ   rM   rO   r@   rU   r   r   r   r   r   "   s    
 




 
r   N)#__doc__
__future__r   enumr   r   typingr   r   r   collectionsr   r	   collections.abcr
    r   abcr   _queriesr   pq.abcr   _connection_baser   tupler&   r   r   rV   
ExecStatusr=   rE   r   r   r   r   r   r   <module>   s"    