U
    yc'                     @   s   d Z ddl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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`.


    N)partial   )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   q/var/www/sbouman/reportbro-server-tornado/env/lib/python3.8/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__L   s    z$AsyncAdapt_aiosqlite_cursor.__init__c                 C   s   g | j d d < d S Nr   r   r   r   r   closeU   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   X   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(   s   s    z'AsyncAdapt_aiosqlite_cursor.executemanyc                 G   s   d S r   r   )r   Z
inputsizesr   r   r   setinputsizes~   s    z)AsyncAdapt_aiosqlite_cursor.setinputsizesc                 c   s   | j r| j dV  q d S Nr   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   r0   r   r   r   r!      s    z$AsyncAdapt_aiosqlite_cursor.fetchall)N)N)__name__
__module____qualname__	__slots__r    r   r   r   r(   r)   r-   r.   r1   r!   r   r   r   r   r   >   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"   r1   )r   r/   r   r   r   r1      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)r2   r3   r4   r5   r    r   r   r.   r1   r!   __classcell__r   r   r:   r   r6      s   
r6   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)dbapic                 C   s   || _ || _d S r   )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   st   dd }t || jj|}t  }| jj||f z| |W S  t	k
rn } z| 
| W 5 d }~X Y nX d S )Nc                 S   s
   || _ d S r   )r@   )r?   valuer   r   r   set_iso   s    z@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso)r   r   Z_connasyncioget_event_loopcreate_futureZ_tx
put_nowaitr   r#   r$   )r   rA   rB   functionfuturer'   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   argsr9   r'   r   r   r   rI      s    z/AsyncAdapt_aiosqlite_connection.create_functionFc                 C   s   |rt | S t| S d S r   )r6   r   )r   r    r   r   r   r      s    z&AsyncAdapt_aiosqlite_connection.cursorc                 O   s   |  | jj||S r   )r   r   r   )r   rJ   r9   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   rK      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$   rL   r   r   r   rM      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$   rL   r   r   r   r      s    z%AsyncAdapt_aiosqlite_connection.closec                 C   s2   t |tr*|jd dkr*| jjd|n|d S )Nr   no active connection)
isinstance
ValueErrorrJ   r>   sqliteOperationalErrorrL   r   r   r   r$      s    z1AsyncAdapt_aiosqlite_connection._handle_exceptionN)F)r2   r3   r4   staticmethodr   r   r5   r   propertyr@   setterrI   r   r   rK   rM   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)r2   r3   r4   r5   rS   r
   r   r   r   r   r   rV     s   rV   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)	aiosqliterQ   Z
paramstyle_init_dbapi_attributes)r   rX   rQ   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NotSupportedErrorrR   ZProgrammingErrorZsqlite_versionZsqlite_version_info)ZPARSE_COLNAMESZPARSE_DECLTYPES)Binary)setattrgetattrrX   rQ   )r   namer   r   r   rY     s    
z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesc                 O   sJ   | dd}| jj||}d|_t|r8t| t|S t| t	|S d S )Nasync_fallbackFT)
r,   rX   connectdaemonr   ZasboolrV   r
   r=   r   )r   r8   r9   r_   r?   r   r   r   r`   !  s    
z"AsyncAdapt_aiosqlite_dbapi.connectN)r2   r3   r4   r   rY   r`   r   r   r   r   rW     s   rW   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_cursor6  s    z:SQLiteExecutionContext_aiosqlite.create_server_side_cursorN)r2   r3   r4   rc   r   r   r   r   rb   5  s   rb   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_aiosqliterX   Tc                 C   s   t tdtdS )NrX   sqlite3)rW   
__import__)clsr   r   r   import_dbapiD  s     z$SQLiteDialect_aiosqlite.import_dbapic                 C   s   |  |rtjS tjS d S r   )Z_is_url_file_dbr   ZNullPoolZ
StaticPool)rg   urlr   r   r   get_pool_classJ  s    
z&SQLiteDialect_aiosqlite.get_pool_classc                    s.   t || jjrdt|krdS t |||S )NrN   T)rO   r>   rR   strr7   is_disconnect)r   er?   r   r:   r   r   rl   Q  s     
z%SQLiteDialect_aiosqlite.is_disconnectc                 C   s   |j S r   )r   )r   r?   r   r   r   get_driver_connectionY  s    z-SQLiteDialect_aiosqlite.get_driver_connection)r2   r3   r4   ZdriverZsupports_statement_cacheZis_asyncZsupports_server_side_cursorsrb   Zexecution_ctx_clsclassmethodrh   rj   rl   rn   r<   r   r   r:   r   rd   :  s   

rd   )__doc__rC   	functoolsr   baser   Zpysqliter    r   r   Zenginer	   Zutil.concurrencyr
   r   r   r6   r=   rV   rW   rb   rd   dialectr   r   r   r   <module>
   s"   ([N.#