
    g<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 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)a]
  

.. 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)deque)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 | _        t               | _        y )Nr   )r   r   r   r   r   r   r   r   )selfadapt_connections     c/var/www/html/AdoubleTech/venv/lib/python3.12/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__z$AsyncAdapt_aiosqlite_cursor.__init__q   sD    !1+77&--W
    c                 8    | j                   j                          y N)r   clearr   s    r   closez!AsyncAdapt_aiosqlite_cursor.closez   s    

r   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   r#   _cursor	Exceptionr   _handle_exception)r   	operation
parametersr)   errors        r   r&   z#AsyncAdapt_aiosqlite_cursor.execute}   s   	<kk$"2"2"9"9";<G!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r    )r   r   r%   executemanyr   r   r   r#   r*   r   r+   )r   r,   seq_of_parametersr)   r.   s        r   r0   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              #   x   K   | j                   r*| j                   j                          | j                   r)y y wr    r   popleftr"   s    r   __iter__z$AsyncAdapt_aiosqlite_cursor.__iter__   s)     jj**$$&& jjs   5::c                 P    | j                   r| j                   j                         S y r    r7   r"   s    r   fetchonez$AsyncAdapt_aiosqlite_cursor.fetchone   s    ::::%%''r   c                     || j                   }| j                  }t        t        |t	        |                  D cg c]  }|j                          c}S c c}w r    )r   r   rangeminlenr8   )r   sizerr_s       r   	fetchmanyz%AsyncAdapt_aiosqlite_cursor.fetchmany   sC    <>>DZZ&+Cc"g,>&?@

@@@s   Ac                 d    t        | j                        }| j                  j                          |S r    )listr   r!   )r   retvals     r   r(   z$AsyncAdapt_aiosqlite_cursor.fetchall   s%    djj!

r   r    )__name__
__module____qualname__	__slots__r'   r   r#   r&   r0   r5   r9   r;   rC   r(   r3   r   r   r   r   `   s>    	I K<6	<'A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)   rC   )r   r@   s     r   rC   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   rT   r   r    )rG   rH   rI   rJ   r'   r   r#   r;   rC   r(   __classcell__rQ   s   @r   rL   rL      s(     IK 
4>
4r   rL   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    )r[   r   )r   r[   
connections      r   r   z(AsyncAdapt_aiosqlite_connection.__init__   s    
%r   c                 .    | j                   j                  S r    )r   isolation_levelr"   s    r   r_   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    )r_   )r]   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   rb   rc   functionfuturer.   s         r   r_   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   argsrP   r.   s       r   rm   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    )rL   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   rn   rP   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   rr   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+   rs   s     r   rv   z&AsyncAdapt_aiosqlite_connection.commit  sC    	*KK((//12 	*""5))	*rt   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+   rs   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)
isinstancerx   rn   r[   sqliteOperationalErrorrs   s     r   r+   z1AsyncAdapt_aiosqlite_connection._handle_exception$  sG    uj)

1!77**##44& Kr   N)F)rG   rH   rI   staticmethodr   r   rJ   r   propertyr_   setterrm   r%   r&   rr   rv   r#   r+   r3   r   r   rZ   rZ      sh    *%FI& 0 0 * *&*5B*** 	r   rZ   c                        e Zd ZdZ ee      Zy)'AsyncAdaptFallback_aiosqlite_connectionr3   N)rG   rH   rI   rJ   r~   r   r   r3   r   r   r   r   0  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)	aiosqliter|   
paramstyle_init_dbapi_attributes)r   r   r|   s      r   r   z#AsyncAdapt_aiosqlite_dbapi.__init__7  s#    "!##%r   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|   )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)
popr   connectdaemonr
   asboolr   r   rZ   r   )r   rO   rP   r   
creator_fnr]   s         r   r   z"AsyncAdapt_aiosqlite_dbapi.connectP  s     0%8VV.5
#S/B/J///;;J $J;;~&:z* 
 3:& r   N)rG   rH   rI   r   r   r   r3   r   r   r   r   6  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_cursorh  s    %%,,,>>r   N)rG   rH   rI   r   r3   r   r   r   r   g  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_dbapiv  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_class|  s$    s#== ??"r   c                     t        || j                  j                        rdt        |      v ryt        |   |||      S )Nrz   T)r{   r[   r}   strrN   is_disconnect)r   er]   r%   rQ   s       r   r   z%SQLiteDialect_aiosqlite.is_disconnect  s=    tzz**
$A.w$Q
F;;r   c                     |j                   S r    )r   )r   r]   s     r   get_driver_connectionz-SQLiteDialect_aiosqlite.get_driver_connection  s    %%%r   )rG   rH   rI   driversupports_statement_cacheis_asyncsupports_server_side_cursorsr   execution_ctx_clsclassmethodr   r   r   r   rW   rX   s   @r   r   r   l  sO    F#H#' 8
 

 # #<&r   r   )__doc__re   collectionsr   	functoolsr   baser   pysqliter    r	   r
   enginer   util.concurrencyr   r   r   rL   rZ   r   r   r   r   dialectr3   r   r   <module>r      s   GR    ( ,   ' . *Z Zz4%@ 48T&7 Tn*.M *. .b?'= ?
 &4  &F "r   