
    ;"ri>                    >   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! 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%l0m1Z1  G d& d'      Z2 G d( d)e2      Z3 G d* d+ee!      Z4 G d, d-e4      Z5 G d. d/e      Z6 G d0 d1e      Z7 G d2 d3e      Z8e8Z9y)4a	  

.. 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)Thread)
ModuleType)Any)cast)Deque)Iterator)NoReturn)Optional)Sequence)TYPE_CHECKING)Union   )SQLiteExecutionContext)SQLiteDialect_pysqlite   )pool)util)AsyncAdapt_dbapi_module)AsyncAdapt_terminate)AdaptedConnection)await_fallback)
await_only)AsyncIODBAPIConnection)AsyncIODBAPICursor)_DBAPICursorDescription)_DBAPIMultiExecuteParams)_DBAPISingleExecuteParams)DBAPIConnection)DBAPICursor)DBAPIModule)URL)PoolProxiedConnectionc                      e Zd ZdZdZd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     l/var/www/fortnox.pascalinesoft.com/venv/lib/python3.12/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__z$AsyncAdapt_aiosqlite_cursor.__init__   sD    !1+77&-->B!&
    c                   K   y wN r2   s    r4   _async_soft_closez-AsyncAdapt_aiosqlite_cursor._async_soft_close   s	     s   c                8    | j                   j                          y r8   )r,   clearr:   s    r4   closez!AsyncAdapt_aiosqlite_cursor.close   s    

r6   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)Nr1   )r+   r)   cursorexecuter*   r/   r.   server_sider   fetchallr,   r>   _cursor	Exceptionr(   _handle_exception)r2   	operation
parametersrD   errors        r4   rA   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r8   )r+   r)   r@   executemanyr*   r/   r.   r>   rE   r(   rF   )r2   rG   seq_of_parametersrD   rI   s        r4   rK   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 r8   r9   )r2   
inputsizess     r4   setinputsizesz)AsyncAdapt_aiosqlite_cursor.setinputsizes   s    r6   c              #  x   K   | j                   r*| j                   j                          | j                   r)y y wr8   r,   popleftr:   s    r4   __iter__z$AsyncAdapt_aiosqlite_cursor.__iter__   s)     jj**$$&& jjs   5::c                P    | j                   r| j                   j                         S y r8   rQ   r:   s    r4   fetchonez$AsyncAdapt_aiosqlite_cursor.fetchone   s    ::::%%''r6   c                    || j                   }| j                  }t        t        |t	        |                  D cg c]  }|j                          c}S c c}w r8   )r-   r,   rangeminlenrR   )r2   sizerr_s       r4   	fetchmanyz%AsyncAdapt_aiosqlite_cursor.fetchmany   sC    <>>DZZ&+Cc"g,>&?@

@@@s   Ac                d    t        | j                        }| j                  j                          |S r8   )listr,   r=   )r2   retvals     r4   rC   z$AsyncAdapt_aiosqlite_cursor.fetchall   s%    djj!

r6   )r3   AsyncAdapt_aiosqlite_connectionreturnNoner8   )rG   r   rH   z#Optional[_DBAPISingleExecuteParams]rc   r   )rG   r   rL   r   rc   r   )rN   r   rc   rd   )rc   zIterator[Any]rc   zOptional[Any]rZ   zOptional[int]rc   Sequence[Any]rc   rg   )__name__
__module____qualname__	__slots__rB   r5   r;   r>   rA   rK   rO   rS   rU   r]   rC   r9   r6   r4   r'   r'   x   s    	I K) ;?<< 8< 
	<@<< 4< 
	<'Ar6   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_cursorrD   Tc                2    t        |   |i | d | _        y r8   )superr5   rD   )r2   argkw	__class__s      r4   r5   z'AsyncAdapt_aiosqlite_ss_cursor.__init__   s    #$$59r6   c                ~    | j                   1| j                  | j                   j                                d | _         y y r8   )rD   r+   r>   r:   s    r4   r>   z$AsyncAdapt_aiosqlite_ss_cursor.close   s1    <<#KK**,-DL $r6   c                p    | j                   J | j                  | j                   j                               S r8   )rD   r+   rU   r:   s    r4   rU   z'AsyncAdapt_aiosqlite_ss_cursor.fetchone   .    ||'''{{4<<00233r6   c                    | j                   J || j                  }| j                  | j                   j                  |            S )N)rZ   )rD   r-   r+   r]   )r2   rZ   s     r4   r]   z(AsyncAdapt_aiosqlite_ss_cursor.fetchmany   sA    ||'''<>>D{{4<<11t1<==r6   c                p    | j                   J | j                  | j                   j                               S r8   )rD   r+   rC   r:   s    r4   rC   z'AsyncAdapt_aiosqlite_ss_cursor.fetchall   rv   r6   )rq   r   rr   r   rc   rd   rb   re   r8   rf   rh   )ri   rj   rk   rl   rB   r5   r>   rU   r]   rC   __classcell__rs   s   @r4   rn   rn      s(     IK: 
4>4r6   rn   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ddZddZy)ra   )dbapic                     || _         || _        y r8   )r|   r)   )r2   r|   
connections      r4   r5   z(AsyncAdapt_aiosqlite_connection.__init__  s    
%r6   c                J    t        t        | j                  j                        S r8   )r	   strr)   isolation_levelr:   s    r4   r   z/AsyncAdapt_aiosqlite_connection.isolation_level  s    C))99::r6   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 r8   )r   )r~   values     r4   set_isoz@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso  s     */J&r6   )r~   ra   r   Optional[str]rc   rd   )r   r)   _connasyncioget_event_loopcreate_future_tx
put_nowaitr+   rE   rF   )r2   r   r   functionfuturerI   s         r4   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r8   )r+   r)   create_functionrE   rF   )r2   argsrr   rI   s       r4   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 r8   )rn   r'   )r2   rB   s     r4   r@   z&AsyncAdapt_aiosqlite_connection.cursor(  s    1$77.t44r6   c                X    | j                   | j                  j                  |i |      S r8   )r+   r)   rA   )r2   r   rr   s      r4   rA   z'AsyncAdapt_aiosqlite_connection.execute.  s*    {{34++33T@R@AAr6   c                    	 | j                  | j                  j                                y # t        $ r}| j	                  |       Y d }~y d }~ww xY wr8   )r+   r)   rollbackrE   rF   r2   rI   s     r4   r   z(AsyncAdapt_aiosqlite_connection.rollback1  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r8   )r+   r)   commitrE   rF   r   s     r4   r   z&AsyncAdapt_aiosqlite_connection.commit7  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r8   )r+   r)   r>   
ValueErrorrE   rF   r   s     r4   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)
isinstancer   r   r|   sqliteOperationalErrorr   s     r4   rF   z1AsyncAdapt_aiosqlite_connection._handle_exceptionM  sG    uj)

1!77**##44& Kr6   c                T   K   | j                   j                          d{    y7 w)z"Try to close connection gracefullyN)r)   r>   r:   s    r4   _terminate_graceful_closez9AsyncAdapt_aiosqlite_connection._terminate_graceful_closeX  s     $$&&&s   (&(c                z    	 | j                   j                  } |        y# t        $ r}t        d      |d}~ww xY w)zTerminate the connectionz:terminate_force_close() not implemented by this DBAPI shimN)r)   stopAttributeErrorNotImplementedError)r2   methaes      r4   _terminate_force_closez6AsyncAdapt_aiosqlite_connection._terminate_force_close\  sD    
	##((D F  	%L	s     	:5:N)r|   r   r~   r   rc   rd   )rc   r   )r   r   rc   rd   )r   r   rr   r   rc   rd   )F)rB   boolrc   r'   )r   r   rr   r   rc   r   rb   )rI   rE   rc   r   )ri   rj   rk   staticmethodr   r+   rl   r5   propertyr   setterr   r@   rA   r   r   r>   rF   r   r   r9   r6   r4   ra   ra      sr    *%FI& ; ; * ***5B*** 	'r6   ra   c                       e Zd ZdZ ee      Zy)'AsyncAdaptFallback_aiosqlite_connectionr9   N)ri   rj   rk   rl   r   r   r+   r9   r6   r4   r   r   k  s    I.)Fr6   r   c                  $    e Zd ZddZddZddZy)AsyncAdapt_aiosqlite_dbapic                    || _         || _        d| _        t        |j                  d      | _        | j                          y )Nqmarkr   )	aiosqliter   
paramstylehasattr
Connectionhas_stop_init_dbapi_attributes)r2   r   r   s      r4   r5   z#AsyncAdapt_aiosqlite_dbapi.__init__r  s7    "!	 4 4f=##%r6   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   )r2   names     r4   r   z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesy  s|    	
 
	?D D$ =>
	? : 	<DD$T :;	<   	<DD$T :;	<r6   c                h   |j                  dd      }|j                  dd       }|r	 ||i |}nE | j                  j                  |i |}t        |t              rd|_        nd|j                  _        t        j                  |      rt        | t        |            S t        | t        |            S )Nasync_fallbackFasync_creator_fnT)popr   connectr   r   daemon_threadr   asboolr   r   ra   r   )r2   rq   rr   r   
creator_fnr~   s         r4   r   z"AsyncAdapt_aiosqlite_dbapi.connect  s     0%8VV.5
#S/B/J///;;J *f-$(
! -1
"");;~&:z* 
 3:& r6   N)r   r   r   r   rb   )rq   r   rr   r   rc   ra   )ri   rj   rk   r5   r   r   r9   r6   r4   r   r   q  s    &<&r6   r   c                      e Zd ZddZy) SQLiteExecutionContext_aiosqlitec                :    | j                   j                  d      S )NT)rB   )_dbapi_connectionr@   r:   s    r4   create_server_side_cursorz:SQLiteExecutionContext_aiosqlite.create_server_side_cursor  s    %%,,,>>r6   N)rc   r"   )ri   rj   rk   r   r9   r6   r4   r   r     s    ?r6   r   c                       e Zd ZdZdZdZdZdZeZ	d	 fdZ
ed
d       Zedd       Z	 	 	 	 	 	 	 	 d fdZ	 	 	 	 ddZddZ xZS )SQLiteDialect_aiosqliter   Tc                z    t        |   di | | j                  r| j                  j                  sd| _        y y y )NFr9   )rp   r5   r|   r   has_terminate)r2   kwargsrs   s     r4   r5   z SQLiteDialect_aiosqlite.__init__  s5    "6"::djj11!&D 2:r6   c                >    t        t        d      t        d            S )Nr   sqlite3)r   
__import__)clss    r4   import_dbapiz$SQLiteDialect_aiosqlite.import_dbapi  s    ){#Z	%:
 	
r6   c                d    | j                  |      rt        j                  S t        j                  S r8   )_is_url_file_dbr   AsyncAdaptedQueuePool
StaticPool)r   urls     r4   get_pool_classz&SQLiteDialect_aiosqlite.get_pool_class  s&    s#---??"r6   c                    t        d| j                        | _        t        || j                  j                        rdt	        |      v ryt
        |   |||      S )Nr#   r   T)r	   r|   r   r   r   rp   is_disconnect)r2   er~   r@   rs   s       r4   r   z%SQLiteDialect_aiosqlite.is_disconnect  sQ     -4
tzz**
$A.w$Q
F;;r6   c                    |j                   S r8   )r)   )r2   r~   s     r4   get_driver_connectionz-SQLiteDialect_aiosqlite.get_driver_connection  s     %%%r6   c                $    |j                          y r8   )	terminate)r2   dbapi_connections     r4   do_terminatez$SQLiteDialect_aiosqlite.do_terminate  s    ""$r6   )r   r   )rc   r   )r   r$   rc   ztype[pool.Pool])r   zDBAPIModule.Errorr~   z7Optional[Union[PoolProxiedConnection, DBAPIConnection]]r@   zOptional[DBAPICursor]rc   r   )r~   r!   rc   r   )r   r!   rc   rd   )ri   rj   rk   driversupports_statement_cacheis_asyncr   supports_server_side_cursorsr   execution_ctx_clsr5   classmethodr   r   r   r   r   ry   rz   s   @r4   r   r     s    F#HM#' 8'
 
 

 # #<< L< &	<
 
<&)&	&
%r6   r   ):__doc__
__future__r   r   collectionsr   	functoolsr   	threadingr   typesr   typingr   r	   r
   r   r   r   r   r   r   baser   pysqliter    r   r   connectors.asyncior   r   enginer   util.concurrencyr   r   r   r   engine.interfacesr   r   r    r!   r"   r#   
engine.urlr$   	pool.baser%   r'   rn   ra   r   r   r   r   dialectr9   r6   r4   <module>r     s   FN #                ( ,   9 6 ' . *<8<=>400!2f fR4%@ 4>h&:<M hV*.M *5!8 5p?'= ?
1%4 1%h "r6   