
    2i                    :   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rd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e      Z! G d d      Z"y)z!
Support for prepared statements
    )annotations)IntEnumauto)TYPE_CHECKINGAny	TypeAlias)OrderedDictdeque)Sequence   )pq)PQGen)PostgresQuery)PGresult)BaseConnection.r   Keyc                  6    e Zd Z e       Z e       Z e       Zy)PrepareN)__name__
__module____qualname__r   NOYESSHOULD     Q/var/www/html/secretsanta/venv/lib/python3.12/site-packages/psycopg/_preparing.pyr   r      s    	B
&CVFr   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y	)PrepareManager   z
int | Noneprepare_thresholdd   intprepared_maxc                    t               | _        t               | _        d| _        t	        t
        d z            | _        y )Nr   )r	   _counts_names_prepared_idxr
   bytes	_to_flushselfs    r   __init__zPrepareManager.__init__)   s3    .9m 0;} ut|,.r   c                2    | j                   | j                  fS N)querytypes)r0   s    r   keyzPrepareManager.key5   s    U[[))r   Nc                   |du s| j                   t        j                  dfS | j                  j	                  | j                  |      x}      x}rt        j                  |fS | j                  j	                  |d      }|| j                   k\  s|rDd| j                   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.
        Fr   r   _pg3_r   )r!   r   r   r'   getr2   r   r&   r(   encoder   )r,   r0   preparer2   namecounts         r   r5   zPrepareManager.get9   s     et55=::s?";;??$((5/#93::4:;;$$  a(D***g4--./668D!#>>4'' ::s?"r   c                    | j                   s|t        j                  k(  rR|D ]M  }|j                  t        k7  r|j
                  }|s&|j                  d      s|dk(  s=| j                         c S  y)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,   prepresultsresultcmdstats        r   _should_discardzPrepareManager._should_discardQ   sg    
 ;;$'..0! (==J. // 2 28 <;@V::<'( r   c                h    t        |       dk7  ryt        | d   j                  cxk7  r	t        k7  ry yy)zCReturn False if 'results' are invalid for prepared statement cache.r   Fr   T)lenr<   r;   	TUPLES_OK)rA   s    r   _check_resultszPrepareManager._check_results_   s7     w<1**7i7 8 r   c                :   t        | j                        | j                  kD  r| j                  j                  d       t        | j                        | j                  kD  r;| j                  j                  d      d   }| j
                  j                  |       yy)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)rF   r&   r$   popitemr'   r*   append)r,   r8   s     r   _rotatezPrepareManager._rotatel   sz     t||t000LL  e ,t{{d///;;&&E&215DNN!!$' 0r   c                   | j                   y| j                  |      x}| j                  v rb|t        j                  u r| j                  |= || j
                  |<   y| j                  |xx   dz  cc<   | j                  j                  |       y|| j
                  v r| j
                  j                  |       y|t        j                  u r|| 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!   r2   r&   r   r   r'   move_to_end)r,   r0   r@   r8   r2   s        r   maybe_add_to_cachez!PrepareManager.maybe_add_to_cachey   s     !!)88E?"Ct||3w~~%LL%#'C   S!Q&!((-DKKKK##C( w~~%#'C  J %&S!Jr   c                    | j                  ||      ry| j                  |      s9| j                  j                  |d       | j                  j                  |d       y| j                          y)zValidate cached entry with 'key' by checking query 'results'.

        Possibly record a command to perform maintenance on database side.
        N)rD   rH   r'   popr&   rM   )r,   r2   r@   r8   rA   s        r   validatezPrepareManager.validate   sT     g.""7+KKOOC&LLS$'LLNr   c                    | j                   j                          | j                  rP| j                  j                          | j                  j                          | j                  j	                  d       yy)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*   rL   r+   s    r   r?   zPrepareManager.clear   sS     	;;KKNN  "NN!!$'r   c              #     K   | j                   rA| j                   j                         }|j                  |      E d{    | j                   r@yy7 w)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,   connr8   s      r   maintain_genzPrepareManager.maintain_gen   sA      nn>>))+D''--- nn-s   ;AAAA)returnNone)r0   r   rZ   r   r/   )r0   r   r7   zbool | NonerZ   ztuple[Prepare, bytes])r@   r   rA   Sequence[PGresult]rZ   bool)rA   r\   rZ   r]   )r0   r   r@   r   r8   r)   rZ   z
Key | None)
r2   r   r@   r   r8   r)   rA   r\   rZ   r[   )rZ   r]   )rX   zBaseConnection[Any]rZ   zPQGen[None])r   r   r   r!   __annotations__r$   r-   staticmethodr2   r5   rD   rH   rM   rP   rS   r?   rY   r   r   r   r   r   "   s    $%z% L#
/ * * <@#"#-8#	#0 
 
("*19>	@%-2=O	 	.r   r   N)#__doc__
__future__r   enumr   r   typingr   r   r   collectionsr	   r
   collections.abcr    r   abcr   _queriesr   pq.abcr   _connection_baser   tupler)   r#   r   r^   
ExecStatusr<   rG   r   r   r   r   r   <module>rm      s    #  0 0 * $   # 0ueCHo-.Y .]]%%
MM##	g _. _.r   