U
    ÌZjï'  ã                   @   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 G d
d„ dƒZG dd„ deƒZG dd„ de	ƒZG dd„ deƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZeZdS )aZ  

.. dialect:: sqlite+aiosqlite
    :name: aiosqlite
    :dbapi: aiosqlite
    :connectstring: sqlite+aiosqlite:///file_path
    :url: https://pypi.org/project/aiosqlite/

The aiosqlite dialect provides support for the SQLAlchemy asyncio interface
running on top of pysqlite.

aiosqlite is a wrapper around pysqlite that uses a background thread for
each connection.   It does not actually use non-blocking IO, as SQLite
databases are not socket-based.  However it does provide a working asyncio
interface that's useful for testing and prototyping purposes.

Using a special asyncio mediation layer, the aiosqlite 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("sqlite+aiosqlite:///filename")

The URL passes through all arguments to the ``pysqlite`` driver, so all
connection arguments are the same as they are for that of :ref:`pysqlite`.

.. _aiosqlite_udfs:

User-Defined Functions
----------------------

aiosqlite extends pysqlite to support async, so we can create our own user-defined functions (UDFs)
in Python and use them directly in SQLite queries as described here: :ref:`pysqlite_udfs`.


é   )ÚSQLiteExecutionContext)ÚSQLiteDialect_pysqliteé   )Úpool)Úutil)ÚAdaptedConnection)Úawait_fallback)Ú
await_onlyc                   @   s`   e Zd ZdZdZdd„ Zdd„ Zd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_aiosqlite_cursor)Ú_adapt_connectionÚ_connectionÚdescriptionÚawait_Ú_rowsÚ	arraysizeÚrowcountÚ	lastrowidFc                 C   s2   || _ |j| _|j| _d| _d| _d | _g | _d S )Nr   éÿÿÿÿ)r   r   r   r   r   r   r   )ÚselfZadapt_connection© r   úg/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.pyÚ__init__G   s    z$AsyncAdapt_aiosqlite_cursor.__init__c                 C   s   g | j d d …< d S ©N©r   ©r   r   r   r   ÚcloseP   s    z!AsyncAdapt_aiosqlite_cursor.closeNc              
   C   sØ   z¤|   | j ¡ ¡}|d kr,|   | |¡¡ n|   | ||¡¡ |jrp|j| _d | _| _| js†|   | ¡ ¡| _	nd | _|j| _|j| _| jsœ|   | 
¡ ¡ n|| _W n. tk
rÒ } z| j |¡ W 5 d }~X Y nX d S )Nr   )r   r   ÚcursorÚexecuter   r   r   Úserver_sideÚfetchallr   r   Ú_cursorÚ	Exceptionr   Ú_handle_exception)r   Ú	operationÚ
parametersr    Úerrorr   r   r   r   S   s$    
z#AsyncAdapt_aiosqlite_cursor.executec              
   C   s~   zJ|   | j ¡ ¡}|   | ||¡¡ d | _|j| _|j| _|   | ¡ ¡ W n. tk
rx } z| j	 
|¡ W 5 d }~X Y nX d S r   )r   r   r   Úexecutemanyr   r   r   r   r!   r   r"   )r   r#   Zseq_of_parametersr    r%   r   r   r   r&   n   s    z'AsyncAdapt_aiosqlite_cursor.executemanyc                 G   s   d S r   r   )r   Z
inputsizesr   r   r   Úsetinputsizesy   s    z)AsyncAdapt_aiosqlite_cursor.setinputsizesc                 c   s   | j r| j  d¡V  q d S ©Né    ©r   Úpopr   r   r   r   Ú__iter__|   s    z$AsyncAdapt_aiosqlite_cursor.__iter__c                 C   s   | j r| j  d¡S d S d S r(   r*   r   r   r   r   Úfetchone€   s    z$AsyncAdapt_aiosqlite_cursor.fetchonec                 C   s8   |d kr| j }| jd|… }| j|d … | jd d …< |S r(   )r   r   )r   ÚsizeÚretvalr   r   r   Ú	fetchmany†   s
    z%AsyncAdapt_aiosqlite_cursor.fetchmanyc                 C   s    | j d d … }g | j d d …< |S r   r   )r   r/   r   r   r   r   Ž   s    z$AsyncAdapt_aiosqlite_cursor.fetchall)N)N)Ú__name__Ú
__module__Ú__qualname__Ú	__slots__r   r   r   r   r&   r'   r,   r-   r0   r   r   r   r   r   r
   9   s   	

r
   c                       sF   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Zdd
d„Zdd„ Z	‡  Z
S )ÚAsyncAdapt_aiosqlite_ss_cursorr    Tc                    s   t ƒ j||Ž d | _d S r   )Úsuperr   r    )r   ÚargÚkw©Ú	__class__r   r   r   ™   s    z'AsyncAdapt_aiosqlite_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_aiosqlite_ss_cursor.closec                 C   s   |   | j ¡ ¡S r   )r   r    r-   r   r   r   r   r-   ¢   s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchoneNc                 C   s"   |d kr| j }|  | jj|d�¡S )N)r.   )r   r   r    r0   )r   r.   r   r   r   r0   ¥   s    z(AsyncAdapt_aiosqlite_ss_cursor.fetchmanyc                 C   s   |   | j ¡ ¡S r   )r   r    r   r   r   r   r   r   ª   s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchall)N)r1   r2   r3   r4   r   r   r   r-   r0   r   Ú__classcell__r   r   r9   r   r5   ”   s   
r5   c                   @   st   e Zd ZeeƒZdZdd„ Zedd„ ƒZ	e	j
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S )ÚAsyncAdapt_aiosqlite_connection©Údbapir   c                 C   s   || _ || _d S r   r=   )r   r>   Ú
connectionr   r   r   r   ²   s    z(AsyncAdapt_aiosqlite_connection.__init__c                 C   s   | j jS r   )r   Úisolation_levelr   r   r   r   r@   ¶   s    z/AsyncAdapt_aiosqlite_connection.isolation_levelc              
   C   s>   z|| j _W n, tk
r8 } z|  |¡ W 5 d }~X Y nX d S r   )r   r@   r!   r"   )r   Úvaluer%   r   r   r   r@   º   s    c              
   O   sJ   z|   | jj||Ž¡ W n, tk
rD } z|  |¡ W 5 d }~X Y nX d S r   )r   r   Úcreate_functionr!   r"   )r   Úargsr8   r%   r   r   r   rB   Á   s    z/AsyncAdapt_aiosqlite_connection.create_functionFc                 C   s   |rt | ƒS t| ƒS d S r   )r5   r
   )r   r   r   r   r   r   Ç   s    z&AsyncAdapt_aiosqlite_connection.cursorc                 O   s   |   | jj||Ž¡S r   )r   r   r   )r   rC   r8   r   r   r   r   Í   s    z'AsyncAdapt_aiosqlite_connection.executec              
   C   sF   z|   | j ¡ ¡ W n, tk
r@ } z|  |¡ W 5 d }~X Y nX d S r   )r   r   Úrollbackr!   r"   ©r   r%   r   r   r   rD   Ð   s    z(AsyncAdapt_aiosqlite_connection.rollbackc              
   C   sF   z|   | j ¡ ¡ W n, tk
r@ } z|  |¡ W 5 d }~X Y nX d S r   )r   r   Úcommitr!   r"   rE   r   r   r   rF   Ö   s    z&AsyncAdapt_aiosqlite_connection.commitc              
   C   sF   z|   | j ¡ ¡ W n, tk
r@ } z|  |¡ W 5 d }~X Y nX d S r   )r   r   r   r!   r"   rE   r   r   r   r   Ü   s    z%AsyncAdapt_aiosqlite_connection.closec                 C   s:   t |tƒr2|jd dkr2tj| jj d¡|d� n|‚d S )Nr)   úno active connection)Zfrom_)Ú
isinstanceÚ
ValueErrorrC   r   Zraise_r>   ÚsqliteÚOperationalErrorrE   r   r   r   r"   â   s    ÿþþz1AsyncAdapt_aiosqlite_connection._handle_exceptionN)F)r1   r2   r3   Ústaticmethodr	   r   r4   r   Úpropertyr@   ÚsetterrB   r   r   rD   rF   r   r"   r   r   r   r   r<   ®   s   


r<   c                   @   s   e Zd ZdZeeƒZdS )Ú'AsyncAdaptFallback_aiosqlite_connectionr   N)r1   r2   r3   r4   rL   r   r   r   r   r   r   rO   ï   s   rO   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚAsyncAdapt_aiosqlite_dbapic                 C   s   || _ || _d| _|  ¡  d S )NZqmark)Ú	aiosqliterJ   Z
paramstyleÚ_init_dbapi_attributes)r   rQ   rJ   r   r   r   r   ö   s    z#AsyncAdapt_aiosqlite_dbapi.__init__c                 C   s^   dD ]}t | |t| j|ƒƒ qdD ]}t | |t| j|ƒƒ q"dD ]}t | |t| j|ƒƒ q@d S )N)ZDatabaseErrorÚErrorZIntegrityErrorZNotSupportedErrorrK   ZProgrammingErrorZsqlite_versionZsqlite_version_info)ZPARSE_COLNAMESZPARSE_DECLTYPES)ÚBinary)ÚsetattrÚgetattrrQ   rJ   )r   Únamer   r   r   rR   ü   s    
z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesc                 O   sR   |  dd¡}d|d< | jj||Ž}d|_t |¡r@t| t|ƒƒS t| t	|ƒƒS d S )NÚasync_fallbackFZcheck_same_threadT)
r+   rQ   ÚconnectÚdaemonr   ZasboolrO   r   r<   r	   )r   r7   r8   rX   r?   r   r   r   rY     s    
þþz"AsyncAdapt_aiosqlite_dbapi.connectN)r1   r2   r3   r   rR   rY   r   r   r   r   rP   õ   s   rP   c                   @   s   e Zd Zdd„ ZdS )Ú SQLiteExecutionContext_aiosqlitec                 C   s   | j jdd�S )NT)r   )Z_dbapi_connectionr   r   r   r   r   Úcreate_server_side_cursor0  s    z:SQLiteExecutionContext_aiosqlite.create_server_side_cursorN)r1   r2   r3   r\   r   r   r   r   r[   /  s   r[   c                       sP   e Zd ZdZdZdZdZeZe	dd„ ƒZ
e	dd„ ƒZ‡ fdd„Zd	d
„ Z‡  ZS )ÚSQLiteDialect_aiosqliterQ   Tc                 C   s   t tdƒtdƒƒS )NrQ   Úsqlite3)rP   Ú
__import__)Úclsr   r   r   r>   >  s     ÿzSQLiteDialect_aiosqlite.dbapic                 C   s   |   |¡rtjS tjS d S r   )Z_is_url_file_dbr   ZNullPoolZ
StaticPool)r`   Úurlr   r   r   Úget_pool_classD  s    
z&SQLiteDialect_aiosqlite.get_pool_classc                    s.   t || jjƒrdt|ƒkrdS tƒ  |||¡S )NrG   T)rH   r>   rK   Ústrr6   Úis_disconnect)r   Úer?   r   r9   r   r   rd   K  s     ÿ
þz%SQLiteDialect_aiosqlite.is_disconnectc                 C   s   |j S r   )r   )r   r?   r   r   r   Úget_driver_connectionS  s    z-SQLiteDialect_aiosqlite.get_driver_connection)r1   r2   r3   ZdriverZsupports_statement_cacheZis_asyncZsupports_server_side_cursorsr[   Zexecution_ctx_clsÚclassmethodr>   rb   rd   rf   r;   r   r   r9   r   r]   4  s   

r]   N)Ú__doc__Úbaser   Zpysqliter   Ú r   r   Zenginer   Zutil.concurrencyr   r	   r
   r5   r<   rO   rP   r[   r]   Údialectr   r   r   r   Ú<module>   s   ([A:#