o
    Rh                     @   s  d dl Z d dlZddlmZ ddlmZmZ ddlmZm	Z	 ddl
mZmZmZmZmZ zd dlmZ d d	lmZ W n eyE   ed
w G dd deZeddZee_de_dddeddfddZdddeddfddZG dd dZeZe	ZG dd dZdS )    N   )SAConnection)InvalidRequestErrorArgumentError   )_PoolContextManager_PoolAcquireContextManager)CursorDeserializationCursor
DictCursorSSCursorSSDictCursor)MySQLDialect_pymysql)MySQLCompiler_mysqldbzaiomysql.sa requires sqlalchemyc                       s&   e Zd Zd fdd	Zdd Z  ZS )MySQLCompiler_pymysqlNTc                    s2   t  |||}| jD ]}| |j||j< q|S N)superconstruct_paramsprefetch_exec_defaultdefaultkey)selfparams_group_number_checkpdcolumn	__class__ T/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/aiomysql/sa/engine.pyr      s   
z&MySQLCompiler_pymysql.construct_paramsc                 C   s   |j r	|| jS |jS r   )is_callableargdialect)r   r   r    r    r!   r      s   z#MySQLCompiler_pymysql._exec_default)NNT)__name__
__module____qualname__r   r   __classcell__r    r    r   r!   r      s    r   pyformat)
paramstyle
   c           	   	      sd   t tttg}|dt t trt fdd|D r tdt	d| |||||d|}t
|S )zA coroutine for Engine creation.

    Returns Engine instance with embedded connection pool.

    The pool has *minsize* opened connections to MySQL server.
    cursorclassc                 3   s    | ]}t  |V  qd S r   )
issubclass).0cursor_classr-   r    r!   	<genexpr>7   s
    
z create_engine.<locals>.<genexpr>z4SQLAlchemy engine does not support this cursor class)minsizemaxsizeloopr$   pool_recyclecompiled_cacheNr    )r
   r   r   r   getr	   r.   anyr   _create_engine_EngineContextManager)	r3   r4   r5   r$   r6   r7   kwargsdeprecated_cursor_classescoror    r1   r!   create_engine)   s   

r?   c           	   	      sp   |d u r	t  }tjd| |||d|I d H }| I d H }zt||fd|i|W || S || w )N)r3   r4   r5   r6   r7   r    )asyncioget_event_loopaiomysqlcreate_poolacquireEnginerelease)	r3   r4   r5   r$   r6   r7   r<   poolconnr    r    r!   r:   D   s   
r:   c                   @   s   e Zd ZdZd)ddZedd Zedd Zed	d
 Zedd Z	e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!d" Zd#d$ Zd%d& Zd'd( ZdS )*rE   zConnects a aiomysql.Pool and
    sqlalchemy.engine.interfaces.Dialect together to provide a
    source of database connectivity and behavior.

    An Engine object is instantiated publicly using the
    create_engine coroutine.
    Nc                 K   s   || _ || _|| _|| _d S r   )_dialect_pool_compiled_cache_conn_kw)r   r$   rG   r7   r<   r    r    r!   __init__]   s   
zEngine.__init__c                 C   s   | j S )zAn dialect for engine.)rI   r   r    r    r!   r$   c   s   zEngine.dialectc                 C      | j jS )zA name of the dialect.)rI   namerN   r    r    r!   rP   h      zEngine.namec                 C   rO   )zA driver of the dialect.)rI   driverrN   r    r    r!   rR   m   rQ   zEngine.driverc                 C   rO   r   )rJ   r3   rN   r    r    r!   r3   r      zEngine.minsizec                 C   rO   r   )rJ   r4   rN   r    r    r!   r4   v   rS   zEngine.maxsizec                 C   rO   r   )rJ   sizerN   r    r    r!   rT   z   rS   zEngine.sizec                 C   rO   r   )rJ   freesizerN   r    r    r!   rU   ~   rS   zEngine.freesizec                 C      | j   dS )zClose engine.

        Mark all engine connections to be closed on getting back to pool.
        Closed engine doesn't allow to acquire new connections.
        N)rJ   closerN   r    r    r!   rW         zEngine.closec                 C   rV   )zwTerminate engine.

        Terminate engine pool with instantly closing all acquired
        connections also.
        N)rJ   	terminaterN   r    r    r!   rY      rX   zEngine.terminatec                    s   | j  I dH  dS )z*Wait for closing all engine's connections.N)rJ   wait_closedrN   r    r    r!   rZ      s   zEngine.wait_closedc                 C   s   |   }t|| S )zGet a connection from pool.)_acquire_EngineAcquireContextManager)r   r>   r    r    r!   rD      s   
zEngine.acquirec                    s&   | j  I d H }t|| | jd}|S )N)r7   )rJ   rD   r   rK   )r   rawrH   r    r    r!   r[      s   zEngine._acquirec                 C   s    |j rtd|j}| j|S )zRevert back connection to pool.z9Cannot release a connection with not finished transaction)in_transactionr   
connectionrJ   rF   )r   rH   r]   r    r    r!   rF      s   zEngine.releasec                 C   s   t d)Nz9"yield from" should be used as context manager expression)RuntimeErrorrN   r    r    r!   	__enter__   s   zEngine.__enter__c                 G   s   d S r   r    r   argsr    r    r!   __exit__   s   zEngine.__exit__c                 c   s    |   E d H }t| |S r   )rD   _ConnectionContextManager)r   rH   r    r    r!   __iter__   s   
zEngine.__iter__c                    s   | S r   r    rN   r    r    r!   
__aenter__   s   zEngine.__aenter__c                    s   |    |  I d H  d S r   )rW   rZ   )r   exc_typeexc_valexc_tbr    r    r!   	__aexit__   s   zEngine.__aexit__r   )r%   r&   r'   __doc__rM   propertyr$   rP   rR   r3   r4   rT   rU   rW   rY   rZ   rD   r[   rF   ra   rd   rf   rg   rk   r    r    r    r!   rE   T   s8    







rE   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
re   a+  Context manager.

    This enables the following idiom for acquiring and releasing a
    connection around a block:

        with (yield from engine) as conn:
            cur = yield from conn.cursor()

    while failing loudly when accidentally using:

        with engine:
            <block>
    _engine_connc                 C   s   || _ || _d S r   rn   )r   enginerH   r    r    r!   rM      s   
z"_ConnectionContextManager.__init__c                 C   s   | j d usJ | j S r   )rp   rN   r    r    r!   ra      s   z#_ConnectionContextManager.__enter__c                 G   s0   z| j | j W d | _ d | _d S d | _ d | _w r   )ro   rF   rp   rb   r    r    r!   rd      s   
z"_ConnectionContextManager.__exit__N)r%   r&   r'   rl   	__slots__rM   ra   rd   r    r    r    r!   re      s    re   )r@   rB   r_   r   excr   r   utilsr   r   cursorsr	   r
   r   r   r   !sqlalchemy.dialects.mysql.pymysqlr   !sqlalchemy.dialects.mysql.mysqldbr   ImportErrorr   rI   statement_compilerdefault_paramstyler?   r:   rE   r;   r\   re   r    r    r    r!   <module>   s6   


u