o
    Šåga'  ã                   @   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 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

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"
    )

é    )Údeque)Úasynccontextmanageré   )ÚMySQLDialect_pymysqlé   )Úpool)Úutil)ÚAdaptedConnection)Ú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 ¡ }|  | ¡ ¡| _tƒ | _d S ©N)r   r   r   ÚcursorÚ
__aenter__r   r   r   ©ÚselfÚadapt_connectionr   © r   ú]/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/dialects/mysql/asyncmy.pyÚ__init__6   s   
z"AsyncAdapt_asyncmy_cursor.__init__c                 C   ó   | j jS r   )r   Údescription©r   r   r   r   r   @   ó   z%AsyncAdapt_asyncmy_cursor.descriptionc                 C   r   r   )r   Úrowcountr   r   r   r   r    D   r   z"AsyncAdapt_asyncmy_cursor.rowcountc                 C   r   r   ©r   Ú	arraysizer   r   r   r   r"   H   r   z#AsyncAdapt_asyncmy_cursor.arraysizec                 C   s   || j _d S r   r!   ©r   Úvaluer   r   r   r"   L   ó   c                 C   r   r   )r   Ú	lastrowidr   r   r   r   r&   P   r   z#AsyncAdapt_asyncmy_cursor.lastrowidc                 C   ó   | j  ¡  d S r   )r   Úclearr   r   r   r   ÚcloseT   s   zAsyncAdapt_asyncmy_cursor.closeNc                 C   ó   |   |  ||¡¡S r   )r   Ú_execute_async)r   Ú	operationÚ
parametersr   r   r   Úexecute^   s   z!AsyncAdapt_asyncmy_cursor.executec                 C   r*   r   )r   Ú_executemany_async©r   r,   Úseq_of_parametersr   r   r   Úexecutemanya   s   
ÿz%AsyncAdapt_asyncmy_cursor.executemanyc              	   Ã   s”   �| j  ¡ 4 I d H š3 |d u r| j |¡I d H }n
| j ||¡I d H }| js1t| j ¡ I d H ƒ| _|W  d   ƒI d H  S 1 I d H sCw   Y  d S r   )r   Ú_mutex_and_adapt_errorsr   r.   Úserver_sider   Úfetchallr   )r   r,   r-   Úresultr   r   r   r+   f   s   €0ôz(AsyncAdapt_asyncmy_cursor._execute_asyncc              	   Ã   sX   �| j  ¡ 4 I d H š | j ||¡I d H W  d   ƒI d H  S 1 I d H s%w   Y  d S r   )r   r3   r   r2   r0   r   r   r   r/   u   s   €0ÿz,AsyncAdapt_asyncmy_cursor._executemany_asyncc                 G   s   d S r   r   )r   Ú
inputsizesr   r   r   Úsetinputsizesy   s   z'AsyncAdapt_asyncmy_cursor.setinputsizesc                 c   s"   � | j r| j  ¡ V  | j sd S d S r   ©r   Úpopleftr   r   r   r   Ú__iter__|   s   €ÿz"AsyncAdapt_asyncmy_cursor.__iter__c                 C   s   | j r| j  ¡ S d S r   r9   r   r   r   r   Úfetchone€   s   
z"AsyncAdapt_asyncmy_cursor.fetchonec                    s4   |d u r| j }| j‰ ‡ fdd„tt|tˆ ƒƒƒD ƒS )Nc                    s   g | ]}ˆ   ¡ ‘qS r   )r:   )Ú.0Ú_©Úrrr   r   Ú
<listcomp>‹   s    z7AsyncAdapt_asyncmy_cursor.fetchmany.<locals>.<listcomp>)r"   r   ÚrangeÚminÚlen©r   Úsizer   r?   r   Ú	fetchmany†   s    z#AsyncAdapt_asyncmy_cursor.fetchmanyc                 C   s   t | jƒ}| j ¡  |S r   )Úlistr   r(   )r   Úretvalr   r   r   r5   �   s   

z"AsyncAdapt_asyncmy_cursor.fetchallr   )Ú__name__Ú
__module__Ú__qualname__r4   Ú	__slots__r   Úpropertyr   r    r"   Úsetterr&   r)   r.   r2   r+   r/   r8   r;   r<   rG   r5   r   r   r   r   r   *   s0    








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ÚasyncmyÚcursorsÚSSCursorr   r   r   r   r   r   r   ™   s   
ÿz%AsyncAdapt_asyncmy_ss_cursor.__init__c                 C   s(   | j d ur|  | j  ¡ ¡ d | _ d S d S r   )r   r   r)   r   r   r   r   r)   ¤   s   

þz"AsyncAdapt_asyncmy_ss_cursor.closec                 C   ó   |   | j ¡ ¡S r   )r   r   r<   r   r   r   r   r<   ©   ó   z%AsyncAdapt_asyncmy_ss_cursor.fetchoneNc                 C   s   |   | jj|d�¡S )N)rF   )r   r   rG   rE   r   r   r   rG   ¬   ó   z&AsyncAdapt_asyncmy_ss_cursor.fetchmanyc                 C   rU   r   )r   r   r5   r   r   r   r   r5   ¯   rV   z%AsyncAdapt_asyncmy_ss_cursor.fetchallr   )
rJ   rK   rL   rM   r4   r   r)   r<   rG   r5   r   r   r   r   rP   “   s    
rP   c                   @   sx   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dd„ZdS )ÚAsyncAdapt_asyncmy_connection)rQ   Ú_execute_mutexc                 C   s   || _ || _t ¡ | _d S r   )rQ   r   r
   ÚLockrY   )r   rQ   Ú
connectionr   r   r   r   ¹   s   z&AsyncAdapt_asyncmy_connection.__init__c              	   C  sh   �| j 4 I d H š zd V  W n ty   | j d¡‚w W d   ƒI d H  d S 1 I d H s-w   Y  d S )Nz7network operation failed due to asyncmy attribute error)rY   ÚAttributeErrorrQ   ÚInternalErrorr   r   r   r   r3   ¾   s   €
ÿÿÿ.þz5AsyncAdapt_asyncmy_connection._mutex_and_adapt_errorsc                 C   s   |rJ ‚|   |  ¡ ¡S r   )r   Ú_do_ping)r   Ú	reconnectr   r   r   ÚpingÈ   s   z"AsyncAdapt_asyncmy_connection.pingc              	   Ã   sT   �|   ¡ 4 I d H š | j d¡I d H W  d   ƒI d H  S 1 I d H s#w   Y  d S )NF)r3   r   r`   r   r   r   r   r^   Ì   s   €0ÿz&AsyncAdapt_asyncmy_connection._do_pingc                 C   s
   | j  ¡ S r   )r   Úcharacter_set_namer   r   r   r   ra   Ð   s   
z0AsyncAdapt_asyncmy_connection.character_set_namec                 C   s   |   | j |¡¡ d S r   )r   r   Ú
autocommitr#   r   r   r   rb   Ó   s   z(AsyncAdapt_asyncmy_connection.autocommitFc                 C   s   |rt | ƒS t| ƒS r   )rP   r   )r   r4   r   r   r   r   Ö   s   z$AsyncAdapt_asyncmy_connection.cursorc                 C   ó   |   | j ¡ ¡ d S r   )r   r   Úrollbackr   r   r   r   rd   Ü   rW   z&AsyncAdapt_asyncmy_connection.rollbackc                 C   rc   r   )r   r   Úcommitr   r   r   r   re   ß   rW   z$AsyncAdapt_asyncmy_connection.commitc                 C   r'   r   )r   r)   r   r   r   r   Ú	terminateâ   s   z'AsyncAdapt_asyncmy_connection.terminateÚreturnNc                 C   rc   r   )r   r   Úensure_closedr   r   r   r   r)   æ   rW   z#AsyncAdapt_asyncmy_connection.close)F©rg   N)rJ   rK   rL   Ústaticmethodr   r   rM   r   r   r3   r`   r^   ra   rb   r   rd   re   rf   r)   r   r   r   r   rX   ³   s    
	
rX   c                   @   s   e Zd ZdZeeƒZdS )Ú%AsyncAdaptFallback_asyncmy_connectionr   N)rJ   rK   rL   rM   rj   r   r   r   r   r   r   rk   ê   s    rk   c                 C   s   t | ƒS )zReturn x as a binary type.)Úbytes)Úxr   r   r   Ú_Binaryð   r   rn   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)rR   Ú
paramstyleÚ_init_dbapi_attributes)r   rR   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ÚInterfaceErrorÚ	DataErrorÚDatabaseErrorÚOperationalErrorru   ÚIntegrityErrorÚProgrammingErrorr]   ÚNotSupportedError)ÚsetattrÚgetattrrR   Úerrors)r   Únamer   r   r   rr   û   s   óz/AsyncAdapt_asyncmy_dbapi._init_dbapi_attributesÚSTRINGÚNUMBERÚBINARYÚDATETIMEÚ	TIMESTAMPc                 O   sV   |  dd¡}|  d| jj¡}t |¡rt| t||i |¤ŽƒƒS t| t||i |¤ŽƒƒS )NÚasync_fallbackFÚasync_creator_fn)	ÚpoprR   Úconnectr   Úasboolrk   r   rX   r   )r   ÚargÚkwr…   Ú
creator_fnr   r   r   rˆ     s   
þþz AsyncAdapt_asyncmy_dbapi.connectN)rJ   rK   rL   r   rr   r   Úsymbolr€   r�   r‚   rƒ   r„   rj   rn   ÚBinaryrˆ   r   r   r   r   ro   õ   s    




ro   c                       sr   e Zd ZdZdZdZeZdZdZ	e
dd„ ƒZe
dd„ ƒZdd	d
„Z‡ fdd„Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚMySQLDialect_asyncmyrR   Tc                 C   s   t tdƒƒS )NrR   )ro   Ú
__import__)Úclsr   r   r   Úimport_dbapi,  r%   z!MySQLDialect_asyncmy.import_dbapic                 C   s$   |j  dd¡}t |¡rtjS tjS )Nr…   F)ÚqueryÚgetr   r‰   r   ÚFallbackAsyncAdaptedQueuePoolÚAsyncAdaptedQueuePool)r‘   Úurlr…   r   r   r   Úget_pool_class0  s   
z#MySQLDialect_asyncmy.get_pool_classrg   Nc                 C   s   |  ¡  d S r   )rf   )r   Údbapi_connectionr   r   r   Údo_terminate9  s   z!MySQLDialect_asyncmy.do_terminatec                    s   t ƒ j|tddd�d�S )NÚuserÚdb)ÚusernameÚdatabase)Ú_translate_args)ÚsuperÚcreate_connect_argsÚdict)r   r—   ©Ú	__class__r   r   r¡   <  s   ÿz(MySQLDialect_asyncmy.create_connect_argsc                    s0   t ƒ  |||¡r
dS t|ƒ ¡ }d|v pd|v S )NTznot connectedznetwork operation failed)r    Úis_disconnectÚstrÚlower)r   Úer[   r   Ústr_er£   r   r   r¥   A  s
   ÿz"MySQLDialect_asyncmy.is_disconnectc                 C   s   ddl m} |jS )Nr   )ÚCLIENT)Úasyncmy.constantsrª   Ú
FOUND_ROWS)r   rª   r   r   r   Ú_found_rows_client_flagJ  s   z,MySQLDialect_asyncmy._found_rows_client_flagc                 C   s   |j S r   )r   )r   r[   r   r   r   Úget_driver_connectionO  s   z*MySQLDialect_asyncmy.get_driver_connectionri   )rJ   rK   rL   ÚdriverÚsupports_statement_cacheÚsupports_server_side_cursorsrP   Ú	_sscursorÚis_asyncÚhas_terminateÚclassmethodr’   r˜   rš   r¡   r¥   r­   r®   Ú__classcell__r   r   r£   r   r�   "  s     


	r�   N)Ú__doc__Úcollectionsr   Ú
contextlibr   Úpymysqlr   Ú r   r   Úenginer	   Úutil.concurrencyr
   r   r   r   rP   rX   rk   rn   ro   r�   Údialectr   r   r   r   Ú<module>   s$   i 7-1