o
    ooi>                     @  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Zd)d	d
Zd)ddZ	d*d+ddZd,ddZ	d-ddZ
d.ddZd/dd Zd*d0d$d%Zd1d&d'ZdS )2AsyncAdapt_aiosqlite_cursor)_adapt_connection_connectiondescriptionawait__rows	arraysizerowcount	lastrowidFadapt_connectionAsyncAdapt_aiosqlite_connectionc                 C  s4   || _ |j| _|j| _d| _d| _d | _t | _d S )Nr   )r&   r'   r)   r+   r,   r(   r   r*   )selfr.    r2   X/home/ubuntu/.local/lib/python3.10/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__   s   z$AsyncAdapt_aiosqlite_cursor.__init__returnNonec                   s   d S Nr2   r1   r2   r2   r3   _async_soft_close   s   z-AsyncAdapt_aiosqlite_cursor._async_soft_closec                 C  s   | j   d S r7   )r*   clearr8   r2   r2   r3   close      z!AsyncAdapt_aiosqlite_cursor.closeN	operationr   
parameters#Optional[_DBAPISingleExecuteParams]c              
   C  s   zW|  | j }|d u r|  || n	|  ||| |jr:|j| _d | _| _| js9t|  |	 | _
nd | _|j| _|j| _| jsR|  |  W d S || _W d S  typ } z| j| W Y d }~d S d }~ww )Nr0   )r)   r'   cursorexecuter(   r-   r,   server_sider   fetchallr*   r;   _cursor	Exceptionr&   _handle_exception)r1   r=   r>   rD   errorr2   r2   r3   rA      s*   z#AsyncAdapt_aiosqlite_cursor.executeseq_of_parametersr   c              
   C  s   z&|  | j }|  ||| d | _|j| _|j| _|  |  W d S  ty? } z| j	
| W Y d }~d S d }~ww r7   )r)   r'   r@   executemanyr(   r-   r,   r;   rE   r&   rF   )r1   r=   rH   rD   rG   r2   r2   r3   rI      s   z'AsyncAdapt_aiosqlite_cursor.executemany
inputsizesc                 G  s   d S r7   r2   )r1   rJ   r2   r2   r3   setinputsizes   s   z)AsyncAdapt_aiosqlite_cursor.setinputsizesIterator[Any]c                 c  s"    | j r| j  V  | j sd S d S r7   r*   popleftr8   r2   r2   r3   __iter__   s   z$AsyncAdapt_aiosqlite_cursor.__iter__Optional[Any]c                 C  s   | j r| j  S d S r7   rM   r8   r2   r2   r3   fetchone   s   
z$AsyncAdapt_aiosqlite_cursor.fetchonesizeOptional[int]Sequence[Any]c                   s4   |d u r| j }| j  fddtt|t D S )Nc                   s   g | ]}   qS r2   )rN   ).0_rrr2   r3   
<listcomp>   s    z9AsyncAdapt_aiosqlite_cursor.fetchmany.<locals>.<listcomp>)r+   r*   rangeminlenr1   rR   r2   rW   r3   	fetchmany   s    z%AsyncAdapt_aiosqlite_cursor.fetchmanyc                 C  s   t | j}| j  |S r7   )listr*   r:   )r1   retvalr2   r2   r3   rC      s   

z$AsyncAdapt_aiosqlite_cursor.fetchall)r.   r/   r5   r6   r7   )r=   r   r>   r?   r5   r   )r=   r   rH   r   r5   r   )rJ   r   r5   r6   )r5   rL   r5   rP   rR   rS   r5   rT   r5   rT   )__name__
__module____qualname__	__slots__rB   r4   r9   r;   rA   rI   rK   rO   rQ   r^   rC   r2   r2   r2   r3   r%   x   s    

	

 


r%   c                      sP   e Zd ZdZdZd fdd	Zdd
dZdddZddddZdddZ	  Z
S )AsyncAdapt_aiosqlite_ss_cursorrD   Targr   kwr5   r6   c                   s   t  j|i | d | _d S r7   )superr4   rD   )r1   rj   rk   	__class__r2   r3   r4      s   
z'AsyncAdapt_aiosqlite_ss_cursor.__init__c                 C  s(   | j d ur| | j   d | _ d S d S r7   )rD   r)   r;   r8   r2   r2   r3   r;      s   

z$AsyncAdapt_aiosqlite_ss_cursor.closerP   c                 C     | j d usJ | | j  S r7   )rD   r)   rQ   r8   r2   r2   r3   rQ         z'AsyncAdapt_aiosqlite_ss_cursor.fetchoneNrR   rS   rT   c                 C  s0   | j d usJ |d u r| j}| | j j|dS )N)rR   )rD   r+   r)   r^   r]   r2   r2   r3   r^      s   z(AsyncAdapt_aiosqlite_ss_cursor.fetchmanyc                 C  ro   r7   )rD   r)   rC   r8   r2   r2   r3   rC      rp   z'AsyncAdapt_aiosqlite_ss_cursor.fetchall)rj   r   rk   r   r5   r6   ra   rb   r7   rc   rd   )re   rf   rg   rh   rB   r4   r;   rQ   r^   rC   __classcell__r2   r2   rm   r3   ri      s    

ri   c                   @  s   e Zd ZeeZdZd+dd	Zed,ddZ	e	j
d-ddZ	d.ddZd/d0ddZd1ddZd2ddZd2ddZd2dd Zd3d$d%Zd2d&d'Zd2d(d)Zd*S )4r/   )dbapirr   r   
connectionr   r5   r6   c                 C  s   || _ || _d S r7   )rr   r'   )r1   rr   rs   r2   r2   r3   r4     s   
z(AsyncAdapt_aiosqlite_connection.__init__Optional[str]c                 C  s   t t| jjS r7   )r   strr'   isolation_levelr8   r2   r2   r3   rv     s   z/AsyncAdapt_aiosqlite_connection.isolation_levelvaluec              
   C  sz   d	dd}t || jj|}t  }| jj||f z| | W d S  t	y< } z| 
| W Y d }~d S d }~ww )
Nrs   r/   rw   rt   r5   r6   c                 S  s
   || _ d S r7   )rv   )rs   rw   r2   r2   r3   set_iso  s   
z@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso)rs   r/   rw   rt   r5   r6   )r   r'   _connasyncioget_event_loopcreate_future_tx
put_nowaitr)   rE   rF   )r1   rw   rx   functionfuturerG   r2   r2   r3   rv     s   
argsrk   c              
   O  sP   z|  | jj|i | W d S  ty' } z| | W Y d }~d S d }~ww r7   )r)   r'   create_functionrE   rF   )r1   r   rk   rG   r2   r2   r3   r   "  s   z/AsyncAdapt_aiosqlite_connection.create_functionFrB   boolr%   c                 C  s   |rt | S t| S r7   )ri   r%   )r1   rB   r2   r2   r3   r@   (  s   z&AsyncAdapt_aiosqlite_connection.cursorc                 O  s   |  | jj|i |S r7   )r)   r'   rA   )r1   r   rk   r2   r2   r3   rA   .  s   z'AsyncAdapt_aiosqlite_connection.executec              
   C  H   z|  | j  W d S  ty# } z| | W Y d }~d S d }~ww r7   )r)   r'   rollbackrE   rF   r1   rG   r2   r2   r3   r   1     z(AsyncAdapt_aiosqlite_connection.rollbackc              
   C  r   r7   )r)   r'   commitrE   rF   r   r2   r2   r3   r   7  r   z&AsyncAdapt_aiosqlite_connection.commitc              
   C  sZ   z|  | j  W d S  ty   Y d S  ty, } z| | W Y d }~d S d }~ww r7   )r)   r'   r;   
ValueErrorrE   rF   r   r2   r2   r3   r;   =  s   	z%AsyncAdapt_aiosqlite_connection.closerG   rE   r   c                 C  s,   t |tr|jd dkr| jjd||)Nr   no active connection)
isinstancer   r   rr   sqliteOperationalErrorr   r2   r2   r3   rF   M  s   z1AsyncAdapt_aiosqlite_connection._handle_exceptionc                   s   | j  I dH  dS )z"Try to close connection gracefullyN)r'   r;   r8   r2   r2   r3   _terminate_graceful_closeX  s   z9AsyncAdapt_aiosqlite_connection._terminate_graceful_closec              
   C  s:   z| j j}W n ty } ztd|d}~ww |  dS )zTerminate the connectionz:terminate_force_close() not implemented by this DBAPI shimN)r'   stopAttributeErrorNotImplementedError)r1   methaer2   r2   r3   _terminate_force_close\  s   
z6AsyncAdapt_aiosqlite_connection._terminate_force_closeN)rr   r   rs   r   r5   r6   )r5   rt   )rw   rt   r5   r6   )r   r   rk   r   r5   r6   )F)rB   r   r5   r%   )r   r   rk   r   r5   r   ra   )rG   rE   r5   r   )re   rf   rg   staticmethodr   r)   rh   r4   propertyrv   setterr   r@   rA   r   r   r;   rF   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)re   rf   rg   rh   r   r   r)   r2   r2   r2   r3   r   k  s    r   c                   @  s*   e Zd ZdddZddd	ZdddZdS )AsyncAdapt_aiosqlite_dbapi	aiosqliter   r   c                 C  s,   || _ || _d| _t|jd| _|   d S )Nqmarkr   )r   r   
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)DatabaseErrorErrorIntegrityErrorNotSupportedErrorr   ProgrammingErrorsqlite_versionsqlite_version_info)PARSE_COLNAMESPARSE_DECLTYPES)Binary)setattrgetattrr   r   )r1   namer2   r2   r3   r   y  s   
z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesrj   r   rk   r/   c                 O  s~   | dd}| dd }|r||i |}n| jj|i |}t|tr(d|_nd|j_t|r8t	| t
|S t| t|S )Nasync_fallbackFasync_creator_fnT)popr   connectr   r   daemon_threadr   asboolr   r   r/   r   )r1   rj   rk   r   
creator_fnrs   r2   r2   r3   r     s"   

z"AsyncAdapt_aiosqlite_dbapi.connectN)r   r   r   r   ra   )rj   r   rk   r   r5   r/   )re   rf   rg   r4   r   r   r2   r2   r2   r3   r   q  s    

r   c                   @  s   e Zd ZdddZdS ) SQLiteExecutionContext_aiosqliter5   r!   c                 C  s   | j jddS )NT)rB   )_dbapi_connectionr@   r8   r2   r2   r3   create_server_side_cursor  r<   z:SQLiteExecutionContext_aiosqlite.create_server_side_cursorN)r5   r!   )re   rf   rg   r   r2   r2   r2   r3   r     s    r   c                      st   e Zd ZdZdZdZdZdZeZ	d! fddZ
ed"d	d
Zed#ddZd$ fddZd%ddZd&dd Z  ZS )'SQLiteDialect_aiosqliter   Tkwargsr   c                   s2   t  jdi | | jr| jjsd| _d S d S d S )NFr2   )rl   r4   rr   r   has_terminate)r1   r   rm   r2   r3   r4     s   
z SQLiteDialect_aiosqlite.__init__r5   r   c                 C  s   t tdtdS )Nr   sqlite3)r   
__import__)clsr2   r2   r3   import_dbapi  s   z$SQLiteDialect_aiosqlite.import_dbapiurlr#   type[pool.Pool]c                 C  s   |  |rtjS tjS r7   )_is_url_file_dbr   AsyncAdaptedQueuePool
StaticPool)r   r   r2   r2   r3   get_pool_class  s   
z&SQLiteDialect_aiosqlite.get_pool_classeDBAPIModule.Errorrs   7Optional[Union[PoolProxiedConnection, DBAPIConnection]]r@   Optional[DBAPICursor]r   c                   s<   t d| j| _t|| jjrdt|v rdS t |||S )Nr"   r   T)r   rr   r   r   ru   rl   is_disconnect)r1   r   rs   r@   rm   r2   r3   r     s   z%SQLiteDialect_aiosqlite.is_disconnectr    r   c                 C  s   |j S r7   )r'   )r1   rs   r2   r2   r3   get_driver_connection  s   z-SQLiteDialect_aiosqlite.get_driver_connectiondbapi_connectionr6   c                 C  s   |   d S r7   )	terminate)r1   r   r2   r2   r3   do_terminate  s   z$SQLiteDialect_aiosqlite.do_terminate)r   r   )r5   r   )r   r#   r5   r   )r   r   rs   r   r@   r   r5   r   )rs   r    r5   r   )r   r    r5   r6   )re   rf   rg   driversupports_statement_cacheis_asyncr   supports_server_side_cursorsr   execution_ctx_clsr4   classmethodr   r   r   r   r   rq   r2   r2   rm   r3   r     s    
r   ):__doc__
__future__r   rz   collectionsr   	functoolsr   	threadingr   typesr   typingr   r   r	   r
   r   r   r   r   r   baser   pysqliter    r   r   connectors.asyncior   r   enginer   util.concurrencyr   r   r   r   engine.interfacesr   r   r   r    r!   r"   
engine.urlr#   	pool.baser$   r%   ri   r/   r   r   r   r   dialectr2   r2   r2   r3   <module>   sX   Gik84