o
    `^h0                     @   sN   d dl mZ G dd dZG dd deZG dd deZG dd	 d	eZd
S )   )excc                   @   sh   e Zd ZdZdd Zedd Zedd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZdS )Transactiona  Represent a database transaction in progress.

    The Transaction object is procured by
    calling the SAConnection.begin() method of
    SAConnection:

        with (yield from engine) as conn:
            trans = yield from conn.begin()
            try:
                yield from conn.execute("insert into x (a, b) values (1, 2)")
            except Exception:
                yield from trans.rollback()
            else:
                yield from trans.commit()

    The object provides .rollback() and .commit()
    methods in order to control transaction boundaries.

    See also:  SAConnection.begin(), SAConnection.begin_twophase(),
    SAConnection.begin_nested().
    c                 C   s   || _ |p| | _d| _d S )NT)_connection_parent
_is_activeself
connectionparent r   U/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/aiomysql/sa/transaction.py__init__   s   

zTransaction.__init__c                 C      | j S )z+Return ``True`` if a transaction is active.)r   r   r   r   r   	is_active"      zTransaction.is_activec                 C   r   )z8Return transaction's connection (SAConnection instance).)r   r   r   r   r   r	   '   r   zTransaction.connectionc                    s4   | j jsdS | j | u r|  I dH  dS d| _dS )a0  Close this transaction.

        If this transaction is the base transaction in a begin/commit
        nesting, the transaction will rollback().  Otherwise, the
        method returns.

        This is used to cancel a Transaction without affecting the scope of
        an enclosing transaction.
        NF)r   r   rollbackr   r   r   r   close,   s   


zTransaction.closec                    s&   | j jsdS |  I dH  d| _dS )zRoll back this transaction.NF)r   r   _do_rollbackr   r   r   r   r   =   s
   
zTransaction.rollbackc                       | j  I d H  d S N)r   r   r   r   r   r   r   D      zTransaction._do_rollbackc                    s,   | j js
td|  I dH  d| _dS )zCommit this transaction.This transaction is inactiveNF)r   r   r   InvalidRequestError
_do_commitr   r   r   r   commitG   s
   

zTransaction.commitc                    s   d S r   r   r   r   r   r   r   O      zTransaction._do_commitc                    s   | S r   r   r   r   r   r   
__aenter__R   r   zTransaction.__aenter__c                    s4   |r|   I d H  d S | jr|  I d H  d S d S r   )r   r   r   )r   exc_typeexc_valexc_tbr   r   r   	__aexit__U   s   zTransaction.__aexit__N)__name__
__module____qualname____doc__r   propertyr   r	   r   r   r   r   r   r   r!   r   r   r   r   r      s    

r   c                       s,   e Zd Z fddZdd Zdd Z  ZS )RootTransactionc                    s   t  |d  d S r   superr   )r   r	   	__class__r   r   r   _      zRootTransaction.__init__c                    r   r   )r   _rollback_implr   r   r   r   r   b   r   zRootTransaction._do_rollbackc                    r   r   )r   _commit_implr   r   r   r   r   e   r   zRootTransaction._do_commit)r"   r#   r$   r   r   r   __classcell__r   r   r*   r   r'   ]   s    r'   c                       s4   e Zd ZdZdZ fddZdd Zdd Z  ZS )	NestedTransactionzRepresent a 'nested', or SAVEPOINT transaction.

    A new NestedTransaction object may be procured
    using the SAConnection.begin_nested() method.

    The interface is the same as that of Transaction class.
    Nc                    s   t  || d S r   r(   r   r*   r   r   r   t   r,   zNestedTransaction.__init__c                    :   | j d us
J d| jr| j| j | jI d H  d S d S NzBroken transaction logic)
_savepointr   r   _rollback_to_savepoint_implr   r   r   r   r   r   w      zNestedTransaction._do_rollbackc                    r1   r2   )r3   r   r   _release_savepoint_implr   r   r   r   r   r   }   r5   zNestedTransaction._do_commit)	r"   r#   r$   r%   r3   r   r   r   r/   r   r   r*   r   r0   i   s    r0   c                       sD   e Zd ZdZ fddZedd Zdd Zdd	 Zd
d Z	  Z
S )TwoPhaseTransactionzRepresent a two-phase transaction.

    A new TwoPhaseTransaction object may be procured
    using the SAConnection.begin_twophase() method.

    The interface is the same as that of Transaction class
    with the addition of the .prepare() method.
    c                    s   t  |d  d| _|| _d S )NF)r)   r   _is_prepared_xid)r   r	   xidr*   r   r   r      s   
zTwoPhaseTransaction.__init__c                 C   r   )z Returns twophase transaction id.)r9   r   r   r   r   r:      r   zTwoPhaseTransaction.xidc                    s2   | j js
td| j| jI dH  d| _dS )zfPrepare this TwoPhaseTransaction.

        After a PREPARE, the transaction can be committed.
        r   NT)r   r   r   r   r   _prepare_twophase_implr9   r8   r   r   r   r   prepare   s
   

zTwoPhaseTransaction.preparec                        | j j| j| jdI d H  d S N)is_prepared)r   rollback_preparedr9   r8   r   r   r   r   r         z TwoPhaseTransaction._do_rollbackc                    r=   r>   )r   commit_preparedr9   r8   r   r   r   r   r      rA   zTwoPhaseTransaction._do_commit)r"   r#   r$   r%   r   r&   r:   r<   r   r   r/   r   r   r*   r   r7      s    	
r7   N) r   r   r'   r0   r7   r   r   r   r   <module>   s
   W