
    i                    &   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j6                  j8                  Zej6                  j:                  Z G d de      Z G d d      Zy)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                  6    e Zd Z e       Z e       Z e       Zy)PrepareN)__name__
__module____qualname__r   NOYESSHOULD     r/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/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                r    t               | _        t               | _        d| _        t	        d          | _        y )Nr   zbytes | None)r	   _counts_names_prepared_idxr   	_to_flushselfs    r   __init__zPrepareManager.__init__)   s/    .9m 0;} ~.0r   c                2    | j                   | j                  fS N)querytypes)r/   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'   getr1   r   r&   r(   encoder   )r+   r/   preparer1   namecounts         r   r4   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)r@   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)rE   r&   r$   popitemr'   r)   append)r+   r7   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!   r1   r&   r   r   r'   move_to_end)r+   r/   r?   r7   r1   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)rC   rG   r'   popr&   rL   )r+   r1   r?   r7   r@   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)   rK   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+   connr7   s      r   maintain_genzPrepareManager.maintain_gen   sA      nn>>))+D''--- nn-s   ;AAAA)returnNone)r/   r   rY   r   r.   )r/   r   r6   zbool | NonerY   ztuple[Prepare, bytes])r?   r   r@   Sequence[PGresult]rY   bool)r@   r[   rY   r\   )r/   r   r?   r   r7   bytesrY   z
Key | None)
r1   r   r?   r   r7   r]   r@   r[   rY   rZ   )rY   r\   )rW   zBaseConnection[Any]rY   zPQGen[None])r   r   r   r!   __annotations__r$   r,   staticmethodr1   r4   rC   rG   rL   rO   rR   r>   rX   r   r   r   r   r   "   s    $%z% L#
1 * * <@#"#-8#	#0 
 
("*19>	@%-2=O	 	.r   r   N) __doc__
__future__r   enumr   r   typingr   r   r   collectionsr	    r   abcr   _compatr   r   _queriesr   pq.abcr   _connection_baser   r   r^   
ExecStatusr;   rF   r   r   r   r   r   <module>rl      sp    #  / / #   % # 00Y 0]]%%
MM##	g _. _.r   