o
    "4g*$                     @  s   d Z ddlmZ ddl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 ddlmZ e
rRddlmZ ddlmZ ejjZejjZee Z!G dd de"Z#G dd dej$Z%G dd dee Z&G dd de&d Z'G dd de&d Z(dS )zC
Transaction context managers returned by Connection.transaction()
    )annotationsN)TracebackType)AnyGenericIteratorTYPE_CHECKING   )pq)sql)errors)ConnectionTypePQGen)Self)connection_summary)
Connection)AsyncConnectionc                   @  s*   e Zd ZdZdZddddZdd
dZdS )Rollbacka  
    Exit the current `Transaction` context immediately and rollback any changes
    made within this context.

    If a transaction context is specified in the constructor, rollback
    enclosing transactions contexts up to and including the one specified.
    psycopgNtransaction%Transaction | AsyncTransaction | Nonec                 C  s
   || _ d S N)r   )selfr    r   \/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/transaction.py__init__+   s   
zRollback.__init__returnstrc                 C  s   | j j d| jdS )N())	__class____qualname__r   r   r   r   r   __repr__.   s   zRollback.__repr__r   )r   r   r   r   )__name__
__module__r    __doc__r   r"   r   r   r   r   r       s
    r   c                   @  s   e Zd ZdZdS )OutOfOrderTransactionNestingz)Out-of-order transaction nesting detectedN)r$   r%   r    r&   r   r   r   r   r'   2   s    r'   c                   @  s   e Zd Z		d/d0d	d
Zed1ddZd2ddZd3ddZd4ddZd3ddZ	d5dd Z
d6d"d#Zd6d$d%Zd6d&d'Zd7d)d*Zd8d-d.ZdS )9BaseTransactionNF
connectionr   savepoint_name
str | Noneforce_rollbackboolc                 C  s<   || _ | j j| _|pd| _|| _d | _| _d| _d| _d S )N F)_connpgconn_savepoint_namer,   _entered_exited_outer_transaction_stack_index)r   r)   r*   r,   r   r   r   r   7   s   


zBaseTransaction.__init__r   c                 C     | j S )zV
        The name of the savepoint; `!None` if handling the main transaction.
        )r2   r!   r   r   r   r*   E   s   zBaseTransaction.savepoint_namer   c                 C  s|   | j j d| j j }t| j}| jsd}n| jsd}nd}| jr'| jdnd}d| d| d| d	| d
t| ddS )N.inactiveactive
terminated r.   <r   z) z at 0xx>)	r   r%   r    r   r1   r3   r4   r*   id)r   clsinfostatusspr   r   r   r"   N   s   
*zBaseTransaction.__repr__PQGen[None]c                 c  sB    | j rtdd| _ |   |  D ]}| j|E d H  qd S )Nz(transaction blocks can be used only onceT)r3   	TypeError_push_savepoint_get_enter_commandsr0   _exec_command)r   commandr   r   r   
_enter_gen[   s   zBaseTransaction._enter_genexc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | NonePQGen[bool]c              
   c  st    |s| j s|  E d H  dS z	| |E d H W S  ty      ty9 } ztd| | W Y d }~dS d }~ww )NFz#error ignored in rollback of %s: %s)r,   _commit_gen_rollback_genr'   	Exceptionloggerwarning)r   rL   rN   rP   exc2r   r   r   	_exit_gend   s   
zBaseTransaction._exit_genc                 c  s>    |  d}d| _|r||  D ]}| j|E d H  qd S )NcommitT)_pop_savepointr4   _get_commit_commandsr0   rI   )r   exrJ   r   r   r   rS   }   s   
zBaseTransaction._commit_genc                 c  s    t |trtj| j ddd | d}d| _|r||  D ]}| j|E d H  q!| jj	
  | jj	| jE d H  t |trM|jrK|j| u rMdS dS )Nz: Explicit rollback from: T)exc_inforollbackF)
isinstancer   rV   debugr0   r[   r4   _get_rollback_commandsrI   	_preparedclearmaintain_genr   )r   rN   r]   rJ   r   r   r   rT      s   


zBaseTransaction._rollback_genIterator[bytes]c                 c  sD    | j r
| j V  | jr tdt| j| jV  d S d S )NzSAVEPOINT {})	r5   r0   _get_tx_start_commandr2   r
   SQLformat
Identifieras_bytesr!   r   r   r   rH      s   z#BaseTransaction._get_enter_commandsc                 c  sP    | j r| jstdt| j | jV  | jr&| jjr!J dV  d S d S )Nz
RELEASE {}s   COMMIT	r2   r5   r
   rh   ri   rj   rk   r0   _num_transactionsr!   r   r   r   r\      s   
z$BaseTransaction._get_commit_commandsc                 c  sv    | j r+| js+tdjt| j d| jV  tdjt| j d| jV  | jr9| jjr4J dV  d S d S )NzROLLBACK TO {n})nzRELEASE {n}s   ROLLBACKrl   r!   r   r   r   rb      s    
z&BaseTransaction._get_rollback_commandsNonec                 C  sZ   | j jtk| _| jr| jjrJ n| jsd| jjd  | _| jj| _| j jd7  _dS )z
        Push the transaction on the connection transactions stack.

        Also set the internal state of the object and verify consistency.
        _pg3_r   N)r1   transaction_statusIDLEr5   r0   rm   r2   r6   r!   r   r   r   rG      s   
zBaseTransaction._push_savepointactionException | Nonec                 C  s6   | j  jd8  _| j j| jkrdS td| d|  S )zy
        Pop the transaction from the connection transactions stack.

        Also verify the state consistency.
        r   Nztransaction z at the wrong nesting level: )r0   rm   r6   r'   )r   rs   r   r   r   r[      s   zBaseTransaction._pop_savepointNF)r)   r   r*   r+   r,   r-   )r   r+   r#   )r   rE   )rL   rM   rN   rO   rP   rQ   r   rR   )rN   rO   r   rR   )r   rf   )r   ro   )rs   r   r   rt   )r$   r%   r    r   propertyr*   r"   rK   rY   rS   rT   rH   r\   rb   rG   r[   r   r   r   r   r(   6   s     


	

	



r(   c                   @  6   e Zd ZdZdZedddZddd	ZdddZdS )TransactionzO
    Returned by `Connection.transaction()` to handle a transaction block.
    r   r   Connection[Any]c                 C  r7   )z&The connection the object is managing.r0   r!   r   r   r   r)      s   zTransaction.connectionr   c                 C  s>   | j j | j |   W d    | S 1 sw   Y  | S r   r0   lockwaitrK   r!   r   r   r   	__enter__   s   

zTransaction.__enter__rL   rM   rN   rO   rP   rQ   r-   c                 C  sR   | j jtkr'| jj | j| |||W  d    S 1 s w   Y  d S dS ru   r1   rC   OKr0   r|   r}   rY   r   rL   rN   rP   r   r   r   __exit__   s
   
$zTransaction.__exit__N)r   ry   r   r   rL   rM   rN   rO   rP   rQ   r   r-   )r$   r%   r    r&   rv   r)   r~   r   r   r   r   r   rx      s    
rx   ry   c                   @  rw   )AsyncTransactionzT
    Returned by `AsyncConnection.transaction()` to handle a transaction block.
    r   r   AsyncConnection[Any]c                 C  r7   r   rz   r!   r   r   r   r)     s   zAsyncTransaction.connectionr   c              	     sZ   | j j4 I d H  | j |  I d H  W d   I d H  | S 1 I d H s&w   Y  | S r   r{   r!   r   r   r   
__aenter__  s   zAsyncTransaction.__aenter__rL   rM   rN   rO   rP   rQ   r-   c              	     sn   | j jtkr5| jj4 I d H  | j| |||I d H W  d   I d H  S 1 I d H s.w   Y  d S dS ru   r   r   r   r   r   	__aexit__  s   0zAsyncTransaction.__aexit__N)r   r   r   r   )r$   r%   r    r&   rv   r)   r   r   r   r   r   r   r     s    
r   r   ))r&   
__future__r   loggingtypesr   typingr   r   r   r   r.   r	   r
   r   eabcr   r   _compatr   pq.miscr   r)   r   connection_asyncr   TransactionStatusrr   
ConnStatusr   	getLoggerr$   rV   rU   r   ProgrammingErrorr'   r(   rx   r   r   r   r   r   <module>   s.    
 1