a
    •˜ëj·C  ã                   @  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�rž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%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 )4u  

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

.. _aiosqlite_memory:

Using a Memory Database with Multiple Coroutines
-------------------------------------------------

The default :class:`.StaticPool` used for ``:memory:`` databases forces all
coroutines to share a single DBAPI connection.  Because SQLite maintains only
one transaction state per connection, concurrent coroutines can interfere
with each other â€” a ``ROLLBACK`` in one coroutine will also discard
uncommitted work from any other coroutine using the same engine.

For async workloads where multiple :class:`.AsyncSession` or
:class:`.AsyncConnection` objects may be active simultaneously, use SQLite's
shared-cache URI mode instead.  This gives each checkout its own DBAPI
connection with independent transaction state while still sharing one
in-memory database::

    engine = create_async_engine(
        "sqlite+aiosqlite:///file::memory:?cache=shared&uri=true"
    )

Because this URL form is treated as a file-based database by the dialect,
:class:`.AsyncAdaptedQueuePool` is used automatically and no additional
configuration is needed.

See the pysqlite documentation at
:ref:`pysqlite_uri_shared_cache` for full details on shared-cache memory
databases, including how to use named databases to maintain multiple
independent in-memory databases within the same process.

é    )Ú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   úc/mnt/bigstor/www/usecases/.venv/lib/python3.9/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 u r,|   | |¡¡ n|   | ||¡¡ |jrt|j| _d | _| _| jsŠt|   | 	¡ ¡ƒ| _
nd | _|j| _|j| _| js |   | ¡ ¡ n|| _W n0 tyØ } z| j |¡ W Y d }~n
d }~0 0 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 n0 tyz } z| j	 
|¡ W Y d }~n
d }~0 0 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 u r| j }| j‰ ‡ fdd„tt|tˆ ƒƒƒD ƒS )Nc                   s   g | ]}ˆ   ¡ ‘qS r2   )rL   )Ú.0Ú_©Úrrr2   r3   Ú
<listcomp>ø   ó    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%   —   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|i |¤Ž d | _d S r8   )Úsuperr4   rC   )r1   rg   rh   ©Ú	__class__r2   r3   r4     s    z'AsyncAdapt_aiosqlite_ss_cursor.__init__r6   c                 C  s$   | j d u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 usJ ‚|  | j  ¡ ¡S r8   )rC   r)   rO   r9   r2   r2   r3   rO     s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchoneNrP   rQ   rR   c                 C  s0   | j d usJ ‚|d u r| j}|  | j j|d�¡S )N)rS   )rC   r+   r)   r^   r]   r2   r2   r3   r^     s    z(AsyncAdapt_aiosqlite_ss_cursor.fetchmanyc                 C  s   | j d usJ ‚|  | j  ¡ ¡S r8   )rC   r)   rB   r9   r2   r2   r3   rB     s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchall)N)ra   rb   rc   rd   rA   r4   r<   rO   r^   rB   Ú__classcell__r2   r2   rj   r3   re      s   re   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   )rm   Ú
connectionr7   c                 C  s   || _ || _d S r8   )rm   r'   )r1   rm   rn   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   rq   '  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	y| } z|  
|¡ W Y d }~n
d }~0 0 d S )Nr.   ro   r5   )rn   rr   r7   c                 S  s
   || _ d S r8   )rq   )rn   rr   r2   r2   r3   Úset_iso2  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   rr   rs   ÚfunctionÚfuturerF   r2   r2   r3   rq   +  s    )Úargsrh   r7   c              
   O  sP   z|   | jj|i |¤Ž¡ W n. tyJ } z|  |¡ W Y d }~n
d }~0 0 d S r8   )r)   r'   Úcreate_functionrD   rE   )r1   rx   rh   rF   r2   r2   r3   ry   A  s    z/AsyncAdapt_aiosqlite_connection.create_functionFÚboolr%   )rA   r7   c                 C  s   |rt | ƒS t| ƒS d S r8   )re   r%   )r1   rA   r2   r2   r3   r?   G  s    z&AsyncAdapt_aiosqlite_connection.cursorc                 O  s   |   | jj|i |¤Ž¡S r8   )r)   r'   r@   )r1   rx   rh   r2   r2   r3   r@   M  s    z'AsyncAdapt_aiosqlite_connection.executec              
   C  sH   z|   | j ¡ ¡ W n. tyB } z|  |¡ W Y d }~n
d }~0 0 d S r8   )r)   r'   ÚrollbackrD   rE   ©r1   rF   r2   r2   r3   r{   P  s    z(AsyncAdapt_aiosqlite_connection.rollbackc              
   C  sH   z|   | j ¡ ¡ W n. tyB } z|  |¡ W Y d }~n
d }~0 0 d S r8   )r)   r'   ÚcommitrD   rE   r|   r2   r2   r3   r}   V  s    z&AsyncAdapt_aiosqlite_connection.commitc              
   C  sX   z|   | j ¡ ¡ W n> ty&   Y n. tyR } z|  |¡ W Y d }~n
d }~0 0 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~   rx   rm   ÚsqliteÚOperationalErrorr|   r2   r2   r3   rE   l  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_closew  s    z9AsyncAdapt_aiosqlite_connection._terminate_graceful_closec              
   C  sF   z| j j}W n. ty: } ztdƒ|‚W Y d}~nd}~0 0 |ƒ  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)ra   rb   rc   Ústaticmethodr   r)   rd   r4   Úpropertyrq   Úsetterry   r?   r@   r{   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)ra   rb   rc   rd   r‰   r   r)   r2   r2   r2   r3   rŒ   Š  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   ‘  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’   ˜  s    
z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesr   r.   rf   c                 O  s‚   |  dd¡}|  dd ¡}|r,||i |¤Ž}n,| jj|i |¤Ž}t|tƒrPd|_nd|j_t |¡rpt	| t
|ƒƒS t| t|ƒƒS d S )NÚasync_fallbackFZasync_creator_fnT)ÚpoprŽ   Úconnectr€   r   ÚdaemonÚ_threadr   ZasboolrŒ   r   r.   r   )r1   rg   rh   r˜   Z
creator_fnrn   r2   r2   r3   rš   «  s"    

þþz"AsyncAdapt_aiosqlite_dbapi.connectN)ra   rb   rc   r4   r’   rš   r2   r2   r2   r3   r�   �  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)ra   rb   rc   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 i |¤Ž | jr&| jjs&d| _d S )NF)ri   r4   rm   r‘   Úhas_terminate)r1   r    rj   r2   r3   r4   Ø  s    z SQLiteDialect_aiosqlite.__init__r�   r6   c                 C  s   t tdƒtdƒƒS )NrŽ   Z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]rz   )Úern   r?   r7   c                   s<   t d| jƒ| _t|| jjƒr,dt|ƒv r,dS tƒ  |||¡S )Nr"   r   T)r   rm   r€   r‚   rp   ri   Úis_disconnect)r1   r§   rn   r?   rj   r2   r3   r¨   ê  s    ÿ
þz%SQLiteDialect_aiosqlite.is_disconnectr    r   )rn   r7   c                 C  s   |j S r8   )r'   )r1   rn   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)ra   rb   rc   ZdriverZsupports_statement_cacheÚis_asyncr¡   Zsupports_server_side_cursorsr�   Zexecution_ctx_clsr4   Úclassmethodr¤   r¦   r¨   r©   r¬   rl   r2   r2   rj   r3   rŸ   Í  s   rŸ   ):Ú__doc__Ú
__future__r   rt   Ú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%   re   r.   rŒ   r�   r�   rŸ   Údialectr2   r2   r2   r3   Ú<module>	   sV   fik84