
    *Jf                        U d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZmZmZmZmZmZmZ ddlmZmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddl m!Z! ddl m"Z# ddl m$Z$ ddl m%Z% ddl&m'Z'm(Z(m)Z)m*Z* ddl&m+Z+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z?m@Z@ ddlAmBZB ddlCmDZDmEZE ddlCmFZFmGZGmHZH ddlImJZJmKZK ddlLmMZMmNZNmOZO ddlPmQZQ dd lRmSZS dd!lTmUZU dd"lVmWZW dd#lXmYZY erdd$lZm[Z[m\Z\ dd%l]m^Z^  ed&          Z_e!j`        ja        Zae!j`        jb        Zbe!jc        jd        Zde!jc        je        Zee!jf        jg        Zge!jf        jh        Zhe!jf        ji        Zie!jj        jk        Zke!jj        jl        Zl ejm        d'          Zn G d( d)e          Zod'eo_p        ee#jq        gdf         Zreesd*<   eeogdf         Zteesd+<    G d, d-e
e3                   Zu G d. d/eue3                   ZvdS )0z
psycopg connection objects
    N)TracebackType)AnyCallablecast	GeneratorGenericIterator)List
NamedTupleOptionalTypeTypeVarTupleUnion)overloadTYPE_CHECKING)refReferenceType)warn)partial)contextmanager)	TypeAlias   )pq)errors)waiting)postgres)AdaptContextParamsQueryRV)PQGen	PQGenConn)
ComposableSQL)Xid)Row
RowFactory	tuple_rowTupleRowargs_row)AdaptersMap)IsolationLevel)Cursor)LiteralStringSelf)connection_summary)make_conninfoconninfo_to_dict)conninfo_attemptsConnDicttimeout_from_conninfo)BasePipelinePipeline)notifiesconnectexecute)pgconn_encoding)PrepareManager)Transaction)ServerCursor)ConnectionInfo)PGconnPGresult)BasePool	CursorRowpsycopgc                   6    e Zd ZU dZeed<   	 eed<   	 eed<   dS )Notifyz8An asynchronous notification received from the database.channelpayloadpidN)__name__
__module____qualname____doc__str__annotations__int     U/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/psycopg/connection.pyrG   rG   D   s9         BBLLLILLL3	HHHEErS   rG   NoticeHandlerNotifyHandlerc            	          e Zd ZdZej        Zej        Zej        Zej        Zej	        Z	ej
        Z
ej        Zej        Zej        Zej        Zej        Zej        ZdJdZdKdZdefdZedefd	            Zedefd
            Zedefd            Zej        deddfd            ZdeddfdZdeded         fdZedee          fd            Z!e!j        dee          ddfd            Z!dee          ddfdZ"dee          ded         fdZ#edee         fd            Z$e$j        dee         ddfd            Z$dee         ddfdZ%dee         ded         fdZ&edee         fd            Z'e'j        dee         ddfd            Z'dee         ddfdZ(dee         ded         fdZ)deded         fdZ*ede+fd            Z,ede-fd            Z.edLd!            Z/de0fd"Z1dKd#Z2e3dMd$            Z4d%e5ddfd&Z6d%e5ddfd'Z7e8	 	 	 	 	 	 dNd,            Z9d%e:ddfd-Z;d%e:ddfd.Z<e8d(d)d/ej=        ddfd0            Z>edee0         fd1            Z?e?j        dee0         ddfd2            Z?ede0fd3            Z@e@j        de0ddfd4            Z@e3dOd6edeAeB         fd7            ZCeDfd8eEd9ejF        deed+                  fd:ZGdKd;ZHded         fd<ZIdeJfd=ZKded         fd>ZLded         fd?ZMd@e0dAedBedeNfdCZOdDePeNef         ded         fdEZQded         fdFZRdGeSdDePeNedf         ded         fdHZTdKdIZUdS )PBaseConnectionz
    Base class for different types of connections.

    Share common functionalities such as access to the wrapped PGconn, but
    allow different interfaces (sync/async).
    pgconnrA   c                    || _         d| _        d | _        g | _        g | _        d| _        d| _        t                      | _        d | _	        t          |           }t          t          j        |          |_        t          t          j        |          |_        |  d | _        |  d | _        d | _        d | _        d| _        d S )NFr   rS   )rY   _autocommit	_adapters_notice_handlers_notify_handlers_num_transactions_closedr=   	_prepared_tpcr   r   rX   _notice_handlernotice_handler_notify_handlernotify_handler	_pipeline_isolation_level
_read_only_deferrable_begin_statement)selfrY   wselfs      rT   __init__zBaseConnection.__init__o   s      155757 "#)7)9)904	D		 '(F N N '(F N N 	-15 	:>*.+/ #rS   returnNc                     t          | d          sd S | j        rd S t          | d          rd S t          d|  dt                     d S )NrY   _poolzconnection zV was deleted while still open. Please use 'with' or '.close()' to close the connection)hasattrclosedr   ResourceWarningrl   s    rT   __del__zBaseConnection.__del__   s|    tX&& 	F ; 	F 4!! 	FG$ G G G	
 	
 	
 	
 	
rS   c                     | j         j         d| j         j         }t          | j                  }d| d| dt          |           ddS )N.< z at 0xx>)	__class__rL   rM   r1   rY   id)rl   clsinfos      rT   __repr__zBaseConnection.__repr__   sX    *JJT^-HJJ!$+..232222RXX22222rS   c                 ,    | j         j        t          k    S )z$`!True` if the connection is closed.)rY   statusBADru   s    rT   rs   zBaseConnection.closed   s     {!S((rS   c                 <    | j         j        t          k    o| j         S )z
        `!True` if the connection was interrupted.

        A broken connection is always `closed`, but wasn't closed in a clean
        way, such as using `close()` or a `!with` block.
        )rY   r   r   r`   ru   s    rT   brokenzBaseConnection.broken   s     {!S(=-==rS   c                     | j         S )z'The autocommit state of the connection.)r[   ru   s    rT   
autocommitzBaseConnection.autocommit   s     rS   valuec                 0    |                      |           d S N)_set_autocommitrl   r   s     rT   r   zBaseConnection.autocommit       U#####rS   c                     t           r   NotImplementedErrorr   s     rT   r   zBaseConnection._set_autocommit       !!rS   c              #   h   K   |                      d          E d {V  t          |          | _        d S )Nr   )_check_intrans_genboolr[   r   s     rT   _set_autocommit_genz"BaseConnection._set_autocommit_gen   s@      **<888888888;;rS   c                     | j         S )zX
        The isolation level of the new transactions started on the connection.
        )rh   ru   s    rT   isolation_levelzBaseConnection.isolation_level   s    
 $$rS   c                 0    |                      |           d S r   )_set_isolation_levelr   s     rT   r   zBaseConnection.isolation_level   s    !!%(((((rS   c                     t           r   r   r   s     rT   r   z#BaseConnection._set_isolation_level   r   rS   c              #   ~   K   |                      d          E d {V  |t          |          nd | _        d| _        d S )Nr   rS   )r   r-   rh   rk   r   s     rT   _set_isolation_level_genz'BaseConnection._set_isolation_level_gen   sV      **+<=========9>9Ju 5 5 5PT #rS   c                     | j         S )zX
        The read-only state of the new transactions started on the connection.
        )ri   ru   s    rT   	read_onlyzBaseConnection.read_only   s    
 rS   c                 0    |                      |           d S r   )_set_read_onlyr   s     rT   r   zBaseConnection.read_only   s    E"""""rS   c                     t           r   r   r   s     rT   r   zBaseConnection._set_read_only   r   rS   c              #   ~   K   |                      d          E d {V  |t          |          nd | _        d| _        d S )Nr   rS   )r   r   ri   rk   r   s     rT   _set_read_only_genz!BaseConnection._set_read_only_gen   sP      **;777777777).):$u+++ #rS   c                     | j         S )zY
        The deferrable state of the new transactions started on the connection.
        )rj   ru   s    rT   
deferrablezBaseConnection.deferrable   s    
 rS   c                 0    |                      |           d S r   )_set_deferrabler   s     rT   r   zBaseConnection.deferrable   r   rS   c                     t           r   r   r   s     rT   r   zBaseConnection._set_deferrable   r   rS   c              #   ~   K   |                      d          E d {V  |t          |          nd | _        d| _        d S )Nr   rS   )r   r   rj   rk   r   s     rT   _set_deferrable_genz"BaseConnection._set_deferrable_gen   sQ      **<888888888*/*;4;;; #rS   	attributec              #   R  K   | j         j        }|t          k    r2| j        r+| j                                        E d {V  | j         j        }|t          k    rP| j        rt          j        d|d          t          j        d|dt          j	        |          j
                   d S )Nzcan't change z2 now: connection.transaction() context in progressz' now: connection in transaction status )rY   transaction_statusIDLErg   	_sync_genr_   eProgrammingErrorr   TransactionStatusname)rl   r   r   s      rT   r   z!BaseConnection._check_intrans_gen  s      /T>>dn>~//111111111[3FT>>% 
(CI C C C  
 (;I ; ;+F338; ;   >rS   c                 *    t          | j                  S )z>A `ConnectionInfo` attribute to inspect connection properties.)r@   rY   ru   s    rT   r   zBaseConnection.info  s     dk***rS   c                 Z    | j         st          t          j                  | _         | j         S r   )r\   r,   r   adaptersru   s    rT   r   zBaseConnection.adapters  s&    ~ 	<():;;DN~rS   BaseConnection[Row]c                     | S r   rR   ru   s    rT   
connectionzBaseConnection.connection!  s	     rS   c                     | j         j        S )zReturn the file descriptor of the connection.

        This function allows to use the connection as file-like object in
        functions waiting for readiness, such as the ones defined in the
        `selectors` module.
        )rY   socketru   s    rT   filenozBaseConnection.fileno&  s     {!!rS   c                     | j         rdS | j        r!| j        d         rt          j        d          |                     | j                   dS )z/Cancel the current operation on the connection.Nr   z=cancel() cannot be used with a prepared two-phase transaction)rs   rb   r   r   _try_cancelrY   ru   s    rT   cancelzBaseConnection.cancel/  s`    
 ; 	F9 	1 	$O   	%%%%%rS   c                     	 |                                 }|                                 d S # t          $ r&}t                              d|           Y d }~d S d }~ww xY w)Nz couldn't try to cancel query: %s)
get_cancelr   	Exceptionloggerwarning)r   rY   cexs       rT   r   zBaseConnection._try_cancel>  sw    	!!##A HHJJJJJ  	C 	C 	CNN=rBBBBBBBBB	Cs   , 
AAAcallbackc                 :    | j                             |           dS )z
        Register a callable to be invoked when a notice message is received.

        :param callback: the callback to call upon message received.
        :type callback: Callable[[~psycopg.errors.Diagnostic], None]
        N)r]   appendrl   r   s     rT   add_notice_handlerz!BaseConnection.add_notice_handlerH  !     	$$X.....rS   c                 :    | j                             |           dS )z
        Unregister a notice message callable previously registered.

        :param callback: the callback to remove.
        :type callback: Callable[[~psycopg.errors.Diagnostic], None]
        N)r]   remover   s     rT   remove_notice_handlerz$BaseConnection.remove_notice_handlerQ  r   rS   rm   "ReferenceType[BaseConnection[Row]]resrB   c                     |             }|r|j         sd S t          j        |t          |j                            }|j         D ]A}	  ||           # t
          $ r&}t                              d||           Y d }~:d }~ww xY wd S )Nz)error processing notice callback '%s': %s)r]   r   
Diagnosticr<   rY   r   r   	exception)rm   r   rl   diagcbr   s         rT   rc   zBaseConnection._notice_handlerZ  s     uww 	. 	F|C!=!=>>' 	V 	VBV4 V V V  !LbRTUUUUUUUUV	V 	Vs   A
BA>>Bc                 :    | j                             |           dS )z
        Register a callable to be invoked whenever a notification is received.

        :param callback: the callback to call upon notification received.
        :type callback: Callable[[~psycopg.Notify], None]
        N)r^   r   r   s     rT   add_notify_handlerz!BaseConnection.add_notify_handleri  r   rS   c                 :    | j                             |           dS )z
        Unregister a notification callable previously registered.

        :param callback: the callback to remove.
        :type callback: Callable[[~psycopg.Notify], None]
        N)r^   r   r   s     rT   remove_notify_handlerz$BaseConnection.remove_notify_handlerr  r   rS   pgnc                     |             }|r|j         sd S t          |j                  }t          |j                            |          |j                            |          |j                  }|j         D ]} ||           d S r   )r^   r<   rY   rG   relnamedecodeextrabe_pid)rm   r   rl   encnr   s         rT   re   zBaseConnection._notify_handler{  s     uww 	. 	Fdk**3;%%c**CI,<,<S,A,A3:NN' 	 	BBqEEEE	 	rS   c                     | j         j        S )a&  
        Number of times a query is executed before it is prepared.

        - If it is set to 0, every query is prepared the first time it is
          executed.
        - If it is set to `!None`, prepared statements are disabled on the
          connection.

        Default value: 5
        ra   prepare_thresholdru   s    rT   r   z BaseConnection.prepare_threshold  s     ~//rS   c                     || j         _        d S r   r   r   s     rT   r   z BaseConnection.prepare_threshold  s    +0(((rS   c                     | j         j        S )zf
        Maximum number of prepared statements on the connection.

        Default value: 100
        ra   prepared_maxru   s    rT   r   zBaseConnection.prepared_max  s     ~**rS   c                     || j         _        d S r   r   r   s     rT   r   zBaseConnection.prepared_max  s    &+###rS    conninfoc              #   J   K   t          |          E d{V } | |          }|S )z?Generator to connect to the database and create a new instance.N)r:   )r   r   rY   conns       rT   _connect_genzBaseConnection._connect_gen  s9       $H--------s6{{rS   commandresult_formatc              #     K   |                                   t          |t                    r(|                    t	          | j                            }n*t          |t                    r|                    |           }| j        r]t          | j        j
        |d|          }| j        j                            |           | j        j                            d           dS | j        
                    |d|           t          | j                  E d{V d         }|j        t           k    r|j        t"          k    r|j        t$          k    r(t'          j        |t	          | j                            t'          j        dt-          j        |j                  j         d|                                          |S )z
        Generator to send a command and receive the result to the backend.

        Only used to implement internal commands such as "commit", with eventual
        arguments bound client-side. The cursor can do more complex stuff.
        N)r   )encodingzunexpected result z from command )_check_connection_ok
isinstancerO   encoder<   rY   r$   as_bytesrg   r   send_query_paramscommand_queuer   result_queuer;   r   
COMMAND_OK	TUPLES_OKFATAL_ERRORr   error_from_resultInterfaceErrorr   
ExecStatusr   r   )rl   r   r   cmdresults        rT   _exec_commandzBaseConnection._exec_command  s      	!!###gs## 	-nn_T[%A%ABBGG,, 	-&&t,,G> 		-+	  C N(//444N'..t4444%%gt=%QQQ$T[1111111126=J&&6=I+E+E}++)&?4;;W;WXXXX&:v})E)E)J : :%,^^%5%5: :   rS   c                     | j         j        t          k    rd S | j         j        t          k    rt	          j        d          t	          j        d| j         j                   )Nzthe connection is closedz7cannot execute operations: the connection is in status )rY   r   OKr   r   OperationalErrorr  ru   s    rT   r   z#BaseConnection._check_connection_ok  se    ;##F;$$$%?@@@/+,/ /
 
 	
rS   c              #      K   | j         rdS | j        j        t          k    rdS |                     |                                           E d{V  | j        r!| j                                        E d{V  dS dS )z.Generator to start a transaction if necessary.N)r[   rY   r   r   r  _get_tx_start_commandrg   r   ru   s    rT   _start_queryzBaseConnection._start_query  s       	F;)T11F%%d&@&@&B&BCCCCCCCCC> 	2~//11111111111	2 	2rS   c                    | j         r| j         S dg}| j        it          | j                  }|                    d           |                    |j                            dd                                                     | j        |                    | j        rdnd           | j        |                    | j        rdnd           d		                    |          | _         | j         S )
Ns   BEGINs   ISOLATION LEVEL_rz   s	   READ ONLYs
   READ WRITEs
   DEFERRABLEs   NOT DEFERRABLE    )
rk   r   r-   r   r   replacer   r   r   join)rl   partsvals      rT   r
  z$BaseConnection._get_tx_start_command  s      	)((
+ !566CLL+,,,LL))#s33::<<===>%LLJ]KKK?&LL$/P?PQQQ $		% 0 0$$rS   c              #   *  K   | j         rt          j        d          | j        rt          j        d          | j        j        t          k    rdS |                     d          E d{V  | j        r!| j        	                                E d{V  dS dS )z-Generator implementing `Connection.commit()`.zExplicit commit() forbidden within a Transaction context. (Transaction will be automatically committed on successful exit from context.)z6commit() cannot be used during a two-phase transactionNs   COMMIT)
r_   r   r   rb   rY   r   r   r  rg   r   ru   s    rT   _commit_genzBaseConnection._commit_gen  s      ! 	$4  
 9 	$H   ;)T11F%%i000000000> 	2~//11111111111	2 	2rS   c              #     K   | j         rt          j        d          | j        rt          j        d          | j        r| j                                        E d{V  | j        j        t          k    rdS | 	                    d          E d{V  | j
                                         | j
                                        D ]}| 	                    |          E d{V  | j        r!| j                                        E d{V  dS dS )z/Generator implementing `Connection.rollback()`.zExplicit rollback() forbidden within a Transaction context. (Either raise Rollback() or allow an exception to propagate out of the context.)z8rollback() cannot be used during a two-phase transactionNs   ROLLBACK)r_   r   r   rb   rg   r   rY   r   r   r  ra   clearget_maintenance_commands)rl   r  s     rT   _rollback_genzBaseConnection._rollback_gen  sW     ! 	$A  
 9 	$J  
 > 	2~//111111111;)T11F%%k222222222>::<< 	/ 	/C))#..........> 	2~//11111111111	2 	2rS   	format_idgtridbqualc                 V    |                                   t          j        |||          S )aL  
        Returns a `Xid` to pass to the `!tpc_*()` methods of this connection.

        The argument types and constraints are explained in
        :ref:`two-phase-commit`.

        The values passed to the method will be available on the returned
        object as the members `~Xid.format_id`, `~Xid.gtrid`, `~Xid.bqual`.
        )
_check_tpcr&   
from_parts)rl   r  r  r  s       rT   xidzBaseConnection.xid;  s(     	~i666rS   r  c              #     K   |                                   t          |t                    st          j        |          }| j        j        t          k    r8t          j        dt          j
        | j        j                  j                   | j        rt          j        d          |df| _        |                     |                                           E d {V  d S )Nz8can't start two-phase transaction: connection in status z3can't use two-phase transactions in autocommit modeF)r  r   r&   from_stringrY   r   r   r   r   r   r   r   r[   rb   r  r
  rl   r  s     rT   _tpc_begin_genzBaseConnection._tpc_begin_genH  s      #s## 	'/#&&C;)T11$P()GHHMP P  
  	$E   %L	%%d&@&@&B&BCCCCCCCCCCCrS   c              #     K   | j         st          j        d          | j         d         rt          j        d          | j         d         }|df| _         |                     t	          d                              t          |                              E d {V  | j        r!| j                                        E d {V  d S d S )Nz='tpc_prepare()' must be called inside a two-phase transactionr   zF'tpc_prepare()' cannot be used during a prepared two-phase transactionr   TzPREPARE TRANSACTION {})	rb   r   r   r  r%   formatrO   rg   r   r"  s     rT   _tpc_prepare_genzBaseConnection._tpc_prepare_gen\  s      y 	$O   9Q< 	$X   il$K	%%c*B&C&C&J&J3s88&T&TUUUUUUUUU> 	2~//11111111111	2 	2rS   actionc              #     K   d|                                  d}|,| j        st          j        | d          | j        d         }nG| j        rt          j        | d          t	          |t
                    st          j        |          }| j        rL| j        d         s?t          | d|                                  d          }d | _         |            E d {V  d S |                     t          d	          
                    t          |          t          |                              E d {V  d | _        d S )
Ntpc_z()z? without xid must must be called inside a two-phase transactionr   z= with xid must must be called outside a two-phase transactionr   r  _genz{} PREPARED {})lowerrb   r   r   r   r&   r!  getattrr  r%   r%  rO   )rl   r'  r  fnamemeths        rT   _tpc_finish_genzBaseConnection._tpc_finish_genk  s      *v||~~)));9 ( = = =   )A,CCy ( 7 7 7   c3'' +oc**9 		TYq\ 		4!9V\\^^!9!9!9::DDItvv))$%%,,S[[#c((CC         DIIIrS   c                     dS )z0Raise NotSupportedError if TPC is not supported.NrR   ru   s    rT   r  zBaseConnection._check_tpc  s	     	rS   )rY   rA   ro   N)ro   r   )rY   rA   ro   N)rm   r   r   rB   ro   Nr   )VrK   rL   rM   rN   r   WarningErrorr  DatabaseError	DataErrorr  IntegrityErrorInternalErrorr   NotSupportedErrorr   
ConnStatusr   rn   rv   rO   r   propertyr   rs   r   r   setterr   r"   r   r   r-   r   r   r   r   r   r   r   r   r   r   r@   r   r,   r   r   rQ   r   r   classmethodr   rU   r   r   staticmethodrc   rV   r   r   PGnotifyre   r   r   r#   r0   r   TEXTr    Formatr  r   r  bytesr
  r  r  r&   r  r   r#  r&  r/   r/  r  rR   rS   rT   rX   rX   W   sA         iGGE%NOMI)%NOM)+ J,!$ !$ !$ !$F
 
 
 
&3# 3 3 3 3
 ) ) ) ) X) > > > > X>  D       X  $ $ $ $ $ $"T "d " " " "' '%+ ' ' ' ' %.!9 % % % X% )Xn%= )$ ) ) ) )"(>*B "t " " " "$h~.F $5QU; $ $ $ $
 8D>    X #x~ #$ # # # #"HTN "t " " " "$ $5; $ $ $ $
  HTN       X  $ $4 $ $ $ $"Xd^ " " " " "$$ $E$K $ $ $ $
C E$K    & +n + + + X+ +    X    X" " " " "& & & &    [/= /T / / / //m / / / / / V3V:DV	V V V \V/= /T / / / //m / / / / / 
3
:<+
	
 
 
 \
 08C= 0 0 0 X0 1x} 1 1 1 1 1 +c + + + X+ ,# ,$ , , , ,  C 4    [ :>& &&-/Y&	x
#	$& & & &P	
 	
 	
 	

2eDk 
2 
2 
2 
2%u % % % %(2U4[ 2 2 2 2(2uT{ 2 2 2 287S 7 7S 7S 7 7 7 7D%S/ DeDk D D D D(2%+ 2 2 2 2#*/S$*?	t   >     rS   rX   c                       e Zd ZU dZdZeee                  ed<   eee                  ed<   e	e         ed<   e
e         ed<    ee	e         e          fddde	e         f fd	Zee	 dJddddddedede	e         de
e         de
eee                           de
e         dedeef         ddfd                        Zee	 dJddddddedede
e         de
eee                           de
e         dedeef         ddfd                        Ze	 dJdddddddedede
e         de
e	e                  de
eee                           de
e         dedefd            ZdefdZde
ee                  de
e         de
e         ddfdZedededefd             ZdKd!Zedd"d#edee         fd$            Z edd"d#ede	e!         dee!         fd%            Z edddd&d'ed#ed(e
e         d)edee         f
d*            Z edddd&d'ed#ede	e!         d(e
e         d)edee!         fd+            Z 	 dJddddd,d'ed#ede
e	e                  d(e
e         d)edeee         ee         f         fd-Z 	 dLddd.d/e"d0e
e#         d1e
e         d#edee         f
d2Z$dKd3Z%dKd4Z&e'	 	 dMd5e
e         d6ede(e)         fd7            Z*de+e,ddf         fd8Z-e'de(e         fd9            Z.dNd;e/e0         d<e
e1         de0fd=Z2ed;e3e0         d<e
e         de0fd>            Z4d?eddfd@Z5d?e
e6         ddfdAZ7d?e
e         ddfdBZ8d?e
e         ddfdCZ9dDee:ef         ddfdEZ;dKdFZ<dLdDee:edf         ddfdGZ=dLdDee:edf         ddfdHZ>de?e:         fdIZ@ xZAS )O
Connectionz3
    Wrapper for a connection to the database.
    rE   cursor_factoryserver_cursor_factoryrow_factoryrg   rY   rA   c                     t                                          |           || _        t          j                    | _        t          | _        t          | _	        d S r   )
superrn   rG  	threadingLocklockr.   rE  r?   rF  )rl   rY   rG  r}   s      rT   rn   zConnection.__init__  sJ    
 	   &N$$	$%1"""rS   r   F   N)r   r   rE  contextr   r   r   rN  kwargsro   zConnection[Row]c                    d S r   rR   )r   r   r   rG  r   rE  rN  rO  s           rT   r:   zConnection.connect  s	      	rS   zConnection[TupleRow]c                    d S r   rR   )r   r   r   r   rE  rN  rO  s          rT   r:   zConnection.connect  s	     "%rS   )r   r   rG  rE  rN  c                    | j         |fi |}t          |          }	d}
t          |          }|D ]}	 t          di |}|                     |                     |          |	          }
 n# t          j        $ r}t          |          dk    rdt          
                    d|                    d          |                    d          |                    d          t          |                     |}Y d}~d}~ww xY w|
s|sJ |                    d          t          |          |
_        |r||
_        |r||
_        |rt%          |j                  |
_        ||
_        |
S )	zV
        Connect to a database server and return a new `Connection` instance.
        Ntimeoutr   zAconnection attempt failed on host: %r, port: %r, hostaddr: %r: %shostporthostaddrrR   )_get_connection_paramsr6   r4   r2   
_wait_connr   r   _NO_TRACEBACKlenr   debuggetrO   with_tracebackr   r[   rG  rE  r,   r   r\   r   )r   r   r   r   rG  rE  rN  rO  paramsrT  rvattemptsattemptr   last_exs                  rT   r:   zConnection.connect  s    ,+H????'//$V,, 	 	G(33733^^C$4$4X$>$>^PP? 
 
 
x==1$$LL,F++F++J//B   
  	/NNN((...j)) 	)(BN 	/ .B 	9&w'788BL0	s   6A,,C>;A9C99C>c                     | S r   rR   ru   s    rT   	__enter__zConnection.__enter__  s    rS   exc_typeexc_valexc_tbc                 $   | j         rd S |rI	 |                                  nG# t          $ r&}t                              d| |           Y d }~nd }~ww xY w|                                  t          | dd           s|                                  d S d S )Nz#error ignored in rollback on %s: %srq   )rs   rollbackr   r   r   commitr,  close)rl   rf  rg  rh  exc2s        rT   __exit__zConnection.__exit__  s     ; 	F 	   9        KKMMM tWd++ 	JJLLLLL	 	s   " 
AAAc                     t          |fi |S )am  Manipulate connection parameters before connecting.

        :param conninfo: Connection string as received by `~Connection.connect()`.
        :param kwargs: Overriding connection arguments as received by `!connect()`.
        :return: Connection arguments merged and eventually modified, in a
            format similar to `~conninfo.conninfo_to_dict()`.
        )r3   )r   r   rO  s      rT   rX  z!Connection._get_connection_params  s      33F333rS   c                 X    | j         rdS d| _        | j                                         dS )zClose the database connection.NT)rs   r`   rY   finishru   s    rT   rl  zConnection.close!  s4    ; 	F 	rS   )binaryrr  c                    d S r   rR   )rl   rr  s     rT   cursorzConnection.cursor+  s    >AcrS   c                    d S r   rR   )rl   rr  rG  s      rT   rt  zConnection.cursor.  s	      CrS   )rr  
scrollablewithholdr   rv  rw  c                    d S r   rR   )rl   r   rr  rv  rw  s        rT   rt  zConnection.cursor3  s	      CrS   c                    d S r   rR   )rl   r   rr  rG  rv  rw  s         rT   rt  zConnection.cursor=  s	     #&#rS   )rr  rG  rv  rw  c                    |                                   |s| j        }|r|                     | ||||          }n|                     | |          }|rt          |_        |S )zU
        Return a new cursor to send commands and queries to the connection.
        )r   rG  rv  rw  rG  )r   rG  rF  rE  BINARYr%  )rl   r   rr  rG  rv  rw  curs          rT   rt  zConnection.cursorH  s     	!!### 	+*K  		E,,'%! -  CC %%d%DDC 	 CJ
rS   )preparerr  queryr_  r~  c                    	 |                                  }|rt          |_        |                    |||          S # t          j        $ r}|                    d          d}~ww xY w)z8Execute a query and return a cursor to read its results.)r~  N)rt  r|  r%  r;   r   rZ  r^  )rl   r  r_  r~  rr  r}  r   s          rT   r;   zConnection.executej  sp    	*++--C $#
;;ufg;>>> 	* 	* 	*##D)))	*s   9< A%A  A%c                     | j         5  |                     |                                            ddd           dS # 1 swxY w Y   dS )z/Commit any pending transaction to the database.N)rL  waitr  ru   s    rT   rk  zConnection.commit}  s    Y 	* 	*IId&&(()))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*   (=AAc                     | j         5  |                     |                                            ddd           dS # 1 swxY w Y   dS )z2Roll back to the start of any pending transaction.N)rL  r  r  ru   s    rT   rj  zConnection.rollback  s    Y 	, 	,IId((**+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,r  savepoint_nameforce_rollbackc              #   j  K   t          | ||          }| j        ry|                                 5  |5  |                                 5  |V  ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS |5  |V  ddd           dS # 1 swxY w Y   dS )a  
        Start a context block with a new transaction or nested transaction.

        :param savepoint_name: Name of the savepoint used to manage a nested
            transaction. If `!None`, one will be chosen automatically.
        :param force_rollback: Roll back the transaction at the end of the
            block even if there were no error (e.g. to try a no-op process).
        :rtype: Transaction
        N)r>   rg   pipeline)rl   r  r  txs       rT   transactionzConnection.transaction  s      ~~>>> 	  "  dmmoo                                                                     sj   BA/AA/AA/A A/#B/A3	3B6A3	7BBBB((B,/B,c              #     K   	 | j         5  	 |                     t          | j                            }n,# t          j        $ r}|                    d          d}~ww xY w	 ddd           n# 1 swxY w Y   t          | j                  }|D ]L}t          |j	        
                    |          |j        
                    |          |j                  }|V  M)zX
        Yield `Notify` objects as soon as they are received from the database.
        TN)rL  r  r9   rY   r   rZ  r^  r<   rG   r   r   r   r   )rl   nsr   r   r   r   s         rT   r9   zConnection.notifies  s'     		 2 228DK#8#899BB 2 2 2++D1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 "$+..C  3;--c22CI4D4DS4I4I3:VV		s1   A.'5A.AAAA..A25A2c              #     K   | j         5  |                                  | j        }|t          |           x}| _        ddd           n# 1 swxY w Y   	 |5  |V  ddd           n# 1 swxY w Y   |j        dk    r4| j         5  || j        u sJ d| _        ddd           dS # 1 swxY w Y   dS dS # |j        dk    r2| j         5  || j        u sJ d| _        ddd           w # 1 swxY w Y   w w xY w)z)Switch the connection into pipeline mode.Nr   )rL  r   rg   r8   level)rl   r  s     rT   r  zConnection.pipeline  s-      Y 	; 	;%%'''~H,4TNN:4>	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;	*                 ~""Y * *#t~5555%)DN* * * * * * * * * * * * * * * * * * #"x~""Y * *#t~5555%)DN* * * * * * * * * * * * * * * * #sw   4A

AAB5 A)B5 )A--B5 0A-1B5 B&&B*-B*5C4C'C4'C++C4.C+/C4皙?genrT  c                    	 t          j        || j        j        |          S # t          $ rT |                     | j                   	 t          j        || j        j        |           n# t          j        $ r Y nw xY w w xY w)z
        Consume a generator operating on the connection.

        The function must be used on generators that don't change connection
        fd (i.e. not on connect and reset).
        rS  )r   r  rY   r   KeyboardInterruptr   r   QueryCanceled)rl   r  rT  s      rT   r  zConnection.wait  s    
	<T[%7IIII  	 	 	 T[)))S$+"4gFFFFF?   	s-    # $B!A*)B*A<9B;A<<Bc                 .    t          j        ||          S )zConsume a connection generator.rS  )r   	wait_conn)r   r  rT  s      rT   rY  zConnection._wait_conn  s      g6666rS   r   c                     | j         5  |                     |                     |                     d d d            d S # 1 swxY w Y   d S r   )rL  r  r   r   s     rT   r   zConnection._set_autocommit      Y 	7 	7IId..u55666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7   )>AAc                     | j         5  |                     |                     |                     d d d            d S # 1 swxY w Y   d S r   )rL  r  r   r   s     rT   r   zConnection._set_isolation_level  s    Y 	< 	<IId33E::;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<r  c                     | j         5  |                     |                     |                     d d d            d S # 1 swxY w Y   d S r   )rL  r  r   r   s     rT   r   zConnection._set_read_only  s    Y 	6 	6IId--e44555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6r  c                     | j         5  |                     |                     |                     d d d            d S # 1 swxY w Y   d S r   )rL  r  r   r   s     rT   r   zConnection._set_deferrable  r  r  r  c                     | j         5  |                     |                     |                     ddd           dS # 1 swxY w Y   dS )zO
        Begin a TPC transaction with the given transaction ID `!xid`.
        N)rL  r  r#  r"  s     rT   	tpc_beginzConnection.tpc_begin  s     Y 	0 	0IId))#..///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0r  c                    	 | j         5  |                     |                                            ddd           dS # 1 swxY w Y   dS # t          j        $ r'}t          j        t          |                    dd}~ww xY w)zV
        Perform the first phase of a transaction started with `tpc_begin()`.
        N)rL  r  r&  r   ObjectNotInPrerequisiteStater9  rO   )rl   r   s     rT   tpc_preparezConnection.tpc_prepare  s    	9 3 3		$//112223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3- 	9 	9 	9%c"gg..D8	9s7   A (>A AA AA B"A<<Bc                     | j         5  |                     |                     d|                     ddd           dS # 1 swxY w Y   dS )z:
        Commit a prepared two-phase transaction.
        COMMITNrL  r  r/  r"  s     rT   
tpc_commitzConnection.tpc_commit  s     Y 	; 	;IId**8S99:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;   *?AAc                     | j         5  |                     |                     d|                     ddd           dS # 1 swxY w Y   dS )z=
        Roll back a prepared two-phase transaction.
        ROLLBACKNr  r"  s     rT   tpc_rollbackzConnection.tpc_rollback  s     Y 	= 	=IId**:s;;<<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=r  c                    |                                   | j        j        }|                     t	          t
          j                            5 }|                    t          j                               |	                                }d d d            n# 1 swxY w Y   |t          k    r)| j        j        t          k    r|                                  |S )Nr{  )r  r   r   rt  r+   r&   _from_recordr;   _get_recover_queryfetchallr   INTRANSrj  )rl   r   r}  r   s       rT   tpc_recoverzConnection.tpc_recover  s    -[[Xc.>%?%?[@@ 	!CKK.00111,,..C	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! T>>di:gEEMMOOO
s   ;BBBr2  r1  r   )NF)r  )BrK   rL   rM   rN   r   r.   r'   rP   r?   r(   r   r8   r   r)   rn   r   r=  rO   r   rQ   r   r   r:   r   r0   re  BaseExceptionr   rn  r5   rX  rl  rt  rD   r    r   r;   rk  rj  r   r	   r>   r  r   rG   r9   r  r"   r!   floatr  r#   rY  r   r-   r   r   r   r&   r  r  r  r  r
   r  __classcell__)r}   s   @rT   rD  rD    s          J%%%%S 12222C   !!!!
 (,tJsOY'G'G	2 	2	2  _	2 	2 	2 	2 	2 	2   !+,6:*.   	
  _ $C= !fSk!23 ,' c3' 
   [ X   	% !+,6:*.	% 	% 	%	% 		%
 $C=	% !fSk!23	% ,'	% c3'	% 
 	% 	% 	% [ X	%  / !+,156:*./ / // 	/
 $C=/ jo./ !fSk!23/ ,'/ / 
/ / / [/b4    4./ -( '	
 
   6 4c 4S 4X 4 4 4 [4    ',AAAAAAA XA %      4>y4I 			      X  
 %)       	 
 TN    
c	      X  
 %)& & && 	&
  	*& TN& & 
i	 & & & X&   15%)       	 
 jo.  TN    
vc{L--	.       J $(*
 #'* * **  *
 $* * 
* * * *&* * * *
, , , ,
  )-$    
+		   ^,)FD$$67     *(8, * * * ^*& b	 HUO b    & 7Yr] 7Xc] 7r 7 7 7 [77T 7d 7 7 7 7<(>*B <t < < < <6HTN 6t 6 6 6 67Xd^ 7 7 7 7 70U38_ 0 0 0 0 09 9 9 9; ;eCdN3 ;t ; ; ; ;= =c3n 5 = = = = =
T#Y 
 
 
 
 
 
 
 
rS   rD  )wrN   loggingrJ  typesr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   weakrefr   r   warningsr   	functoolsr   
contextlibr   typing_extensionsr   r   r   r   r   r   r   abcr   r   r    r!   r"   r#   sqlr$   r%   rb   r&   rowsr'   r(   r)   r*   r+   adaptr,   _enumsr-   rt  r.   _compatr/   r0   pq.miscr1   r   r2   r3   r4   r5   r6   rg   r7   r8   
generatorsr9   r:   r;   
_encodingsr<   
_preparingr=   r  r>   server_cursorr?   _connection_infor@   pq.abcrA   rB   psycopg_pool.baserC   rD   rA  r@  r|  r:  r  r   r  r   r   r   r   r   r  	getLoggerr   rG   rL   r   rU   rP   rV   rX   rD  rR   rS   rT   <module>r     s                D D D D D D D D D D D D D D D D J J J J J J J J J J J J J J J J J J * * * * * * * * & & & & & & & &             % % % % % % ' ' ' ' ' '                         0 0 0 0 0 0 0 0 0 0 0 0 ! ! ! ! ! ! ! !                       @ @ @ @ @ @ @ @ @ @ @ @ @ @       " " " " " "       ( ( ( ( ( ( ( ( ' ' ' ' ' ' 5 5 5 5 5 5 5 5 H H H H H H H H H H - - - - - - - - 2 2 2 2 2 2 2 2 2 2 ' ' ' ' ' ' & & & & & & $ $ $ $ $ $ ' ' ' ' ' ' , , , , , , +((((((((******
 GK  		y~		]m]%
M#	m'	 


&		9	%	%
F 
F 
F 
F 
FZ 
F 
F 
F  #Q\ND$89y 9 9 9#VHdN3y 3 3 3v v v v vWS\ v v vrB B B B B$ B B B B BrS   