
    +h'                         d 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`.


   )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     W/var/www/html/venv/lib/python3.12/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__z$AsyncAdapt_aiosqlite_cursor.__init__G   sB    !1+77&--
    c                 "    g | j                   d d  y Nr   r   s    r   closez!AsyncAdapt_aiosqlite_cursor.closeP   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.executeS   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.executemanyn   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.setinputsizesy   s    r   c              #   z   K   | j                   r+| j                   j                  d       | j                   r*y y wN    r   popr   s    r   __iter__z$AsyncAdapt_aiosqlite_cursor.__iter__|   s)     jj**..## jjs   6;;c                 R    | j                   r| j                   j                  d      S y r4   r6   r   s    r   fetchonez$AsyncAdapt_aiosqlite_cursor.fetchone   s    ::::>>!$$r   c                 x    || j                   }| j                  d| }| j                  |d  | j                  d d  |S r4   )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-   r2   r8   r:   r>   r%   r0   r   r   r   r   9   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   rM   r   r   )r@   rA   rB   rC   r$   r   r    r:   r>   r%   __classcell__rJ   s   @r   rE   rE      s&    IK 
4>
4r   rE   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dbapir   c                      || _         || _        y r   rT   )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                 v    	 || j                   _        y # t        $ r}| j                  |       Y d }~y d }~ww xY wr   )r   rY   r'   r(   )r   valuer+   s      r   rY   z/AsyncAdapt_aiosqlite_connection.isolation_level   s6    	*/4D, 	*""5))	*s    	838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   argsrI   r+   s       r   r]   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   )rE   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   r^   rI   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   rb   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(   rc   s     r   rf   z&AsyncAdapt_aiosqlite_connection.commit   sC    	*KK((//12 	*""5))	*rd   c                     	 | j                  | j                  j                                y # t        $ r}| j	                  |       Y d }~y d }~ww xY wr   )r   r   r    r'   r(   rc   s     r   r    z%AsyncAdapt_aiosqlite_connection.close   sC    	*KK((..01 	*""5))	*rd   c                     t        |t              rM|j                  d   dk(  r;t        j                  | j
                  j                  j                  d      |       y |)Nr5   no active connection)from_)
isinstance
ValueErrorr^   r   raise_rU   sqliteOperationalErrorrc   s     r   r(   z1AsyncAdapt_aiosqlite_connection._handle_exception   sL    uj)

1!77KK

!!223IJ
 Kr   N)F)r@   rA   rB   staticmethodr
   r   rC   r   propertyrY   setterr]   r"   r#   rb   rf   r    r(   r0   r   r   rS   rS      sh    *%F(I& 0 0 * **5B***
r   rS   c                        e Zd ZdZ ee      Zy)'AsyncAdaptFallback_aiosqlite_connectionr0   N)r@   rA   rB   rC   rp   r	   r   r0   r   r   rt   rt      s    I.)Fr   rt   c                       e Zd Zd Zd Zd Zy)AsyncAdapt_aiosqlite_dbapic                 N    || _         || _        d| _        | j                          y )Nqmark)	aiosqlitern   
paramstyle_init_dbapi_attributes)r   ry   rn   s      r   r   z#AsyncAdapt_aiosqlite_dbapi.__init__   s#    "!##%r   c           	          dD ]#  }t        | |t        | j                  |             % dD ]#  }t        | |t        | j                  |             % dD ]#  }t        | |t        | j                  |             % y )N)DatabaseErrorErrorIntegrityErrorNotSupportedErrorro   ProgrammingErrorsqlite_versionsqlite_version_info)PARSE_COLNAMESPARSE_DECLTYPES)Binary)setattrgetattrry   rn   )r   names     r   r{   z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributes   s|    	
 
	?D D$ =>
	? : 	<DD$T :;	<   	<DD$T :;	<r   c                     |j                  dd      }d|d<    | j                  j                  |i |}d|_        t	        j
                  |      rt        | t        |            S t        | t        |            S )Nasync_fallbackFcheck_same_threadT)
r7   ry   connectdaemonr   asboolrt   r	   rS   r
   )r   rH   rI   r   rW   s        r   r   z"AsyncAdapt_aiosqlite_dbapi.connect  s     0%8 #(+T^^++S7B7
 !
;;~&:z* 
 3:& r   N)r@   rA   rB   r   r{   r   r0   r   r   rv   rv      s    &<&r   rv   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_cursor0  s    %%,,,>>r   N)r@   rA   rB   r   r0   r   r   r   r   /  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_aiosqlitery   Tc                 >    t        t        d      t        d            S )Nry   sqlite3)rv   
__import__)clss    r   rU   zSQLiteDialect_aiosqlite.dbapi>  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_classD  s$    s#== ??"r   c                     t        || j                  j                        rdt        |      v ryt        |   |||      S )Nri   T)rk   rU   ro   strrG   is_disconnect)r   erW   r"   rJ   s       r   r   z%SQLiteDialect_aiosqlite.is_disconnectK  s=    tzz**
$A.w$Q
F;;r   c                     |j                   S r   )r   )r   rW   s     r   get_driver_connectionz-SQLiteDialect_aiosqlite.get_driver_connectionS  s    %%%r   )r@   rA   rB   driversupports_statement_cacheis_asyncsupports_server_side_cursorsr   execution_ctx_clsclassmethodrU   r   r   r   rP   rQ   s   @r   r   r   4  sO    F#H#' 8
 

 # #<&r   r   N)__doc__baser   pysqliter    r   r   enginer   util.concurrencyr	   r
   r   rE   rS   rt   rv   r   r   dialectr0   r   r   <module>r      s   &P ) ,   ' . *X Xv4%@ 44>&7 >B*.M *7 7t?'= ?
 &4  &F "r   