
    h9                    $   d Z ddlm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 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 ddlm Z  ddlm!Z! er<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, dd#l-m.Z.  G d$ d%      Z/ G d& d'e/      Z0 G d( d)e      Z1 G d* d+e1      Z2 G d, d-e      Z3 G d. d/e      Z4 G d0 d1e      Z5e5Z6y)2a	  

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

    )annotationsN)deque)partial)
ModuleType)Any)cast)Deque)Iterator)NoReturn)Optional)Sequence)TYPE_CHECKING)Union   )SQLiteExecutionContext)SQLiteDialect_pysqlite   )pool)util)AsyncAdapt_dbapi_module)AdaptedConnection)await_fallback)
await_only)AsyncIODBAPIConnection)AsyncIODBAPICursor)_DBAPICursorDescription)_DBAPIMultiExecuteParams)_DBAPISingleExecuteParams)DBAPIConnection)DBAPICursor)DBAPIModule)URL)PoolProxiedConnectionc                  x    e Zd ZdZdZddZddZ	 d	 	 	 	 	 ddZ	 	 	 	 	 	 ddZddZ	dd	Z
dd
ZdddZddZy)AsyncAdapt_aiosqlite_cursor)_adapt_connection_connectiondescriptionawait__rows	arraysizerowcount	lastrowidFc                    || _         |j                  | _        |j                  | _        d| _        d| _        d | _        t               | _        y )Nr   )r&   r'   r)   r+   r,   r(   r   r*   )selfadapt_connections     b/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__z$AsyncAdapt_aiosqlite_cursor.__init__   sD    !1+77&-->B!&
    c                8    | j                   j                          y N)r*   clearr0   s    r2   closez!AsyncAdapt_aiosqlite_cursor.close   s    

r4   Nc                   	 | j                  | j                  j                               }|!| j                  |j                  |             n!| j                  |j                  ||             |j                  rY|j                  | _        dx| _        | _        | j                  sWt        | 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_sider   fetchallr*   r9   _cursor	Exceptionr&   _handle_exception)r0   	operation
parametersr?   errors        r2   r<   z#AsyncAdapt_aiosqlite_cursor.execute   s   	<*.++d6F6F6M6M6O*PG!GOOI67GOOIzBC""#*#6#6 133''!&t{{73C3C3E'F!GDJ#' !(!2!2 ' 0 0##GMMO,& 	<""44U;;	<s   D&D1 )D1 1	E: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r6   )r)   r'   r;   executemanyr(   r-   r,   r9   r@   r&   rA   )r0   rB   seq_of_parametersr?   rD   s        r2   rF   z'AsyncAdapt_aiosqlite_cursor.executemany   s    
	<*.++d6F6F6M6M6O*PGKK++I7HIJ#D$..DN#,,DMKK( 	<""44U;;	<s   BB 	CB>>Cc                     y r6    )r0   
inputsizess     r2   setinputsizesz)AsyncAdapt_aiosqlite_cursor.setinputsizes   s    r4   c              #  x   K   | j                   r*| j                   j                          | j                   r)y y wr6   r*   popleftr8   s    r2   __iter__z$AsyncAdapt_aiosqlite_cursor.__iter__   s)     jj**$$&& jjs   5::c                P    | j                   r| j                   j                         S y r6   rM   r8   s    r2   fetchonez$AsyncAdapt_aiosqlite_cursor.fetchone   s    ::::%%''r4   c                    || j                   }| j                  }t        t        |t	        |                  D cg c]  }|j                          c}S c c}w r6   )r+   r*   rangeminlenrN   )r0   sizerr_s       r2   	fetchmanyz%AsyncAdapt_aiosqlite_cursor.fetchmany   sC    <>>DZZ&+Cc"g,>&?@

@@@s   Ac                d    t        | j                        }| j                  j                          |S r6   )listr*   r7   )r0   retvals     r2   r>   z$AsyncAdapt_aiosqlite_cursor.fetchall   s%    djj!

r4   )r1   AsyncAdapt_aiosqlite_connectionreturnNoner6   )rB   r   rC   z#Optional[_DBAPISingleExecuteParams]r_   r   )rB   r   rG   r   r_   r   )rJ   r   r_   r`   )r_   zIterator[Any]r_   zOptional[Any]rV   zOptional[int]r_   Sequence[Any]r_   rc   )__name__
__module____qualname__	__slots__r=   r3   r9   r<   rF   rK   rO   rQ   rY   r>   rI   r4   r2   r%   r%   v   s|    	I K) ;?<< 8< 
	<@<< 4< 
	<'Ar4   r%   c                  J     e Zd ZdZdZd fdZd	dZd
dZdddZddZ	 xZ
S )AsyncAdapt_aiosqlite_ss_cursorr?   Tc                2    t        |   |i | d | _        y r6   )superr3   r?   )r0   argkw	__class__s      r2   r3   z'AsyncAdapt_aiosqlite_ss_cursor.__init__   s    #$$59r4   c                ~    | j                   1| j                  | j                   j                                d | _         y y r6   )r?   r)   r9   r8   s    r2   r9   z$AsyncAdapt_aiosqlite_ss_cursor.close   s1    <<#KK**,-DL $r4   c                p    | j                   J | j                  | j                   j                               S r6   )r?   r)   rQ   r8   s    r2   rQ   z'AsyncAdapt_aiosqlite_ss_cursor.fetchone   .    ||'''{{4<<00233r4   c                    | j                   J || j                  }| j                  | j                   j                  |            S )N)rV   )r?   r+   r)   rY   )r0   rV   s     r2   rY   z(AsyncAdapt_aiosqlite_ss_cursor.fetchmany   sA    ||'''<>>D{{4<<11t1<==r4   c                p    | j                   J | j                  | j                   j                               S r6   )r?   r)   r>   r8   s    r2   r>   z'AsyncAdapt_aiosqlite_ss_cursor.fetchall   rr   r4   )rm   r   rn   r   r_   r`   r^   ra   r6   rb   rd   )re   rf   rg   rh   r=   r3   r9   rQ   rY   r>   __classcell__ro   s   @r2   rj   rj      s(     IK: 
4>4r4   rj   c                      e Zd Z ee      ZdZddZedd       Z	e	j                  dd       Z	ddZdddZddZddZdd	Zdd
ZddZy)r]   )dbapic                     || _         || _        y r6   )rx   r'   )r0   rx   
connections      r2   r3   z(AsyncAdapt_aiosqlite_connection.__init__   s    
%r4   c                J    t        t        | j                  j                        S r6   )r   strr'   isolation_levelr8   s    r2   r}   z/AsyncAdapt_aiosqlite_connection.isolation_level  s    C))99::r4   c                ^   	 	 	 	 	 	 dd}t        || j                  j                  |      }t        j                         j                         }| j                  j                  j                  ||f       	 | j                  |       y # t        $ r}| j                  |       Y d }~y d }~ww xY w)Nc                    || _         y r6   )r}   )rz   values     r2   set_isoz@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso  s     */J&r4   )rz   r]   r   Optional[str]r_   r`   )r   r'   _connasyncioget_event_loopcreate_future_tx
put_nowaitr)   r@   rA   )r0   r   r   functionfuturerD   s         r2   r}   z/AsyncAdapt_aiosqlite_connection.isolation_level  s    	/7	/@M	/	/
 7D$4$4$:$:EB'')779''(:;	*KK 	*""5))	*s   6B 	B,B''B,c                    	 | j                   | j                  j                  |i |       y # t        $ r}| j	                  |       Y d }~y d }~ww xY wr6   )r)   r'   create_functionr@   rA   )r0   argsrn   rD   s       r2   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 r6   )rj   r%   )r0   r=   s     r2   r;   z&AsyncAdapt_aiosqlite_connection.cursor#  s    1$77.t44r4   c                X    | j                   | j                  j                  |i |      S r6   )r)   r'   r<   )r0   r   rn   s      r2   r<   z'AsyncAdapt_aiosqlite_connection.execute)  s*    {{34++33T@R@AAr4   c                    	 | j                  | j                  j                                y # t        $ r}| j	                  |       Y d }~y d }~ww xY wr6   )r)   r'   rollbackr@   rA   r0   rD   s     r2   r   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r6   )r)   r'   commitr@   rA   r   s     r2   r   z&AsyncAdapt_aiosqlite_connection.commit2  sC    	*KK((//12 	*""5))	*r   c                    	 | j                  | j                  j                                y # t        $ r Y y t        $ r}| j                  |       Y d }~y d }~ww xY wr6   )r)   r'   r9   
ValueErrorr@   rA   r   s     r2   r9   z%AsyncAdapt_aiosqlite_connection.close8  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)
isinstancer   r   rx   sqliteOperationalErrorr   s     r2   rA   z1AsyncAdapt_aiosqlite_connection._handle_exceptionH  sG    uj)

1!77**##44& Kr4   N)rx   r   rz   r   r_   r`   )r_   r   )r   r   r_   r`   )r   r   rn   r   r_   r`   )F)r=   boolr_   r%   )r   r   rn   r   r_   r   r^   )rD   r@   r_   r   )re   rf   rg   staticmethodr   r)   rh   r3   propertyr}   setterr   r;   r<   r   r   r9   rA   rI   r4   r2   r]   r]      sh    *%FI& ; ; * ***5B*** 	r4   r]   c                       e Zd ZdZ ee      Zy)'AsyncAdaptFallback_aiosqlite_connectionrI   N)re   rf   rg   rh   r   r   r)   rI   r4   r2   r   r   T  s    I.)Fr4   r   c                  $    e Zd ZddZddZddZy)AsyncAdapt_aiosqlite_dbapic                N    || _         || _        d| _        | j                          y )Nqmark)	aiosqliter   
paramstyle_init_dbapi_attributes)r0   r   r   s      r2   r3   z#AsyncAdapt_aiosqlite_dbapi.__init__[  s#    "!##%r4   c           	         dD ]#  }t        | |t        | j                  |             % dD ]#  }t        | |t        | j                  |             % dD ]#  }t        | |t        | j                  |             % y )N)DatabaseErrorErrorIntegrityErrorNotSupportedErrorr   ProgrammingErrorsqlite_versionsqlite_version_info)PARSE_COLNAMESPARSE_DECLTYPES)Binary)setattrgetattrr   r   )r0   names     r2   r   z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesa  s|    	
 
	?D D$ =>
	? : 	<DD$T :;	<   	<DD$T :;	<r4   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)
popr   connectdaemonr   asboolr   r   r]   r   )r0   rm   rn   r   
creator_fnrz   s         r2   r   z"AsyncAdapt_aiosqlite_dbapi.connectt  s     0%8VV.5
#S/B/J///;;J $J;;~&:z* 
 3:& r4   N)r   r   r   r   r^   )rm   r   rn   r   r_   r]   )re   rf   rg   r3   r   r   rI   r4   r2   r   r   Z  s    &<&r4   r   c                      e Zd ZddZy) SQLiteExecutionContext_aiosqlitec                :    | j                   j                  d      S )NT)r=   )_dbapi_connectionr;   r8   s    r2   create_server_side_cursorz:SQLiteExecutionContext_aiosqlite.create_server_side_cursor  s    %%,,,>>r4   N)r_   r    )re   rf   rg   r   rI   r4   r2   r   r     s    ?r4   r   c                  x     e Zd ZdZdZdZdZeZe	dd       Z
e	dd       Z	 	 	 	 	 	 	 	 d	 fdZ	 	 	 	 d
dZ xZS )SQLiteDialect_aiosqliter   Tc                >    t        t        d      t        d            S )Nr   sqlite3)r   
__import__)clss    r2   import_dbapiz$SQLiteDialect_aiosqlite.import_dbapi  s    ){#Z	%:
 	
r4   c                d    | j                  |      rt        j                  S t        j                  S r6   )_is_url_file_dbr   AsyncAdaptedQueuePool
StaticPool)r   urls     r2   get_pool_classz&SQLiteDialect_aiosqlite.get_pool_class  s&    s#---??"r4   c                    t        d| j                        | _        t        || j                  j                        rdt	        |      v ryt
        |   |||      S )Nr!   r   T)r   rx   r   r   r|   rl   is_disconnect)r0   erz   r;   ro   s       r2   r   z%SQLiteDialect_aiosqlite.is_disconnect  sQ     -4
tzz**
$A.w$Q
F;;r4   c                    |j                   S r6   )r'   )r0   rz   s     r2   get_driver_connectionz-SQLiteDialect_aiosqlite.get_driver_connection  s     %%%r4   )r_   r   )r   r"   r_   ztype[pool.Pool])r   zDBAPIModule.Errorrz   z7Optional[Union[PoolProxiedConnection, DBAPIConnection]]r;   zOptional[DBAPICursor]r_   r   )rz   r   r_   r   )re   rf   rg   driversupports_statement_cacheis_asyncsupports_server_side_cursorsr   execution_ctx_clsclassmethodr   r   r   r   ru   rv   s   @r2   r   r     s    F#H#' 8
 

 # #<< L< &	<
 
<&)&	&r4   r   )7__doc__
__future__r   r   collectionsr   	functoolsr   typesr   typingr   r   r	   r
   r   r   r   r   r   baser   pysqliter    r   r   connectors.asyncior   enginer   util.concurrencyr   r   r   r   engine.interfacesr   r   r   r   r    r!   
engine.urlr"   	pool.baser#   r%   rj   r]   r   r   r   r   dialectrI   r4   r2   <module>r      s   FN #               ( ,   9 ' . *<8<=>400!2c cL4%@ 4>V&7 Vr*.M *.!8 .b?'= ?
(&4 (&V "r4   