
     i                       d dl mZ d dlZd dlmZm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mZm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ddej         ej!        ddZ"dS )    )annotationsN)	AwaitableCallable)TracebackType)AnyOptionalType   )compat
connection)
exceptions)protocolc                  >     e Zd Zddd fdZedd            Z xZS )PoolConnectionProxyMetaF)wrapnamestrbasestuple[Type[Any], ...]dctdict[str, Any]r   boolreturnc                  |rt          t          j                  D ]}|                    d          s||v rt	          t          j        |          }t          j        |          sKt          j        |          }|                     ||          }t          j
        ||          }|||<   d|vrt          j        j        |d<   t                                          | |||          S )N___doc__)dirr   
Connection
startswithgetattrinspect
isfunctioniscoroutinefunction_wrap_connection_method	functoolsupdate_wrapperr   super__new__)
mclsr   r   r   r   attrnamemethiscoroutinewrapper	__class__s
            F/var/www/html/gemini/venv/lib/python3.11/site-packages/asyncpg/pool.pyr)   zPoolConnectionProxyMeta.__new__   s      	?
 566 ( (&&s++ x3z4h??)$// %9$??66xMM#27DAA 'H##!+!6!>IwwtT5#666    	meth_namer-   Callable[..., Any]c                @     d fd}|rt          j        |           |S )Nselfr   argskwargsr   c                    | j         't          j        d                                        t	          | j         j                  } || j         g|R i |S )NzJcannot call Connection.{}(): connection has been released back to the pool)_conr   InterfaceErrorformatr!   r/   )r5   r6   r7   r,   r2   s       r0   call_con_methodzHPoolConnectionProxyMeta._wrap_connection_method.<locals>.call_con_method=   sr    y  /DDJF!E# E#$ $ $
 49.	::D4	3D333F333r1   )r5   r   r6   r   r7   r   r   r   )r   markcoroutinefunction)r2   r-   r<   s   `  r0   r%   z/PoolConnectionProxyMeta._wrap_connection_method9   sB    		4 		4 		4 		4 		4 		4  	:(999r1   )
r   r   r   r   r   r   r   r   r   r   )r2   r   r-   r   r   r3   )__name__
__module____qualname__r)   staticmethodr%   __classcell__)r/   s   @r0   r   r      sl         7 7 7 7 7 7 7 76    \    r1   r   c                  2    e Zd ZdZddZddZddZddZdS )PoolConnectionProxy)r9   _holderholderPoolConnectionHolderconconnection.Connectionr   Nonec                L    || _         || _        |                    |            d S N)r9   rE   
_set_proxy)r5   rF   rH   s      r0   __init__zPoolConnectionProxy.__init__T   s*     	tr1   attrr   r   c                ,    t          | j        |          S rL   )r!   r9   )r5   rO   s     r0   __getattr__zPoolConnectionProxy.__getattr__[   s    ty$'''r1   Optional[connection.Connection]c                `    | j         d S | j         d c}| _         |                    d            |S rL   )r9   rM   )r5   rH   s     r0   _detachzPoolConnectionProxy._detach_   s5    9FDTYt
r1   c                    | j         .d                    | j        j        t	          |                     S d                    | j        j        | j         t	          |                     S )Nz <{classname} [released] {id:#x}>)	classnameidz<{classname} {con!r} {id:#x}>)rV   rH   rW   )r9   r;   r/   r>   rW   r5   s    r0   __repr__zPoolConnectionProxy.__repr__g   sp    95<<.1bhh = @ @ @ 399.1tyRXX : O O Or1   N)rF   rG   rH   rI   r   rJ   )rO   r   r   r   )r   rR   )r   r   )r>   r?   r@   	__slots__rN   rQ   rT   rY    r1   r0   rD   rD   N   sq         $I   ( ( ( (   O O O O O Or1   rD   T)	metaclassr   c                      e Zd ZdZddZddZddZd dZd!dZd"dZ	d dZ
d dZd dZd dZd dZd dZd dZd dZdS )#rG   )r9   _pool_loop_proxy_max_queries_setup_max_inactive_time_in_use_inactive_callback_timeout_generationpool'Pool'max_queriesfloatsetup:Optional[Callable[[PoolConnectionProxy], Awaitable[None]]]max_inactive_timer   rJ   c                   || _         d | _        d | _        || _        || _        || _        d | _        d | _        d | _        d | _	        d S rL   )
r^   r9   r`   ra   rc   rb   re   rd   rf   rg   )r5   rh   rj   rl   rn   s        r0   rN   zPoolConnectionHolder.__init__x   sT     
59	59'"36:15)-*.r1   r   c                H    | j         d uo| j                                          S rL   )r9   	is_closedrX   s    r0   is_connectedz!PoolConnectionHolder.is_connected   s%    y$BTY-@-@-B-B)BBr1   c                    | j          S rL   rd   rX   s    r0   is_idlezPoolConnectionHolder.is_idle   s    <r1   c                   K   | j         t          j        d          | j                                         d {V | _         | j        j        | _        |                                  |                                  d S )NzMPoolConnectionHolder.connect() called while another connection already exists)r9   r   InternalClientErrorr^   _get_new_connectionrg   _maybe_cancel_inactive_callback_setup_inactive_callbackrX   s    r0   connectzPoolConnectionHolder.connect   s      9 0,- - - *88::::::::	:1,,...%%'''''r1   rD   c                  K   | j         | j                                         r"d | _         |                                  d {V  ns| j        | j        j        k    r^| j        j                            | j                             | j                             d | _         |                                  d {V  | 	                                 t          | | j                   x| _        }| j        c	 |                     |           d {V  nF# t          t          j        f$ r-}	 | j                                          d {V  |# |xY wd }~ww xY w| j        j                                        | _        |S )Ntimeout)r9   rq   r{   rg   r^   r_   create_taskcloserf   ry   rD   r`   rb   	ExceptionasyncioCancelledErrorcreate_futurerd   )r5   proxyexs      r0   acquirezPoolConnectionHolder.acquire   s     9	 3 3 5 5DI,,..        !777J((	668 8 8DI,,..       ,,...1$	BBBe;"kk%((((((((((w56   

 )//+++++++++H"HHHH z'5577s*   0D E#EE
EE

Er~   Optional[float]c                  K   | j         t          j        d          | j                                        rd S d | _        | j        j        j        | j        k    r#| j        	                    |           d {V  d S | j
        | j        j
        k    r#| j        	                    |           d {V  d S 	 |}| j        j                                        ret          j                    }t          j        | j        j                                        |           d {V  ||t          j                    |z
  z  }| j        j        t          j        |          4 d {V  | j                                         d {V  | j                            | j                   d {V  d d d           d {V  n# 1 d {V swxY w Y   n!| j                            |           d {V  n@# t*          t,          j        f$ r'}	 | j                                         |# |xY wd }~ww xY w|                                  |                                  d S )NzAPoolConnectionHolder.release() called on a free connection holderr}   )rd   r   rw   r9   rq   rf   	_protocolqueries_countra   r   rg   r^   _is_cancellingtime	monotonicr   wait_for_wait_for_cancellation_resetr~   resetr   r   r   	terminate_releaserz   )r5   r~   budgetstartedr   s        r0   releasezPoolConnectionHolder.release   s0     <0+, , , 9   	 F9,0AAA )//'/222222222Ftz555 )//'/222222222F	Fy"1133 9 .**oI'>>@@         %dn..88Fz ,!>&11 7 7 7 7 7 7 7 7)**,,,,,,,,,*++DI6666666667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ioofo555555555712 		 		 		 	##%%%b		 	 	%%'''''sP   6B+G, !AF8&G, 8
GG, G%G, ,H)HH$H!!H$$H)c                6   K   | j         d S | j          d {V  d S rL   rt   rX   s    r0   wait_until_releasedz(PoolConnectionHolder.wait_until_released  s3      <F,r1   c                Z   K   | j         !| j                                          d {V  d S d S rL   )r9   r   rX   s    r0   r   zPoolConnectionHolder.close  sB      9  )//########### ! r1   c                J    | j         | j                                          d S d S rL   )r9   r   rX   s    r0   r   zPoolConnectionHolder.terminate  s.    9  I!!!!! ! r1   c                    | j         t          j        d          | j        r1| j        j                            | j        | j                  | _         d S d S )Nz/pool connection inactivity timer already exists)re   r   rw   rc   r^   r_   
call_later_deactivate_inactive_connectionrX   s    r0   rz   z-PoolConnectionHolder._setup_inactive_callback  sp    ".0AC C C " 	O&*j&6&A&A')M'O 'OD###	O 	Or1   c                X    | j         "| j                                          d | _         d S d S rL   )re   cancelrX   s    r0   ry   z4PoolConnectionHolder._maybe_cancel_inactive_callback  s6    ".#**,,,&*D### /.r1   c                    | j         t          j        d          | j        /| j                                         |                                  d S d S )Nz/attempting to deactivate an acquired connection)rd   r   rw   r9   r   _release_on_closerX   s    r0   r   z4PoolConnectionHolder._deactivate_inactive_connection"  sg    <#0AC C C 9  I!!! ""$$$$$ ! r1   c                d    |                                   |                                  d | _        d S rL   )ry   r   r9   rX   s    r0   r   z&PoolConnectionHolder._release_on_close0  s,    ,,...			r1   c                   | j         dS | j                                         s| j                             d           d| _         | j         | j                                         d| _        | j        j                            |            dS )zRelease this connection holder.N)rd   done
set_resultr`   rT   r^   _queue
put_nowaitrX   s    r0   r   zPoolConnectionHolder._release5  s    <F|  "" 	*L##D))) ;"K!!!DK 	
$$T*****r1   N)
rh   ri   rj   rk   rl   rm   rn   rk   r   rJ   )r   r   )r   rJ   )r   rD   )r~   r   r   rJ   )r>   r?   r@   rZ   rN   rr   ru   r{   r   r   r   r   r   rz   ry   r   r   r   r[   r1   r0   rG   rG   p   s=        I/ / / /*C C C C       	( 	( 	( 	(# # # #J=( =( =( =(~   $ $ $ $" " " "O O O O+ + + +
% % % %   
+ + + + + +r1   rG   c                     e Zd ZdZdZddddddZd Zd Zd Zd	 Z	d
 Z
d Zd Zd5dZd Zddd6dZddd7dZdddd8dZddd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 d0 Z!d1 Z"d2 Z#d3 Z$d4 Z%dS )9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   r_   _minsize_maxsize_init_connectr   _connect_args_connect_kwargs_holders_initialized_initializing_closing_closed_connection_class_record_classrg   rb   ra   !_max_inactive_connection_lifetimeN)r{   rl   initr   c                  t          |          dk    rt          j        dt          d           |	t	          j                    }	|	| _        |dk    rt          d          |dk     rt          d          ||k    rt          d          |dk    rt          d	          |dk     rt          d
          t          |
t          j
                  s"t          d                    |
                    t          |t          j                  s"t          d                    |                    || _        || _        g | _        d| _        d| _        d | _        |
| _        || _        d| _        d| _        d| _        ||nt          j        | _        || _        || _        || _        || _        || _         || _!        || _"        d S )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DeprecationWarningr   get_event_loopr_   
ValueError
issubclassr   r   	TypeErrorr;   r   Recordr   r   r   r   r   r   r   r   r   r   rg   r{   r   r   r   rb   r   r   ra   r   )r5   min_sizemax_sizerj    max_inactive_connection_lifetimer{   rl   r   r   loopconnection_classrecord_classconnect_argsconnect_kwargss                 r0   rN   zPool.__init__\  s    |q  M0 #q2 2 2 2 <)++D
q==KLLLa<<EG G G h@AAA!NOOO+a//#$ $ $ *J,ABB 	I//5v6F/G/GI I I ,88 	A++16,+?+?A A A ! !"!1)#*#6J<N)-
', 	...r1   c                  K   | j         r| S | j        rt          j        d          | j        rt          j        d          d| _        	 |                                  d {V  | d| _        d| _         S # d| _        d| _         w xY w)Nz)pool is being initialized in another taskpool is closedTF)r   r   r   r:   r   _initializerX   s    r0   _async__init__zPool._async__init__  s       	K 	=+;= = =< 	>+,<===!	%""$$$$$$$$$!&D $D "'D $D$$$$s   
A4 4Bc                  K   t          j        | j                  | _        t	          | j                  D ]X}t          | | j        | j        | j                  }| j	        
                    |           | j                            |           Y| j        r| j	        d         }|                                 d {V  | j        dk    rg }t          t          | j	        d d                             D ]<\  }}|| j        dz
  k    r n(|
                    |                                           =t          j        |  d {V  d S d S d S )N)maxsize)rj   rn   rl   r
   )r   	LifoQueuer   r   rangerG   ra   r   rb   r   appendr   r   r{   	enumeratereversedgather)r5   r   chfirst_chconnect_tasksis         r0   r   zPool._initialize  sx     '>>>t}%% 	' 	'A% -"&"Hk	# # #B M  $$$K""2&&&&= 	5 }R(H""$$$$$$$$$}q   "&xcrc0B'C'CDD 7 7EArDMA---!((6666nm4444444444'	5 	5 ! r1   c                    | j         p| j        S )z_Return ``True`` if the pool is closing or is closed.

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

        .. versionadded:: 0.25.0
        c              3  >   K   | ]}|                                 V  d S rL   )rr   .0hs     r0   	<genexpr>z Pool.get_size.<locals>.<genexpr>  s,      ;;1>>##;;;;;;r1   sumr   rX   s    r0   get_sizezPool.get_size  s#    
 ;;T];;;;;;r1   c                    | j         S )zaReturn the minimum number of connections in this pool.

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

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

        .. versionadded:: 0.25.0
        c              3  f   K   | ],}|                                 o|                                V  -d S rL   )rr   ru   r   s     r0   r   z%Pool.get_idle_size.<locals>.<genexpr>  s9      KK1>>##3		KKKKKKr1   r   rX   s    r0   get_idle_sizezPool.get_idle_size  s#    
 KKT]KKKKKKr1   c                $    |g| _         || _        dS )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   )r5   dsnr   s      r0   set_connect_argszPool.set_connect_args  s    * "U-r1   c                *  K    | j         | j        | j        | j        | j        d| j         d {V }t          || j                  sf| j        }|j         d|j         }t          |          }|j        dk    r|j        }n|j         d|j         }t          j        d| d| d          | j        ^	 |                     |           d {V  nA# t          t          j        f$ r(}	 |                                 d {V  |# |xY wd }~ww xY w|S )N)r   r   r   .builtinsz9expected pool connect callback to return an instance of 'z', got '')r   r   r_   r   r   r   
isinstancer?   r>   typer   r:   r   r   r   r   r   )r5   rH   goodgood_nbadbad_nr   s          r0   rx   zPool._get_new_connection  s     !DM!3+	
 

 "
 
 
 
 
 
 
 
 #t566 	)D99$-99Fs))C~++>::CL::+11 1(-1 1 1  
 :!jjoo%%%%%%%%w56   

 ))++%%%%%%%H"HHHH 
s*   6C D)DDDDDr}   queryr   r~   r   r   c                  K   |                                  4 d{V } |j        |g|R d|i d{V cddd          d{V  S # 1 d{V swxY w Y   dS )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
        Nr~   )r   execute)r5   r   r~   r6   rH   s        r0   r  zPool.executeA  s'      <<>> 	D 	D 	D 	D 	D 	D 	DS$UCTCCC7CCCCCCCC	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	Ds   A
AAcommandc                  K   |                                  4 d{V }|                    |||           d{V cddd          d{V  S # 1 d{V swxY w Y   dS )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
        Nr}   )r   executemany)r5   r  r6   r~   rH   s        r0   r  zPool.executemanyR  s       <<>> 	I 	I 	I 	I 	I 	I 	IS$HHHHHHHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Is   A
AAr~   r   listc                  K   |                                  4 d{V } |j        |g|R ||d d{V cddd          d{V  S # 1 d{V swxY w Y   dS )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  )r   fetchr5   r   r~   r   r6   rH   s         r0   r  z
Pool.fetche  s"      <<>> 	 	 	 	 	 	 	S"   )	        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   A
AAr   columnr~   c                  K   |                                  4 d{V } |j        |g|R ||d d{V cddd          d{V  S # 1 d{V swxY w Y   dS )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  )r   fetchval)r5   r   r  r~   r6   rH   s         r0   r  zPool.fetchval|  s      <<>> 	> 	> 	> 	> 	> 	> 	>S%>> >%+W> > > > > > > > >	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>r
  c                  K   |                                  4 d{V } |j        |g|R ||d d{V cddd          d{V  S # 1 d{V swxY w Y   dS )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  )r   fetchrowr	  s         r0   r  zPool.fetchrow  s"      <<>> 	 	 	 	 	 	 	S%   )	        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	r
  c                  K   |                                  4 d{V }|                    ||||           d{V cddd          d{V  S # 1 d{V swxY w Y   dS )an  Run a query for each sequence of arguments in *args*
        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.fetchmany()
        <asyncpg.connection.Connection.fetchmany>`.

        .. versionadded:: 0.30.0
        Nr  )r   	fetchmany)r5   r   r6   r~   r   rH   s         r0   r  zPool.fetchmany  s      <<>> 	 	 	 	 	 	 	StW< '        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A
AA)columnsschema_namer~   r;   oids	delimiternullheaderquoteescapeforce_quoteencodingc                  K   |                                  4 d{V }|                    |||||||||	|
||||           d{V cddd          d{V  S # 1 d{V swxY w Y   dS )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  r~   r;   r  r  r  r  r  r  r  r  )r   copy_from_table)r5   
table_namer  r  r  r~   r;   r  r  r  r  r  r  r  r  rH   s                   r0   r  zPool.copy_from_table  s1     6 <<>> 	 	 	 	 	 	 	S,,'#'! -        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   )A
A#&A#)
r~   r;   r  r  r  r  r  r  r  r  c                  K   |                                  4 d{V } |j        |g|R ||||||||	|
||d d{V cddd          d{V  S # 1 d{V swxY w Y   dS )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  r~   r;   r  r  r  r  r  r  r  r  )r   copy_from_query)r5   r   r  r~   r;   r  r  r  r  r  r  r  r  r6   rH   s                  r0   r"  zPool.copy_from_query  s>     2 <<>> 	 	 	 	 	 	 	S,,  #'!        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   !A
AA)r  r  r~   r;   r  freezer  r  r  r  r  r  force_not_null
force_nullr  wherec                  K   |                                  4 d{V }|                    |||||||||	|
||||||||           d{V cddd          d{V  S # 1 d{V swxY w Y   dS )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  r~   r;   r  r$  r  r  r  r  r  r  r%  r&  r  r'  )r   copy_to_table)r5   r   r)  r  r  r~   r;   r  r$  r  r  r  r  r  r  r%  r&  r  r'  rH   s                       r0   r*  zPool.copy_to_table  s=     > <<>> 	 	 	 	 	 	 	S**'#'-%!% +        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   -A
A'*A')r  r  r~   r'  c          	        K   |                                  4 d{V }|                    ||||||           d{V cddd          d{V  S # 1 d{V swxY w Y   dS )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  r~   r'  )r   copy_records_to_table)r5   r   r,  r  r  r~   r'  rH   s           r0   r-  zPool.copy_records_to_table7  s     & <<>> 	 	 	 	 	 	 	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)r5   r~   s     r0   r   zPool.acquireT  s    . "$000r1   c                    K    fd} j         rt          j        d                                             |             d {V S t	          j         |                       d {V S )Nc                    K   j                                          d {V } 	 |                                  d {V }| _        |S # t          t
          j        f$ r j                             |             w xY wrL   )r   getr   rf   r   r   r   r   )r   r   r5   r~   s     r0   _acquire_implz$Pool._acquire.<locals>._acquire_impln  s      {((((((((B	 jjll****** & w56   &&r***s   A 2A9zpool is closingr}   )r   r   r:   _check_initr   r   )r5   r~   r3  s   `` r0   _acquirezPool._acquirem  s      	 	 	 	 	 	 = 	?+,=>>>?&(((((((2 2 2 2 2 2 2 2 2 2r1   c                 K   t          |          t          us|j        j        | ur(t	          j        d                    |                    |j        dS |                                  |j        	                                 |j        }||j
        }t          j        |                    |                     d{V S )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)r   rD   rE   r^   r   r:   r;   r9   r4  _on_releaserf   r   shieldr   )r5   r   r~   r   s       r0   r   zPool.release  s       $777"(44+>>Df) ?E ?+ ?+, , ,
 ?"F 	##%%%?kG
 ^BJJw$7$7888888888r1   c                (  K   | j         rdS |                                  d| _        d}	 | j                            d| j                  }d | j        D             }t          j        |  d{V  d | j        D             }t          j        |  d{V  n/# t          t          j
        f$ r |                                   w xY w	 ||                                 d| _         d| _        dS # ||                                 d| _         d| _        w xY 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<   c                6    g | ]}|                                 S r[   )r   r   r   s     r0   
<listcomp>zPool.close.<locals>.<listcomp>  s7     B B B-/&&((B B Br1   c                6    g | ]}|                                 S r[   r   r<  s     r0   r=  zPool.close.<locals>.<listcomp>  s-     4 4 4!

4 4 4r1   F)r   r4  r   r_   r   _warn_on_long_closer   r   r   r   r   r   r   )r5   warning_callbackrelease_corosclose_coross       r0   r   z
Pool.close  s}       < 	F	"#z44D, .  .B B37=B B BM.-000000004 4%)]4 4 4K.+.........712 	 	 	NN	 /  + '')))DL!DMMM  + '')))DL!DM!!!!s   A*B C+ ,CC+ +&Dc                :    t                               d           d S )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warningrX   s    r0   r@  zPool._warn_on_long_close  s'     ' 	( 	( 	( 	( 	(r1   c                    | j         rdS |                                  | j        D ]}|                                 d| _         dS )z&Terminate all connections in the pool.NT)r   r4  r   r   r5   r   s     r0   r   zPool.terminate  sN    < 	F- 	 	BLLNNNNr1   c                *   K   | xj         dz  c_         dS )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)rg   rX   s    r0   expire_connectionszPool.expire_connections  s"       	Ar1   c                    | j         s/| j        rt          j        d          t          j        d          | j        rt          j        d          d S )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   rX   s    r0   r4  zPool._check_init  sm      	G!   /      +,EFFF< 	>+,<===	> 	>r1   c                Z    | j         D ]"}|j        |j                                         #d S rL   )r   r9   _drop_local_statement_cacherH  s     r0   _drop_statement_cachezPool._drop_statement_cache  s:    - 	6 	6Bw"33555	6 	6r1   c                Z    | j         D ]"}|j        |j                                         #d S rL   )r   r9   _drop_local_type_cacherH  s     r0   _drop_type_cachezPool._drop_type_cache  s:    - 	1 	1Bw"..000	1 	1r1   c                N    |                                                                  S rL   )r   	__await__rX   s    r0   rS  zPool.__await__  s     ""$$..000r1   c                >   K   |                                   d {V  | S rL   )r   rX   s    r0   
__aenter__zPool.__aenter__  s/      !!#########r1   c                >   K   |                                   d {V  d S rL   r?  )r5   excs     r0   	__aexit__zPool.__aexit__  s,      jjllr1   rL   )r   r   r~   r   r   r   )r  r   r~   r   )r   r  )&r>   r?   r@   r   rZ   rN   r   r   r   r   r   r   r   r   rx   r  r  r  r  r  r  r  r"  r*  r-  r   r5  r   r   r@  r   rJ  r4  rN  rQ  rS  rU  rX  r[   r1   r0   r   r   I  s        I O- O- O- O- O-b% % % 5 5 5B- - -< < <    L L L. . . .0& & &X "&	D D D D D D, "&I I I I I I.      . 34T > > > > > 48d     " 7;     * !+ + + + +d ( ( ( ( (^ )3 3 3 3 3t     : "& 1 1 1 1 122 2 20 48 $9 $9 $9 $9 $9L+" +" +"Z( ( (    	> 	> 	>6 6 61 1 11 1 1      r1   r   c                  6    e Zd ZdZddZd	 Z	 	 	 dddZd Zd
S )r/  )r~   r   r   rh   rh   r   r~   r   r   rJ   c                >    || _         || _        d | _        d| _        d S )NF)rh   r~   r   r   )r5   rh   r~   s      r0   rN   zPoolAcquireContext.__init__  s"    				r1   c                   K   | j         | j        rt          j        d          | j                            | j                   d {V | _         | j         S )Nz a connection is already acquired)r   r   r   r:   rh   r5  r~   rX   s    r0   rU  zPoolAcquireContext.__aenter__  sZ      ?&$)&+,NOOO $	 2 24< @ @@@@@@@r1   Nexc_typeOptional[Type[BaseException]]exc_valOptional[BaseException]exc_tbOptional[TracebackType]c                t   K   d| _         | j        }d | _        | j                            |           d {V  d S NT)r   r   rh   r   )r5   r\  r^  r`  rH   s        r0   rX  zPoolAcquireContext.__aexit__#  sK       	oi$$$$$$$$$$$r1   c                r    d| _         | j                            | j                                                  S rc  )r   rh   r5  r~   rS  rX   s    r0   rS  zPoolAcquireContext.__await__.  s-    	y!!$,//99;;;r1   )rh   r   r~   r   r   rJ   )NNN)r\  r]  r^  r_  r`  ra  r   rJ   )r>   r?   r@   rZ   rN   rU  rX  rS  r[   r1   r0   r/  r/    so        9I      37+/*.		% 	% 	% 	% 	%< < < < <r1   r/  
   iP  g     r@)r   r   rj   r   r{   rl   r   r   r   r   r   c               4    t          | f|
|||||	|||||d|S )a8  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 connect:
        A coroutine that is called instead of
        :func:`~asyncpg.connection.connect` whenever the pool needs to make a
        new connection.  Must return an instance of type specified by
        *connection_class* or :class:`~asyncpg.connection.Connection` if
        *connection_class* was not specified.

    :param coroutine setup:
        A coroutine to prepare a connection right before it is returned
        from :meth:`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 coroutine reset:
        A coroutine to reset a connection before it is returned to the pool by
        :meth:`Pool.release()`.  The function is supposed
        to reset any changes made to the database session so that the next
        acquirer gets the connection in a well-defined state.

        The default implementation calls :meth:`Connection.reset() <\
        asyncpg.connection.Connection.reset>`, which runs the following::

            SELECT pg_advisory_unlock_all();
            CLOSE ALL;
            UNLISTEN *;
            RESET ALL;

        The exact reset query is determined by detected server capabilities,
        and a custom *reset* implementation can obtain the default query
        by calling :meth:`Connection.get_reset_query() <\
        asyncpg.connection.Connection.get_reset_query>`.

    :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.

    .. versionchanged:: 0.30.0
       Added the *connect* and *reset* parameters.
    )r   r   r   r   rj   r   r{   rl   r   r   r   )r   )r   r   r   rj   r   r{   rl   r   r   r   r   r   r   s                r0   create_poolrg  3  sO    ~ )!)I    r1   rL   )#
__future__r   r   collections.abcr   r   r&   r"   loggingr   typesr   typingr   r   r	   r    r   r   r   r   	getLoggerr>   rE  r   r   _ConnectionProxyrD   rG   r   r/  r   r   rg  r[   r1   r0   <module>rp     sD   # " " " " "  / / / / / / / /              & & & & & & & & & &                          
	8	$	$/ / / / /d / / /dO O O O O*5$;#O O O ODV+ V+ V+ V+ V+ V+ V+ V+rG G G G G G G GT< < < < < < < <@m!16!+!6%_m m m m m m mr1   