3
ax&                 @   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
 G d	d
 d
ZG dd deZG dd d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 )aV  

.. 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`.


   )SQLiteExecutionContext)SQLiteDialect_pysqlite   )pool)util)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   J/tmp/pip-build-6_cqtusv/SQLAlchemy/sqlalchemy/dialects/sqlite/aiosqlite.py__init__>   s    z$AsyncAdapt_aiosqlite_cursor.__init__c             C   s   g | j d d < d S )N)r   )r   r   r   r   closeG   s    z!AsyncAdapt_aiosqlite_cursor.closeNc             C   s   y| j | jj }|d kr,| j |j| n| j |j|| |jrp|j| _d | _| _| js| j |j | _	nd | _|j| _|j| _| js| j |j
  n|| _W n. tk
r } z| jj| W Y d d }~X nX d S )Nr   r   )r   r   cursorexecuter   r   r   server_sidefetchallr   r   _cursor	Exceptionr
   _handle_exception)r   	operation
parametersr   errorr   r   r   r   J   s$    
z#AsyncAdapt_aiosqlite_cursor.executec             C   s~   yJ| j | jj }| j |j|| d | _|j| _|j| _| j |j  W n. tk
rx } z| j	j
| W Y d d }~X nX d S )N)r   r   r   executemanyr   r   r   r   r   r
   r   )r   r   Zseq_of_parametersr   r!   r   r   r   r"   e   s    z'AsyncAdapt_aiosqlite_cursor.executemanyc             G   s   d S )Nr   )r   Z
inputsizesr   r   r   setinputsizesp   s    z)AsyncAdapt_aiosqlite_cursor.setinputsizesc             c   s   x| j r| j jdV  qW d S )N    )r   pop)r   r   r   r   __iter__s   s    z$AsyncAdapt_aiosqlite_cursor.__iter__c             C   s   | j r| j jdS d S d S )Nr$   )r   r%   )r   r   r   r   fetchonew   s    z$AsyncAdapt_aiosqlite_cursor.fetchonec             C   s8   |d kr| j }| jd| }| j|d  | jd d < |S )Nr$   )r   r   )r   sizeretvalr   r   r   	fetchmany}   s
    z%AsyncAdapt_aiosqlite_cursor.fetchmanyc             C   s    | j d d  }g | j d d < |S )N)r   )r   r)   r   r   r   r      s    z$AsyncAdapt_aiosqlite_cursor.fetchall)r
   r   r   r   r   r   r   r   )N)N)__name__
__module____qualname__	__slots__r   r   r   r   r"   r#   r&   r'   r*   r   r   r   r   r   r	   0   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 )N)superr   r   )r   argkw)	__class__r   r   r      s    z'AsyncAdapt_aiosqlite_ss_cursor.__init__c             C   s$   | j d k	r | j| j j  d | _ d S )N)r   r   r   )r   r   r   r   r      s    
z$AsyncAdapt_aiosqlite_ss_cursor.closec             C   s   | j | jj S )N)r   r   r'   )r   r   r   r   r'      s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchoneNc             C   s"   |d kr| j }| j| jj|dS )N)r(   )r   r   r   r*   )r   r(   r   r   r   r*      s    z(AsyncAdapt_aiosqlite_ss_cursor.fetchmanyc             C   s   | j | jj S )N)r   r   r   )r   r   r   r   r      s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchall)N)r+   r,   r-   r.   r   r   r   r'   r*   r   __classcell__r   r   )r3   r   r/      s   
r/   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 )N)r6   r   )r   r6   
connectionr   r   r   r      s    z(AsyncAdapt_aiosqlite_connection.__init__c             C   s   | j jS )N)r   isolation_level)r   r   r   r   r8      s    z/AsyncAdapt_aiosqlite_connection.isolation_levelc             C   s>   y|| j _W n, tk
r8 } z| j| W Y d d }~X nX d S )N)r   r8   r   r   )r   valuer!   r   r   r   r8      s    c             O   sJ   y| j | jj|| W n, tk
rD } z| j| W Y d d }~X nX d S )N)r   r   create_functionr   r   )r   argsr2   r!   r   r   r   r:      s    z/AsyncAdapt_aiosqlite_connection.create_functionFc             C   s   |rt | S t| S d S )N)r/   r	   )r   r   r   r   r   r      s    z&AsyncAdapt_aiosqlite_connection.cursorc             O   s   | j | jj||S )N)r   r   r   )r   r;   r2   r   r   r   r      s    z'AsyncAdapt_aiosqlite_connection.executec             C   sF   y| j | jj  W n, tk
r@ } z| j| W Y d d }~X nX d S )N)r   r   rollbackr   r   )r   r!   r   r   r   r<      s    z(AsyncAdapt_aiosqlite_connection.rollbackc             C   sF   y| j | jj  W n, tk
r@ } z| j| W Y d d }~X nX d S )N)r   r   commitr   r   )r   r!   r   r   r   r=      s    z&AsyncAdapt_aiosqlite_connection.commitc             C   sF   y| j | jj  W n, tk
r@ } z| j| W Y d d }~X nX d S )N)r   r   r   r   r   )r   r!   r   r   r   r      s    z%AsyncAdapt_aiosqlite_connection.closec             C   s:   t |tr2|jd dkr2tj| jjjd|d n|d S )Nr$   zno active connection)from_)
isinstance
ValueErrorr;   r   Zraise_r6   sqliteOperationalError)r   r!   r   r   r   r      s    

z1AsyncAdapt_aiosqlite_connection._handle_exceptionN)r6   r   )F)r+   r,   r-   staticmethodr   r   r.   r   propertyr8   setterr:   r   r   r<   r=   r   r   r   r   r   r   r5      s   
r5   c               @   s   e Zd Zf ZeeZdS )'AsyncAdaptFallback_aiosqlite_connectionN)r+   r,   r-   r.   rC   r   r   r   r   r   r   rF      s   rF   c               @   s$   e Zd Zdd Zdd Zdd ZdS )AsyncAdapt_aiosqlite_dbapic             C   s   || _ || _d| _| j  d S )NZqmark)	aiosqliterA   Z
paramstyle_init_dbapi_attributes)r   rH   rA   r   r   r   r      s    z#AsyncAdapt_aiosqlite_dbapi.__init__c             C   sj   x dD ]}t | |t| j| qW x dD ]}t | |t| j| q(W x dD ]}t | |t| j| qJW d S )NDatabaseErrorErrorIntegrityErrorNotSupportedErrorrB   ProgrammingErrorsqlite_versionsqlite_version_infoPARSE_COLNAMESPARSE_DECLTYPESBinary)rJ   rK   rL   rM   rB   rN   rO   rP   )rQ   rR   )rS   )setattrgetattrrH   rA   )r   namer   r   r   rI      s           

z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesc             O   sR   |j dd}d|d< | jj||}d|_tj|r@t| t|S t| t	|S d S )Nasync_fallbackFZcheck_same_threadT)
r%   rH   connectdaemonr   ZasboolrF   r   r5   r   )r   r1   r2   rW   r7   r   r   r   rX     s    

z"AsyncAdapt_aiosqlite_dbapi.connectN)r+   r,   r-   r   rI   rX   r   r   r   r   rG      s   rG   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_cursor(  s    z:SQLiteExecutionContext_aiosqlite.create_server_side_cursorN)r+   r,   r-   r[   r   r   r   r   rZ   '  s   rZ   c                   sH   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  ZS )	SQLiteDialect_aiosqliterH   Tc             C   s   t tdtdS )NrH   Zsqlite3)rG   
__import__)clsr   r   r   r6   6  s    zSQLiteDialect_aiosqlite.dbapic             C   s   | j |rtjS tjS d S )N)Z_is_url_file_dbr   ZNullPoolZ
StaticPool)r^   urlr   r   r   get_pool_class<  s    
z&SQLiteDialect_aiosqlite.get_pool_classc                s.   t || jjrdt|krdS t j|||S )Nzno active connectionT)r?   r6   rB   strr0   is_disconnect)r   er7   r   )r3   r   r   rb   C  s
    z%SQLiteDialect_aiosqlite.is_disconnect)r+   r,   r-   ZdriverZsupports_statement_cacheZis_asyncZsupports_server_side_cursorsrZ   Zexecution_ctx_clsclassmethodr6   r`   rb   r4   r   r   )r3   r   r\   ,  s   r\   N)__doc__baser   Zpysqliter    r   r   Zutil.concurrencyr   r   r	   r/   r5   rF   rG   rZ   r\   dialectr   r   r   r   <module>&   s   [B: 