
    [h3                     B   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 ddlmZ  G d d          Z G d de          Z G d de          Z G d de          Z G d d          Z G d de          Z G d de	          ZeZdS )a  

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

Similarly to pysqlite, aiosqlite does not support SAVEPOINT feature.

The solution is similar to :ref:`pysqlite_serializable`. This is achieved by the event listeners in async::

    from sqlalchemy import create_engine, event
    from sqlalchemy.ext.asyncio import create_async_engine

    engine = create_async_engine("sqlite+aiosqlite:///myfile.db")


    @event.listens_for(engine.sync_engine, "connect")
    def do_connect(dbapi_connection, connection_record):
        # disable aiosqlite's emitting of the BEGIN statement entirely.
        # also stops it from emitting COMMIT before any DDL.
        dbapi_connection.isolation_level = None


    @event.listens_for(engine.sync_engine, "begin")
    def do_begin(conn):
        # emit our own BEGIN
        conn.exec_driver_sql("BEGIN")

.. warning:: When using the above recipe, it is advised to not use the
   :paramref:`.Connection.execution_options.isolation_level` setting on
   :class:`_engine.Connection` and :func:`_sa.create_engine`
   with the SQLite driver,
   as this function necessarily will also alter the ".isolation_level" setting.

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

    N)deque)partial   )SQLiteExecutionContext)SQLiteDialect_pysqlite   )pool)util)AdaptedConnection)await_fallback)
await_onlyc                   P    e Zd ZdZdZd Zd ZddZd Zd Z	d	 Z
d
 ZddZd ZdS )AsyncAdapt_aiosqlite_cursor)_adapt_connection_connectiondescriptionawait__rows	arraysizerowcount	lastrowidFc                     || _         |j        | _        |j        | _        d| _        d| _        d | _        t                      | _        d S )Nr   )r   r   r   r   r   r   r   r   )selfadapt_connections     s/var/www/api.easyaligner.net/htdocs/venv_linux/lib/python3.11/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__z$AsyncAdapt_aiosqlite_cursor.__init__   sD    !1+7&-WW


    c                 8    | j                                          d S N)r   clearr   s    r   closez!AsyncAdapt_aiosqlite_cursor.close   s    
r   Nc                    	 |                      | j                                                  }|)|                      |                    |                     n)|                      |                    ||                     |j        r[|j        | _        dx| _        | _        | j        s9t          |                      |	                                                    | _
        nd | _        |j        | _        |j        | _        | j        s)|                      |                                           d S || _        d S # t          $ r%}| j                            |           Y d }~d S d }~ww xY w)Nr   )r   r   cursorexecuter   r   r   server_sider   fetchallr   r#   _cursor	Exceptionr   _handle_exception)r   	operation
parametersr)   errors        r   r&   z#AsyncAdapt_aiosqlite_cursor.execute   s`   	<kk$"2"9"9";";<<G!GOOI667777GOOIzBBCCC" 	1#*#6 133' H!&t{{73C3C3E3E'F'F!G!GDJ#' !(!2 ' 0# 'GMMOO,,,,,& 	< 	< 	<"44U;;;;;;;;;	<s   D/D< 3D< <
E+E&&E+c                    	 |                      | j                                                  }|                      |                    ||                     d | _        |j        | _        |j        | _        |                      |                                           d S # t          $ r%}| j	        
                    |           Y d }~d S d }~ww xY wr    )r   r   r%   executemanyr   r   r   r#   r*   r   r+   )r   r,   seq_of_parametersr)   r.   s        r   r0   z'AsyncAdapt_aiosqlite_cursor.executemany   s    	<kk$"2"9"9";";<<GKK++I7HIIJJJ#D$.DN#,DMKK((((( 	< 	< 	<"44U;;;;;;;;;	<s   BB 
C)C		Cc                     d S r     )r   
inputsizess     r   setinputsizesz)AsyncAdapt_aiosqlite_cursor.setinputsizes   s    r   c              #   `   K   | j         r$| j                                         V  | j         "d S d S r    r   popleftr"   s    r   __iter__z$AsyncAdapt_aiosqlite_cursor.__iter__   sJ      j 	'*$$&&&&& j 	' 	' 	' 	' 	'r   c                 F    | j         r| j                                         S d S r    r7   r"   s    r   fetchonez$AsyncAdapt_aiosqlite_cursor.fetchone   s%    : 	:%%'''4r   c           	          || j         }| j        fdt          t          |t	                                        D             S )Nc                 8    g | ]}                                 S r3   )r8   ).0_rrs     r   
<listcomp>z9AsyncAdapt_aiosqlite_cursor.fetchmany.<locals>.<listcomp>   s!    @@@

@@@r   )r   r   rangeminlen)r   sizer@   s     @r   	fetchmanyz%AsyncAdapt_aiosqlite_cursor.fetchmany   sH    <>DZ@@@@eCc"gg,>,>&?&?@@@@r   c                 `    t          | j                  }| j                                         |S r    )listr   r!   )r   retvals     r   r(   z$AsyncAdapt_aiosqlite_cursor.fetchall   s*    dj!!
r   r    )__name__
__module____qualname__	__slots__r'   r   r#   r&   r0   r5   r9   r;   rF   r(   r3   r   r   r   r   v   s        	I K    < < < <6	< 	< 	<  ' ' '  A A A A    r   r   c                   @     e Zd ZdZdZ fdZd Zd Zd	dZd Z	 xZ
S )
AsyncAdapt_aiosqlite_ss_cursorr)   Tc                 H     t                      j        |i | d | _        d S r    )superr   r)   )r   argkw	__class__s      r   r   z'AsyncAdapt_aiosqlite_ss_cursor.__init__   s*    #$$$$r   c                 ~    | j         5|                     | j                                                    d | _         d S d S r    )r)   r   r#   r"   s    r   r#   z$AsyncAdapt_aiosqlite_ss_cursor.close   s=    <#KK**,,---DLLL $#r   c                 Z    |                      | j                                                  S r    )r   r)   r;   r"   s    r   r;   z'AsyncAdapt_aiosqlite_ss_cursor.fetchone   "    {{4<0022333r   Nc                 p    || j         }|                     | j                            |                    S )N)rE   )r   r   r)   rF   )r   rE   s     r   rF   z(AsyncAdapt_aiosqlite_ss_cursor.fetchmany   s3    <>D{{4<11t1<<===r   c                 Z    |                      | j                                                  S r    )r   r)   r(   r"   s    r   r(   z'AsyncAdapt_aiosqlite_ss_cursor.fetchall   rW   r   r    )rJ   rK   rL   rM   r'   r   r#   r;   rF   r(   __classcell__rT   s   @r   rO   rO      s         IK         
4 4 4> > > >
4 4 4 4 4 4 4r   rO   c                       e Zd Z ee          ZdZd Zed             Z	e	j
        d             Z	d ZddZd Zd	 Zd
 Zd Zd ZdS )AsyncAdapt_aiosqlite_connection)dbapic                 "    || _         || _        d S r    )r^   r   )r   r^   
connections      r   r   z(AsyncAdapt_aiosqlite_connection.__init__   s    
%r   c                     | j         j        S r    )r   isolation_levelr"   s    r   rb   z/AsyncAdapt_aiosqlite_connection.isolation_level   s    //r   c                 P   d }t          || j        j        |          }t          j                                                    }| j        j                            ||f           	 |                     |          S # t          $ r }| 
                    |           Y d }~d S d }~ww xY w)Nc                     || _         d S r    )rb   )r`   values     r   set_isoz@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso  s    ).J&&&r   )r   r   _connasyncioget_event_loopcreate_future_tx
put_nowaitr   r*   r+   )r   re   rf   functionfuturer.   s         r   rb   z/AsyncAdapt_aiosqlite_connection.isolation_level   s    	/ 	/ 	/ 7D$4$:EBB'))7799''(:;;;	*;;v&&& 	* 	* 	*""5)))))))))	*s   &A; ;
B%B  B%c                     	 |                       | j        j        |i |           d S # t          $ r }|                     |           Y d }~d S d }~ww xY wr    )r   r   create_functionr*   r+   )r   argsrS   r.   s       r   rp   z/AsyncAdapt_aiosqlite_connection.create_function  sz    	*KK8(8$E"EEFFFFF 	* 	* 	*""5)))))))))	*s   %) 
AAAFc                 B    |rt          |           S t          |           S r    )rO   r   )r   r'   s     r   r%   z&AsyncAdapt_aiosqlite_connection.cursor  s&     	51$777.t444r   c                 L    |                       | j        j        |i |          S r    )r   r   r&   )r   rq   rS   s      r   r&   z'AsyncAdapt_aiosqlite_connection.execute  s*    {{34+3T@R@@AAAr   c                     	 |                      | j                                                   d S # t          $ r }|                     |           Y d }~d S d }~ww xY wr    )r   r   rollbackr*   r+   r   r.   s     r   ru   z(AsyncAdapt_aiosqlite_connection.rollback  ss    	*KK(113344444 	* 	* 	*""5)))))))))	*   ,0 
AAAc                     	 |                      | j                                                   d S # t          $ r }|                     |           Y d }~d S d }~ww xY wr    )r   r   commitr*   r+   rv   s     r   ry   z&AsyncAdapt_aiosqlite_connection.commit$  ss    	*KK(//1122222 	* 	* 	*""5)))))))))	*rw   c                     	 |                      | j                                                   d S # t          $ r Y d S t          $ r }|                     |           Y d }~d S d }~ww xY wr    )r   r   r#   
ValueErrorr*   r+   rv   s     r   r#   z%AsyncAdapt_aiosqlite_connection.close*  s    	*KK(..0011111 		 		 		 DD 	* 	* 	*""5)))))))))	*s   ,0 
A&	A&A!!A&c                     t          |t                    r1|j        d         dk    r | j        j                            d          ||)Nr   no active connection)
isinstancer{   rq   r^   sqliteOperationalErrorrv   s     r   r+   z1AsyncAdapt_aiosqlite_connection._handle_exception:  sP    uj))	
1!777*#44&  Kr   N)F)rJ   rK   rL   staticmethodr   r   rM   r   propertyrb   setterrp   r%   r&   ru   ry   r#   r+   r3   r   r   r]   r]      s        \*%%FI& & & 0 0 X0 * * *&* * *5 5 5 5B B B* * ** * ** * * 	 	 	 	 	r   r]   c                   (    e Zd ZdZ ee          ZdS )'AsyncAdaptFallback_aiosqlite_connectionr3   N)rJ   rK   rL   rM   r   r   r   r3   r   r   r   r   F  s"        I\.))FFFr   r   c                        e Zd Zd Zd Zd ZdS )AsyncAdapt_aiosqlite_dbapic                 X    || _         || _        d| _        |                                  d S )Nqmark)	aiosqliter   
paramstyle_init_dbapi_attributes)r   r   r   s      r   r   z#AsyncAdapt_aiosqlite_dbapi.__init__M  s/    "!##%%%%%r   c           	          dD ]&}t          | |t          | j        |                     'dD ]&}t          | |t          | j        |                     'dD ]&}t          | |t          | j        |                     'd S )N)DatabaseErrorErrorIntegrityErrorNotSupportedErrorr   ProgrammingErrorsqlite_versionsqlite_version_info)PARSE_COLNAMESPARSE_DECLTYPES)Binary)setattrgetattrr   r   )r   names     r   r   z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesS  s    	
 
	? 
	?D D$ = =>>>>9 	< 	<DD$T : :;;;; 	< 	<DD$T : :;;;;	< 	<r   c                 >   |                     dd          }|                     dd           }|r	 ||i |}n | j        j        |i |}d|_        t	          j        |          rt          | t          |                    S t          | t          |                    S )Nasync_fallbackFasync_creator_fnT)
popr   connectdaemonr
   asboolr   r   r]   r   )r   rR   rS   r   
creator_fnr`   s         r   r   z"AsyncAdapt_aiosqlite_dbapi.connectf  s     0%88VV.55
 	%#S/B//JJ//;;;J $J;~&& 		:z**  
 3:&&  r   N)rJ   rK   rL   r   r   r   r3   r   r   r   r   L  sA        & & &< < <&    r   r   c                       e Zd Zd ZdS ) SQLiteExecutionContext_aiosqlitec                 8    | j                             d          S )NT)r'   )_dbapi_connectionr%   r"   s    r   create_server_side_cursorz:SQLiteExecutionContext_aiosqlite.create_server_side_cursor~  s    %,,,>>>r   N)rJ   rK   rL   r   r3   r   r   r   r   }  s#        ? ? ? ? ?r   r   c                   d     e Zd ZdZdZdZdZeZe	d             Z
e	d             Z fdZd Z xZS )SQLiteDialect_aiosqliter   Tc                 V    t          t          d          t          d                    S )Nr   sqlite3)r   
__import__)clss    r   import_dbapiz$SQLiteDialect_aiosqlite.import_dbapi  s)    ){##Z	%:%:
 
 	
r   c                 \    |                      |          rt          j        S t          j        S r    )_is_url_file_dbr	   AsyncAdaptedQueuePool
StaticPool)r   urls     r   get_pool_classz&SQLiteDialect_aiosqlite.get_pool_class  s)    s## 	#--?"r   c                     t          || j        j                  rdt          |          v rdS t	                                          |||          S )Nr}   T)r~   r^   r   strrQ   is_disconnect)r   er`   r%   rT   s       r   r   z%SQLiteDialect_aiosqlite.is_disconnect  sQ    tz*
 
 	$A..4ww$$Q
F;;;r   c                     |j         S r    )r   )r   r`   s     r   get_driver_connectionz-SQLiteDialect_aiosqlite.get_driver_connection  s    %%r   )rJ   rK   rL   driversupports_statement_cacheis_asyncsupports_server_side_cursorsr   execution_ctx_clsclassmethodr   r   r   r   rZ   r[   s   @r   r   r     s        F#H#' 8
 
 [

 # # [#< < < < <& & & & & & &r   r   )__doc__rh   collectionsr   	functoolsr   baser   pysqliter    r	   r
   enginer   util.concurrencyr   r   r   rO   r]   r   r   r   r   dialectr3   r   r   <module>r      s  ] ]~              ( ( ( ( ( ( , , , , , ,             ' ' ' ' ' ' . . . . . . * * * * * *Z Z Z Z Z Z Z Zz4 4 4 4 4%@ 4 4 48T T T T T&7 T T Tn* * * * *.M * * *. . . . . . . .b? ? ? ? ?'= ? ? ?
 &  &  &  &  &4  &  &  &F "r   