o
    Rh/:                     @   s   d dl 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 ZG dd dZdd ZdS )    N)ClauseElement)
UpdateBase)
DDLElement   )exc)create_result_proxy)RootTransactionTransactionNestedTransactionTwoPhaseTransaction   )_TransactionContextManager_SAConnectionContextManagerc                 C   s   | S N )kr   r   X/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/aiomysql/sa/connection.pynoop   s   r   c                   @   s   e Zd Zd8ddZdd Zdd Zdd	 Zd
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d8ddZd d! Zd"d# Zd8d$d%Zd&d' Zd(d) Zd*d+d,d-Zd*d+d.d/Ze	d0d1 Zd2d3 Zd4d5 Zd6d7 ZdS )9SAConnectionNc                 C   s4   || _ d | _d| _t | _|| _|j| _|| _	d S )Nr   )
_connection_transaction_savepoint_seqweakrefWeakSet_weak_results_enginedialect_dialect_compiled_cache)self
connectionenginecompiled_cacher   r   r   __init__   s   

zSAConnection.__init__c                 O   s    | j |g|R i |}t|S )a   Executes a SQL query with optional parameters.

        query - a SQL query string or any sqlalchemy expression.

        *multiparams/**params - represent bound parameter values to be
        used in the execution.  Typically, the format is a dictionary
        passed to *multiparams:

            await conn.execute(
                table.insert(),
                {"id":1, "value":"v1"},
            )

        ...or individual key/values interpreted by **params::

            await conn.execute(
                table.insert(), id=1, value="v1"
            )

        In the case that a plain SQL string is passed, a tuple or
        individual values in *multiparams may be passed::

            await conn.execute(
                "INSERT INTO table (id, value) VALUES (%d, %s)",
                (1, "v1")
            )

            await conn.execute(
                "INSERT INTO table (id, value) VALUES (%s, %s)",
                1, "v1"
            )

        Returns ResultProxy instance with results of SQL query
        execution.

        )_executer   )r   querymultiparamsparamscoror   r   r   execute   s   %zSAConnection.executec                    st   |rt |ttfr|rdd t|jj|D }ntd|| |j	 fdd D g}| j
|}|d S )z
        handle params
        c                 S   s   i | ]\}}|j |qS r   )key).0cpvalr   r   r   
<dictcomp>M   s    z-SAConnection._base_params.<locals>.<dictcomp>z=Don't mix sqlalchemy SELECT clause with positional parametersc                    s"   i | ]}| |t | qS r   )getr   )r+   r*   compiled_params
processorsr   r   r.   V   s    r   )
isinstancelisttupleziptabler,   r   ArgumentErrorconstruct_params_bind_processorsr   execute_sequence_format)r   r%   dpcompiled	is_updater'   post_processed_paramsr   r0   r   _base_paramsG   s   
zSAConnection._base_paramsc           
   	      s   d}t |tr|||I dH  nAt |trtdt |trN|j| jd}g }t |t	}|D ]}|
| |||| q1|t||I dH  |j}ntdt| || j|I dH }	| j|	 |	S )z
        executemany
        N=Don't mix sqlalchemy DDL clause and execution with parametersr   Lsql statement should be str or SQLAlchemy data selection/modification clause)r3   strexecutemanyr   r   r8   r   compiler   r   appendr@   _result_columnsr   r   add)
r   r%   dpscursor
result_mapr=   r'   r>   r<   retr   r   r   _executemany]   sD   




zSAConnection._executemanyc                    sh  | j  I d H }t||}t|dkr| |||I d H S |r$|d }d }t|tr7|||p1d I d H  njt|tr| j	d urh|}| j	
|}|sg|j| jd}|r]| |j ksb|sg|jsg|| j	|< n|j| jd}t|ts| |||t|t}	|j}n|rtd| }	d }|t||	I d H  ntdt| || j|I d H }
| j|
 |
S )Nr   r   rB   rA   rC   )r   rK   _distill_paramslenrN   r3   rD   r)   r   r   r/   rF   r   keysr'   r   r@   r   rH   r   r8   r9   r   r   rI   )r   r%   r&   r'   rK   r<   rL   r*   r=   r?   rM   r   r   r   r$      sV   









zSAConnection._executec                    s.   | j |g|R i |I dH }| I dH S )z0Executes a SQL query and returns a scalar value.N)r)   scalar)r   r%   r&   r'   resr   r   r   rR      s   zSAConnection.scalarc                 C   s   | j du p| j jS )zAThe readonly property that returns True if connections is closed.N)r   closedr   r   r   r   rT         zSAConnection.closedc                 C   s   | j S r   )r   rU   r   r   r   r       s   zSAConnection.connectionc                 C   s   |   }t|S )a   Begin a transaction and return a transaction handle.

        The returned object is an instance of Transaction.  This
        object represents the "scope" of the transaction, which
        completes when either the .rollback or .commit method is
        called.

        Nested calls to .begin on the same SAConnection instance will
        return new Transaction objects that represent an emulated
        transaction within the scope of the enclosing transaction,
        that is::

            trans = await conn.begin()   # outermost transaction
            trans2 = await conn.begin()  # "nested"
            await trans2.commit()        # does nothing
            await trans.commit()         # actually commits

        Calls to .commit only have an effect when invoked via the
        outermost Transaction object, though the .rollback method of
        any of the Transaction objects will roll back the transaction.

        See also:
          .begin_nested - use a SAVEPOINT
          .begin_twophase - use a two phase/XA transaction

        )_beginr   )r   r(   r   r   r   begin   s   zSAConnection.beginc                    s6   | j d u rt| | _ |  I d H  | j S t| | j S r   )r   r   _begin_implr	   rU   r   r   r   rW      s   

zSAConnection._beginc                    sH   | j  I d H }z|dI d H  W | I d H  d S | I d H  w )NBEGIN)r   rK   r)   closer   curr   r   r   rY      s
   "zSAConnection._begin_implc                    T   | j  I d H }z|dI d H  W | I d H  d | _d S | I d H  d | _w )NCOMMITr   rK   r)   r[   r   r\   r   r   r   _commit_impl      
zSAConnection._commit_implc                    r^   )NROLLBACKr`   r\   r   r   r   _rollback_impl   rb   zSAConnection._rollback_implc                    sP   | j du rt| | _ |  I dH  | j S t| | j | _ |  I dH | j _| j S )a  Begin a nested transaction and return a transaction handle.

        The returned object is an instance of :class:`.NestedTransaction`.

        Nested transactions require SAVEPOINT support in the
        underlying database.  Any transaction in the hierarchy may
        .commit() and .rollback(), however the outermost transaction
        still controls the overall .commit() or .rollback() of the
        transaction of a whole.
        N)r   r   rY   r
   _savepoint_impl
_savepointrU   r   r   r   begin_nested  s   

zSAConnection.begin_nestedc                    sd   |  j d7  _ d| j  }| j I d H }z|d| I d H  |W | I d H  S | I d H  w )Nr   zaiomysql_sa_savepoint_%sz
SAVEPOINT )r   r   rK   r)   r[   )r   namer]   r   r   r   re     s   
 zSAConnection._savepoint_implc                    T   | j  I d H }z|d| I d H  W | I d H  n| I d H  w || _d S )NzROLLBACK TO SAVEPOINT r`   r   rh   parentr]   r   r   r   _rollback_to_savepoint_impl"      
z(SAConnection._rollback_to_savepoint_implc                    ri   )NzRELEASE SAVEPOINT r`   rj   r   r   r   _release_savepoint_impl*  rm   z$SAConnection._release_savepoint_implc                    sL   | j durtd|du r| j }t| || _ | d|I dH  | j S )a  Begin a two-phase or XA transaction and return a transaction
        handle.

        The returned object is an instance of
        TwoPhaseTransaction, which in addition to the
        methods provided by Transaction, also provides a
        TwoPhaseTransaction.prepare() method.

        xid - the two phase transaction id.  If not supplied, a
        random id will be generated.
        NzOCannot start a two phase transaction when a transaction is already in progress.zXA START %s)r   r   InvalidRequestErrorr   
create_xidr   r)   r   xidr   r   r   begin_twophase2  s   

zSAConnection.begin_twophasec                    s.   |  d| I d H  |  d| I d H  d S )NXA END '%s'zXA PREPARE '%s'r)   rq   r   r   r   _prepare_twophase_implI  s   z#SAConnection._prepare_twophase_implc                    s    |  dI dH }dd |D S )z3Return a list of prepared twophase transaction ids.zXA RECOVER;Nc                 S   s   g | ]}|d  qS )r   r   )r+   rowr   r   r   
<listcomp>P  s    z1SAConnection.recover_twophase.<locals>.<listcomp>ru   )r   resultr   r   r   recover_twophaseM  s   zSAConnection.recover_twophaseT)is_preparedc                   2   |s|  d| I dH  |  d| I dH  dS )z'Rollback prepared twophase transaction.rt   NzXA ROLLBACK '%s'ru   r   rr   r{   r   r   r   rollback_preparedR     zSAConnection.rollback_preparedc                   r|   )z%Commit prepared twophase transaction.rt   NzXA COMMIT '%s'ru   r}   r   r   r   commit_preparedX  r   zSAConnection.commit_preparedc                 C   s   | j duo| j jS )z,Return True if a transaction is in progress.N)r   	is_activerU   r   r   r   in_transaction^  rV   zSAConnection.in_transactionc                    sL   | j du rdS | jdur| j I dH  d| _| j|  d| _ d| _dS )ao  Close this SAConnection.

        This results in a release of the underlying database
        resources, that is, the underlying connection referenced
        internally. The underlying connection is typically restored
        back to the connection-holding Pool referenced by the Engine
        that produced this SAConnection. Any transactional state
        present on the underlying connection is also unconditionally
        released via calling Transaction.rollback() method.

        After .close() is called, the SAConnection is permanently in a
        closed state, and will allow no further operations.
        N)r   r   rollbackr   releaserU   r   r   r   r[   c  s   


zSAConnection.closec                    s   | S r   r   rU   r   r   r   
__aenter__}  s   zSAConnection.__aenter__c                    s   |   I d H  d S r   )r[   )r   exc_typeexc_valexc_tbr   r   r   	__aexit__  s   zSAConnection.__aexit__r   )__name__
__module____qualname__r#   r)   r@   rN   r$   rR   propertyrT   r    rX   rW   rY   ra   rd   rg   re   rl   rn   rs   rv   rz   r~   r   r   r[   r   r   r   r   r   r   r      s<    
	(*2




r   c                 C   s   | s	|r|gS g S t | dkr;| d }t|ttfr/|r*t|d dr,t|d ds,|S |gS t|dr7|gS |ggS t| d drKt| d dsK| S | gS )a	  Given arguments from the calling form *multiparams, **params,
    return a list of bind parameter structures, usually a list of
    dictionaries.

    In the case of 'raw' execution which accepts positional parameters,
    it may be a list of tuples or lists.

    r   r   __iter__striprQ   )rP   r3   r4   r5   hasattr)r&   r'   zeror   r   r   rO     s(   

rO   )r   sqlalchemy.sqlr   sqlalchemy.sql.dmlr   sqlalchemy.sql.ddlr    r   ry   r   transactionr   r	   r
   r   utilsr   r   r   r   rO   r   r   r   r   <module>   s     r