o
    "4gI=                     @  s  d Z ddlmZ ddlZddlm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 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mZmZmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z,m-Z- ddl+m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z? ddl@mAZA erddlBmCZC dZDejEjFZFejEjGZGejHjIZIejHjJZJejHjKZKeLZMeNdZOG dd  d e=e! ZPdS )!z*
Psycopg connection object (sync version)
    )annotationsN)	monotonic)TracebackType)Any	GeneratorIteratorcastoverloadTYPE_CHECKING)contextmanager   )pq)errors)waiting)AdaptContextConnDict	ConnParamParamsPQGenQueryRV)Xid)Row
RowFactory	tuple_rowargs_row)AdaptersMap)IsolationLevel)Self)make_conninfoconninfo_to_dict)conninfo_attemptstimeout_from_conninfo)Pipeline)notifies)Transaction)Cursor)capabilities)ServerCursor)BaseConnection	CursorRowNotify)Lock)PGconng?psycopgc                      s$  e Zd ZU dZdZded< ded< ded< d	ed
< eee efd fddZ	e
	ddddddddd"d#Zdd$d%Zdd-d.Ze
dd1d2Zdd3d4Zedd5dd8d9Zedd5dd<d9Zedddd=ddCd9Zedddd=ddEd9Z	ddddddFddId9Z	ddddJddPdQZddRdSZddTdUZdVdWddZd[Zd\dWdd]d^Ze	dddcddZdddeddidjZeddldmZefddrdsZddudvZddwdxZddzd{Zdd|d}Z dd~dZ!dddZ"dddZ#dddZ$dddZ%dddZ&ddddZ'ddddZ(dddZ)  Z*S )
Connectionz3
    Wrapper for a connection to the database.
    r.   ztype[Cursor[Row]]cursor_factoryztype[ServerCursor[Row]]server_cursor_factoryRowFactory[Row]row_factoryzPipeline | None	_pipelinepgconnr-   c                   s*   t  | || _t | _t| _t| _d S N)	super__init__r3   r,   lockr&   r0   r(   r1   )selfr5   r3   	__class__ [/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/connection.pyr8   D   s
   
zConnection.__init__ F   N)
autocommitprepare_thresholdcontextr3   r0   conninfostrrA   boolrB   
int | NonerC   AdaptContext | NoneRowFactory[Row] | Nonetype[Cursor[Row]] | Nonekwargsr   returnr   c                K  s  | j |fi |}t|}	d}
t|}|D ]I}ztd
i |}| j||	d}tj|td}
W n/ tj	y] } z"t
|dkrQtd|d|d|d	t| |}W Y d}~qd}~ww  |
sj|seJ |dt||
_|rt||
_|ry||
_|rt|j|
_||
_|
S )zV
        Connect to a database server and return a new `Connection` instance.
        Nr?   timeoutintervalr   z=connection attempt failed: host: %r port: %r, hostaddr %r: %shostporthostaddrr?   )_get_connection_paramsr"   r!   r   _connect_genr   	wait_conn_WAIT_INTERVALe_NO_TRACEBACKlenloggerdebuggetrE   with_tracebackrF   _autocommitr3   r0   r   adapters	_adaptersrB   )clsrD   rA   rB   rC   r3   r0   rK   paramsrN   rvattemptsattemptgenexlast_exr=   r=   r>   connectO   sD   

zConnection.connectc                 C  s   | S r6   r=   r:   r=   r=   r>   	__enter__      zConnection.__enter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | NoneNonec              
   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: %s_pool)closedrollback	Exceptionr\   warningcommitgetattrclose)r:   ro   rq   rs   exc2r=   r=   r>   __exit__   s   zConnection.__exit__r   r   c                 K  s   t |fi |S )z3Manipulate connection parameters before connecting.)r    )rc   rD   rK   r=   r=   r>   rU      s   z!Connection._get_connection_paramsc                 C  s   | j rdS d| _| j  dS )zClose the database connection.NT)rw   _closedr5   finishrl   r=   r=   r>   r}      s   zConnection.close)binaryr   Cursor[Row]c                C     d S r6   r=   )r:   r   r=   r=   r>   cursor   rn   zConnection.cursorRowFactory[CursorRow]Cursor[CursorRow]c                C  r   r6   r=   )r:   r   r3   r=   r=   r>   r      s   )r   
scrollablewithholdnamer   bool | Noner   ServerCursor[Row]c                C  r   r6   r=   )r:   r   r   r   r   r=   r=   r>   r      s   ServerCursor[CursorRow]c                C  r   r6   r=   )r:   r   r   r3   r   r   r=   r=   r>   r      s   	)r   r3   r   r   RowFactory[Any] | NoneCursor[Any] | ServerCursor[Any]c                C  sH   |    |s	| j}|r| j| ||||d}n| j| |d}|r"t|_|S )zW
        Return a new `Cursor` to send commands and queries to the connection.
        )r   r3   r   r   r3   )_check_connection_okr3   r1   r0   BINARYformat)r:   r   r   r3   r   r   curr=   r=   r>   r      s   )preparer   queryr   rd   Params | Noner   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.)r   N)r   r   r   executerY   rZ   r_   )r:   r   rd   r   r   r   ri   r=   r=   r>   r      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)r9   wait_commit_genrl   r=   r=   r>   r{         "zConnection.commitc                 C  r   )z2Roll back to the start of any pending transaction.N)r9   r   _rollback_genrl   r=   r=   r>   rx     r   zConnection.rollbackg      >@rM   rN   floatc                C  s:   |   sdS t rtj| j|dtd dS |   dS )a  Cancel the current operation on the connection.

        :param timeout: raise a `~errors.CancellationTimeout` if the
            cancellation request does not succeed within `timeout` seconds.

        Note that a successful cancel attempt on the client is not a guarantee
        that the server will successfully manage to cancel the operation.

        This is a non-blocking version of `~Connection.cancel()` which
        leverages a more secure and improved cancellation feature of the libpq,
        which is only available from version 17.

        If the underlying libpq is older than version 17, the method will fall
        back to using the same implementation of `!cancel()`.
        NrM   rO   )_should_cancelr'   has_cancel_safer   rW   _cancel_genrX   cancel)r:   rN   r=   r=   r>   cancel_safe  s   
zConnection.cancel_safe      @c             
   C  sF   z	| j |d W d S  ty" } ztd| W Y d }~d S d }~ww )NrM   zquery cancellation failed: %s)r   ry   r\   rz   )r:   rN   ri   r=   r=   r>   _try_cancel!  s   zConnection._try_cancelsavepoint_name
str | Noneforce_rollbackIterator[Transaction]c              
   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%   r4   pipeline)r:   r   r   txr=   r=   r>   transaction'  s   P "zConnection.transaction)rN   
stop_afterfloat | Noner   Generator[Notify, None, None]c                c  s   |durt  | }t|t}nd}t}d}| ji | jj}	 z| jt| j|d}W n tj	y= } z|
dd}~ww |D ]}	t|	j||	j||	j}
|
V  |d7 }q@|durb||krbn|rqtt|t   }|dk rqn	qW d   dS W d   dS 1 sw   Y  dS )a  
        Yield `Notify` objects as soon as they are received from the database.

        :param timeout: maximum amount of time to wait for notifications.
            `!None` means no timeout.
        :param stop_after: stop after receiving this number of notifications.
            You might actually receive more than this number if more than one
            notifications arrives in the same packet.
        Nr   TrO   r   g        )r   minrX   r9   r5   	_encodingr   r$   rY   rZ   r_   r+   relnamedecodeextrabe_pid)r:   rN   r   deadlinerP   	nreceivedencnsri   pgnnr=   r=   r>   r$   <  sB   


"zConnection.notifiesIterator[Pipeline]c                 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<Context manager to switch the connection into pipeline mode.Nr   )r9   r   r4   r#   level)r:   r   r=   r=   r>   r   n  s.   
"zConnection.pipelinerh   	PQGen[RV]rP   r   c                 C  sn   zt j|| jj|dW S  ty6   | jjtkr5| jdd zt j|| jj|d W   tj	y4   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).
        rO   r   rM   )
r   r   r5   socket_INTERRUPTEDtransaction_statusACTIVEr   rY   QueryCanceled)r:   rh   rP   r=   r=   r>   r     s   zConnection.waitvaluec                 C     |  | d S r6   )set_autocommitr:   r   r=   r=   r>   _set_autocommit     zConnection._set_autocommitc                 C  <   | j  | | | W d   dS 1 sw   Y  dS )z6Method version of the `~Connection.autocommit` setter.N)r9   r   _set_autocommit_genr   r=   r=   r>   r        "zConnection.set_autocommitIsolationLevel | Nonec                 C  r   r6   )set_isolation_levelr   r=   r=   r>   _set_isolation_level  r   zConnection._set_isolation_levelc                 C  r   )z;Method version of the `~Connection.isolation_level` setter.N)r9   r   _set_isolation_level_genr   r=   r=   r>   r     r   zConnection.set_isolation_levelc                 C  r   r6   )set_read_onlyr   r=   r=   r>   _set_read_only  r   zConnection._set_read_onlyc                 C  r   )z5Method version of the `~Connection.read_only` setter.N)r9   r   _set_read_only_genr   r=   r=   r>   r     r   zConnection.set_read_onlyc                 C  r   r6   )set_deferrabler   r=   r=   r>   _set_deferrable  r   zConnection._set_deferrablec                 C  r   )z6Method version of the `~Connection.deferrable` setter.N)r9   r   _set_deferrable_genr   r=   r=   r>   r     r   zConnection.set_deferrablexid	Xid | strc                 C  r   )zO
        Begin a TPC transaction with the given transaction ID `!xid`.
        N)r9   r   _tpc_begin_genr:   r   r=   r=   r>   	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)r9   r   _tpc_prepare_genrY   ObjectNotInPrerequisiteStateNotSupportedErrorrE   )r:   ri   r=   r=   r>   tpc_prepare  s   &zConnection.tpc_prepareXid | str | Nonec                 C  >   | j  | | d| W d   dS 1 sw   Y  dS )z:
        Commit a prepared two-phase transaction.
        COMMITNr9   r   _tpc_finish_genr   r=   r=   r>   
tpc_commit     "zConnection.tpc_commitc                 C  r   )z=
        Roll back a prepared two-phase transaction.
        ROLLBACKNr   r   r=   r=   r>   tpc_rollback  r   zConnection.tpc_rollback	list[Xid]c                 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 )Nr   )
_check_tpcinfor   r   r   r   _from_recordr   _get_recover_queryfetchallIDLEINTRANSrx   )r:   statusr   resr=   r=   r>   tpc_recover  s   
zConnection.tpc_recover)r5   r-   r3   r2   rT   )rD   rE   rA   rF   rB   rG   rC   rH   r3   rI   r0   rJ   rK   r   rL   r   )rL   r   )ro   rp   rq   rr   rs   rt   rL   ru   )rD   rE   rK   r   rL   r   )rL   ru   )r   rF   rL   r   )r   rF   r3   r   rL   r   )
r   rE   r   rF   r   r   r   rF   rL   r   )r   rE   r   rF   r3   r   r   r   r   rF   rL   r   )r   rE   r   rF   r3   r   r   r   r   rF   rL   r   r6   )
r   r   rd   r   r   r   r   rF   rL   r   )rN   r   rL   ru   )NF)r   r   r   rF   rL   r   )rN   r   r   rG   rL   r   )rL   r   )rh   r   rP   r   rL   r   )r   rF   rL   ru   )r   r   rL   ru   )r   r   rL   ru   )r   r   rL   ru   )r   r   rL   ru   )rL   r   )+__name__
__module____qualname____doc____annotations__r   r   r   r   r8   classmethodrk   rm   r   rU   r}   r	   r   r   r{   rx   r   r   r   r   r$   r   rX   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r=   r=   r;   r>   r/   8   s   
 
3


	%

2










r/   )Qr   
__future__r   loggingtimer   typesr   typingr   r   r   r   r	   r
   
contextlibr   r?   r   r   rY   r   abcr   r   r   r   r   r   r   _tpcr   rowsr   r   r   r   adaptr   _enumsr   _compatr   rD   r   r    r!   r"   r4   r#   
generatorsr$   r   r%   r   r&   _capabilitiesr'   server_cursorr(   _connection_baser)   r*   r+   	threadingr,   pq.abcr-   rX   FormatTEXTr   TransactionStatusr   r   r   KeyboardInterruptr   	getLoggerr\   r/   r=   r=   r=   r>   <module>   sJ    $
