o
     h9                     @  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! 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#l-m.Z. G d$d% d%Z/G d&d' d'e/Z0G d(d) d)eZ1G d*d+ d+e1Z2G d,d- d-eZ3G d.d/ d/eZ4G d0d1 d1eZ5e5Z6dS )2a	  

.. 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)
ModuleType)Any)cast)Deque)Iterator)NoReturn)Optional)Sequence)TYPE_CHECKING)Union   )SQLiteExecutionContext)SQLiteDialect_pysqlite   )pool)util)AsyncAdapt_dbapi_module)AdaptedConnection)await_fallback)
await_only)AsyncIODBAPIConnection)AsyncIODBAPICursor)_DBAPICursorDescription)_DBAPIMultiExecuteParams)_DBAPISingleExecuteParams)DBAPIConnection)DBAPICursor)DBAPIModule)URL)PoolProxiedConnectionc                   @  st   e Zd ZdZ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(d.d"d#Zd/d$d%ZdS )0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,    r0   W/var/www/html/venv/lib/python3.10/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__   s   z$AsyncAdapt_aiosqlite_cursor.__init__returnNonec                 C  s   | j   d S N)r(   clearr/   r0   r0   r1   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 )Nr.   )r'   r%   cursorexecuter&   r+   r*   server_sider   fetchallr(   r8   _cursor	Exceptionr$   _handle_exception)r/   r:   r;   rA   errorr0   r0   r1   r>      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 r5   )r'   r%   r=   executemanyr&   r+   r*   r8   rB   r$   rC   )r/   r:   rE   rA   rD   r0   r0   r1   rF      s   z'AsyncAdapt_aiosqlite_cursor.executemany
inputsizesc                 G  s   d S r5   r0   )r/   rG   r0   r0   r1   setinputsizes   s   z)AsyncAdapt_aiosqlite_cursor.setinputsizesIterator[Any]c                 c  s"    | j r| j  V  | j sd S d S r5   r(   popleftr7   r0   r0   r1   __iter__   s   z$AsyncAdapt_aiosqlite_cursor.__iter__Optional[Any]c                 C  s   | j r| j  S d S r5   rJ   r7   r0   r0   r1   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 r0   )rK   ).0_rrr0   r1   
<listcomp>   s    z9AsyncAdapt_aiosqlite_cursor.fetchmany.<locals>.<listcomp>)r)   r(   rangeminlenr/   rO   r0   rT   r1   	fetchmany   s    z%AsyncAdapt_aiosqlite_cursor.fetchmanyc                 C  s   t | j}| j  |S r5   )listr(   r6   )r/   retvalr0   r0   r1   r@      s   

z$AsyncAdapt_aiosqlite_cursor.fetchall)r,   r-   r3   r4   r5   )r:   r   r;   r<   r3   r   )r:   r   rE   r   r3   r   )rG   r   r3   r4   )r3   rI   r3   rM   rO   rP   r3   rQ   r3   rQ   )__name__
__module____qualname__	__slots__r?   r2   r8   r>   rF   rH   rL   rN   r[   r@   r0   r0   r0   r1   r#   v   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_cursorrA   Targr   kwr3   r4   c                   s   t  j|i | d | _d S r5   )superr2   rA   )r/   rg   rh   	__class__r0   r1   r2      s   
z'AsyncAdapt_aiosqlite_ss_cursor.__init__c                 C  s(   | j d ur| | j   d | _ d S d S r5   )rA   r'   r8   r7   r0   r0   r1   r8      s   

z$AsyncAdapt_aiosqlite_ss_cursor.closerM   c                 C     | j d usJ | | j  S r5   )rA   r'   rN   r7   r0   r0   r1   rN         z'AsyncAdapt_aiosqlite_ss_cursor.fetchoneNrO   rP   rQ   c                 C  s0   | j d usJ |d u r| j}| | j j|dS )N)rO   )rA   r)   r'   r[   rZ   r0   r0   r1   r[      s   z(AsyncAdapt_aiosqlite_ss_cursor.fetchmanyc                 C  rl   r5   )rA   r'   r@   r7   r0   r0   r1   r@      rm   z'AsyncAdapt_aiosqlite_ss_cursor.fetchall)rg   r   rh   r   r3   r4   r^   r_   r5   r`   ra   )rb   rc   rd   re   r?   r2   r8   rN   r[   r@   __classcell__r0   r0   rj   r1   rf      s    

rf   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+d,ddZd-ddZd.ddZd.ddZd.dd Zd/d$d%Zd&S )0r-   )dbapiro   r   
connectionr   r3   r4   c                 C  s   || _ || _d S r5   )ro   r%   )r/   ro   rp   r0   r0   r1   r2      s   
z(AsyncAdapt_aiosqlite_connection.__init__Optional[str]c                 C  s   t t| jjS r5   )r   strr%   isolation_levelr7   r0   r0   r1   rs     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 )
Nrp   r-   rt   rq   r3   r4   c                 S  s
   || _ d S r5   )rs   )rp   rt   r0   r0   r1   set_iso  s   
z@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso)rp   r-   rt   rq   r3   r4   )r   r%   _connasyncioget_event_loopcreate_future_tx
put_nowaitr'   rB   rC   )r/   rt   ru   functionfuturerD   r0   r0   r1   rs     s   
argsrh   c              
   O  sP   z|  | jj|i | W d S  ty' } z| | W Y d }~d S d }~ww r5   )r'   r%   create_functionrB   rC   )r/   r~   rh   rD   r0   r0   r1   r     s   z/AsyncAdapt_aiosqlite_connection.create_functionFr?   boolr#   c                 C  s   |rt | S t| S r5   )rf   r#   )r/   r?   r0   r0   r1   r=   #  s   z&AsyncAdapt_aiosqlite_connection.cursorc                 O  s   |  | jj|i |S r5   )r'   r%   r>   )r/   r~   rh   r0   r0   r1   r>   )  s   z'AsyncAdapt_aiosqlite_connection.executec              
   C  H   z|  | j  W d S  ty# } z| | W Y d }~d S d }~ww r5   )r'   r%   rollbackrB   rC   r/   rD   r0   r0   r1   r   ,     z(AsyncAdapt_aiosqlite_connection.rollbackc              
   C  r   r5   )r'   r%   commitrB   rC   r   r0   r0   r1   r   2  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 r5   )r'   r%   r8   
ValueErrorrB   rC   r   r0   r0   r1   r8   8  s   	z%AsyncAdapt_aiosqlite_connection.closerD   rB   r
   c                 C  s,   t |tr|jd dkr| jjd||)Nr   no active connection)
isinstancer   r~   ro   sqliteOperationalErrorr   r0   r0   r1   rC   H  s   z1AsyncAdapt_aiosqlite_connection._handle_exceptionN)ro   r   rp   r   r3   r4   )r3   rq   )rt   rq   r3   r4   )r~   r   rh   r   r3   r4   )F)r?   r   r3   r#   )r~   r   rh   r   r3   r   r^   )rD   rB   r3   r
   )rb   rc   rd   staticmethodr   r'   re   r2   propertyrs   setterr   r=   r>   r   r   r8   rC   r0   r0   r0   r1   r-      s    





r-   c                   @  s   e Zd ZdZeeZdS )'AsyncAdaptFallback_aiosqlite_connectionr0   N)rb   rc   rd   re   r   r   r'   r0   r0   r0   r1   r   T  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| _|   d S )Nqmark)r   r   
paramstyle_init_dbapi_attributes)r/   r   r   r0   r0   r1   r2   [  s   z#AsyncAdapt_aiosqlite_dbapi.__init__r3   r4   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   )r/   namer0   r0   r1   r   a  s   
z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesrg   r   rh   r-   c                 O  sj   | dd}| dd }|r||i |}n| jj|i |}d|_t|r.t| t|S t| t	|S )Nasync_fallbackFasync_creator_fnT)
popr   connectdaemonr   asboolr   r   r-   r   )r/   rg   rh   r   
creator_fnrp   r0   r0   r1   r   t  s   
z"AsyncAdapt_aiosqlite_dbapi.connectN)r   r   r   r   r^   )rg   r   rh   r   r3   r-   )rb   rc   rd   r2   r   r   r0   r0   r0   r1   r   Z  s    

r   c                   @  s   e Zd ZdddZdS ) SQLiteExecutionContext_aiosqliter3   r   c                 C  s   | j jddS )NT)r?   )_dbapi_connectionr=   r7   r0   r0   r1   create_server_side_cursor  r9   z:SQLiteExecutionContext_aiosqlite.create_server_side_cursorN)r3   r   )rb   rc   rd   r   r0   r0   r0   r1   r     s    r   c                      sX   e Zd ZdZdZdZdZeZe	dddZ
e	dd
dZd fddZdddZ  ZS )SQLiteDialect_aiosqliter   Tr3   r   c                 C  s   t tdtdS )Nr   sqlite3)r   
__import__)clsr0   r0   r1   import_dbapi  s   z$SQLiteDialect_aiosqlite.import_dbapiurlr!   type[pool.Pool]c                 C  s   |  |rtjS tjS r5   )_is_url_file_dbr   AsyncAdaptedQueuePool
StaticPool)r   r   r0   r0   r1   get_pool_class  s   
z&SQLiteDialect_aiosqlite.get_pool_classeDBAPIModule.Errorrp   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   ro   r   r   rr   ri   is_disconnect)r/   r   rp   r=   rj   r0   r1   r     s   z%SQLiteDialect_aiosqlite.is_disconnectr   r   c                 C  s   |j S r5   )r%   )r/   rp   r0   r0   r1   get_driver_connection  s   z-SQLiteDialect_aiosqlite.get_driver_connection)r3   r   )r   r!   r3   r   )r   r   rp   r   r=   r   r3   r   )rp   r   r3   r   )rb   rc   rd   driversupports_statement_cacheis_asyncsupports_server_side_cursorsr   execution_ctx_clsclassmethodr   r   r   r   rn   r0   r0   rj   r1   r     s    r   )7__doc__
__future__r   rw   collectionsr   	functoolsr   typesr   typingr   r   r   r	   r
   r   r   r   r   baser   pysqliter    r   r   connectors.asyncior   enginer   util.concurrencyr   r   r   r   engine.interfacesr   r   r   r   r   r    
engine.urlr!   	pool.baser"   r#   rf   r-   r   r   r   r   dialectr0   r0   r0   r1   <module>   sT   GfY1+