
    )f0                     
   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  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y)aZ
  

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

    N)partial   )SQLiteExecutionContext)SQLiteDialect_pysqlite   )pool)util)AdaptedConnection)await_fallback)
await_onlyc                   N    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y)AsyncAdapt_aiosqlite_cursor)_adapt_connection_connectiondescriptionawait__rows	arraysizerowcount	lastrowidFc                     || _         |j                  | _        |j                  | _        d| _        d| _        d | _        g | _        y )Nr   )r   r   r   r   r   r   r   )selfadapt_connections     a/var/www/html/flask-app/venv/lib/python3.12/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__z$AsyncAdapt_aiosqlite_cursor.__init__m   sB    !1+77&--
    c                 "    g | j                   d d  y Nr   r   s    r   closez!AsyncAdapt_aiosqlite_cursor.closev   s    

1r   Nc                    	 | j                  | j                  j                               }|!| j                  |j                  |             n!| j                  |j                  ||             |j                  rP|j                  | _        dx| _        | _        | j                  sN| j                  |j                               | _	        n)d | _        |j
                  | _        |j                  | _        | j                  s | j                  |j                                y || _        y # t        $ r%}| j                  j                  |       Y d }~y d }~ww xY w)Nr   )r   r   cursorexecuter   r   r   server_sidefetchallr   r"   _cursor	Exceptionr   _handle_exception)r   	operation
parametersr(   errors        r   r%   z#AsyncAdapt_aiosqlite_cursor.executey   s   	<kk$"2"2"9"9";<G!GOOI67GOOIzBC""#*#6#6 133''!%W-=-=-?!@DJ#' !(!2!2 ' 0 0##GMMO,& 	<""44U;;	<s   DD(  D( (	E1EEc                    	 | j                  | j                  j                               }| j                  |j                  ||             d | _        |j
                  | _        |j                  | _        | j                  |j                                y # t        $ r%}| j                  j                  |       Y d }~y 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   r/   z'AsyncAdapt_aiosqlite_cursor.executemany   s    	<kk$"2"2"9"9";<GKK++I7HIJ#D$..DN#,,DMKK( 	<""44U;;	<s   BB 	CB>>Cc                      y r    )r   
inputsizess     r   setinputsizesz)AsyncAdapt_aiosqlite_cursor.setinputsizes   s    r   c              #   z   K   | j                   r+| j                   j                  d       | j                   r*y y wNr   r   popr!   s    r   __iter__z$AsyncAdapt_aiosqlite_cursor.__iter__   s)     jj**..## jjs   6;;c                 R    | j                   r| j                   j                  d      S y r6   r7   r!   s    r   fetchonez$AsyncAdapt_aiosqlite_cursor.fetchone   s    ::::>>!$$r   c                 x    || j                   }| j                  d| }| j                  |d  | j                  d d  |S r6   )r   r   )r   sizeretvals      r   	fetchmanyz%AsyncAdapt_aiosqlite_cursor.fetchmany   s=    <>>DAd#

45)

1r   c                 B    | j                   d d  }g | j                   d d  |S r   r    )r   r>   s     r   r'   z$AsyncAdapt_aiosqlite_cursor.fetchall   s!    A

1r   r   )__name__
__module____qualname__	__slots__r&   r   r"   r%   r/   r4   r9   r;   r?   r'   r2   r   r   r   r   \   s=    	I K<6	<$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                 2    t        |   |i | d | _        y r   )superr   r(   )r   argkw	__class__s      r   r   z'AsyncAdapt_aiosqlite_ss_cursor.__init__   s    #$$r   c                 ~    | j                   1| j                  | j                   j                                d | _         y y r   )r(   r   r"   r!   s    r   r"   z$AsyncAdapt_aiosqlite_ss_cursor.close   s1    <<#KK**,-DL $r   c                 T    | j                  | j                  j                               S r   )r   r(   r;   r!   s    r   r;   z'AsyncAdapt_aiosqlite_ss_cursor.fetchone       {{4<<00233r   c                 t    || j                   }| j                  | j                  j                  |            S )N)r=   )r   r   r(   r?   )r   r=   s     r   r?   z(AsyncAdapt_aiosqlite_ss_cursor.fetchmany   s1    <>>D{{4<<11t1<==r   c                 T    | j                  | j                  j                               S r   )r   r(   r'   r!   s    r   r'   z'AsyncAdapt_aiosqlite_ss_cursor.fetchall   rN   r   r   )rA   rB   rC   rD   r&   r   r"   r;   r?   r'   __classcell__rK   s   @r   rF   rF      s(     IK 
4>
4r   rF   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y)AsyncAdapt_aiosqlite_connection)dbapic                      || _         || _        y r   )rU   r   )r   rU   
connections      r   r   z(AsyncAdapt_aiosqlite_connection.__init__   s    
%r   c                 .    | j                   j                  S r   )r   isolation_levelr!   s    r   rY   z/AsyncAdapt_aiosqlite_connection.isolation_level   s    ///r   c                 N   d }t        || j                  j                  |      }t        j                         j                         }| j                  j                  j                  ||f       	 | j                  |      S # t        $ r}| j                  |       Y d }~y d }~ww xY w)Nc                     || _         y r   )rY   )rW   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   r\   r]   functionfuturer-   s         r   rY   z/AsyncAdapt_aiosqlite_connection.isolation_level   s    	/ 7D$4$4$:$:EB'')779''(:;	*;;v&& 	*""5))	*s   /B   	B$	BB$c                     	 | j                   | j                  j                  |i |       y # t        $ r}| j	                  |       Y d }~y d }~ww xY wr   )r   r   create_functionr)   r*   )r   argsrJ   r-   s       r   rg   z/AsyncAdapt_aiosqlite_connection.create_function   sM    	*KK8((88$E"EF 	*""5))	*s   +. 	AAAc                 2    |rt        |       S t        |       S r   )rF   r   )r   r&   s     r   r$   z&AsyncAdapt_aiosqlite_connection.cursor   s    1$77.t44r   c                 X    | j                   | j                  j                  |i |      S r   )r   r   r%   )r   rh   rJ   s      r   r%   z'AsyncAdapt_aiosqlite_connection.execute  s*    {{34++33T@R@AAr   c                     	 | j                  | j                  j                                y # t        $ r}| j	                  |       Y d }~y d }~ww xY wr   )r   r   rollbackr)   r*   r   r-   s     r   rl   z(AsyncAdapt_aiosqlite_connection.rollback  sC    	*KK((1134 	*""5))	*   ), 	AAAc                     	 | j                  | j                  j                                y # t        $ r}| j	                  |       Y d }~y d }~ww xY wr   )r   r   commitr)   r*   rm   s     r   rp   z&AsyncAdapt_aiosqlite_connection.commit  sC    	*KK((//12 	*""5))	*rn   c                     	 | j                  | j                  j                                y # t        $ r Y y t        $ r}| j                  |       Y d }~y d }~ww xY wr   )r   r   r"   
ValueErrorr)   r*   rm   s     r   r"   z%AsyncAdapt_aiosqlite_connection.close  sP    	*KK((..01 		  	*""5))	*s   ), 	AAAAc                     t        |t              r8|j                  d   dk(  r&| j                  j                  j                  d      ||)Nr   no active connection)
isinstancerr   rh   rU   sqliteOperationalErrorrm   s     r   r*   z1AsyncAdapt_aiosqlite_connection._handle_exception!  sG    uj)

1!77**##44& Kr   N)F)rA   rB   rC   staticmethodr   r   rD   r   propertyrY   setterrg   r$   r%   rl   rp   r"   r*   r2   r   r   rT   rT      sh    *%FI& 0 0 * *&*5B*** 	r   rT   c                        e Zd ZdZ ee      Zy)'AsyncAdaptFallback_aiosqlite_connectionr2   N)rA   rB   rC   rD   rx   r   r   r2   r   r   r|   r|   -  s    I.)Fr   r|   c                       e Zd Zd Zd Zd Zy)AsyncAdapt_aiosqlite_dbapic                 N    || _         || _        d| _        | j                          y )Nqmark)	aiosqliterv   
paramstyle_init_dbapi_attributes)r   r   rv   s      r   r   z#AsyncAdapt_aiosqlite_dbapi.__init__4  s#    "!##%r   c           	          dD ]#  }t        | |t        | j                  |             % dD ]#  }t        | |t        | j                  |             % dD ]#  }t        | |t        | j                  |             % y )N)DatabaseErrorErrorIntegrityErrorNotSupportedErrorrw   ProgrammingErrorsqlite_versionsqlite_version_info)PARSE_COLNAMESPARSE_DECLTYPES)Binary)setattrgetattrr   rv   )r   names     r   r   z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributes:  s|    	
 
	?D D$ =>
	? : 	<DD$T :;	<   	<DD$T :;	<r   c                 $   |j                  dd      }|j                  dd       }|r	 ||i |}n# | j                  j                  |i |}d|_        t	        j
                  |      rt        | t        |            S t        | t        |            S )Nasync_fallbackFasync_creator_fnT)
r8   r   connectdaemonr	   asboolr|   r   rT   r   )r   rI   rJ   r   
creator_fnrW   s         r   r   z"AsyncAdapt_aiosqlite_dbapi.connectM  s     0%8VV.5
#S/B/J///;;J $J;;~&:z* 
 3:& r   N)rA   rB   rC   r   r   r   r2   r   r   r~   r~   3  s    &<&r   r~   c                       e Zd Zd Zy) SQLiteExecutionContext_aiosqlitec                 :    | j                   j                  d      S )NT)r&   )_dbapi_connectionr$   r!   s    r   create_server_side_cursorz:SQLiteExecutionContext_aiosqlite.create_server_side_cursore  s    %%,,,>>r   N)rA   rB   rC   r   r2   r   r   r   r   d  s    ?r   r   c                   X     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                 >    t        t        d      t        d            S )Nr   sqlite3)r~   
__import__)clss    r   import_dbapiz$SQLiteDialect_aiosqlite.import_dbapis  s    ){#Z	%:
 	
r   c                 d    | j                  |      rt        j                  S t        j                  S r   )_is_url_file_dbr   NullPool
StaticPool)r   urls     r   get_pool_classz&SQLiteDialect_aiosqlite.get_pool_classy  s$    s#== ??"r   c                     t        || j                  j                        rdt        |      v ryt        |   |||      S )Nrt   T)ru   rU   rw   strrH   is_disconnect)r   erW   r$   rK   s       r   r   z%SQLiteDialect_aiosqlite.is_disconnect  s=    tzz**
$A.w$Q
F;;r   c                     |j                   S r   )r   )r   rW   s     r   get_driver_connectionz-SQLiteDialect_aiosqlite.get_driver_connection  s    %%%r   )rA   rB   rC   driversupports_statement_cacheis_asyncsupports_server_side_cursorsr   execution_ctx_clsclassmethodr   r   r   r   rQ   rR   s   @r   r   r   i  sO    F#H#' 8
 

 # #<&r   r   )__doc__r_   	functoolsr   baser   pysqliter    r   r	   enginer
   util.concurrencyr   r   r   rF   rT   r|   r~   r   r   dialectr2   r   r   <module>r      s   DL   ( ,   ' . *[ [|4%@ 48T&7 Tn*.M *. .b?'= ?
 &4  &F "r   