
    2i                     P   d dl Z d dlZd dlZd dlZd dlZd dlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
  ej                  e      Z G d de      Z G d	 d
ej                   ed      Z G d d      Z G d d      Z G d d      Zddddddddej*                  e
j,                  d	dZy)    N   )compat
connection)
exceptions)protocolc                   4     e Zd Zdd fd
Zed        Z xZS )PoolConnectionProxyMetaF)wrapc                   |rt        t        j                        D ]t  }|j                  d      s||v rt	        t        j                  |      }t        j                  |      sI| j                  |      }t        j                  ||      }|||<   v d|vrt        j                  j                  |d<   t        | 1  | |||      S )N___doc__)dirr   
Connection
startswithgetattrinspect
isfunction_wrap_connection_method	functoolsupdate_wrapperr   super__new__)	mclsnamebasesdctr   attrnamemethwrapper	__class__s	           T/var/www/html/marco-python-backend/venv/lib/python3.12/site-packages/asyncpg/pool.pyr   zPoolConnectionProxyMeta.__new__   s    
 5 56 
(&&s+x3z44h?))$/66x@#227DA 'H
( #!+!6!6!>!>IwtT5#66    c                       fd}|S )Nc                     | j                   $t        j                  dj                              t	        | j                   j
                        } || j                   g|i |S )NzJcannot call Connection.{}(): connection has been released back to the pool)_conr   InterfaceErrorformatr   r!   )selfargskwargsr   	meth_names       r"   call_con_methodzHPoolConnectionProxyMeta._wrap_connection_method.<locals>.call_con_method/   se    yy  //DDJF!E#$ $
 499..	:D		3D3F33r#    )r,   r-   s   ` r"   r   z/PoolConnectionProxyMeta._wrap_connection_method-   s    		4 r#   )__name__
__module____qualname__r   staticmethodr   __classcell__)r!   s   @r"   r
   r
      s    05 7&  r#   r
   c                   `    e Zd ZdZdddej
                  fdZd Zdej
                  fdZd	 Z	y
)PoolConnectionProxy)r&   _holderholderPoolConnectionHolderconc                 B    || _         || _        |j                  |        y N)r&   r6   
_set_proxy)r)   r7   r9   s      r"   __init__zPoolConnectionProxy.__init__C   s    	tr#   c                 .    t        | j                  |      S r;   )r   r&   )r)   attrs     r"   __getattr__zPoolConnectionProxy.__getattr__I   s    tyy$''r#   returnc                 j    | j                   y | j                   d c}| _         |j                  d        |S r;   )r&   r<   )r)   r9   s     r"   _detachzPoolConnectionProxy._detachM   s1    99DTYt
r#   c                     | j                   0dj                  | j                  j                  t	        |             S dj                  | j                  j                  | j                   t	        |             S )Nz <{classname} [released] {id:#x}>)	classnameidz<{classname} {con!r} {id:#x}>)rE   r9   rF   )r&   r(   r!   r/   rF   r)   s    r"   __repr__zPoolConnectionProxy.__repr__U   sn    995<<..11bh = @ @ 399..11tyyRX : O Or#   N)
r/   r0   r1   	__slots__r   r   r=   r@   rC   rH   r.   r#   r"   r5   r5   =   sA     $I5  ++(.. Or#   r5   T)	metaclassr   c                   j    e Zd ZdZd Zd Zd Zd ZdefdZ	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)r8   )r&   _pool_loop_proxy_max_queries_setup_max_inactive_time_in_use_inactive_callback_timeout_generationc                    || _         d | _        d | _        || _        || _        || _        d | _        d | _        d | _        d | _	        y r;   )
rL   r&   rN   rO   rQ   rP   rS   rR   rT   rU   )r)   poolmax_queriessetupmax_inactive_times        r"   r=   zPoolConnectionHolder.__init__f   sL    
	'"3"&r#   c                 X    | j                   d uxr | j                   j                          S r;   )r&   	is_closedrG   s    r"   is_connectedz!PoolConnectionHolder.is_connectedt   s&    yy$BTYY-@-@-B)BBr#   c                     | j                    S r;   rR   rG   s    r"   is_idlezPoolConnectionHolder.is_idlew   s    <<r#   c                   K   | j                   t        j                  d      | j                  j	                          d {   | _         | j                  j
                  | _        | j                          | j                          y 7 Ew)NzMPoolConnectionHolder.connect() called while another connection already exists)r&   r   InternalClientErrorrL   _get_new_connectionrU   _maybe_cancel_inactive_callback_setup_inactive_callbackrG   s    r"   connectzPoolConnectionHolder.connectz   sp     99 00,- - **88::	::11,,.%%' ;s   ?B	BAB	rA   c                 P  K   | j                   | j                   j                         r d | _         | j                          d {    n| j                  | j                  j                  k7  rh| j                  j
                  j                  | j                   j                  | j                               d | _         | j                          d {    | j                          t        | | j                         x| _        }| j                  	 | j                  |       d {    | j                  j
                  j!                         | _        |S 7 7 7 4# t        t        j                  f$ r1}	 | j                   j                          d {  7   |# |xY wd }~ww xY ww)Ntimeout)r&   r\   rf   rU   rL   rM   create_taskcloserT   rd   r5   rN   rP   	ExceptionasyncioCancelledErrorcreate_futurerR   )r)   proxyexs      r"   acquirezPoolConnectionHolder.acquire   sG    99		 3 3 5DI,,.  !7!77JJ((		68DI,,.  ,,.1$		BBe;;"kk%((( zz''557A ! ! )w556 

 ))//+++H"Hsy   AF&EBF&E=F&E "E#E ',F&F&E F#3FFFFFFF##F&c                   K   | j                   t        j                  d      | j                  j	                         ry d | _        | j                  j                  j                  | j                  k\  r%| j                  j                  |       d {    y | j                  | j                  j                  k7  r%| j                  j                  |       d {    y 	 |}| j                  j                  j                         rpt        j                         }t        j                   | j                  j                  j#                         |       d {    ||t        j                         |z
  z  }| j                  j%                  |       d {    | j/                          | j1                          y 7 *7 7 j7 ,# t&        t(        j*                  f$ r(}	 | j                  j-                          |# |xY wd }~ww xY ww)NzAPoolConnectionHolder.release() called on a free connection holderrh   )rR   r   rb   r&   r\   rT   	_protocolqueries_countrO   rk   rU   rL   _is_cancellingtime	monotonicr   wait_for_wait_for_cancellationresetrl   rm   rn   	terminate_releasere   )r)   ri   budgetstartedrq   s        r"   releasezPoolConnectionHolder.release   s    <<00+, , 99  99,,0A0AA ))//'/222tzz555 ))//'/222	Fyy""113 ..*ooII''>>@   %dnn.88F))//&/111 	 	%%'Q 3 3 27112 		 		##%b		s   BH	F<AH	F?H	!A5G G?G GG "H	?H	G G HG;9H;G>>HHH	c                 R   K   | j                   y | j                    d {    y 7 wr;   r_   rG   s    r"   wait_until_releasedz(PoolConnectionHolder.wait_until_released   s      <<,,s   '%'c                 n   K   | j                   #| j                   j                          d {    y y 7 wr;   )r&   rk   rG   s    r"   rk   zPoolConnectionHolder.close   s0     99  ))//### ! $s   *535c                 R    | j                   | j                   j                          y y r;   )r&   r|   rG   s    r"   r|   zPoolConnectionHolder.terminate   s#    99  II! !r#   c                     | j                   t        j                  d      | j                  r@| j                  j
                  j                  | j                  | j                        | _         y y )Nz/pool connection inactivity timer already exists)rS   r   rb   rQ   rL   rM   
call_later_deactivate_inactive_connectionrG   s    r"   re   z-PoolConnectionHolder._setup_inactive_callback   se    "".00AC C ""&*jj&6&6&A&A'')M)M'OD# #r#   c                 `    | j                   "| j                   j                          d | _         y y r;   )rS   cancelrG   s    r"   rd   z4PoolConnectionHolder._maybe_cancel_inactive_callback   s-    "".##**,&*D# /r#   c                     | j                   t        j                  d      | j                  +| j                  j	                          | j                          y y )Nz/attempting to deactivate an acquired connection)rR   r   rb   r&   r|   _release_on_closerG   s    r"   r   z4PoolConnectionHolder._deactivate_inactive_connection  sU    <<#00AC C 99  II! ""$ !r#   c                 R    | j                          | j                          d | _        y r;   )rd   r}   r&   rG   s    r"   r   z&PoolConnectionHolder._release_on_close  s    ,,.	r#   c                 :   | j                   y| j                   j                         s| j                   j                  d       d| _         | j                  !| j                  j	                          d| _        | j
                  j                  j                  |        y)zRelease this connection holder.N)rR   done
set_resultrN   rC   rL   _queue
put_nowaitrG   s    r"   r}   zPoolConnectionHolder._release  su    <<||  "LL##D) ;;"KK!DK 	

$$T*r#   N)r/   r0   r1   rI   r=   r]   r`   rf   r5   rr   r   r   rk   r|   re   rd   r   r   r}   r.   r#   r"   r8   r8   ^   s[     I C 	(#2 #J8(t$"O+
%
+r#   r8   c                      e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd0dZd ZdddededefdZdddedefdZddddefdZddddZddddZddddddddddddddZddddddddddd
dZdddddddddddddddddd Zddddd!d"Zddd#Zd$ Zddd%Zd& Zd' Zd( Z d) Z!d* Z"d+ Z#d, Z$d- Z%d. Z&d/ Z'y)1Poola  A connection pool.

    Connection pool can be used to manage a set of connections to the database.
    Connections are first acquired from the pool, then used, and then released
    back to the pool.  Once a connection is released, it's reset to close all
    open cursors and other resources *except* prepared statements.

    Pools are created by calling :func:`~asyncpg.pool.create_pool`.
    )r   rM   _minsize_maxsize_init_connect_args_connect_kwargs_holders_initialized_initializing_closing_closed_connection_class_record_classrU   rP   rO   !_max_inactive_connection_lifetimec       	            t        |
      dkD  rt        j                  dt        d       |t	        j
                         }|| _        |dk  rt        d      |dk  rt        d      ||kD  rt        d      |dk  rt        d	      |dk  rt        d
      t        |t        j                        st        dj                  |            t        |	t        j                        st        dj                  |	            || _        || _        g | _        d| _        d| _        d | _        || _        |	| _        d| _        d| _        d| _        || _        |
| _        || _        || _        || _        || _        y )Nr   zPassing multiple positional arguments to asyncpg.Pool constructor is deprecated and will be removed in asyncpg 0.17.0.  The non-deprecated form is asyncpg.Pool(<dsn>, **kwargs)   )
stacklevelr   z,max_size is expected to be greater than zeroz3min_size is expected to be greater or equal to zeroz!min_size is greater than max_sizez/max_queries is expected to be greater than zerozKmax_inactive_connection_lifetime is expected to be greater or equal to zerozMconnection_class is expected to be a subclass of asyncpg.Connection, got {!r}zErecord_class is expected to be a subclass of asyncpg.Record, got {!r}F) lenwarningswarnDeprecationWarningrm   get_event_looprM   
ValueError
issubclassr   r   	TypeErrorr(   r   Recordr   r   r   r   r   r   r   r   r   r   rU   r   r   r   rP   rO   r   )r)   min_sizemax_sizerX    max_inactive_connection_lifetimerY   initloopconnection_classrecord_classconnect_argsconnect_kwargss               r"   r=   zPool.__init__>  s    |q MM0 #q2 <))+D
q=KLLa<EG G h@AA!NOO+a/#$ $ *J,A,AB//5v6F/GI I ,8++16,+?A A ! !"!1)
)-', 	.r#   c                 2  K   | j                   ry | j                  rt        j                  d      | j                  rt        j                  d      d| _        	 | j                          d {    | d| _        d| _         S 7 # d| _        d| _         w xY ww)Nz)pool is being initialized in another taskpool is closedTF)r   r   r   r'   r   _initializerG   s    r"   _async__init__zPool._async__init__  s     ++;= =<<++,<==!	%""$$$!&D $D	 % "'D $Ds0   ABB -B.B 3BB BBc                   K   t        j                  | j                        | _        t	        | j                        D ]e  }t        | | j                  | j                  | j                        }| j                  j                  |       | j                  j                  |       g | j                  r| j                  d   }|j                          d {    | j                  dkD  ryg }t        t        | j                  d d             D ]8  \  }}|| j                  dz
  k\  r n!|j                  |j                                : t        j                   |  d {    y y y 7 7 	w)N)maxsize)rX   rZ   rY   r   )rm   	LifoQueuer   r   ranger8   rO   r   rP   r   appendr   r   rf   	enumeratereversedgather)r)   r   chfirst_chconnect_tasksis         r"   r   zPool._initialize  s5    ''>t}}% 	'A% --"&"H"Hkk	#B MM  $KK""2&	' == }}R(H""$$$}}q  "&xcr0B'CD 7EArDMMA--!((6	7 nnm444 !  % 5s%   CE&E"BE&E$E&$E&c                 6    | j                   xs | j                  S )z_Return ``True`` if the pool is closing or is closed.

        .. versionadded:: 0.28.0
        )r   r   rG   s    r"   
is_closingzPool.is_closing  s    
 ||,t}},r#   c                 :    t        d | j                  D              S )zaReturn the current number of connections in this pool.

        .. versionadded:: 0.25.0
        c              3   <   K   | ]  }|j                           y wr;   )r]   .0hs     r"   	<genexpr>z Pool.get_size.<locals>.<genexpr>  s     ;1>>#;s   sumr   rG   s    r"   get_sizezPool.get_size  s    
 ;T]];;;r#   c                     | j                   S )zaReturn the minimum number of connections in this pool.

        .. versionadded:: 0.25.0
        )r   rG   s    r"   get_min_sizezPool.get_min_size      
 }}r#   c                     | j                   S )ziReturn the maximum allowed number of connections in this pool.

        .. versionadded:: 0.25.0
        )r   rG   s    r"   get_max_sizezPool.get_max_size  r   r#   c                 :    t        d | j                  D              S )zfReturn the current number of idle connections in this pool.

        .. versionadded:: 0.25.0
        c              3   `   K   | ]&  }|j                         xr |j                          ( y wr;   )r]   r`   r   s     r"   r   z%Pool.get_idle_size.<locals>.<genexpr>  s%     K1>>#3		3Ks   ,.r   rG   s    r"   get_idle_sizezPool.get_idle_size  s    
 KT]]KKKr#   Nc                 "    |g| _         || _        y)a  Set the new connection arguments for this pool.

        The new connection arguments will be used for all subsequent
        new connection attempts.  Existing connections will remain until
        they expire. Use :meth:`Pool.expire_connections()
        <asyncpg.pool.Pool.expire_connections>` to expedite the connection
        expiry.

        :param str dsn:
            Connection arguments specified using as a single string in
            the following format:
            ``postgres://user:pass@host:port/database?option=value``.

        :param \*\*connect_kwargs:
            Keyword arguments for the :func:`~asyncpg.connection.connect`
            function.

        .. versionadded:: 0.16.0
        N)r   r   )r)   dsnr   s      r"   set_connect_argszPool.set_connect_args  s    * "U-r#   c                   K   t        j                  | j                  | j                  | j                  | j
                  d| j                   d {   }| j                  	 | j                  |       d {    |S |S 7 .7 
# t        t        j                  f$ r'}	 |j                          d {  7   |# |xY wd }~ww xY ww)N)r   r   r   )r   rf   r   rM   r   r   r   r   rl   rm   rn   rk   )r)   r9   rq   s      r"   rc   zPool._get_new_connection  s     &&!33++	

 ""
 
 ::!jjo%% 
s
3
 &w556 

 ))+%%H"Hsf   ACB C#B 7B8B <CB CB91B42B97B?9B<<B??CCrh   queryri   rA   c                   K   | j                         4 d{   } |j                  |g|d|i d{   cddd      d{    S 7 37 7 	# 1 d{  7  sw Y   yxY ww)a  Execute an SQL command (or commands).

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.execute() <asyncpg.connection.Connection.execute>`.

        .. versionadded:: 0.10.0
        Nri   )rr   execute)r)   r   ri   r*   r9   s        r"   r   zPool.execute  sj      <<> 	D 	DS$UCTC7CC	D 	D 	DC	D 	D 	D 	DsS   A&AA&AAAA&AA&AA&A#AA#A&commandc                   K   | j                         4 d{   }|j                  |||       d{   cddd      d{    S 7 17 7 	# 1 d{  7  sw Y   yxY ww)aC  Execute an SQL *command* for each sequence of arguments in *args*.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.executemany()
        <asyncpg.connection.Connection.executemany>`.

        .. versionadded:: 0.10.0
        Nrh   )rr   executemany)r)   r   r*   ri   r9   s        r"   r   zPool.executemany  sd      <<> 	I 	IS$HH	I 	I 	IH	I 	I 	I 	IsS   A$A	A$AAAA$AA$AA$A!AA!A$ri   r   c                   K   | j                         4 d{   } |j                  |g|||d d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)a-  Run a query and return the results as a list of :class:`Record`.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetch() <asyncpg.connection.Connection.fetch>`.

        .. versionadded:: 0.10.0
        Nr   )rr   fetchr)   r   ri   r   r*   r9   s         r"   r   z
Pool.fetch*  ss      <<> 	 	S"  )	 	 	 		 	 	 	S   A'AA'AAAA'AA'AA'A$AA$ A'r   columnri   c                   K   | j                         4 d{   } |j                  |g|||d d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)a+  Run a query and return a value in the first row.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchval()
        <asyncpg.connection.Connection.fetchval>`.

        .. versionadded:: 0.10.0
        Nr   )rr   fetchval)r)   r   r   ri   r*   r9   s         r"   r   zPool.fetchvalA  sp      <<> 	> 	>S%>>%+W> >	> 	> 	>>	> 	> 	> 	>r   c                   K   | j                         4 d{   } |j                  |g|||d d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)a  Run a query and return the first row.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchrow() <asyncpg.connection.Connection.fetchrow>`.

        .. versionadded:: 0.10.0
        Nr   )rr   fetchrowr   s         r"   r   zPool.fetchrowO  ss      <<> 	 	S%  )	 	 	 		 	 	 	r   )columnsschema_nameri   r(   oids	delimiternullheaderquoteescapeforce_quoteencodingc                   K   | j                         4 d{   }|j                  |||||||||	|
||||       d{   cddd      d{    S 7 <7 7 	# 1 d{  7  sw Y   yxY ww)a;  Copy table contents to a file or file-like object.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_from_table()
        <asyncpg.connection.Connection.copy_from_table>`.

        .. versionadded:: 0.24.0
        N)outputr   r   ri   r(   r   r   r   r   r   r   r   r   )rr   copy_from_table)r)   
table_namer   r   r   ri   r(   r   r   r   r   r   r   r   r   r9   s                   r"   r   zPool.copy_from_table`  s     6 <<> 	 	S,,'#'! -  	 	 		 	 	 	sT   A/AA/#AAAA/AA/AA/A, A#!A,(A/)
ri   r(   r   r   r   r   r   r   r   r   c                   K   | j                         4 d{   } |j                  |g|||||||||	|
||d d{   cddd      d{    S 7 =7 7 	# 1 d{  7  sw Y   yxY ww)aC  Copy the results of a query to a file or file-like object.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_from_query()
        <asyncpg.connection.Connection.copy_from_query>`.

        .. versionadded:: 0.24.0
        N)r   ri   r(   r   r   r   r   r   r   r   r   )rr   copy_from_query)r)   r   r   ri   r(   r   r   r   r   r   r   r   r   r*   r9   s                  r"   r   zPool.copy_from_query  s     2 <<> 	 	S,,, #'! 	 	 		 	 	 	sU   A0AA0$AA AA0AA0AA0A-!A$"A-)A0)r   r   ri   r(   r   freezer   r   r   r   r   r   force_not_null
force_nullr   wherec                   K   | j                         4 d{   }|j                  |||||||||	|
||||||||       d{   cddd      d{    S 7 @7 7 	# 1 d{  7  sw Y   yxY ww)a&  Copy data to the specified table.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_to_table()
        <asyncpg.connection.Connection.copy_to_table>`.

        .. versionadded:: 0.24.0
        N)sourcer   r   ri   r(   r   r   r   r   r   r   r   r   r   r  r   r  )rr   copy_to_table)r)   r   r  r   r   ri   r(   r   r   r   r   r   r   r   r   r   r  r   r  r9   s                       r"   r  zPool.copy_to_table  s     > <<> 	 	S**'#'-%!% +  	 	 		 	 	 	sV   A3AA3'AAAA3AA3AA3A0$A'%A0,A3)r   r   ri   r  c          	         K   | j                         4 d{   }|j                  ||||||       d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)aU  Copy a list of records to the specified table using binary COPY.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_records_to_table()
        <asyncpg.connection.Connection.copy_records_to_table>`.

        .. versionadded:: 0.24.0
        N)recordsr   r   ri   r  )rr   copy_records_to_table)r)   r   r  r   r   ri   r  r9   s           r"   r  zPool.copy_records_to_table  sr     & <<> 	 	S22' 3  	 	 		 	 	 	r   c                    t        | |      S )aT  Acquire a database connection from the pool.

        :param float timeout: A timeout for acquiring a Connection.
        :return: An instance of :class:`~asyncpg.connection.Connection`.

        Can be used in an ``await`` expression or with an ``async with`` block.

        .. code-block:: python

            async with pool.acquire() as con:
                await con.execute(...)

        Or:

        .. code-block:: python

            con = await pool.acquire()
            try:
                await con.execute(...)
            finally:
                await pool.release(con)
        )PoolAcquireContext)r)   ri   s     r"   rr   zPool.acquire	  s    . "$00r#   c                     K    fd} j                   rt        j                  d       j                           |        d {   S t	        j
                   |              d {   S 7 (7 w)Nc                    K   j                   j                          d {   } 	 | j                          d {   }| _        |S 7 &7 # t        t
        j                  f$ r j                   j                  |         w xY wwr;   )r   getrr   rT   rl   rm   rn   r   )r   rp   r)   ri   s     r"   _acquire_implz$Pool._acquire.<locals>._acquire_impl#  sr     {{((B	 jjl* & )*w556 &&r*s5   BABA A
A 
B
A 6BBzpool is closingrh   )r   r   r'   _check_initr   ry   )r)   ri   r  s   `` r"   _acquirezPool._acquire"  sk     	 ==++,=>>?&((2 2 2 )2s$   AA5A1	#A5,A3-A53A5c                  K   t        |      t        us|j                  j                  | ur%t	        j
                  dj                  |            |j                  y| j                          |j                  j                          |j                  }||j                  }t        j                  |j                  |             d{   S 7 w)a  Release a database connection back to the pool.

        :param Connection connection:
            A :class:`~asyncpg.connection.Connection` object to release.
        :param float timeout:
            A timeout for releasing the connection.  If not specified, defaults
            to the timeout provided in the corresponding call to the
            :meth:`Pool.acquire() <asyncpg.pool.Pool.acquire>` method.

        .. versionchanged:: 0.14.0
            Added the *timeout* parameter.
        zWPool.release() received invalid connection: {connection!r} is not a member of this poolr   N)typer5   r6   rL   r   r'   r(   r&   r  _on_releaserT   rm   shieldr   )r)   r   ri   r   s       r"   r   zPool.release:  s      $77""((4++>>Df) ?E ?+, ,
 ??" 	##%?kkG
 ^^BJJw$78888s   CC	C
Cc                   K   | j                   ry| j                          d| _        d}	 | j                  j	                  d| j
                        }| j                  D cg c]  }|j                          }}t        j                  |  d{    | j                  D cg c]  }|j                          }}t        j                  |  d{    	 ||j                          d| _         d| _        yc c}w 7 lc c}w 7 2# t        t        j                  f$ r | j                           w xY w# ||j                          d| _         d| _        w xY ww)a  Attempt to gracefully close all connections in the pool.

        Wait until all pool connections are released, close them and
        shut down the pool.  If any error (including cancellation) occurs
        in ``close()`` the pool will terminate by calling
        :meth:`Pool.terminate() <pool.Pool.terminate>`.

        It is advisable to use :func:`python:asyncio.wait_for` to set
        a timeout.

        .. versionchanged:: 0.16.0
            ``close()`` now waits until all pool connections are released
            before closing them and the pool.  Errors raised in ``close()``
            will cause immediate pool termination.
        NT<   F)r   r  r   rM   r   _warn_on_long_closer   r   rm   r   rk   rl   rn   r|   r   )r)   warning_callbackr   release_corosclose_coross        r"   rk   z
Pool.close`  sB      <<	"#zz44D,, . 48==B-/&&(BM B..-000 &*]]4!
4K 4..+...  + '')DL!DM!B04.7112 	NN	
  + '')DL!DMsd   'E4D C45D C9D C;6D D D !E4D ;D +D--D0 0"EEc                 .    t         j                  d       y )NzPool.close() is taking over 60 seconds to complete. Check if you have any unreleased connections left. Use asyncio.wait_for() to set a timeout for Pool.close().)loggerwarningrG   s    r"   r  zPool._warn_on_long_close  s     ' 	(r#   c                     | j                   ry| j                          | j                  D ]  }|j                           d| _         y)z&Terminate all connections in the pool.NT)r   r  r   r|   r)   r   s     r"   r|   zPool.terminate  s;    <<-- 	BLLN	r#   c                 6   K   | xj                   dz  c_         yw)zExpire all currently open connections.

        Cause all currently open connections to get replaced on the
        next :meth:`~asyncpg.pool.Pool.acquire()` call.

        .. versionadded:: 0.16.0
        r   N)rU   rG   s    r"   expire_connectionszPool.expire_connections  s      	As   c                     | j                   s6| j                  rt        j                  d      t        j                  d      | j                  rt        j                  d      y )Nzipool is being initialized, but not yet ready: likely there is a race between creating a pool and using itzpool is not initializedr   )r   r   r   r'   r   rG   s    r"   r  zPool._check_init  s^      !! //    ++,EFF<<++,<== r#   c                 t    | j                   D ])  }|j                  |j                  j                          + y r;   )r   r&   _drop_local_statement_cacher  s     r"   _drop_statement_cachezPool._drop_statement_cache  s/    -- 	6Bww"335	6r#   c                 t    | j                   D ])  }|j                  |j                  j                          + y r;   )r   r&   _drop_local_type_cacher  s     r"   _drop_type_cachezPool._drop_type_cache  s/    -- 	1Bww"..0	1r#   c                 >    | j                         j                         S r;   )r   	__await__rG   s    r"   r*  zPool.__await__  s    ""$..00r#   c                 B   K   | j                          d {    | S 7 wr;   )r   rG   s    r"   
__aenter__zPool.__aenter__  s"     !!### 	$s   c                 @   K   | j                          d {    y 7 wr;   )rk   )r)   excs     r"   	__aexit__zPool.__aexit__  s     jjls   r;   )(r/   r0   r1   r   rI   r=   r   r   r   r   r   r   r   r   rc   strfloatr   r   listr   r   r   r   r   r  r  rr   r  r   rk   r  r|   r!  r  r%  r(  r*  r,  r/  r.   r#   r"   r   r   +  s   II-V% 5B-<L.08 ?C 
D3 
D 
Ds 
D GK I I I"  
. 34T > 48d , !+d (^ )3t : "& 1220 48 $9L+"Z(	>611r#   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r
  )ri   r   r   rW   c                 <    || _         || _        d | _        d| _        y )NF)rW   ri   r   r   )r)   rW   ri   s      r"   r=   zPoolAcquireContext.__init__  s    		r#   c                    K   | j                   | j                  rt        j                  d      | j                  j                  | j                         d {   | _         | j                   S 7 w)Nz a connection is already acquired)r   r   r   r'   rW   r  ri   rG   s    r"   r,  zPoolAcquireContext.__aenter__  sU     ??&$))++,NOO $		 2 24<< @@ As   AA0A.A0c                    K   d| _         | j                  }d | _        | j                  j                  |       d {    y 7 wNT)r   r   rW   r   )r)   r.  r9   s      r"   r/  zPoolAcquireContext.__aexit__  s5     	ooii$$$s   9AAAc                 v    d| _         | j                  j                  | j                        j	                         S r7  )r   rW   r  ri   r*  rG   s    r"   r*  zPoolAcquireContext.__await__  s+    	yy!!$,,/99;;r#   N)r/   r0   r1   rI   r=   r,  r/  r*  r.   r#   r"   r
  r
    s    9I%<r#   r
  
   iP  g     r@)	r   r   rX   r   rY   r   r   r   r   c       	         .    t        | f||	|||||||d	|
S )ac  Create a connection pool.

    Can be used either with an ``async with`` block:

    .. code-block:: python

        async with asyncpg.create_pool(user='postgres',
                                       command_timeout=60) as pool:
            await pool.fetch('SELECT 1')

    Or to perform multiple operations on a single connection:

    .. code-block:: python

        async with asyncpg.create_pool(user='postgres',
                                       command_timeout=60) as pool:
            async with pool.acquire() as con:
                await con.execute('''
                   CREATE TABLE names (
                      id serial PRIMARY KEY,
                      name VARCHAR (255) NOT NULL)
                ''')
                await con.fetch('SELECT 1')

    Or directly with ``await`` (not recommended):

    .. code-block:: python

        pool = await asyncpg.create_pool(user='postgres', command_timeout=60)
        con = await pool.acquire()
        try:
            await con.fetch('SELECT 1')
        finally:
            await pool.release(con)

    .. warning::
        Prepared statements and cursors returned by
        :meth:`Connection.prepare() <asyncpg.connection.Connection.prepare>`
        and :meth:`Connection.cursor() <asyncpg.connection.Connection.cursor>`
        become invalid once the connection is released.  Likewise, all
        notification and log listeners are removed, and ``asyncpg`` will
        issue a warning if there are any listener callbacks registered on a
        connection that is being released to the pool.

    :param str dsn:
        Connection arguments specified using as a single string in
        the following format:
        ``postgres://user:pass@host:port/database?option=value``.

    :param \*\*connect_kwargs:
        Keyword arguments for the :func:`~asyncpg.connection.connect`
        function.

    :param Connection connection_class:
        The class to use for connections.  Must be a subclass of
        :class:`~asyncpg.connection.Connection`.

    :param type record_class:
        If specified, the class to use for records returned by queries on
        the connections in this pool.  Must be a subclass of
        :class:`~asyncpg.Record`.

    :param int min_size:
        Number of connection the pool will be initialized with.

    :param int max_size:
        Max number of connections in the pool.

    :param int max_queries:
        Number of queries after a connection is closed and replaced
        with a new connection.

    :param float max_inactive_connection_lifetime:
        Number of seconds after which inactive connections in the
        pool will be closed.  Pass ``0`` to disable this mechanism.

    :param coroutine setup:
        A coroutine to prepare a connection right before it is returned
        from :meth:`Pool.acquire() <pool.Pool.acquire>`.  An example use
        case would be to automatically set up notifications listeners for
        all connections of a pool.

    :param coroutine init:
        A coroutine to initialize a connection when it is created.
        An example use case would be to setup type codecs with
        :meth:`Connection.set_builtin_type_codec() <\
        asyncpg.connection.Connection.set_builtin_type_codec>`
        or :meth:`Connection.set_type_codec() <\
        asyncpg.connection.Connection.set_type_codec>`.

    :param loop:
        An asyncio event loop instance.  If ``None``, the default
        event loop will be used.

    :return: An instance of :class:`~asyncpg.pool.Pool`.

    .. versionchanged:: 0.10.0
       An :exc:`~asyncpg.exceptions.InterfaceError` will be raised on any
       attempted operation on a released connection.

    .. versionchanged:: 0.13.0
       An :exc:`~asyncpg.exceptions.InterfaceError` will be raised on any
       attempted operation on a prepared statement or a cursor created
       on a connection that has been released to the pool.

    .. versionchanged:: 0.13.0
       An :exc:`~asyncpg.exceptions.InterfaceWarning` will be produced
       if there are any active listeners (added via
       :meth:`Connection.add_listener()
       <asyncpg.connection.Connection.add_listener>`
       or :meth:`Connection.add_log_listener()
       <asyncpg.connection.Connection.add_log_listener>`) present on the
       connection at the moment of its release to the pool.

    .. versionchanged:: 0.22.0
       Added the *record_class* parameter.
    )	r   r   r   r   rX   r   rY   r   r   )r   )r   r   r   rX   r   rY   r   r   r   r   r   s              r"   create_poolr;    s;    @ )!Hd%d)I  r#   r;   )rm   r   r   loggingrw   r    r   r   r   r   	getLoggerr/   r  r  r
   _ConnectionProxyr5   r8   r   r
  r   r   r;  r.   r#   r"   <module>r@     s              
		8	$"d "JO*55$;#OBJ+ J+ZZ
 Z
z< <6G!16!+!6!6%__Gr#   