Ë
    Š)f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  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`.

.. _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)Ú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 | _        g | _        y )Nr   éÿÿÿÿ)r   r   r   r   r   r   r   )ÚselfÚadapt_connections     úa/var/www/html/flask-app/venv/lib/python3.12/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.pyÚ__init__z$AsyncAdapt_aiosqlite_cursor.__init__m   sB   € Ø!1ˆÔØ+×7Ñ7ˆÔØ&×-Ñ-ˆŒØˆŒØˆŒØˆÔØˆ�
ó    c                 ó"   — g | j                   d d  y ©N©r   ©r   s    r   Úclosez!AsyncAdapt_aiosqlite_cursor.closev   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.executey   s  € ð	<Ø—k‘k $×"2Ñ"2×"9Ñ"9Ó";Ó<ˆGàÐ!Ø—‘˜GŸO™O¨IÓ6Õ7à—‘˜GŸO™O¨I°zÓBÔCà×"Ò"Ø#*×#6Ñ#6�Ô Ø13Ð3�” ¤à×'Ò'Ø!%§¡¨W×-=Ñ-=Ó-?Ó!@�D•Jà#'�Ô Ø!(×!2Ñ!2�”Ø '× 0Ñ 0�”à×#Ò#Ø—‘˜GŸM™M›OÕ,à&�•øÜò 	<Ø×"Ñ"×4Ñ4°U×;Ñ;ûð	<ú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.executemany”   s™   € ð	<Ø—k‘k $×"2Ñ"2×"9Ñ"9Ó";Ó<ˆGØ�K‰K˜×+Ñ+¨IÐ7HÓIÔJØ#ˆDÔØ$×.Ñ.ˆDŒNØ#×,Ñ,ˆDŒMØ�K‰K˜Ÿ™›Õ(øÜò 	<Ø×"Ñ"×4Ñ4°U×;Ñ;ûð	<ús   ‚BB Â	CÂB>Â>Cc                  ó   — y r   © )r   Ú
inputsizess     r   Úsetinputsizesz)AsyncAdapt_aiosqlite_cursor.setinputsizesŸ   s   € Ør   c              #   óz   K  — | j                   r+| j                   j                  d«      –— | j                   rŒ*y y ­w©Nr   ©r   Úpopr!   s    r   Ú__iter__z$AsyncAdapt_aiosqlite_cursor.__iter__¢   s)   è ø€ Ø�jŠjØ—*‘*—.‘. Ó#Ò#ð �j�jùs   ‚6;¹;c                 óR   — | j                   r| j                   j                  d«      S y r6   r7   r!   s    r   Úfetchonez$AsyncAdapt_aiosqlite_cursor.fetchone¦   s   € Ø�:Š:Ø—:‘:—>‘> !Ó$Ð$àr   c                 óx   — |€| j                   }| j                  d| }| j                  |d  | j                  d d  |S r6   )r   r   )r   ÚsizeÚretvals      r   Ú	fetchmanyz%AsyncAdapt_aiosqlite_cursor.fetchmany¬   s=   € Øˆ<Ø—>‘>ˆDà—‘˜A˜dÐ#ˆØŸ
™
 4 5Ð)ˆ�
‰
‘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/   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                 ó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   € Ø�<‰<Ð#Ø�K‰K˜Ÿ™×*Ñ*Ó,Ô-ØˆD�Lð $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Ñ1°tÐ1Ó<Ó=Ð=r   c                 óT   — | j                  | j                  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ó>ö
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y)ÚAsyncAdapt_aiosqlite_connection)Údbapic                 ó    — || _         || _        y 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                 ó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   )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Ž   € ò	/ô ˜7 D×$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   ÚargsrJ   r-   s       r   rg   z/AsyncAdapt_aiosqlite_connection.create_functionö   sM   € ð	*Ø�K‰KÐ8˜×(Ñ(×8Ñ8¸$ÐEÀ"ÑEÕFøÜò 	*Ø×"Ñ" 5×)Ñ)ûð	*ús   ‚+. ®	A·AÁAc                 ó2   — |rt        | «      S t        | «      S r   )rF   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   rh   rJ   s      r   r%   z'AsyncAdapt_aiosqlite_connection.execute  s*   € Ø�{‰{Ð3˜4×+Ñ+×3Ñ3°TÐ@¸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   rl   z(AsyncAdapt_aiosqlite_connection.rollback  sC   € ð	*Ø�K‰K˜×(Ñ(×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*   rm   s     r   rp   z&AsyncAdapt_aiosqlite_connection.commit  sC   € ð	*Ø�K‰K˜×(Ñ(×/Ñ/Ó1Õ2øÜò 	*Ø×"Ñ" 5×)Ñ)ûð	*úrn   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*   rm   s     r   r"   z%AsyncAdapt_aiosqlite_connection.close  sP   € ð	*Ø�K‰K˜×(Ñ(×.Ñ.Ó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)Ú
isinstancerr   rh   rU   ÚsqliteÚOperationalErrorrm   s     r   r*   z1AsyncAdapt_aiosqlite_connection._handle_exception!  sG   € ä�uœjÔ)Ø—
‘
˜1‘Ð!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   Ö   sh   „ Ù˜*Ó%€FØ€Iò&ð ñ0ó ð0ð ×Ññ*ó ð*ò&*ó5òBò*ò*ò*ó 	r   rT   c                   ó    — e Zd ZdZ ee«      Zy)Ú'AsyncAdaptFallback_aiosqlite_connectionr2   N)rA   rB   rC   rD   rx   r   r   r2   r   r   r|   r|   -  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)Ú	aiosqliterv   Ú
paramstyleÚ_init_dbapi_attributes)r   r�   rv   s      r   r   z#AsyncAdapt_aiosqlite_dbapi.__init__4  s#   € Ø"ˆŒØˆŒØ!ˆŒØ×#Ñ#Õ%r   c           	      óô   — dD ]#  }t        | |t        | j                  |«      «       Œ% dD ]#  }t        | |t        | j                  |«      «       Œ% dD ]#  }t        | |t        | j                  |«      «       Œ% y )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˜$¤¨¯©¸Ó =Õ>ð
	?ð :ò 	<ˆ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)
r8   r�   ÚconnectÚdaemonr	   Úasboolr|   r   rT   r   )r   rI   rJ   r“   Ú
creator_fnrW   s         r   r•   z"AsyncAdapt_aiosqlite_dbapi.connectM  s—   € ØŸ™Ð 0°%Ó8ˆà—V‘VÐ.°Ó5ˆ
ÙÙ# SÐ/¨BÑ/‰Jà/˜Ÿ™×/Ñ/°Ð;¸Ñ;ˆJà $ˆJÔä�;‰;�~Ô&Ü:ØÜ˜zÓ*óð ô
 3ØÜ˜:Ó&óð r   N)rA   rB   rC   r   rƒ   r•   r2   r   r   r~   r~   3  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_cursore  s   € Ø×%Ñ%×,Ñ,¸Ð,Ó>Ð>r   N)rA   rB   rC   r�   r2   r   r   rš   rš   d  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_dbapis  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_classy  s$   € à×Ñ˜sÔ#Ü—=‘=Ð ä—?‘?Ð"r   c                 ó‚   •— t        || j                  j                  «      rdt        |«      v ryt        ‰| �  |||«      S )Nrt   T)ru   rU   rw   ÚstrrH   Úis_disconnect)r   ÚerW   r$   rK   s       €r   r­   z%SQLiteDialect_aiosqlite.is_disconnect€  s=   ø€ ÜØˆt�z‰z×*Ñ*ô
à$¬¨A«Ñ.Øä‰wÑ$ Q¨
°FÓ;Ð;r   c                 ó   — |j                   S r   )r   )r   rW   s     r   Úget_driver_connectionz-SQLiteDialect_aiosqlite.get_driver_connectionˆ  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Ÿ   i  sO   ø„ Ø€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�   ðñDóL Ý å (Ý ,Ý Ý Ý 'Ý .Ý *÷[ñ [ô|4Ð%@ô 4ô8TÐ&7ô Tôn*Ð.Mô *÷.ñ .ôb?Ð'=ô ?ô
 &Ð4ô  &ðF "�r   