
    ,h/                        S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  SS	K
Jr  SS
K
Jr  SSKJr  SSKJr  SSKJr   " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S5      r " S S\5      r " S S\	5      r\rg)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)
-------------------------------------------------------------------------

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.

    N)deque)partial   )SQLiteExecutionContext)SQLiteDialect_pysqlite   )pool)util)AdaptedConnection)await_fallback)
await_onlyc                   Z    \ rS rSrSrSrS rS rSS jrS r	S	 r
S
 rS rSS jrS rSrg)AsyncAdapt_aiosqlite_cursor_   )_adapt_connection_connectiondescriptionawait__rows	arraysizerowcount	lastrowidFc                     Xl         UR                  U l        UR                  U l        SU l        SU l        S U l        [        5       U l        g )Nr   )r   r   r   r   r   r   r   r   )selfadapt_connections     \/var/www/auris/envauris/lib/python3.13/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__$AsyncAdapt_aiosqlite_cursor.__init__p   sB    !1+77&--W
    c                 8    U R                   R                  5         g N)r   clearr   s    r   close!AsyncAdapt_aiosqlite_cursor.closey   s    

r    Nc                     U R                  U R                  R                  5       5      nUc!  U R                  UR                  U5      5        O U R                  UR                  X5      5        UR                  (       a^  UR                  U l        S=U l        U l        U R                  (       d-  [        U R                  UR                  5       5      5      U l
        O)S U l        UR
                  U l        UR                  U l        U R                  (       d   U R                  UR                  5       5        g X0l        g ! [         a%  nU R                  R                  U5         S nAg S nAff = f)Nr   )r   r   cursorexecuter   r   r   server_sider   fetchallr   r%   _cursor	Exceptionr   _handle_exception)r   	operation
parametersr,   errors        r   r)   #AsyncAdapt_aiosqlite_cursor.execute|   s
   	<kk$"2"2"9"9";<G!GOOI67GOOIBC""#*#6#6 133''!&t{{73C3C3E'F!GDJ#' !(!2!2 ' 0 0##GMMO,& 	<""44U;;	<s   D5D? 8D? ?
E.	E))E.c                     U R                  U R                  R                  5       5      nU R                  UR                  X5      5        S U l        UR
                  U l        UR                  U l        U R                  UR                  5       5        g ! [         a%  nU R                  R                  U5         S nAg S nAff = fr"   )r   r   r(   executemanyr   r   r   r%   r-   r   r.   )r   r/   seq_of_parametersr,   r1   s        r   r4   'AsyncAdapt_aiosqlite_cursor.executemany   s    	<kk$"2"2"9"9";<GKK++IIJ#D$..DN#,,DMKK( 	<""44U;;	<s   BB 
CB>>Cc                     g r"    )r   
inputsizess     r   setinputsizes)AsyncAdapt_aiosqlite_cursor.setinputsizes   s    r    c              #      #    U R                   (       a0  U R                   R                  5       v   U R                   (       a  M/  g g 7fr"   r   popleftr$   s    r   __iter__$AsyncAdapt_aiosqlite_cursor.__iter__   s*     jj**$$&& jjjs   ?AAc                 Z    U R                   (       a  U R                   R                  5       $ g r"   r=   r$   s    r   fetchone$AsyncAdapt_aiosqlite_cursor.fetchone   s    ::::%%''r    c                     Uc  U R                   nU R                  n[        [        U[	        U5      5      5       Vs/ s H  o2R                  5       PM     sn$ s  snf r"   )r   r   rangeminlenr>   )r   sizerr_s       r   	fetchmany%AsyncAdapt_aiosqlite_cursor.fetchmany   sG    <>>DZZ&+Cc"g,>&?@&?

&?@@@s   Ac                 d    [        U R                  5      nU R                  R                  5         U$ r"   )listr   r#   )r   retvals     r   r+   $AsyncAdapt_aiosqlite_cursor.fetchall   s%    djj!

r    )	r   r   r,   r   r   r   r   r   r   r"   )__name__
__module____qualname____firstlineno__	__slots__r*   r   r%   r)   r4   r:   r?   rB   rK   r+   __static_attributes__r8   r    r   r   r   _   s>    	I K<6	<'Ar    r   c                   L   ^  \ rS rSrSrSrU 4S jrS rS rS
S jr	S r
S	rU =r$ )AsyncAdapt_aiosqlite_ss_cursor   r,   Tc                 4   > [         TU ]  " U0 UD6  S U l        g r"   )superr   r,   )r   argkw	__class__s      r   r   'AsyncAdapt_aiosqlite_ss_cursor.__init__   s    #$$r    c                     U R                   b1  U R                  U R                   R                  5       5        S U l         g g r"   )r,   r   r%   r$   s    r   r%   $AsyncAdapt_aiosqlite_ss_cursor.close   s1    <<#KK**,-DL $r    c                 T    U R                  U R                  R                  5       5      $ r"   )r   r,   rB   r$   s    r   rB   'AsyncAdapt_aiosqlite_ss_cursor.fetchone       {{4<<00233r    c                 p    Uc  U R                   nU R                  U R                  R                  US95      $ )N)rH   )r   r   r,   rK   )r   rH   s     r   rK   (AsyncAdapt_aiosqlite_ss_cursor.fetchmany   s1    <>>D{{4<<11t1<==r    c                 T    U R                  U R                  R                  5       5      $ r"   )r   r,   r+   r$   s    r   r+   'AsyncAdapt_aiosqlite_ss_cursor.fetchall   rd   r    )r,   r"   )rQ   rR   rS   rT   rU   r*   r   r%   rB   rK   r+   rV   __classcell__r^   s   @r   rX   rX      s-     IK 
4>
4 4r    rX   c                       \ rS rSr\" \5      rSrS r\	S 5       r
\
R                  S 5       r
S rSS jrS rS	 rS
 rS rS rSrg)AsyncAdapt_aiosqlite_connection   )dbapic                     Xl         X l        g r"   )rn   r   )r   rn   
connections      r   r   (AsyncAdapt_aiosqlite_connection.__init__   s    
%r    c                 .    U R                   R                  $ r"   )r   isolation_levelr$   s    r   rs   /AsyncAdapt_aiosqlite_connection.isolation_level   s    ///r    c                 N   S n[        X R                  R                  U5      n[        R                  " 5       R                  5       nU R                  R                  R                  XC45         U R                  U5      $ ! [         a  nU R                  U5         S nAg S nAff = f)Nc                     Xl         g r"   )rs   )rp   values     r   set_iso@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso   s    ).&r    )r   r   _connasyncioget_event_loopcreate_future_tx
put_nowaitr   r-   r.   )r   rw   rx   functionfuturer1   s         r   rs   rt      s    	/ 7$4$4$:$:EB'')779''(:;	*;;v&& 	*""5))	*s   .A? ?
B$	BB$c                      U R                  U R                  R                  " U0 UD65        g ! [         a  nU R	                  U5         S nAg S nAff = fr"   )r   r   create_functionr-   r.   )r   argsr]   r1   s       r   r   /AsyncAdapt_aiosqlite_connection.create_function   sJ    	*KK((88$E"EF 	*""5))	*s   +. 
AAAc                 <    U(       a  [        U 5      $ [        U 5      $ r"   )rX   r   )r   r*   s     r   r(   &AsyncAdapt_aiosqlite_connection.cursor   s    1$77.t44r    c                 X    U R                  U R                  R                  " U0 UD65      $ r"   )r   r   r)   )r   r   r]   s      r   r)   'AsyncAdapt_aiosqlite_connection.execute  s'    {{4++33T@R@AAr    c                      U R                  U R                  R                  5       5        g ! [         a  nU R	                  U5         S nAg S nAff = fr"   )r   r   rollbackr-   r.   r   r1   s     r   r   (AsyncAdapt_aiosqlite_connection.rollback  sC    	*KK((1134 	*""5))	*   ), 
AAAc                      U R                  U R                  R                  5       5        g ! [         a  nU R	                  U5         S nAg S nAff = fr"   )r   r   commitr-   r.   r   s     r   r   &AsyncAdapt_aiosqlite_connection.commit  sC    	*KK((//12 	*""5))	*r   c                      U R                  U R                  R                  5       5        g ! [         a     g [         a  nU R                  U5         S nAg S nAff = fr"   )r   r   r%   
ValueErrorr-   r.   r   s     r   r%   %AsyncAdapt_aiosqlite_connection.close  sP    	*KK((..01 		  	*""5))	*s   ), 
A	AAAc                     [        U[        5      (       a9  UR                  S   S:X  a&  U R                  R                  R                  S5      UeUe)Nr   no active connection)
isinstancer   r   rn   sqliteOperationalErrorr   s     r   r.   1AsyncAdapt_aiosqlite_connection._handle_exception#  sJ    uj))

1!77**##44& Kr    )r   rn   N)F)rQ   rR   rS   rT   staticmethodr   r   rU   r   propertyrs   setterr   r(   r)   r   r   r%   r.   rV   r8   r    r   rl   rl      sh    *%FI& 0 0 * *&*5B*** 	r    rl   c                   (    \ rS rSrSr\" \5      rSrg)'AsyncAdaptFallback_aiosqlite_connectioni/  r8   N)	rQ   rR   rS   rT   rU   r   r   r   rV   r8   r    r   r   r   /  s    I.)Fr    r   c                   &    \ rS rSrS rS rS rSrg)AsyncAdapt_aiosqlite_dbapii5  c                 J    Xl         X l        SU l        U R                  5         g )Nqmark)	aiosqliter   
paramstyle_init_dbapi_attributes)r   r   r   s      r   r   #AsyncAdapt_aiosqlite_dbapi.__init__6  s    "!##%r    c           	          S H#  n[        X[        U R                  U5      5        M%     S H#  n[        X[        U R                  U5      5        M%     S H#  n[        X[        U R                  U5      5        M%     g )N)DatabaseErrorErrorIntegrityErrorNotSupportedErrorr   ProgrammingErrorsqlite_versionsqlite_version_info)PARSE_COLNAMESPARSE_DECLTYPES)Binary)setattrgetattrr   r   )r   names     r   r   1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributes<  sg    	
D D =>	
 :DDT :; :  DDT :;  r    c                 :   UR                  SS5      nUR                  SS 5      nU(       a	  U" U0 UD6nO#U R                  R                  " U0 UD6nSUl        [        R
                  " U5      (       a  [        U [        U5      5      $ [        U [        U5      5      $ )Nasync_fallbackFasync_creator_fnT)
popr   connectdaemonr
   asboolr   r   rl   r   )r   r\   r]   r   
creator_fnrp   s         r   r   "AsyncAdapt_aiosqlite_dbapi.connectO  s     0%8VV.5
#S/B/J//;;J $J;;~&&:z* 
 3:& r    )r   r   r   N)rQ   rR   rS   rT   r   r   r   rV   r8   r    r   r   r   5  s    &<&r    r   c                       \ rS rSrS rSrg) SQLiteExecutionContext_aiosqliteif  c                 4    U R                   R                  SS9$ )NT)r*   )_dbapi_connectionr(   r$   s    r   create_server_side_cursor:SQLiteExecutionContext_aiosqlite.create_server_side_cursorg  s    %%,,,>>r    r8   N)rQ   rR   rS   rT   r   rV   r8   r    r   r   r   f  s    ?r    r   c                   b   ^  \ rS rSrSrSrSrSr\r	\
S 5       r\
S 5       rU 4S jrS rSrU =r$ )	SQLiteDialect_aiosqliteik  r   Tc                 >    [        [        S5      [        S5      5      $ )Nr   sqlite3)r   
__import__)clss    r   import_dbapi$SQLiteDialect_aiosqlite.import_dbapiu  s    ){#Z	%:
 	
r    c                 n    U R                  U5      (       a  [        R                  $ [        R                  $ r"   )_is_url_file_dbr	   AsyncAdaptedQueuePool
StaticPool)r   urls     r   get_pool_class&SQLiteDialect_aiosqlite.get_pool_class{  s)    s##---??"r    c                    > [        XR                  R                  5      (       a  S[        U5      ;   a  g[        TU ]  XU5      $ )Nr   T)r   rn   r   strr[   is_disconnect)r   erp   r(   r^   s       r   r   %SQLiteDialect_aiosqlite.is_disconnect  s>    zz**
 
$A.w$QF;;r    c                     UR                   $ r"   )r   )r   rp   s     r   get_driver_connection-SQLiteDialect_aiosqlite.get_driver_connection  s    %%%r    r8   )rQ   rR   rS   rT   driversupports_statement_cacheis_asyncsupports_server_side_cursorsr   execution_ctx_clsclassmethodr   r   r   r   rV   ri   rj   s   @r   r   r   k  sT    F#H#' 8
 

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