o
    6	f                     @   s  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`jaZae!j`jbZbe!jcjdZde!jcjeZee!jfjgZge!jfjhZhe!jfjiZie!jjjkZke!jjjlZlemd'ZnG d(d) d)eZod'eo_pee#jqgdf Zreesd*< eeogdf Zteesd+< G d,d- d-e
e3 ZuG d.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                   @   s.   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 rP   rP   K/home/ertert/spirit/venv/lib/python3.10/site-packages/psycopg/connection.pyrE   D   s   
 rE   NoticeHandlerNotifyHandlerc                	   @   sd  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dvddZdwdd	Zdefd
dZedefddZedefddZedefddZejdeddfddZdeddfddZdeded fddZedee  fddZ!e!jdee  ddfddZ!dee  ddfddZ"dee  ded fddZ#edee fdd Z$e$jdee ddfd!d Z$dee ddfd"d#Z%dee ded fd$d%Z&edee fd&d'Z'e'jdee ddfd(d'Z'dee ddfd)d*Z(dee ded fd+d,Z)d-eded fd.d/Z*ede+fd0d1Z,ede-fd2d3Z.edxd5d6Z/de0fd7d8Z1dwd9d:Z2e3dyd;d<Z4d=e5ddfd>d?Z6d=e5ddfd@dAZ7e8	B	C	D	E		dzdFdGZ9d=e:ddfdHdIZ;d=e:ddfdJdKZ<e8dBdCdLej=ddfdMdNZ>edee0 fdOdPZ?e?jdee0 ddfdQdPZ?ede0fdRdSZ@e@jde0ddfdTdSZ@e3d{dVedeAeB fdWdXZCeDfdYeEdZejFdeedE  fd[d\ZGdwd]d^ZHded fd_d`ZIdeJfdadbZKded fdcddZLded fdedfZMdge0dhediedeNfdjdkZOdlePeNef ded fdmdnZQded fdodpZRdqeSdlePeNedf ded fdrdsZTdwdtduZUdS )|BaseConnectionz
    Base class for different types of connections.

    Share common functionalities such as access to the wrapped PGconn, but
    allow different interfaces (sync/async).
    pgconnr@   c                 C   s   || _ d| _d | _g | _g | _d| _d| _t | _d | _	t
| }ttj||_ttj||_|  d | _|  d | _d | _d | _d| _d S )NFr       )rU   _autocommit	_adapters_notice_handlers_notify_handlers_num_transactions_closedr<   	_prepared_tpcr   r   rT   _notice_handlernotice_handler_notify_handlernotify_handler	_pipeline_isolation_level
_read_only_deferrable_begin_statement)selfrU   wselfrP   rP   rQ   __init__o   s&   
zBaseConnection.__init__returnNc                 C   s<   t | dsd S | jrd S t | drd S td|  dt d S )NrU   _poolzconnection zV was deleted while still open. Please use 'with' or '.close()' to close the connection)hasattrclosedr   ResourceWarningrh   rP   rP   rQ   __del__   s   


zBaseConnection.__del__c                 C   s>   | j j d| j j }t| j}d| d| dt| ddS )N.< z at 0xx>)	__class__rJ   rK   r0   rU   id)rh   clsinforP   rP   rQ   __repr__   s   
zBaseConnection.__repr__c                 C   s   | j jtkS )z$`!True` if the connection is closed.)rU   statusBADrp   rP   rP   rQ   rn      s   zBaseConnection.closedc                 C   s   | 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.
        )rU   r|   r}   r\   rp   rP   rP   rQ   broken   s   zBaseConnection.brokenc                 C      | j S )z'The autocommit state of the connection.)rW   rp   rP   rP   rQ   
autocommit   s   zBaseConnection.autocommitvaluec                 C      |  | d S N)_set_autocommitrh   r   rP   rP   rQ   r         c                 C      t r   NotImplementedErrorr   rP   rP   rQ   r         zBaseConnection._set_autocommitc                 c   s     |  dE d H  t|| _d S )Nr   )_check_intrans_genboolrW   r   rP   rP   rQ   _set_autocommit_gen   s   z"BaseConnection._set_autocommit_genc                 C   r   )zX
        The isolation level of the new transactions started on the connection.
        )rd   rp   rP   rP   rQ   isolation_level      zBaseConnection.isolation_levelc                 C   r   r   )_set_isolation_levelr   rP   rP   rQ   r      r   c                 C   r   r   r   r   rP   rP   rQ   r      r   z#BaseConnection._set_isolation_levelc                 c   2    |  dE d H  |d urt|nd | _d| _d S )Nr   rV   )r   r,   rd   rg   r   rP   rP   rQ   _set_isolation_level_gen      
z'BaseConnection._set_isolation_level_genc                 C   r   )zX
        The read-only state of the new transactions started on the connection.
        )re   rp   rP   rP   rQ   	read_only   r   zBaseConnection.read_onlyc                 C   r   r   )_set_read_onlyr   rP   rP   rQ   r      r   c                 C   r   r   r   r   rP   rP   rQ   r      r   zBaseConnection._set_read_onlyc                 c   r   )Nr   rV   )r   r   re   rg   r   rP   rP   rQ   _set_read_only_gen   r   z!BaseConnection._set_read_only_genc                 C   r   )zY
        The deferrable state of the new transactions started on the connection.
        )rf   rp   rP   rP   rQ   
deferrable   r   zBaseConnection.deferrablec                 C   r   r   )_set_deferrabler   rP   rP   rQ   r      r   c                 C   r   r   r   r   rP   rP   rQ   r      r   zBaseConnection._set_deferrablec                 c   r   )Nr   rV   )r   r   rf   rg   r   rP   rP   rQ   _set_deferrable_gen   r   z"BaseConnection._set_deferrable_gen	attributec                 c   sr    | j j}|tkr| jr| j E d H  | j j}|tkr7| jr(td|dtd|dt	|j
 d S )Nzcan't change z2 now: connection.transaction() context in progressz' now: connection in transaction status )rU   transaction_statusIDLErc   	_sync_genr[   eProgrammingErrorr   TransactionStatusname)rh   r   r|   rP   rP   rQ   r     s    

z!BaseConnection._check_intrans_genc                 C   s
   t | jS )z>A `ConnectionInfo` attribute to inspect connection properties.)r?   rU   rp   rP   rP   rQ   rz     s   
zBaseConnection.infoc                 C   s   | j s	ttj| _ | j S r   )rX   r+   r   adaptersrp   rP   rP   rQ   r     s   zBaseConnection.adaptersBaseConnection[Row]c                 C      | S r   rP   rp   rP   rP   rQ   
connection!     zBaseConnection.connectionc                 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.
        )rU   socketrp   rP   rP   rQ   fileno&     zBaseConnection.filenoc                 C   s4   | j rdS | jr| jd rtd| | j dS )z/Cancel the current operation on the connection.Nr   z=cancel() cannot be used with a prepared two-phase transaction)rn   r^   r   r   _try_cancelrU   rp   rP   rP   rQ   cancel/  s   zBaseConnection.cancelc              
   C   sL   z|  }W n ty } ztd| W Y d }~d S d }~ww |  d S )Nz couldn't try to cancel query: %s)
get_cancel	Exceptionloggerwarningr   )ry   rU   cexrP   rP   rQ   r   >  s   zBaseConnection._try_cancelcallbackc                 C      | 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)rY   appendrh   r   rP   rP   rQ   add_notice_handlerH     z!BaseConnection.add_notice_handlerc                 C   r   )z
        Unregister a notice message callable previously registered.

        :param callback: the callback to remove.
        :type callback: Callable[[~psycopg.errors.Diagnostic], None]
        N)rY   remover   rP   rP   rQ   remove_notice_handlerQ  r   z$BaseConnection.remove_notice_handlerri   "ReferenceType[BaseConnection[Row]]resrA   c                 C   st   |  }|r|j s
d S t|t|j}|j D ]!}z|| W q ty7 } ztd|| W Y d }~qd }~ww d S )Nz)error processing notice callback '%s': %s)rY   r   
Diagnosticr;   rU   r   r   	exception)ri   r   rh   diagcbr   rP   rP   rQ   r_   Z  s   

zBaseConnection._notice_handlerc                 C   r   )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)rZ   r   r   rP   rP   rQ   add_notify_handleri  r   z!BaseConnection.add_notify_handlerc                 C   r   )z
        Unregister a notification callable previously registered.

        :param callback: the callback to remove.
        :type callback: Callable[[~psycopg.Notify], None]
        N)rZ   r   r   rP   rP   rQ   remove_notify_handlerr  r   z$BaseConnection.remove_notify_handlerpgnc                 C   sT   |  }|r|j s
d S t|j}t|j||j||j}|j D ]}|| q!d S r   )rZ   r;   rU   rE   relnamedecodeextrabe_pid)ri   r   rh   encnr   rP   rP   rQ   ra   {  s   



zBaseConnection._notify_handlerc                 C   r   )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
        r]   prepare_thresholdrp   rP   rP   rQ   r     s   z BaseConnection.prepare_thresholdc                 C      || j _d S r   r   r   rP   rP   rQ   r        c                 C   r   )zf
        Maximum number of prepared statements on the connection.

        Default value: 100
        r]   prepared_maxrp   rP   rP   rQ   r     r   zBaseConnection.prepared_maxc                 C   r   r   r   r   rP   rP   rQ   r     r    conninfoc                 c   s    t |E dH }| |}|S )z?Generator to connect to the database and create a new instance.N)r9   )ry   r   rU   connrP   rP   rQ   _connect_gen  s   zBaseConnection._connect_gencommandresult_formatc                 c   s    |    t|tr|t| j}n
t|tr|| }| jr:t	| jj
|d|d}| jj| | jjd dS | jj
|d|d t| jE dH d }|jtkrx|jtkrx|jtkrftj|t| jdtdt|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
isinstancerM   encoder;   rU   r#   as_bytesrc   r   send_query_paramscommand_queuer   result_queuer:   r|   
COMMAND_OK	TUPLES_OKFATAL_ERRORr   error_from_resultInterfaceErrorr   
ExecStatusr   r   )rh   r   r   cmdresultrP   rP   rQ   _exec_command  s6   	



zBaseConnection._exec_commandc                 C   s:   | j jtkrd S | j jtkrtdtd| j j )Nzthe connection is closedz7cannot execute operations: the connection is in status )rU   r|   OKr}   r   OperationalErrorr   rp   rP   rP   rQ   r     s   
z#BaseConnection._check_connection_okc                 c   sN    | j rdS | jjtkrdS | |  E dH  | jr%| j E dH  dS dS )z.Generator to start a transaction if necessary.N)rW   rU   r   r   r   _get_tx_start_commandrc   r   rp   rP   rP   rQ   _start_query  s   zBaseConnection._start_queryc                 C   s   | j r| j S dg}| jd ur$t| j}|d ||jdd  | jd ur3|| jr0dnd | jd urB|| jr?dnd d		|| _ | j S )
Ns   BEGINs   ISOLATION LEVEL_rt   s	   READ ONLYs
   READ WRITEs
   DEFERRABLEs   NOT DEFERRABLE    )
rg   r   r,   r   r   replacer   r   r   join)rh   partsvalrP   rP   rQ   r     s   




z$BaseConnection._get_tx_start_commandc                 c   s`    | j r	td| jrtd| jjtkrdS | dE dH  | jr.| j	 E dH  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   r^   rU   r   r   r   rc   r   rp   rP   rP   rQ   _commit_gen  s   zBaseConnection._commit_genc                 c   s    | j r	td| jrtd| jr| j E dH  | jjtkr$dS | 	dE dH  | j
  | j
 D ]
}| 	|E dH  q6| jrN| j E dH  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   r^   rc   r   rU   r   r   r   r]   clearget_maintenance_commands)rh   r   rP   rP   rQ   _rollback_gen  s(   
zBaseConnection._rollback_gen	format_idgtridbqualc                 C   s   |    t|||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)rh   r   r   r   rP   rP   rQ   xid;  s   
zBaseConnection.xidr   c                 c   sx    |    t|tst|}| jjtkr#tdt	
| jjj | jr+td|df| _| |  E d H  d S )Nz8can't start two-phase transaction: connection in status z3can't use two-phase transactions in autocommit modeF)r   r   r%   from_stringrU   r   r   r   r   r   r   r   rW   r^   r   r   rh   r   rP   rP   rQ   _tpc_begin_genH  s    


zBaseConnection._tpc_begin_genc                 c   sv    | j s	td| j d rtd| j d }|df| _ | tdt|E d H  | jr9| j E d H  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 {})	r^   r   r   r   r$   formatrM   rc   r   r   rP   rP   rQ   _tpc_prepare_gen\  s   


zBaseConnection._tpc_prepare_genactionc                 c   s    d|   d}|d u r| jst| d| jd }n| jr)t| dt|ts3t|}| jrQ| jd sQt| d|   d}d | _| E d H  d S | t	d	
t	|t|E d H  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 {})lowerr^   r   r   r   r%   r   getattrr   r$   r   rM   )rh   r  r   fnamemethrP   rP   rQ   _tpc_finish_genk  s,   



zBaseConnection._tpc_finish_genc                 C   s   dS )z0Raise NotSupportedError if TPC is not supported.NrP   rp   rP   rP   rQ   r     r   zBaseConnection._check_tpc)rU   r@   rk   N)rk   r   )rU   r@   rk   N)ri   r   r   rA   rk   Nr   )VrI   rJ   rK   rL   r   WarningErrorr   DatabaseError	DataErrorr   IntegrityErrorInternalErrorr   NotSupportedErrorr   
ConnStatusr   rj   rq   rM   r{   propertyr   rn   r~   r   setterr   r!   r   r   r,   r   r   r   r   r   r   r   r   r   r   r?   rz   r+   r   r   rO   r   r   classmethodr   rR   r   r   staticmethodr_   rS   r   r   PGnotifyra   r   r   r"   r/   r   TEXTr   Formatr   r   r   bytesr   r   r   r%   r   r   r   r   r.   r  r   rP   rP   rP   rQ   rT   W   s    

#	
						


(
rT   c                       s  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	d
Zee	dbd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dZee	dbd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dZe	dbd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dZdefddZde
ee  d e
e d!e
e ddfd"d#Zedededefd$d%Zdcd&d'Zedd(d)edee fd*d+Z edd(d)ede	e! dee! fd,d+Z edddd-d.ed)ed/e
e d0edee f
d1d+Z edddd-d.ed)ede	e! d/e
e d0edee! fd2d+Z 	dbddddd3d.ed)ede
e	e  d/e
e d0edeee ee f fd4d+Z 	ddddd5d6e"d7e
e# d8e
e d)edee f
d9d:Z$dcd;d<Z%dcd=d>Z&e'		ded?e
e d@ede(e) fdAdBZ*de+e,ddf fdCdDZ-e'de(e fdEdFZ.dfdHe/e0 dIe
e1 de0fdJdKZ2edHe3e0 dIe
e de0fdLdMZ4dNeddfdOdPZ5dNe
e6 ddfdQdRZ7dNe
e ddfdSdTZ8dNe
e ddfdUdVZ9dWee:ef ddfdXdYZ;dcdZd[Z<dddWee:edf ddfd\d]Z=dddWee:edf ddfd^d_Z>de?e: fd`daZ@  ZAS )g
Connectionz3
    Wrapper for a connection to the database.
    rD   cursor_factoryserver_cursor_factoryrow_factoryrc   rU   r@   c                    s,   t  | || _t | _t| _t| _	d S r   )
superrj   r  	threadingLocklockr-   r  r>   r  )rh   rU   r  rw   rP   rQ   rj     s
   

zConnection.__init__r   F   N)r   r   r  contextr   r   r   r%  kwargsrk   zConnection[Row]c                K      d S r   rP   )ry   r   r   r  r   r  r%  r&  rP   rP   rQ   r9     s   zConnection.connectzConnection[TupleRow]c                K   r'  r   rP   )ry   r   r   r   r  r%  r&  rP   rP   rQ   r9     s   )r   r   r  r  r%  c                K   s  | j |fi |}t|}	d}
t|}|D ]E}ztdi |}| j| ||	d}
W  n/ tjyZ } z"t|dkrNt	
d|d|d|dt| |}W Y d}~qd}~ww |
sf|saJ |dt||
_|rp||
_|ru||
_|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hostaddrrP   )_get_connection_paramsr5   r3   r1   
_wait_connr   r   _NO_TRACEBACKlenr   debuggetrM   with_tracebackr   rW   r  r  r+   r   rX   r   )ry   r   r   r   r  r  r%  r&  paramsr)  rvattemptsattemptr   last_exrP   rP   rQ   r9     sB   

c                 C   r   r   rP   rp   rP   rP   rQ   	__enter__  r   zConnection.__enter__exc_typeexc_valexc_tbc              
   C   sr   | j rd S |r'z|   W n ty& } ztd| | W Y d }~n	d }~ww |   t| dd s7|   d S d S )Nz#error ignored in rollback on %s: %srl   )rn   rollbackr   r   r   commitr  close)rh   r:  r;  r<  exc2rP   rP   rQ   __exit__  s"   zConnection.__exit__c                 K   s   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()`.
        )r2   )ry   r   r&  rP   rP   rQ   r-    s   	z!Connection._get_connection_paramsc                 C   s   | j rdS d| _| j  dS )zClose the database connection.NT)rn   r\   rU   finishrp   rP   rP   rQ   r?  !  s   zConnection.close)binaryrC  c                C   r'  r   rP   )rh   rC  rP   rP   rQ   cursor+  r   zConnection.cursorc                C   r'  r   rP   )rh   rC  r  rP   rP   rQ   rD  .  r   )rC  
scrollablewithholdr   rE  rF  c                C   r'  r   rP   )rh   r   rC  rE  rF  rP   rP   rQ   rD  3  s   c                C   r'  r   rP   )rh   r   rC  r  rE  rF  rP   rP   rQ   rD  =  s   	)rC  r  rE  rF  c                C   sH   |    |s	| j}|r| j| ||||d}n| j| |d}|r"t|_|S )zU
        Return a new cursor to send commands and queries to the connection.
        )r   r  rE  rF  r  )r   r  r  r  BINARYr   )rh   r   rC  r  rE  rF  currP   rP   rQ   rD  H  s   )preparerC  queryr4  rJ  c             
   C   sJ   z|   }|r
t|_|j|||dW S  tjy$ } z|dd}~ww )z8Execute a query and return a cursor to read its results.)rJ  N)rD  rH  r   r:   r   r/  r3  )rh   rK  r4  rJ  rC  rI  r   rP   rP   rQ   r:   j  s   	
zConnection.executec                 C   :   | j  | |   W d   dS 1 sw   Y  dS )z/Commit any pending transaction to the database.N)r"  waitr   rp   rP   rP   rQ   r>  }     "zConnection.commitc                 C   rL  )z2Roll back to the start of any pending transaction.N)r"  rM  r   rp   rP   rP   rQ   r=    rN  zConnection.rollbacksavepoint_nameforce_rollbackc              
   c   s    t | ||}| jrR|  : | |   |V  W d   n1 s$w   Y  W d   n1 s3w   Y  W d   dS W d   dS 1 sKw   Y  dS | |V  W d   dS 1 scw   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=   rc   pipeline)rh   rO  rP  txrP   rP   rQ   transaction  s   P "zConnection.transactionc                 c   s    	 | j % z
| t| j}W n tjy" } z|dd}~ww W d   n1 s-w   Y  t| j}|D ]}t|j	
||j
||j}|V  q9q)zX
        Yield `Notify` objects as soon as they are received from the database.
        TN)r"  rM  r8   rU   r   r/  r3  r;   rE   r   r   r   r   )rh   nsr   r   r   r   rP   rP   rQ   r8     s    

zConnection.notifiesc                 c   s   | j  |   | j}|du rt|  }| _W d   n1 s!w   Y  z=| |V  W d   n1 s7w   Y  W |jdkrb| j  || ju sMJ d| _W d   dS 1 s[w   Y  dS dS |jdkr| j  || ju stJ d| _W d   w 1 sw   Y  w )z)Switch the connection into pipeline mode.Nr   )r"  r   rc   r7   level)rh   rQ  rP   rP   rQ   rQ    s.   
"zConnection.pipeline皙?genr)  c                 C   s`   zt j|| jj|dW S  ty/   | | j zt j|| jj|d W   tjy.   Y  w 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).
        r(  )r   rM  rU   r   KeyboardInterruptr   r   QueryCanceled)rh   rW  r)  rP   rP   rQ   rM    s   zConnection.waitc                 C   s   t j||dS )zConsume a connection generator.r(  )r   	wait_conn)ry   rW  r)  rP   rP   rQ   r.    s   zConnection._wait_connr   c                 C   <   | j  | | | W d    d S 1 sw   Y  d S r   )r"  rM  r   r   rP   rP   rQ   r        "zConnection._set_autocommitc                 C   r[  r   )r"  rM  r   r   rP   rP   rQ   r     r\  zConnection._set_isolation_levelc                 C   r[  r   )r"  rM  r   r   rP   rP   rQ   r     r\  zConnection._set_read_onlyc                 C   r[  r   )r"  rM  r   r   rP   rP   rQ   r     r\  zConnection._set_deferrabler   c                 C   s<   | j  | | | W d   dS 1 sw   Y  dS )zO
        Begin a TPC transaction with the given transaction ID `!xid`.
        N)r"  rM  r   r   rP   rP   rQ   	tpc_begin  s   "zConnection.tpc_beginc              
   C   sj   z| j  | |   W d   W dS 1 sw   Y  W dS  tjy4 } ztt|dd}~ww )zV
        Perform the first phase of a transaction started with `tpc_begin()`.
        N)r"  rM  r   r   ObjectNotInPrerequisiteStater  rM   )rh   r   rP   rP   rQ   tpc_prepare  s   &zConnection.tpc_preparec                 C   >   | j  | | d| W d   dS 1 sw   Y  dS )z:
        Commit a prepared two-phase transaction.
        COMMITNr"  rM  r  r   rP   rP   rQ   
tpc_commit     "zConnection.tpc_commitc                 C   r`  )z=
        Roll back a prepared two-phase transaction.
        ROLLBACKNrb  r   rP   rP   rQ   tpc_rollback  rd  zConnection.tpc_rollbackc                 C   sx   |    | jj}| jttjd}|t  |	 }W d    n1 s'w   Y  |t
kr:| jjtkr:|   |S )NrG  )r   rz   r   rD  r*   r%   _from_recordr:   _get_recover_queryfetchallr   INTRANSr=  )rh   r|   rI  r   rP   rP   rQ   tpc_recover  s   
zConnection.tpc_recoverr
  r	  r   )NF)rV  )BrI   rJ   rK   rL   r   r-   r&   rN   r>   r'   r   r7   r   r(   rj   r   r  rM   r   rO   r   r   r9   r   r/   r9  BaseExceptionr   rA  r4   r-  r?  rD  rC   r   r   r:   r>  r=  r   r   r=   rS  r   rE   r8   rQ  r!   r    floatrM  r"   r.  r   r,   r   r   r   r%   r]  r_  rc  rf  r	   rk  __classcell__rP   rP   r#  rQ   r    s  
 	
	
	
1




	

%


  

r  )wrL   loggingr   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$   r^   r%   rowsr&   r'   r(   r)   r*   adaptr+   _enumsr,   rD  r-   _compatr.   r/   pq.miscr0   r   r1   r2   r3   r4   r5   rc   r6   r7   
generatorsr8   r9   r:   
_encodingsr;   
_preparingr<   rS  r=   server_cursorr>   _connection_infor?   pq.abcr@   rA   psycopg_pool.baserB   rC   r  r  rH  r  r   r}   r   r   r   r   r   r   rj  	getLoggerr   rE   rJ   r   rR   rN   rS   rT   r  rP   rP   rP   rQ   <module>   sv     $
    =