o
    iZ9                     @  s  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rd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% d%Z/G d&d' d'e/Z0G d(d) d)eZ1G d*d+ d+e1Z2G d,d- d-eZ3G d.d/ d/eZ4G d0d1 d1eZ5e5Z6dS )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                   @  s~   e Zd ZdZdZd(ddZd)d	d
Zd)ddZ	d*d+ddZd,ddZ	d-ddZ
d.ddZd/dd Zd*d0d$d%Zd1d&d'ZdS )2AsyncAdapt_aiosqlite_cursor)_adapt_connection_connectiondescriptionawait__rows	arraysizerowcount	lastrowidFadapt_connectionAsyncAdapt_aiosqlite_connectionc                 C  s4   || _ |j| _|j| _d| _d| _d | _t | _d S )Nr   )r$   r%   r'   r)   r*   r&   r   r(   )selfr,    r0   j/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__   s   z$AsyncAdapt_aiosqlite_cursor.__init__returnNonec                   s   d S Nr0   r/   r0   r0   r1   _async_soft_close   s   z-AsyncAdapt_aiosqlite_cursor._async_soft_closec                 C  s   | j   d S r5   )r(   clearr6   r0   r0   r1   close      z!AsyncAdapt_aiosqlite_cursor.closeN	operationr   
parameters#Optional[_DBAPISingleExecuteParams]c              
   C  s   zW|  | j }|d u r|  || n	|  ||| |jr:|j| _d | _| _| js9t|  |	 | _
nd | _|j| _|j| _| jsR|  |  W d S || _W d S  typ } z| j| W Y d }~d S d }~ww )Nr.   )r'   r%   cursorexecuter&   r+   r*   server_sider   fetchallr(   r9   _cursor	Exceptionr$   _handle_exception)r/   r;   r<   rB   errorr0   r0   r1   r?      s*   z#AsyncAdapt_aiosqlite_cursor.executeseq_of_parametersr   c              
   C  s   z&|  | j }|  ||| d | _|j| _|j| _|  |  W d S  ty? } z| j	
| W Y d }~d S d }~ww r5   )r'   r%   r>   executemanyr&   r+   r*   r9   rC   r$   rD   )r/   r;   rF   rB   rE   r0   r0   r1   rG      s   z'AsyncAdapt_aiosqlite_cursor.executemany
inputsizesc                 G  s   d S r5   r0   )r/   rH   r0   r0   r1   setinputsizes   s   z)AsyncAdapt_aiosqlite_cursor.setinputsizesIterator[Any]c                 c  s"    | j r| j  V  | j sd S d S r5   r(   popleftr6   r0   r0   r1   __iter__   s   z$AsyncAdapt_aiosqlite_cursor.__iter__Optional[Any]c                 C  s   | j r| j  S d S r5   rK   r6   r0   r0   r1   fetchone   s   
z$AsyncAdapt_aiosqlite_cursor.fetchonesizeOptional[int]Sequence[Any]c                   s4   |d u r| j }| j  fddtt|t D S )Nc                   s   g | ]}   qS r0   )rL   ).0_rrr0   r1   
<listcomp>   s    z9AsyncAdapt_aiosqlite_cursor.fetchmany.<locals>.<listcomp>)r)   r(   rangeminlenr/   rP   r0   rU   r1   	fetchmany   s    z%AsyncAdapt_aiosqlite_cursor.fetchmanyc                 C  s   t | j}| j  |S r5   )listr(   r8   )r/   retvalr0   r0   r1   rA      s   

z$AsyncAdapt_aiosqlite_cursor.fetchall)r,   r-   r3   r4   r5   )r;   r   r<   r=   r3   r   )r;   r   rF   r   r3   r   )rH   r   r3   r4   )r3   rJ   r3   rN   rP   rQ   r3   rR   r3   rR   )__name__
__module____qualname__	__slots__r@   r2   r7   r9   r?   rG   rI   rM   rO   r\   rA   r0   r0   r0   r1   r#   v   s    

	

 


r#   c                      sP   e Zd ZdZdZd fdd	Zdd
dZdddZddddZdddZ	  Z
S )AsyncAdapt_aiosqlite_ss_cursorrB   Targr   kwr3   r4   c                   s   t  j|i | d | _d S r5   )superr2   rB   )r/   rh   ri   	__class__r0   r1   r2      s   
z'AsyncAdapt_aiosqlite_ss_cursor.__init__c                 C  s(   | j d ur| | j   d | _ d S d S r5   )rB   r'   r9   r6   r0   r0   r1   r9      s   

z$AsyncAdapt_aiosqlite_ss_cursor.closerN   c                 C     | j d usJ | | j  S r5   )rB   r'   rO   r6   r0   r0   r1   rO         z'AsyncAdapt_aiosqlite_ss_cursor.fetchoneNrP   rQ   rR   c                 C  s0   | j d usJ |d u r| j}| | j j|dS )N)rP   )rB   r)   r'   r\   r[   r0   r0   r1   r\      s   z(AsyncAdapt_aiosqlite_ss_cursor.fetchmanyc                 C  rm   r5   )rB   r'   rA   r6   r0   r0   r1   rA      rn   z'AsyncAdapt_aiosqlite_ss_cursor.fetchall)rh   r   ri   r   r3   r4   r_   r`   r5   ra   rb   )rc   rd   re   rf   r@   r2   r9   rO   r\   rA   __classcell__r0   r0   rk   r1   rg      s    

rg   c                   @  s   e Zd ZeeZdZd'dd	Zed(ddZ	e	j
d)ddZ	d*ddZd+d,ddZd-ddZd.ddZd.ddZd.dd Zd/d$d%Zd&S )0r-   )dbapirp   r   
connectionr   r3   r4   c                 C  s   || _ || _d S r5   )rp   r%   )r/   rp   rq   r0   r0   r1   r2     s   
z(AsyncAdapt_aiosqlite_connection.__init__Optional[str]c                 C  s   t t| jjS r5   )r   strr%   isolation_levelr6   r0   r0   r1   rt     s   z/AsyncAdapt_aiosqlite_connection.isolation_levelvaluec              
   C  sz   d	dd}t || jj|}t  }| jj||f z| | W d S  t	y< } z| 
| W Y d }~d S d }~ww )
Nrq   r-   ru   rr   r3   r4   c                 S  s
   || _ d S r5   )rt   )rq   ru   r0   r0   r1   set_iso  s   
z@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso)rq   r-   ru   rr   r3   r4   )r   r%   _connasyncioget_event_loopcreate_future_tx
put_nowaitr'   rC   rD   )r/   ru   rv   functionfuturerE   r0   r0   r1   rt   
  s   
argsri   c              
   O  sP   z|  | jj|i | W d S  ty' } z| | W Y d }~d S d }~ww r5   )r'   r%   create_functionrC   rD   )r/   r   ri   rE   r0   r0   r1   r      s   z/AsyncAdapt_aiosqlite_connection.create_functionFr@   boolr#   c                 C  s   |rt | S t| S r5   )rg   r#   )r/   r@   r0   r0   r1   r>   &  s   z&AsyncAdapt_aiosqlite_connection.cursorc                 O  s   |  | jj|i |S r5   )r'   r%   r?   )r/   r   ri   r0   r0   r1   r?   ,  s   z'AsyncAdapt_aiosqlite_connection.executec              
   C  H   z|  | j  W d S  ty# } z| | W Y d }~d S d }~ww r5   )r'   r%   rollbackrC   rD   r/   rE   r0   r0   r1   r   /     z(AsyncAdapt_aiosqlite_connection.rollbackc              
   C  r   r5   )r'   r%   commitrC   rD   r   r0   r0   r1   r   5  r   z&AsyncAdapt_aiosqlite_connection.commitc              
   C  sZ   z|  | j  W d S  ty   Y d S  ty, } z| | W Y d }~d S d }~ww r5   )r'   r%   r9   
ValueErrorrC   rD   r   r0   r0   r1   r9   ;  s   	z%AsyncAdapt_aiosqlite_connection.closerE   rC   r
   c                 C  s,   t |tr|jd dkr| jjd||)Nr   no active connection)
isinstancer   r   rp   sqliteOperationalErrorr   r0   r0   r1   rD   K  s   z1AsyncAdapt_aiosqlite_connection._handle_exceptionN)rp   r   rq   r   r3   r4   )r3   rr   )ru   rr   r3   r4   )r   r   ri   r   r3   r4   )F)r@   r   r3   r#   )r   r   ri   r   r3   r   r_   )rE   rC   r3   r
   )rc   rd   re   staticmethodr   r'   rf   r2   propertyrt   setterr   r>   r?   r   r   r9   rD   r0   r0   r0   r1   r-      s    





r-   c                   @  s   e Zd ZdZeeZdS )'AsyncAdaptFallback_aiosqlite_connectionr0   N)rc   rd   re   rf   r   r   r'   r0   r0   r0   r1   r   W  s    r   c                   @  s*   e Zd ZdddZddd	ZdddZdS )AsyncAdapt_aiosqlite_dbapi	aiosqliter   r   c                 C  s   || _ || _d| _|   d S )Nqmark)r   r   
paramstyle_init_dbapi_attributes)r/   r   r   r0   r0   r1   r2   ^  s   z#AsyncAdapt_aiosqlite_dbapi.__init__r3   r4   c                 C  s^   dD ]}t | |t| j| qdD ]}t | |t| j| qdD ]}t | |t| j| q d S )N)DatabaseErrorErrorIntegrityErrorNotSupportedErrorr   ProgrammingErrorsqlite_versionsqlite_version_info)PARSE_COLNAMESPARSE_DECLTYPES)Binary)setattrgetattrr   r   )r/   namer0   r0   r1   r   d  s   
z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesrh   r   ri   r-   c                 O  sj   | dd}| dd }|r||i |}n| jj|i |}d|_t|r.t| t|S t| t	|S )Nasync_fallbackFasync_creator_fnT)
popr   connectdaemonr   asboolr   r   r-   r   )r/   rh   ri   r   
creator_fnrq   r0   r0   r1   r   w  s   
z"AsyncAdapt_aiosqlite_dbapi.connectN)r   r   r   r   r_   )rh   r   ri   r   r3   r-   )rc   rd   re   r2   r   r   r0   r0   r0   r1   r   ]  s    

r   c                   @  s   e Zd ZdddZdS ) SQLiteExecutionContext_aiosqliter3   r   c                 C  s   | j jddS )NT)r@   )_dbapi_connectionr>   r6   r0   r0   r1   create_server_side_cursor  r:   z:SQLiteExecutionContext_aiosqlite.create_server_side_cursorN)r3   r   )rc   rd   re   r   r0   r0   r0   r1   r     s    r   c                      sX   e Zd ZdZdZdZdZeZe	dddZ
e	dd
dZd fddZdddZ  ZS )SQLiteDialect_aiosqliter   Tr3   r   c                 C  s   t tdtdS )Nr   sqlite3)r   
__import__)clsr0   r0   r1   import_dbapi  s   z$SQLiteDialect_aiosqlite.import_dbapiurlr!   type[pool.Pool]c                 C  s   |  |rtjS tjS r5   )_is_url_file_dbr   AsyncAdaptedQueuePool
StaticPool)r   r   r0   r0   r1   get_pool_class  s   
z&SQLiteDialect_aiosqlite.get_pool_classeDBAPIModule.Errorrq   7Optional[Union[PoolProxiedConnection, DBAPIConnection]]r>   Optional[DBAPICursor]r   c                   s<   t d| j| _t|| jjrdt|v rdS t |||S )Nr    r   T)r   rp   r   r   rs   rj   is_disconnect)r/   r   rq   r>   rk   r0   r1   r     s   z%SQLiteDialect_aiosqlite.is_disconnectr   r   c                 C  s   |j S r5   )r%   )r/   rq   r0   r0   r1   get_driver_connection  s   z-SQLiteDialect_aiosqlite.get_driver_connection)r3   r   )r   r!   r3   r   )r   r   rq   r   r>   r   r3   r   )rq   r   r3   r   )rc   rd   re   driversupports_statement_cacheis_asyncsupports_server_side_cursorsr   execution_ctx_clsclassmethodr   r   r   r   ro   r0   r0   rk   r1   r     s    r   )7__doc__
__future__r   rx   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#   rg   r-   r   r   r   r   dialectr0   r0   r0   r1   <module>   sT   GiY1+