U
    ui>                     @  s  d Z ddlm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 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 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$ erd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/ dd%l0m1Z1 G d&d' d'Z2G d(d) d)e2Z3G d*d+ d+ee!Z4G d,d- d-e4Z5G d.d/ d/eZ6G d0d1 d1eZ7G d2d3 d3eZ8e8Z9dS )4a	  

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

.. _aiosqlite_serializable:

Serializable isolation / Savepoints / Transactional DDL (asyncio version)
-------------------------------------------------------------------------

A newly revised version of this important section is now available
at the top level of the SQLAlchemy SQLite documentation, in the section
:ref:`sqlite_transactions`.


.. _aiosqlite_pooling:

Pooling Behavior
----------------

The SQLAlchemy ``aiosqlite`` DBAPI establishes the connection pool differently
based on the kind of SQLite database that's requested:

* When a ``:memory:`` SQLite database is specified, the dialect by default
  will use :class:`.StaticPool`. This pool maintains a single
  connection, so that all access to the engine
  use the same ``:memory:`` database.
* When a file-based database is specified, the dialect will use
  :class:`.AsyncAdaptedQueuePool` as the source of connections.

  .. versionchanged:: 2.0.38

    SQLite file database engines now use :class:`.AsyncAdaptedQueuePool` by default.
    Previously, :class:`.NullPool` were used.  The :class:`.NullPool` class
    may be used by specifying it via the
    :paramref:`_sa.create_engine.poolclass` parameter.

    )annotationsN)deque)partial)Thread)
ModuleType)Any)cast)Deque)Iterator)NoReturn)Optional)Sequence)TYPE_CHECKING)Union   )SQLiteExecutionContext)SQLiteDialect_pysqlite   )pool)util)AsyncAdapt_dbapi_module)AsyncAdapt_terminate)AdaptedConnection)await_fallback)
await_only)AsyncIODBAPIConnection)AsyncIODBAPICursor)_DBAPICursorDescription)_DBAPIMultiExecuteParams)_DBAPISingleExecuteParams)DBAPIConnection)DBAPICursor)DBAPIModule)URL)PoolProxiedConnectionc                   @  s   e Zd ZdZdZddddZddd	d
ZddddZd'ddddddZddddddZ	dddddZ
ddddZddddZd(d d!d"d#d$Zd!dd%d&ZdS ))AsyncAdapt_aiosqlite_cursor)_adapt_connection_connectiondescriptionawait__rows	arraysizerowcount	lastrowidFAsyncAdapt_aiosqlite_connection)adapt_connectionc                 C  s4   || _ |j| _|j| _d| _d| _d | _t | _d S )Nr   )r&   r'   r)   r+   r,   r(   r   r*   )selfr/    r2   ^/home/lhs5088/backend/venv/lib/python3.8/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__   s    z$AsyncAdapt_aiosqlite_cursor.__init__Nonereturnc                   s   d S Nr2   r1   r2   r2   r3   _async_soft_close   s    z-AsyncAdapt_aiosqlite_cursor._async_soft_closec                 C  s   | j   d S r8   )r*   clearr9   r2   r2   r3   close   s    z!AsyncAdapt_aiosqlite_cursor.closeNr   z#Optional[_DBAPISingleExecuteParams])	operation
parametersr7   c              
   C  s   z|  | j }|d kr,|  || n|  ||| |jrt|j| _d | _| _| jst|  |	 | _
nd | _|j| _|j| _| js|  |  n|| _W n. tk
r } z| j| W 5 d }~X Y nX d S )Nr0   )r)   r'   cursorexecuter(   r-   r,   server_sider   fetchallr*   r<   _cursor	Exceptionr&   _handle_exception)r1   r=   r>   rC   errorr2   r2   r3   r@      s$    
z#AsyncAdapt_aiosqlite_cursor.executer   )r=   seq_of_parametersr7   c              
   C  s~   zJ|  | j }|  ||| d | _|j| _|j| _|  |  W n. tk
rx } z| j	
| W 5 d }~X Y nX d S r8   )r)   r'   r?   executemanyr(   r-   r,   r<   rD   r&   rE   )r1   r=   rG   rC   rF   r2   r2   r3   rH      s    z'AsyncAdapt_aiosqlite_cursor.executemany)
inputsizesr7   c                 G  s   d S r8   r2   )r1   rI   r2   r2   r3   setinputsizes   s    z)AsyncAdapt_aiosqlite_cursor.setinputsizeszIterator[Any]c                 c  s   | j r| j  V  q d S r8   r*   popleftr9   r2   r2   r3   __iter__   s    z$AsyncAdapt_aiosqlite_cursor.__iter__Optional[Any]c                 C  s   | j r| j  S d S d S r8   rK   r9   r2   r2   r3   fetchone   s    
z$AsyncAdapt_aiosqlite_cursor.fetchoneOptional[int]Sequence[Any]sizer7   c                   s4   |d kr| j }| j  fddtt|t D S )Nc                   s   g | ]}   qS r2   )rL   ).0_Zrrr2   r3   
<listcomp>   s     z9AsyncAdapt_aiosqlite_cursor.fetchmany.<locals>.<listcomp>)r+   r*   rangeminlenr1   rS   r2   rV   r3   	fetchmany   s    z%AsyncAdapt_aiosqlite_cursor.fetchmanyc                 C  s   t | j}| j  |S r8   )listr*   r;   )r1   retvalr2   r2   r3   rB      s    

z$AsyncAdapt_aiosqlite_cursor.fetchall)N)N)__name__
__module____qualname__	__slots__rA   r4   r:   r<   r@   rH   rJ   rM   rO   r\   rB   r2   r2   r2   r3   r%   x   s   	  r%   c                      sj   e Zd ZdZdZdddd fddZddd	d
ZddddZddddddZddddZ	  Z
S )AsyncAdapt_aiosqlite_ss_cursorrC   Tr   r5   argkwr7   c                   s   t  j|| d | _d S r8   )superr4   rC   )r1   re   rf   	__class__r2   r3   r4      s    z'AsyncAdapt_aiosqlite_ss_cursor.__init__r6   c                 C  s$   | j d k	r | | j   d | _ d S r8   )rC   r)   r<   r9   r2   r2   r3   r<      s    
z$AsyncAdapt_aiosqlite_ss_cursor.closerN   c                 C  s   | j d k	st| | j  S r8   )rC   AssertionErrorr)   rO   r9   r2   r2   r3   rO      s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchoneNrP   rQ   rR   c                 C  s0   | j d k	st|d kr| j}| | j j|dS )N)rS   )rC   rj   r+   r)   r\   r[   r2   r2   r3   r\      s    z(AsyncAdapt_aiosqlite_ss_cursor.fetchmanyc                 C  s   | j d k	st| | j  S r8   )rC   rj   r)   rB   r9   r2   r2   r3   rB      s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchall)N)r_   r`   ra   rb   rA   r4   r<   rO   r\   rB   __classcell__r2   r2   rh   r3   rc      s   rc   c                   @  s   e Zd ZeeZdZddddddZedd	d
dZ	e	j
dddddZ	ddddddZd)dddddZddddddZdd	ddZdd	ddZdd	ddZdd d!d"d#Zdd	d$d%Zdd	d&d'Zd(S )*r.   )dbapir   r   r5   )rl   
connectionr7   c                 C  s   || _ || _d S r8   )rl   r'   )r1   rl   rm   r2   r2   r3   r4     s    z(AsyncAdapt_aiosqlite_connection.__init__Optional[str]r6   c                 C  s   t t| jjS r8   )r   strr'   isolation_levelr9   r2   r2   r3   rp     s    z/AsyncAdapt_aiosqlite_connection.isolation_level)valuer7   c              
   C  s   dddddd}t || jj|}t  }| jj||f z| | W n, t	k
rz } z| 
| W 5 d }~X Y nX d S )Nr.   rn   r5   )rm   rq   r7   c                 S  s
   || _ d S r8   )rp   )rm   rq   r2   r2   r3   set_iso  s    z@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso)r   r'   Z_connasyncioZget_event_loopZcreate_futureZ_tx
put_nowaitr)   rD   rE   )r1   rq   rr   functionfuturerF   r2   r2   r3   rp     s    )argsrf   r7   c              
   O  sJ   z|  | jj|| W n, tk
rD } z| | W 5 d }~X Y nX d S r8   )r)   r'   create_functionrD   rE   )r1   rw   rf   rF   r2   r2   r3   rx   "  s    z/AsyncAdapt_aiosqlite_connection.create_functionFboolr%   )rA   r7   c                 C  s   |rt | S t| S d S r8   )rc   r%   )r1   rA   r2   r2   r3   r?   (  s    z&AsyncAdapt_aiosqlite_connection.cursorc                 O  s   |  | jj||S r8   )r)   r'   r@   )r1   rw   rf   r2   r2   r3   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 r8   )r)   r'   rollbackrD   rE   r1   rF   r2   r2   r3   rz   1  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 r8   )r)   r'   commitrD   rE   r{   r2   r2   r3   r|   7  s    z&AsyncAdapt_aiosqlite_connection.commitc              
   C  sX   z|  | j  W n> tk
r(   Y n, tk
rR } z| | W 5 d }~X Y nX d S r8   )r)   r'   r<   
ValueErrorrD   rE   r{   r2   r2   r3   r<   =  s    	z%AsyncAdapt_aiosqlite_connection.closerD   r   )rF   r7   c                 C  s2   t |tr*|jd dkr*| jjd|n|d S )Nr   no active connection)
isinstancer}   rw   rl   sqliteOperationalErrorr{   r2   r2   r3   rE   M  s    z1AsyncAdapt_aiosqlite_connection._handle_exceptionc                   s   | j  I dH  dS )z"Try to close connection gracefullyN)r'   r<   r9   r2   r2   r3   _terminate_graceful_closeX  s    z9AsyncAdapt_aiosqlite_connection._terminate_graceful_closec              
   C  sD   z| j j}W n, tk
r8 } ztd|W 5 d}~X Y nX |  dS )zTerminate the connectionz:terminate_force_close() not implemented by this DBAPI shimN)r'   stopAttributeErrorNotImplementedError)r1   methZaer2   r2   r3   _terminate_force_close\  s    z6AsyncAdapt_aiosqlite_connection._terminate_force_closeN)F)r_   r`   ra   staticmethodr   r)   rb   r4   propertyrp   setterrx   r?   r@   rz   r|   r<   rE   r   r   r2   r2   r2   r3   r.      s    r.   c                   @  s   e Zd ZdZeeZdS )'AsyncAdaptFallback_aiosqlite_connectionr2   N)r_   r`   ra   rb   r   r   r)   r2   r2   r2   r3   r   k  s   r   c                   @  s<   e Zd ZdddddZddddZd	d	d
dddZdS )AsyncAdapt_aiosqlite_dbapir   )	aiosqliter   c                 C  s,   || _ || _d| _t|jd| _|   d S )NZqmarkr   )r   r   Z
paramstylehasattr
Connectionhas_stop_init_dbapi_attributes)r1   r   r   r2   r2   r3   r4   r  s
    z#AsyncAdapt_aiosqlite_dbapi.__init__r5   r6   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NotSupportedErrorr   ZProgrammingErrorZsqlite_versionZsqlite_version_info)ZPARSE_COLNAMESZPARSE_DECLTYPES)Binary)setattrgetattrr   r   )r1   namer2   r2   r3   r   y  s    
z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesr   r.   rd   c                 O  sz   | dd}| dd }|r(|||}n(| jj||}t|trHd|_nd|j_t|rht	| t
|S t| t|S d S )Nasync_fallbackFZasync_creator_fnT)popr   connectr   r   daemon_threadr   Zasboolr   r   r.   r   )r1   re   rf   r   Z
creator_fnrm   r2   r2   r3   r     s"    

z"AsyncAdapt_aiosqlite_dbapi.connectN)r_   r`   ra   r4   r   r   r2   r2   r2   r3   r   q  s   r   c                   @  s   e Zd ZddddZdS ) SQLiteExecutionContext_aiosqliter!   r6   c                 C  s   | j jddS )NT)rA   )Z_dbapi_connectionr?   r9   r2   r2   r3   create_server_side_cursor  s    z:SQLiteExecutionContext_aiosqlite.create_server_side_cursorN)r_   r`   ra   r   r2   r2   r2   r3   r     s   r   c                      s   e Zd ZdZdZdZdZdZeZ	dd fddZ
eddd	d
ZedddddZddddd fddZdddddZdddddZ  ZS ) SQLiteDialect_aiosqliter   Tr   )kwargsc                   s&   t  jf | | jr"| jjs"d| _d S )NF)rg   r4   rl   r   has_terminate)r1   r   rh   r2   r3   r4     s    z SQLiteDialect_aiosqlite.__init__r   r6   c                 C  s   t tdtdS )Nr   sqlite3)r   
__import__)clsr2   r2   r3   import_dbapi  s     z$SQLiteDialect_aiosqlite.import_dbapir#   ztype[pool.Pool])urlr7   c                 C  s   |  |rtjS tjS d S r8   )Z_is_url_file_dbr   ZAsyncAdaptedQueuePoolZ
StaticPool)r   r   r2   r2   r3   get_pool_class  s    
z&SQLiteDialect_aiosqlite.get_pool_classzDBAPIModule.Errorz7Optional[Union[PoolProxiedConnection, DBAPIConnection]]zOptional[DBAPICursor]ry   )erm   r?   r7   c                   s<   t d| j| _t|| jjr,dt|kr,dS t |||S )Nr"   r~   T)r   rl   r   r   ro   rg   is_disconnect)r1   r   rm   r?   rh   r2   r3   r     s     
z%SQLiteDialect_aiosqlite.is_disconnectr    r   )rm   r7   c                 C  s   |j S r8   )r'   )r1   rm   r2   r2   r3   get_driver_connection  s    z-SQLiteDialect_aiosqlite.get_driver_connectionr5   )dbapi_connectionr7   c                 C  s   |   d S r8   )	terminate)r1   r   r2   r2   r3   do_terminate  s    z$SQLiteDialect_aiosqlite.do_terminate)r_   r`   ra   ZdriverZsupports_statement_cacheZis_asyncr   Zsupports_server_side_cursorsr   Zexecution_ctx_clsr4   classmethodr   r   r   r   r   rk   r2   r2   rh   r3   r     s   r   ):__doc__
__future__r   rs   collectionsr   	functoolsr   	threadingr   typesr   typingr   r   r	   r
   r   r   r   r   r   baser   Zpysqliter    r   r   Zconnectors.asyncior   r   Zenginer   Zutil.concurrencyr   r   r   r   Zengine.interfacesr   r   r   r    r!   r"   Z
engine.urlr#   Z	pool.baser$   r%   rc   r.   r   r   r   r   dialectr2   r2   r2   r3   <module>	   sV   Gik84