
    (ph                    0   % S r SSKJr  SSKJrJr  SSKJrJrJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  \	(       a  SSKJr  SSKJr  SrS\S'   \R6                  R8                  r\R6                  R:                  r " S S\5      r " S S5      rg)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                  >    \ rS rSr\" 5       r\" 5       r\" 5       rSrg)Prepare    N)	__name__
__module____qualname____firstlineno__r   NOYESSHOULD__static_attributes__r       E/var/www/html/venv/lib/python3.13/site-packages/psycopg/_preparing.pyr   r      s    	B
&CVFr   r   c                      \ rS rSr% SrS\S'   SrS\S'   SS jr\SS	 j5       r	 S     SS jjr
SS jr\SS j5       rSS jr        SS jr          SS jrSS jrSS jrSrg
)PrepareManager"      z
int | Noneprepare_thresholdd   intprepared_maxc                t    [        5       U l        [        5       U l        SU l        [        S   " 5       U l        g )Nr   zbytes | None)r	   _counts_names_prepared_idxr   	_to_flushselfs    r    __init__PrepareManager.__init__)   s/    .9m 0;} ~.0r   c                2    U R                   U R                  4$ N)querytypes)r4   s    r    keyPrepareManager.key5   s    U[[))r   Nc                   USL d  U R                   c  [        R                  S4$ U R                  U5      nU R                  R                  U5      nU(       a  [        R                  U4$ U R                  R                  US5      nXPR                   :  d  U(       aD  SU R                   3R                  5       nU =R                  S-  sl        [        R                  U4$ [        R                  S4$ )z@
Check if a query is prepared, tell back whether to prepare it.
Fr   r   _pg3_r
   )r%   r   r   r6   r+   getr   r*   r,   encoder   )r/   r4   preparer6   namecounts         r    r:   PrepareManager.get9   s     et55=::s?"hhuo{{s#;;$$  a(***g4--./668D!#>>4'' ::s?"r   c                   U R                   (       d  U[        R                  :X  ab  U H\  nUR                  [        :w  a  M  UR
                  nU(       d  M.  UR                  S5      (       d  US:X  d  ML  U R                  5       s  $    g)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_discardPrepareManager._should_discardS   sg    
 ;;$'..0!==J. //7 2 28 < <;@V::<' " r   c                t    [        U 5      S:w  a  gU S   R                  n[        Us=:w  a  [        :w  a   g  gg)zCReturn False if 'results' are invalid for prepared statement cache.r
   Fr   T)lenrA   rB   	TUPLES_OK)rG   rA   s     r    _check_resultsPrepareManager._check_resultsa   s<     w<1"",9, - r   c                2   [        U R                  5      U R                  :  a  U R                  R                  SS9  [        U R                  5      U R                  :  a8  U R                  R                  SS9S   nU R
                  R                  U5        gg)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)rM   r*   r(   popitemr+   r-   append)r/   r=   s     r    _rotatePrepareManager._rotateo   sz     t||t000LL  e ,t{{d///;;&&E&215DNN!!$' 0r   c                   U R                   c  gU R                  U5      nX@R                  ;   ab  U[        R                  L a  U R                  U	 X0R
                  U'   gU R                  U==   S-  ss'   U R                  R                  U5        gX@R
                  ;   a  U R
                  R                  U5        gU[        R                  L a  X0R
                  U'   U$ SU R                  U'   U$ )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%   r6   r*   r   r   r+   move_to_end)r/   r4   rF   r=   r6   s        r    maybe_add_to_cache!PrepareManager.maybe_add_to_cache|   s     !!)hhuo,,w~~%LL%#'C   S!Q&!((-KKKK##C( w~~%#'C  J %&S!Jr   c                    U R                  X$5      (       a  gU R                  U5      (       d9  U R                  R                  US5        U R                  R                  US5        gU R                  5         g)zValidate cached entry with 'key' by checking query 'results'.

Possibly record a command to perform maintenance on database side.
N)rJ   rO   r+   popr*   rU   )r/   r6   rF   r=   rG   s        r    validatePrepareManager.validate   sX     ..""7++KKOOC&LLS$'LLNr   c                    U R                   R                  5         U R                  (       aP  U R                  R                  5         U R                  R                  5         U R                  R	                  S5        gg)zClear the cache of the maintenance commands.

Clear the internal state and prepare a command to clear the state of
the server.
NTF)r*   rE   r+   r-   rT   r.   s    r    rE   PrepareManager.clear   sS     	;;KKNN  "NN!!$'r   c              #     #    U R                   (       aG  U R                   R                  5       nUR                  U5       Sh  vN   U R                   (       a  MF  gg N7f)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/   connr=   s      r    maintain_genPrepareManager.maintain_gen   sB      nn>>))+D''--- nnn-s   A AAAA)r*   r+   r,   r-   )returnNone)r4   r   rg   r   r3   )r4   r   r<   zbool | Nonerg   ztuple[Prepare, bytes])rF   r   rG   Sequence[PGresult]rg   bool)rG   ri   rg   rj   )r4   r   rF   r   r=   bytesrg   z
Key | None)
r6   r   rF   r   r=   rk   rG   ri   rg   rh   )rg   rj   )rd   zBaseConnection[Any]rg   zPQGen[None])r   r   r   r   r%   __annotations__r(   r0   staticmethodr6   r:   rJ   rO   rU   rY   r]   rE   re   r   r   r   r    r"   r"   "   s    $%z% L#
1 * * <@#"#-8#	#4  ("*19>	B  	
 $ 
(	.r   r"   N) __doc__
__future__r   enumr   r   typingr   r   r   collectionsr	    r   abcr   _compatr   r   _queriesr   pq.abcr   _connection_baser   r   rl   
ExecStatusrB   rN   r   r"   r   r   r    <module>rz      sp    #  / / #   % # 00Y 0]]%%
MM##	g g. g.r   