
    ۇehP                     R   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ddej        e
j        ddZdS )    N   )compat
connection)
exceptions)protocolc                   :     e Zd Zdd fd
Zed             Z xZS )PoolConnectionProxyMetaF)wrapc                   |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__)
mclsnamebasesdctr   attrnamemethiscoroutinewrapper	__class__s
            >/var/www/api/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    c                 >      fd}|rt          j        |           |S )Nc                     | 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#   )selfargskwargsr    	meth_names       r$   call_con_methodzHPoolConnectionProxyMeta._wrap_connection_method.<locals>.call_con_method0   sr    y  /DDJF!E# E#$ $ $
 49.	::D4	3D333F333r%   )r   markcoroutinefunction)r.   r!   r/   s   `  r$   r   z/PoolConnectionProxyMeta._wrap_connection_method.   s=    		4 		4 		4 		4 		4  	:(999r%   )__name__
__module____qualname__r   staticmethodr   __classcell__)r#   s   @r$   r
   r
      s`        05 7 7 7 7 7 7 7(   \    r%   r
   c                   N    e Zd ZdZdddej        fdZd Zdej        fdZd	 Z	d
S )PoolConnectionProxy)r(   _holderholderPoolConnectionHolderconc                 L    || _         || _        |                    |            d S N)r(   r8   
_set_proxy)r+   r9   r;   s      r$   __init__zPoolConnectionProxy.__init__G   s(    	tr%   c                 ,    t          | j        |          S r=   )r   r(   )r+   attrs     r$   __getattr__zPoolConnectionProxy.__getattr__M   s    ty$'''r%   returnc                 `    | j         d S | j         d c}| _         |                    d            |S r=   )r(   r>   )r+   r;   s     r$   _detachzPoolConnectionProxy._detachQ   s5    9FDTYt
r%   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}>)rG   r;   rH   )r(   r*   r#   r1   rH   r+   s    r$   __repr__zPoolConnectionProxy.__repr__Y   sp    95<<.1bhh = @ @ @ 399.1tyRXX : O O Or%   N)
r1   r2   r3   	__slots__r   r   r?   rB   rE   rJ    r%   r$   r7   r7   A   s         $I5  +   ( ( (.    O O O O Or%   r7   T)	metaclassr   c                   l    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dS )r:   )r(   _pool_loop_proxy_max_queries_setup_max_inactive_time_in_use_inactive_callback_timeout_generationc                    || _         d | _        d | _        || _        || _        || _        d | _        d | _        d | _        d | _	        d S r=   )
rO   r(   rQ   rR   rT   rS   rV   rU   rW   rX   )r+   poolmax_queriessetupmax_inactive_times        r$   r?   zPoolConnectionHolder.__init__j   sR    
	'"3"&r%   c                 H    | j         d uo| j                                          S r=   )r(   	is_closedrI   s    r$   is_connectedz!PoolConnectionHolder.is_connectedx   s%    y$BTY-@-@-B-B)BBr%   c                     | j          S r=   rU   rI   s    r$   is_idlezPoolConnectionHolder.is_idle{   s    <r%   c                    K   | j         t          j        d          | j                                         d {V | _         | j        j        | _        |                                  |                                  d S )NzMPoolConnectionHolder.connect() called while another connection already exists)r(   r   InternalClientErrorrO   _get_new_connectionrX   _maybe_cancel_inactive_callback_setup_inactive_callbackrI   s    r$   connectzPoolConnectionHolder.connect~   s      9 0,- - - *88::::::::	:1,,...%%'''''r%   rC   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)r(   r_   ri   rX   rO   rP   create_taskcloserW   rg   r7   rQ   rS   	ExceptionasyncioCancelledErrorcreate_futurerU   )r+   proxyexs      r$   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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 holderrk   )rU   r   re   r(   r_   rW   	_protocolqueries_countrR   rn   rX   rO   _is_cancellingtime	monotonicr   wait_for_wait_for_cancellation_resetrl   resetro   rp   rq   	terminate_releaserh   )r+   rl   budgetstartedrt   s        r$   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 r=   rb   rI   s    r$   wait_until_releasedz(PoolConnectionHolder.wait_until_released   s3      <F,r%   c                 Z   K   | j         !| j                                          d {V  d S d S r=   )r(   rn   rI   s    r$   rn   zPoolConnectionHolder.close   sB      9  )//########### ! r%   c                 J    | j         | j                                          d S d S r=   )r(   r   rI   s    r$   r   zPoolConnectionHolder.terminate   s.    9  I!!!!! ! r%   c                     | j         t          j        d          | j        r1| j        j                            | j        | j                  | _         d S d S )Nz/pool connection inactivity timer already exists)rV   r   re   rT   rO   rP   
call_later_deactivate_inactive_connectionrI   s    r$   rh   z-PoolConnectionHolder._setup_inactive_callback   sp    ".0AC C C " 	O&*j&6&A&A')M'O 'OD###	O 	Or%   c                 X    | j         "| j                                          d | _         d S d S r=   )rV   cancelrI   s    r$   rg   z4PoolConnectionHolder._maybe_cancel_inactive_callback  s6    ".#**,,,&*D### /.r%   c                     | j         t          j        d          | j        /| j                                         |                                  d S d S )Nz/attempting to deactivate an acquired connection)rU   r   re   r(   r   _release_on_closerI   s    r$   r   z4PoolConnectionHolder._deactivate_inactive_connection  sg    <#0AC C C 9  I!!! ""$$$$$ ! r%   c                 d    |                                   |                                  d | _        d S r=   )rg   r   r(   rI   s    r$   r   z&PoolConnectionHolder._release_on_close  s,    ,,...			r%   c                    | j         dS | j                                         s| j                             d           d| _         | j         | j                                         d| _        | j        j                            |            dS )zRelease this connection holder.N)rU   done
set_resultrQ   rE   rO   _queue
put_nowaitrI   s    r$   r   zPoolConnectionHolder._release   s    <F|  "" 	*L##D))) ;"K!!!DK 	
$$T*****r%   N)r1   r2   r3   rK   r?   r`   rc   ri   r7   ru   r   r   rn   r   rh   rg   r   r   r   rL   r%   r$   r:   r:   b   s        I     C C C     	( 	( 	(#2 # # # #J=( =( =(~  $ $ $" " "O O O+ + +
% % %  
+ + + + +r%   r:   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d2d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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&d0 Z'd1 Z(dS )3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   rP   _minsize_maxsize_init_connectr~   _connect_args_connect_kwargs_holders_initialized_initializing_closing_closed_connection_class_record_classrX   rS   rR   !_max_inactive_connection_lifetimeN)ri   r\   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DeprecationWarningrp   get_event_looprP   
ValueError
issubclassr   r   	TypeErrorr*   r   Recordr   r   r   r   r   r   r   r   r   r   rX   ri   r   r   r   rS   r   r~   rR   r   )r+   min_sizemax_sizer[    max_inactive_connection_lifetimeri   r\   r   r   loopconnection_classrecord_classconnect_argsconnect_kwargss                 r$   r?   zPool.__init__G  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)-
', 	...r%   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   _initializerI   s    r$   _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)r[   r]   r\   r   )rp   	LifoQueuer   r   ranger:   rR   r   rS   r   appendr   r   ri   	enumeratereversedgather)r+   r   chfirst_chconnect_tasksis         r$   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 ! r%   c                     | j         p| j        S )z_Return ``True`` if the pool is closing or is closed.

        .. versionadded:: 0.28.0
        )r   r   rI   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   | ]}|                                 V  d S r=   )r`   .0hs     r$   	<genexpr>z Pool.get_size.<locals>.<genexpr>  s,      ;;1>>##;;;;;;r%   sumr   rI   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   rI   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   rI   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   f   K   | ],}|                                 o|                                V  -d S r=   )r`   rc   r   s     r$   r   z%Pool.get_idle_size.<locals>.<genexpr>  s9      KK1>>##3		KKKKKKr%   r   rI   s    r$   get_idle_sizezPool.get_idle_size  s#    
 KKT]KKKKKKr%   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   )r+   dsnr   s      r$   set_connect_argszPool.set_connect_args  s    * "U-r%   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   rP   r   r   r   
isinstancer2   r1   typer   r)   r   ro   rp   rq   rn   )r+   r;   goodgood_nbadbad_nrt   s          r$   rf   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rk   queryrl   rC   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
        Nrl   )ru   execute)r+   r   rl   r,   r;   s        r$   r   zPool.execute,  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
        Nrk   )ru   executemany)r+   r   r,   rl   r;   s        r$   r   zPool.executemany8  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rl   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 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   )ru   fetchr+   r   rl   r   r,   r;   s         r$   r   z
Pool.fetchE  s"      <<>> 	 	 	 	 	 	 	S"   )	        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   A
AAr   columnrl   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   )ru   fetchval)r+   r   r   rl   r,   r;   s         r$   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   )ru   fetchrowr   s         r$   r   zPool.fetchrowj  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   )ru   	fetchmany)r+   r   r,   rl   r   r;   s         r$   r   zPool.fetchmany{  s      <<>> 	 	 	 	 	 	 	StW< '        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A
AA)columnsschema_namerl   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   rl   r*   r  r  r  r  r  r  r  r  )ru   copy_from_table)r+   
table_namer
  r   r   rl   r*   r  r  r  r  r  r  r  r  r;   s                   r$   r  zPool.copy_from_table  s1     6 <<>> 	 	 	 	 	 	 	S,,'#'! -        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   )A
A#&A#)
rl   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
  rl   r*   r  r  r  r  r  r  r  r  )ru   copy_from_query)r+   r   r
  rl   r*   r  r  r  r  r  r  r  r  r,   r;   s                  r$   r  zPool.copy_from_query  s>     2 <<>> 	 	 	 	 	 	 	S,,  #'!        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   !A
AA)r   r   rl   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   rl   r*   r  r  r  r  r  r  r  r  r  r  r  r  )ru   copy_to_table)r+   r  r  r   r   rl   r*   r  r  r  r  r  r  r  r  r  r  r  r  r;   s                       r$   r  zPool.copy_to_table  s=     > <<>> 	 	 	 	 	 	 	S**'#'-%!% +        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   -A
A'*A')r   r   rl   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   rl   r  )ru   copy_records_to_table)r+   r  r  r   r   rl   r  r;   s           r$   r  zPool.copy_records_to_table  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)r+   rl   s     r$   ru   zPool.acquire4  s    . "$000r%   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r=   )r   getru   rW   ro   rp   rq   r   )r   rs   r+   rl   s     r$   _acquire_implz$Pool._acquire.<locals>._acquire_implN  s      {((((((((B	 jjll****** & w56   &&r***s   A 2A9zpool is closingrk   )r   r   r)   _check_initr   r|   )r+   rl   r  s   `` r$   _acquirezPool._acquireM  s      	 	 	 	 	 	 = 	?+,=>>>?&(((((((2 2 2 2 2 2 2 2 2 2r%   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   r7   r8   rO   r   r)   r*   r(   r   _on_releaserW   rp   shieldr   )r+   r   rl   r   s       r$   r   zPool.releasee  s       $777"(44+>>Df) ?E ?+ ?+, , ,
 ?"F 	##%%%?kG
 ^BJJw$7$7888888888r%   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 rL   )r   r   r   s     r$   
<listcomp>zPool.close.<locals>.<listcomp>  s7     B B B-/&&((B B Br%   c                 6    g | ]}|                                 S rL   rn   r(  s     r$   r)  zPool.close.<locals>.<listcomp>  s-     4 4 4!

4 4 4r%   F)r   r   r   rP   r   _warn_on_long_closer   rp   r   ro   rq   r   r   )r+   warning_callbackrelease_corosclose_coross       r$   rn   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warningrI   s    r$   r,  zPool._warn_on_long_close  s'     ' 	( 	( 	( 	( 	(r%   c                     | j         rdS |                                  | j        D ]}|                                 d| _         dS )z&Terminate all connections in the pool.NT)r   r   r   r   r+   r   s     r$   r   zPool.terminate  sN    < 	F- 	 	BLLNNNNr%   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)rX   rI   s    r$   expire_connectionszPool.expire_connections  s"       	Ar%   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   rI   s    r$   r   zPool._check_init  sm      	G!   /      +,EFFF< 	>+,<===	> 	>r%   c                 Z    | j         D ]"}|j        |j                                         #d S r=   )r   r(   _drop_local_statement_cacher4  s     r$   _drop_statement_cachezPool._drop_statement_cache  s:    - 	6 	6Bw"33555	6 	6r%   c                 Z    | j         D ]"}|j        |j                                         #d S r=   )r   r(   _drop_local_type_cacher4  s     r$   _drop_type_cachezPool._drop_type_cache  s:    - 	1 	1Bw"..000	1 	1r%   c                 N    |                                                                  S r=   )r   	__await__rI   s    r$   r?  zPool.__await__  s     ""$$..000r%   c                 >   K   |                                   d {V  | S r=   )r   rI   s    r$   
__aenter__zPool.__aenter__  s/      !!#########r%   c                 >   K   |                                   d {V  d S r=   r+  )r+   excs     r$   	__aexit__zPool.__aexit__  s,      jjllr%   r=   ))r1   r2   r3   r   rK   r?   r   r   r   r   r   r   r   r   rf   strfloatr   r   listr   r   r   r   r  r  r  r  ru   r!  r   rn   r,  r   r6  r   r:  r=  r?  rA  rD  rL   r%   r$   r   r   4  s        I O- O- O- O- O-b% % % 5 5 5B- - -< < <    L L L. . . .0& & &P ?C 
D 
D 
D3 
D 
Ds 
D 
D 
D 
D GK I I 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      r%   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )r  )rl   r   r   rZ   c                 >    || _         || _        d | _        d| _        d S )NF)rZ   rl   r   r   )r+   rZ   rl   s      r$   r?   zPoolAcquireContext.__init__  s"    				r%   c                    K   | j         | j        rt          j        d          | j                            | j                   d {V | _         | j         S )Nz a connection is already acquired)r   r   r   r)   rZ   r!  rl   rI   s    r$   rA  zPoolAcquireContext.__aenter__  sZ      ?&$)&+,NOOO $	 2 24< @ @@@@@@@r%   c                 t   K   d| _         | j        }d | _        | j                            |           d {V  d S NT)r   r   rZ   r   )r+   rC  r;   s      r$   rD  zPoolAcquireContext.__aexit__  sI      	oi$$$$$$$$$$$r%   c                 r    d| _         | j                            | j                                                  S rL  )r   rZ   r!  rl   r?  rI   s    r$   r?  zPoolAcquireContext.__await__	  s-    	y!!$,//99;;;r%   N)r1   r2   r3   rK   r?   rA  rD  r?  rL   r%   r$   r  r    sU        9I    % % %< < < < <r%   r  
   iP  g     r@)r   r   r[   r   ri   r\   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   r[   r   ri   r\   r   r   r   )r   )r   r   r   r[   r   ri   r\   r   r   r   r   r   r   s                r$   create_poolrP    sO    ~ )!)I    r%   r=   )rp   r   r   loggingrz   r    r   r   r   r   	getLoggerr1   r1  r   r
   _ConnectionProxyr7   r:   r   r  r   r   rP  rL   r%   r$   <module>rU     s                                    
	8	$	$& & & & &d & & &RO O O O O*5$;#O O O OBO+ O+ O+ O+ O+ O+ O+ O+d|
 |
 |
 |
 |
 |
 |
 |
~< < < < < < < <6m!16!+!6%_m m m m m m mr%   