
    љi                     B    S SK r S SKJr  S SKrSSKJr   " S S\5      rg)    N)Optional   )PoolProviderc                       \ rS rSrSr        SS\S\S\S\S\S	\S
\S\4S jjr\S\4S j5       r	S\
R                  4S jrSS jrS\4S jrSrg)PostgreSQLPoolProvider	   a  
Centralized PostgreSQL connection pool provider.

Use this to share a single connection pool across multiple components,
enabling better control over total database connections.

Example:
    pool_provider = PostgreSQLPoolProvider(
        connection_str="postgresql://user:pass@localhost:5432/db",
        min_size=5,
        max_size=30,
    )

    context_manager = PostgreSQLContextManager(get_pool=pool_provider.get_pool)
    session_manager = PostgreSQLSessionStateManager(get_pool=pool_provider.get_pool)
Nconnection_strhostportdbnameuserpasswordmin_sizemax_sizec	                     Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        S U l        [        R                  " 5       U l        g )N)r	   r
   r   r   r   r   r   r   _poolasyncioLock
_pool_lock)	selfr	   r
   r   r   r   r   r   r   s	            T/home/james-whalen/.local/lib/python3.13/site-packages/aiavatar/database/postgres.py__init__PostgreSQLPoolProvider.__init__   sA     -			   -1
!,,.    returnc                     g)N
postgresql r   s    r   db_typePostgreSQLPoolProvider.db_type2   s    r   c                   #    U R                   b  U R                   $ U R                   ISh  vN   U R                   b  U R                   sSSS5      ISh  vN   $ U R                  (       aB  [        R                  " U R                  U R
                  U R                  S9I Sh  vN U l         Om[        R                  " U R                  U R                  U R                  U R                  U R                  U R
                  U R                  S9I Sh  vN U l         U R                   sSSS5      ISh  vN   $  GN N N N- N! , ISh  vN  (       d  f       g= f7f)z
Get the shared connection pool, creating it on first access.

This method is thread-safe and will only create one pool
even if called concurrently from multiple coroutines.
N)dsnr   r   )r
   r   databaser   r   r   r   )r   r   r	   asyncpgcreate_poolr   r   r
   r   r   r   r   r   s    r   get_poolPostgreSQLPoolProvider.get_pool6   s      ::!::???zz%zz #?? ""#*#6#6++!]]!]]$ 
 $+#6#6![[!]]!]]!]]$ 
 ::+ #??
 #???s   *ED8EE	ED;EAE#D=$A-ED?E&E2E3E;E=E?EEE	E
EEc                 ~   #    U R                   b*  U R                   R                  5       I Sh  vN   SU l         gg N7f)z'Close the connection pool if it exists.N)r   closer   s    r   r*   PostgreSQLPoolProvider.closeW   s4     ::!**""$$$DJ "$s   +=;=c                 \   U R                   c  SS0$ SU R                   R                  5       U R                   R                  5       U R                   R                  5       U R                   R                  5       -
  U R                   R                  5       U R                   R	                  5       S.$ )ap  
Get current pool statistics.

Returns:
    dict: Pool statistics including:
        - initialized: Whether the pool has been created
        - size: Total connections in the pool
        - idle: Idle (available) connections
        - in_use: Currently used connections
        - min_size: Configured minimum pool size
        - max_size: Configured maximum pool size
initializedFT)r-   sizeidlein_user   r   )r   get_sizeget_idle_sizeget_min_sizeget_max_sizer   s    r   	get_stats PostgreSQLPoolProvider.get_stats]   s     ::!5))  JJ'')JJ,,.jj))+djj.F.F.HH

//1

//1
 	
r   )
r   r   r	   r   r
   r   r   r   r   r   )N	localhosti8  NNN      )r   N)__name__
__module____qualname____firstlineno____doc__strintr   propertyr    r%   Poolr'   r*   dictr5   __static_attributes__r   r   r   r   r   	   s    & #)) ) 	)
 ) ) ) ) ).    B
4 
r   r   )r   typingr   r%   baser   r   r   r   r   <module>rG      s       k
\ k
r   