
    xIf-                     J    d dl Z d dlZd dlZd dlmZ dZ G d de          ZdS )    N)STATUS@   c                       e Zd ZdZd Zd Zd ZddZd Zd Z	d	 Z
d
 Zed             Zed             Zed             Zed             Zed             Zej        d             ZdS )ConnectionPoolai  
    Class defining a pool of database connections

    MariaDB Connector/Python supports simple connection pooling.
    A connection pool holds a number of open connections and handles
    thread safety when providing connections to threads.

    The size of a connection pool is configurable at creation time,
    but cannot be changed afterwards. The maximum size of a connection
    pool is limited to 64 connections.

    Keyword Arguments:

        * pool_name (str) -- Name of connection pool

        * pool_size (int)=5 -- Size of pool. If not specified default value
          of 5 will be used. Maximum allowed number is 64.

        * pool_reset_connection (bool)=True -- Will reset the connection before
          returning it to the pool.  Default value is True.

        * pool_validation_interval (int)=500 -- Specifies the validation
          interval in milliseconds after which the status of a connection
          requested from the pool is checked.
          The default values is 500 milliseconds, a value of 0 means that
          the status will always be checked.
          (Added in version 1.1.6)
    c           	      T   g | _         g | _        i | _        i | _        t	          j                    | _        d| _        g d}|r6d|v r2|d         t          j	        v rt          j
        d|d         z            nt          j
        d          |                    d          | j        d<   t          |                    dd                    | j        d	<   t          |                    d
d                    | j        d<   t          |                    dd                    | j        d<   d| j        d	         cxk     rt          k    sn t          j
        dt          z            |                                | _        |D ]}|| j        v r| j        |= t!          | j                  dk    r| j        5  t#          d| j        d	                   D ]}	 t          j        di | j        }nw# t          j        $ re t#          dt!          | j                             D ]@}	 | j         |                                          n# t          j        $ r Y nw xY w| j         |= A w xY w|                     |           	 ddd           n# 1 swxY w Y   | t          j	        | j        d         <   dS )a  
        Creates a connection pool class

        :param str pool_name:
            Name of connection pool

        :param int pool_size:
            Size of pool. If not specified default value of 5 will be used.
            Maximum allowed number is 64.

        :param bool pool_reset_connection:
            Will reset the connection before returning it to the pool.
            Default value is True.
        r   )	pool_name	pool_sizepool_reset_connectionpool_validation_intervalr   zPool '%s' already existszNo pool name specifiednamer	      sizer
   Treset_connectionr   i  validation_intervalz&Pool size must be in range of 1 and %sN )_connections_free_connections_used
_pool_args
_conn_args_threadRLock
_lock_pool_ConnectionPool__closedmariadb_CONNECTION_POOLSProgrammingErrorgetintboolMAX_POOL_SIZEcopylenrange
ConnectionErrorcloseadd_connection)selfargskwargs	key_wordskeyi
connectionjs           J/var/www/piapp/venv/lib/python3.11/site-packages/mariadb/connectionpool.py__init__zConnectionPool.__init__;   sN    "$!#!-//1 1 1	  	EkV++ k"g&???./I171D0E F F F @ *+CDDD #)**["9"9"%fjja&@&@"A"A3T::;; 	*+ 

5s;;<< 	-. DOF+<<<<}<<<<* ,68E,F G G G !++-- 	) 	)Cdo%%OC(t!## 4 4q$/&"9:: 4 4A%,%7%J%J$/%J%J

"=    "'q#d.D*E*E!F!F : :A% $ 6q 9 ? ? A A A A#*= % % % !%	%
 !% 6q 9 9 ''
3333!44 4 4 4 4 4 4 4 4 4 4 4 4 4 4* >B!$/&"9:::sN   3JG)(J)3IH=<I=IIIIJJ
Jc                    |rr|| j         v r#| j                             |          }| j         |= n+|| j        v r"| j                            |          }| j        |= d|_        |                                 |                                 S )zI
        Removes the given connection and adds a new connection.
        N)r   indexr   _Connection__poolr&   r'   r(   r.   xs      r0   _replace_connectionz"ConnectionPool._replace_connection   s    
  		T333*00<<*1--t555*00<<*1-+/J(""$$$    c                     | j         r dt          t          |                     dS d| j        dt          t          |                     dS )Nz:<mariadb.connectionPool.ConnectionPool object (closed) at >z4<mariadb.connectionPool.ConnectionPool object (name=z) at )r   hexidr   r(   s    r0   __repr__zConnectionPool.__repr__   sZ    M 	> 	>"2d88}}}}/ /	>
  $~~~s2d88}}}}> >r8   Nc                    | j         s"t          j        d| j        d         z            |3t	          |t          j        j                  st          j        d          |:t          | j                   dk    r"t          j        d| j        d         z            t          | j	        | j
        z             }|| j        d         k    r&t          j        d| j        d         d	|d
          | j        5  |t          j        di | j         }| |_        t          j                    |_        | j	                            |           |cddd           S # 1 swxY w Y   dS )u   
        Adds a connection object to the connection pool.

        In case that the pool doesn’t have a free slot or is not configured
        a PoolError exception will be raised.
        z&Couldn't get configuration for pool %sr   Nz+Passed parameter is not a connection objectr   z#Can't get configuration for pool %sr   zCan't add connection to pool z: No free slot available (z).r   )r   r   	PoolErrorr   
isinstanceconnectionsr$   r   r"   r   r   r   r4   timeperf_counter_ns_ConnectionPool__last_usedappend)r(   r.   totals      r0   r'   zConnectionPool.add_connection   s     	=#$L$(OF$;%< = = = "z7+>+IJJ #* ,? @ @ @ #do"6"6!";";#$I$(OF$;%< = = = D*T-CCDDDOF+++##%)_V%<%<%<%*UU%, - - -
 _ 	 	!$/BB$/BB
+/J(%)%9%;%;J""))*555	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   5AEEEc                 n   d}| j         5  t          dt          | j                            D ]}| j        |         }t	          j                    |j        z
  dz  }|| j        d         k    rD	 |                                 n.# t          j
        $ r |                     |          }|sY |Y nw xY w|xj        dz  c_        | j                            |           | j                            |          }| j        |= |c cddd           S 	 ddd           n# 1 swxY w Y   t          j        d          )z
        Returns a connection from the connection pool or raises a PoolError
        exception if a connection is not available.
        Nr   i@B r      zNo connection available)r   r#   r"   r   rC   rD   rE   r   pingr   r%   r7   _usedr   rF   r3   r@   )r(   connr-   dtidxs        r0   get_connectionzConnectionPool.get_connection   s    _ 	 	1c$"899::  -a0*,,t/??7J(=>>>%		"= % % %#77==# %$H% %%
 

a

&--d333,22488*3/!	 	 	 	 	 	 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	$  9:::s7   A!D,B D&B,'D+B,,ADDDc                    | j         5  	 | j        d         r|                                 n(|j        t          j        z  r|                                 n*# t          j        $ r | 	                    |           Y nw xY w|r]|| j
        v rT| j
                            |          }| j
        |= t          j                    |_        | j                            |           ddd           dS # 1 swxY w Y   dS )z_
        Returns connection to the pool. Internally used
        by connection object.
        r   N)r   r   resetserver_statusr   IN_TRANSrollbackr   r%   r7   r   r3   rC   rD   rE   r   rF   r5   s      r0   _close_connectionz ConnectionPool._close_connection   sW   
 _ 	> 	>5?#56 *$$&&&&-? *'')))= 5 5 5((444445  >!777.44Z@@A.q1-1-A-C-CJ**11*===	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>s6   C+A
AC+$A<9C+;A<<A"C++C/2C/c                     || _         dS )u  
        Sets the connection configuration for the connection pool.
        For valid connection arguments check the mariadb.connect() method.

        Note: This method doesn't create connections in the pool.
        To fill the pool one has to use add_connection() ḿethod.
        N)r   )r(   r*   s     r0   
set_configzConnectionPool.set_config   s     !r8   c                    	 | j         | j        z   D ]}d|_        |                                 	 d| _         d| _        t          j        | j        d         = dS # d| _         d| _        t          j        | j        d         = w xY w)z+Closes connection pool and all connections.Nr   )r   r   r4   r&   r   r   r   )r(   cs     r0   r&   zConnectionPool.close  s    	C,t/EE  &*#				 &*D"%)D")$/&*ABBB &*D"%)D")$/&*ABBBBBs   -A (B c                     | j         d         S )z(Returns the name of the connection pool.r   r   r=   s    r0   r   zConnectionPool.pool_name       v&&r8   c                     | j         d         S )z(Returns the size of the connection pool.r   r[   r=   s    r0   r	   zConnectionPool.pool_size  r\   r8   c                     t           S )z.Returns the maximum size for connection pools.)r    r=   s    r0   max_sizezConnectionPool.max_size  s
     r8   c                 ^    	 t          | j        | j        z             S # t          $ r Y dS w xY w)z5Returns the number of connections in connection pool.r   )r"   r   r   	Exceptionr=   s    r0   connection_countzConnectionPool.connection_count!  sB    	t-0FFGGG 	 	 	11	s    
,,c                     | j         d         S )z
        If set to true, the connection will be reset on both client and server
        side after .close() method was called
        r   r[   r=   s    r0   r
   z$ConnectionPool.pool_reset_connection*  s     122r8   c                     || j         d<   d S )Nr   r[   )r(   rQ   s     r0   r
   z$ConnectionPool.pool_reset_connection2  s    .3*+++r8   )N)__name__
__module____qualname____doc__r1   r7   r>   r'   rO   rU   rW   r&   propertyr   r	   r_   rb   r
   setterr   r8   r0   r   r      sL        :LB LB LB\% % %"> > ># # # #J; ; ;8> > >,	! 	! 	!	C 	C 	C ' ' X'
 ' ' X'
   X
   X 3 3 X3 !4 4 "!4 4 4r8   r   )r   r   rC   mariadb.constantsr   r    objectr   r   r8   r0   <module>rm      sw   (    $ $ $ $ $ $W4 W4 W4 W4 W4V W4 W4 W4 W4 W4r8   