
    e*                     6   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dS )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`.


    N)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 | _        g | _        d S )Nr   )r   r   r   r   r   r   r   )selfadapt_connections     Dlib/python3.11/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__z$AsyncAdapt_aiosqlite_cursor.__init__L   s@    !1+7&-


    c                     g | j         d d <   d S Nr   r   s    r   closez!AsyncAdapt_aiosqlite_cursor.closeU   s    
111r   Nc                    	 |                      | j                                                  }|)|                      |                    |                     n)|                      |                    ||                     |j        rN|j        | _        dx| _        | _        | j        s,|                      |                                          | _	        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_sidefetchallr   r"   _cursor	Exceptionr   _handle_exception)r   	operation
parametersr(   errors        r   r%   z#AsyncAdapt_aiosqlite_cursor.executeX   sX   	<kk$"2"9"9";";<<G!GOOI667777GOOIzBBCCC" 	1#*#6 133' A!%W-=-=-?-?!@!@DJ#' !(!2 ' 0# 'GMMOO,,,,,& 	< 	< 	<"44U;;;;;;;;;	<s   D"D/ &D/ /
E9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   r/   z'AsyncAdapt_aiosqlite_cursor.executemanys   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              #   b   K   | j         r%| j                             d          V  | j         #d S d S Nr   r   popr!   s    r   __iter__z$AsyncAdapt_aiosqlite_cursor.__iter__   sJ      j 	$*..##### j 	$ 	$ 	$ 	$ 	$r   c                 H    | j         r| j                             d          S d S r6   r7   r!   s    r   fetchonez$AsyncAdapt_aiosqlite_cursor.fetchone   s%    : 	:>>!$$$4r   c                 h    || j         }| j        d|         }| j        |d          | j        d d <   |S r6   )r   r   )r   sizeretvals      r   	fetchmanyz%AsyncAdapt_aiosqlite_cursor.fetchmany   s=    <>DAdF#
455)
111r   c                 <    | j         d d          }g | j         d d <   |S r   r    )r   r>   s     r   r'   z$AsyncAdapt_aiosqlite_cursor.fetchall   s%    AAA
111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                 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)r=   )r   r   r(   r?   )r   r=   s     r   r?   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   rN   r   r   )rA   rB   rC   rD   r&   r   r"   r;   r?   r'   __classcell__rK   s   @r   rF   rF      s        IK         
4 4 4> > > >
4 4 4 4 4 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dS )AsyncAdapt_aiosqlite_connection)dbapic                 "    || _         || _        d S 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                 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   )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$: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   argsrJ   r-   s       r   rg   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   )rF   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   rh   rJ   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   rl   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*   rm   s     r   rp   z&AsyncAdapt_aiosqlite_connection.commit   ss    	*KK(//1122222 	* 	* 	*""5)))))))))	*rn   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*   rm   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)
isinstancerr   rh   rU   sqliteOperationalErrorrm   s     r   r*   z1AsyncAdapt_aiosqlite_connection._handle_exception   sP    uj))	
1!7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      s        \*%%FI& & & 0 0 X0 * * *&* * *5 5 5 5B B B* * ** * ** * * 	 	 	 	 	r   rT   c                   (    e Zd ZdZ ee          ZdS )'AsyncAdaptFallback_aiosqlite_connectionr2   N)rA   rB   rC   rD   rx   r   r   r2   r   r   r|   r|   
  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)	aiosqliterv   
paramstyle_init_dbapi_attributes)r   r   rv   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        |                     'd S )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$ = =>>>>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)
r8   r   connectdaemonr	   asboolr|   r   rT   r   )r   rI   rJ   r   
creator_fnrW   s         r   r   z"AsyncAdapt_aiosqlite_dbapi.connect*  s     0%88VV.55
 	%#S/B//JJ//;;;J $J;~&& 		:z**  
 3:&&  r   N)rA   rB   rC   r   r   r   r2   r   r   r~   r~     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_cursorB  s    %,,,>>>r   N)rA   rB   rC   r   r2   r   r   r   r   A  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_dbapiP  s)    ){##Z	%:%:
 
 	
r   c                 \    |                      |          rt          j        S t          j        S r   )_is_url_file_dbr   NullPool
StaticPool)r   urls     r   get_pool_classz&SQLiteDialect_aiosqlite.get_pool_classV  s(    s## 	#= ?"r   c                     t          || j        j                  rdt          |          v rdS t	                                          |||          S )Nrt   T)ru   rU   rw   strrH   is_disconnect)r   erW   r$   rK   s       r   r   z%SQLiteDialect_aiosqlite.is_disconnect]  sQ    tz*
 
 	$A..4ww$$Q
F;;;r   c                     |j         S r   )r   )r   rW   s     r   get_driver_connectionz-SQLiteDialect_aiosqlite.get_driver_connectione  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   F  s        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  & &P        ( ( ( ( ( ( , , , , , ,             ' ' ' ' ' ' . . . . . . * * * * * *X X X X X X X Xv4 4 4 4 4%@ 4 4 44T T T T T&7 T T Tn* * * * *.M * * *. . . . . . . .b? ? ? ? ?'= ? ? ?
 &  &  &  &  &4  &  &  &F "r   