U
    cc&                     @   s   d 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
 dd	lmZ 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d ZG dd dZG dd deZeZdS )a\  
.. dialect:: mysql+asyncmy
    :name: asyncmy
    :dbapi: asyncmy
    :connectstring: mysql+asyncmy://user:password@host:port/dbname[?key=value&key=value...]
    :url: https://github.com/long2ice/asyncmy

.. note:: The asyncmy dialect as of September, 2021 was added to provide
   MySQL/MariaDB asyncio compatibility given that the :ref:`aiomysql` database
   driver has become unmaintained, however asyncmy is itself very new.

Using a special asyncio mediation layer, the asyncmy dialect is usable
as the backend for the :ref:`SQLAlchemy asyncio <asyncio_toplevel>`
extension package.

This dialect should normally be used only with the
:func:`_asyncio.create_async_engine` engine creation function::

    from sqlalchemy.ext.asyncio import create_async_engine
    engine = create_async_engine("mysql+asyncmy://user:pass@hostname/dbname?charset=utf8mb4")


   )MySQLDialect_pymysql   )pool)util)AdaptedConnection)asynccontextmanager)asyncio)await_fallback)
await_onlyc                   @   s   e Zd ZdZdZdd Zedd Zedd Zed	d
 Z	e	j
dd
 Z	edd Zdd Zd#ddZdd Zdd Zdd Zdd Zdd Zdd Zd$dd Zd!d" ZdS )%AsyncAdapt_asyncmy_cursorF)_adapt_connection_connectionawait__cursor_rowsc                 C   s:   || _ |j| _|j| _| j }| | | _g | _d S N)r   r   r   cursor
__aenter__r   r   selfZadapt_connectionr    r   E/tmp/pip-unpacked-wheel-8u86ls_i/sqlalchemy/dialects/mysql/asyncmy.py__init__3   s    
z"AsyncAdapt_asyncmy_cursor.__init__c                 C   s   | j jS r   )r   descriptionr   r   r   r   r   =   s    z%AsyncAdapt_asyncmy_cursor.descriptionc                 C   s   | j jS r   )r   rowcountr   r   r   r   r   A   s    z"AsyncAdapt_asyncmy_cursor.rowcountc                 C   s   | j jS r   r   	arraysizer   r   r   r   r   E   s    z#AsyncAdapt_asyncmy_cursor.arraysizec                 C   s   || j _d S r   r   r   valuer   r   r   r   I   s    c                 C   s   | j jS r   )r   	lastrowidr   r   r   r   r    M   s    z#AsyncAdapt_asyncmy_cursor.lastrowidc                 C   s   g | j d d < d S r   r   r   r   r   r   closeQ   s    zAsyncAdapt_asyncmy_cursor.closeNc                 C   s   |  | ||S r   )r   _execute_async)r   	operation
parametersr   r   r   execute[   s    z!AsyncAdapt_asyncmy_cursor.executec                 C   s   |  | ||S r   )r   _executemany_asyncr   r$   Zseq_of_parametersr   r   r   executemany^   s    
z%AsyncAdapt_asyncmy_cursor.executemanyc                    s   | j  4 I d H d |d kr0| j|I d H }n| j||I d H }| js`t| j I d H | _|W  5 Q I d H R  S Q I d H R X d S r   )r   _mutex_and_adapt_errorsr   r&   server_sidelistfetchallr   )r   r$   r%   resultr   r   r   r#   c   s    z(AsyncAdapt_asyncmy_cursor._execute_asyncc                    sJ   | j  4 I d H ( | j||I d H W  5 Q I d H R  S Q I d H R X d S r   )r   r*   r   r)   r(   r   r   r   r'   r   s    z,AsyncAdapt_asyncmy_cursor._executemany_asyncc                 G   s   d S r   r   )r   Z
inputsizesr   r   r   setinputsizesv   s    z'AsyncAdapt_asyncmy_cursor.setinputsizesc                 c   s   | j r| j dV  q d S N    r   popr   r   r   r   __iter__y   s    z"AsyncAdapt_asyncmy_cursor.__iter__c                 C   s   | j r| j dS d S d S r0   r2   r   r   r   r   fetchone}   s    z"AsyncAdapt_asyncmy_cursor.fetchonec                 C   s8   |d kr| j }| jd| }| j|d  | jd d < |S r0   )r   r   )r   sizeretvalr   r   r   	fetchmany   s
    z#AsyncAdapt_asyncmy_cursor.fetchmanyc                 C   s    | j d d  }g | j d d < |S r   r!   )r   r7   r   r   r   r-      s    z"AsyncAdapt_asyncmy_cursor.fetchall)N)N)__name__
__module____qualname__r+   	__slots__r   propertyr   r   r   setterr    r"   r&   r)   r#   r'   r/   r4   r5   r8   r-   r   r   r   r   r   )   s.   








r   c                   @   s>   e Zd ZdZdZdd Zdd Zdd Zdd
dZdd Z	d	S )AsyncAdapt_asyncmy_ss_cursorr   Tc                 C   s>   || _ |j| _|j| _| j|jjjj}| | | _	d S r   )
r   r   r   r   dbapiasyncmyZcursorsZSSCursorr   r   r   r   r   r   r      s    
z%AsyncAdapt_asyncmy_ss_cursor.__init__c                 C   s$   | j d k	r | | j   d | _ d S r   )r   r   r"   r   r   r   r   r"      s    
z"AsyncAdapt_asyncmy_ss_cursor.closec                 C   s   |  | j S r   )r   r   r5   r   r   r   r   r5      s    z%AsyncAdapt_asyncmy_ss_cursor.fetchoneNc                 C   s   |  | jj|dS )N)r6   )r   r   r8   )r   r6   r   r   r   r8      s    z&AsyncAdapt_asyncmy_ss_cursor.fetchmanyc                 C   s   |  | j S r   )r   r   r-   r   r   r   r   r-      s    z%AsyncAdapt_asyncmy_ss_cursor.fetchall)N)
r9   r:   r;   r<   r+   r   r"   r5   r8   r-   r   r   r   r   r?      s   
r?   c                   @   sn   e Zd ZeeZdZdd Zedd Z	dd Z
dd	 Zd
d Zdd ZdddZdd Zdd Zdd ZdS )AsyncAdapt_asyncmy_connection)r@   r   _execute_mutexc                 C   s   || _ || _t | _d S r   )r@   r   r   LockrC   )r   r@   
connectionr   r   r   r      s    z&AsyncAdapt_asyncmy_connection.__init__c              
   C  sP   | j 4 I d H 2 z
d V  W n  tk
r:   | jdY nX W 5 Q I d H R X d S )Nz7network operation failed due to asyncmy attribute error)rC   AttributeErrorr@   InternalErrorr   r   r   r   r*      s    
z5AsyncAdapt_asyncmy_connection._mutex_and_adapt_errorsc                 C   s   |rt | |  S r   )AssertionErrorr   _do_ping)r   Z	reconnectr   r   r   ping   s    z"AsyncAdapt_asyncmy_connection.pingc                    sF   |   4 I d H & | jdI d H W  5 Q I d H R  S Q I d H R X d S )NF)r*   r   rJ   r   r   r   r   rI      s    z&AsyncAdapt_asyncmy_connection._do_pingc                 C   s
   | j  S r   )r   character_set_namer   r   r   r   rK      s    z0AsyncAdapt_asyncmy_connection.character_set_namec                 C   s   |  | j| d S r   )r   r   
autocommitr   r   r   r   rL      s    z(AsyncAdapt_asyncmy_connection.autocommitFc                 C   s   |rt | S t| S d S r   )r?   r   )r   r+   r   r   r   r      s    z$AsyncAdapt_asyncmy_connection.cursorc                 C   s   |  | j  d S r   )r   r   rollbackr   r   r   r   rM      s    z&AsyncAdapt_asyncmy_connection.rollbackc                 C   s   |  | j  d S r   )r   r   commitr   r   r   r   rN      s    z$AsyncAdapt_asyncmy_connection.commitc                 C   s   | j   d S r   )r   r"   r   r   r   r   r"      s    z#AsyncAdapt_asyncmy_connection.closeN)F)r9   r:   r;   staticmethodr
   r   r<   r   r   r*   rJ   rI   rK   rL   r   rM   rN   r"   r   r   r   r   rB      s   
	
rB   c                   @   s   e Zd ZdZeeZdS )%AsyncAdaptFallback_asyncmy_connectionr   N)r9   r:   r;   r<   rO   r	   r   r   r   r   r   rP      s   rP   c                 C   s   t | S )zReturn x as a binary type.)bytes)xr   r   r   _Binary   s    rS   c                   @   s^   e Zd Zdd Zdd ZedZedZedZ	edZ
ed	ZeeZd
d ZdS )AsyncAdapt_asyncmy_dbapic                 C   s   || _ d| _|   d S )Nformat)rA   Z
paramstyle_init_dbapi_attributes)r   rA   r   r   r   r      s    z!AsyncAdapt_asyncmy_dbapi.__init__c                 C   s$   dD ]}t | |t| jj| qd S )N)WarningErrorInterfaceErrorZ	DataErrorZDatabaseErrorZOperationalErrorrY   ZIntegrityErrorZProgrammingErrorrG   ZNotSupportedError)setattrgetattrrA   errors)r   namer   r   r   rV      s    z/AsyncAdapt_asyncmy_dbapi._init_dbapi_attributesSTRINGNUMBERBINARYDATETIME	TIMESTAMPc                 O   sJ   | dd}t|r.t| t| jj||S t| t| jj||S d S Nasync_fallbackF)	r3   r   asboolrP   r	   rA   connectrB   r
   )r   argkwrd   r   r   r   rf   	  s    
z AsyncAdapt_asyncmy_dbapi.connectN)r9   r:   r;   r   rV   r   symbolr^   r_   r`   ra   rb   rO   rS   Binaryrf   r   r   r   r   rT      s   




rT   c                       sd   e Zd ZdZdZdZeZdZe	dd Z
e	dd Z fddZ fd	d
Zdd Zdd Z  ZS )MySQLDialect_asyncmyrA   Tc                 C   s   t tdS )NrA   )rT   
__import__)clsr   r   r   r@   !  s    zMySQLDialect_asyncmy.dbapic                 C   s(   |j dd}t|rtjS tjS d S rc   )querygetr   re   r   ZFallbackAsyncAdaptedQueuePoolZAsyncAdaptedQueuePool)rm   urlrd   r   r   r   get_pool_class%  s    
z#MySQLDialect_asyncmy.get_pool_classc                    s   t t| j|tddddS )Nuserdb)usernameZdatabase)Z_translate_args)superrk   create_connect_argsdict)r   rp   	__class__r   r   rv   /  s    
 
z(MySQLDialect_asyncmy.create_connect_argsc                    s8   t t| |||rdS t| }d|kp2d|kS d S )NTznot connectedznetwork operation failed)ru   rk   is_disconnectstrlower)r   erE   r   Zstr_erx   r   r   rz   4  s    
  z"MySQLDialect_asyncmy.is_disconnectc                 C   s   ddl m} |jS )Nr1   )CLIENT)Zasyncmy.constantsr~   Z
FOUND_ROWS)r   r~   r   r   r   _found_rows_client_flag?  s    z,MySQLDialect_asyncmy._found_rows_client_flagc                 C   s   |j S r   )r   )r   rE   r   r   r   get_driver_connectionD  s    z*MySQLDialect_asyncmy.get_driver_connection)r9   r:   r;   ZdriverZsupports_statement_cacheZsupports_server_side_cursorsr?   Z	_sscursorZis_asyncclassmethodr@   rq   rv   rz   r   r   __classcell__r   r   rx   r   rk     s   

	rk   N)__doc__Zpymysqlr    r   r   Zenginer   Zutil.concurrencyr   r   r	   r
   r   r?   rB   rP   rS   rT   rk   dialectr   r   r   r   <module>   s    h2,0