a
    [g3                     @   s   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 dZG dd deZG dd deZG dd deZG dd dZG dd deZG dd de	ZeZdS )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.

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

    N)deque)partial   )SQLiteExecutionContext)SQLiteDialect_pysqlite   )pool)util)AdaptedConnection)await_fallback)
await_onlyc                   @   s`   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dS )AsyncAdapt_aiosqlite_cursor)_adapt_connection_connectiondescriptionawait__rows	arraysizerowcount	lastrowidFc                 C   s4   || _ |j| _|j| _d| _d| _d | _t | _d S )Nr   )r   r   r   r   r   r   r   r   )selfZadapt_connection r   r/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py__init__   s    z$AsyncAdapt_aiosqlite_cursor.__init__c                 C   s   | j   d S N)r   clearr   r   r   r   close   s    z!AsyncAdapt_aiosqlite_cursor.closeNc              
   C   s   z|  | j }|d u r,|  || n|  ||| |jrt|j| _d | _| _| jst|  |	 | _
nd | _|j| _|j| _| js|  |  n|| _W n0 ty } z| j| W Y d }~n
d }~0 0 d S )Nr   )r   r   cursorexecuter   r   r   server_sider   fetchallr   r   _cursor	Exceptionr   _handle_exception)r   	operation
parametersr#   errorr   r   r   r       s$    
z#AsyncAdapt_aiosqlite_cursor.executec              
   C   s   zJ|  | j }|  ||| d | _|j| _|j| _|  |  W n0 tyz } z| j	
| W Y d }~n
d }~0 0 d S r   )r   r   r   executemanyr   r   r   r   r$   r   r%   )r   r&   Zseq_of_parametersr#   r(   r   r   r   r)      s    z'AsyncAdapt_aiosqlite_cursor.executemanyc                 G   s   d S r   r   )r   Z
inputsizesr   r   r   setinputsizes   s    z)AsyncAdapt_aiosqlite_cursor.setinputsizesc                 c   s   | j r| j  V  q d S r   r   popleftr   r   r   r   __iter__   s    z$AsyncAdapt_aiosqlite_cursor.__iter__c                 C   s   | j r| j  S d S d S r   r+   r   r   r   r   fetchone   s    
z$AsyncAdapt_aiosqlite_cursor.fetchonec                    s4   |d u r| j }| j  fddtt|t D S )Nc                    s   g | ]}   qS r   )r,   ).0_rrr   r   
<listcomp>       z9AsyncAdapt_aiosqlite_cursor.fetchmany.<locals>.<listcomp>)r   r   rangeminlenr   sizer   r1   r   	fetchmany   s    z%AsyncAdapt_aiosqlite_cursor.fetchmanyc                 C   s   t | j}| j  |S r   )listr   r   )r   retvalr   r   r   r"      s    

z$AsyncAdapt_aiosqlite_cursor.fetchall)N)N)__name__
__module____qualname__	__slots__r!   r   r   r    r)   r*   r-   r.   r:   r"   r   r   r   r   r   v   s   	

r   c                       sF   e Zd ZdZdZ fddZdd Zdd Zdd
dZdd Z	  Z
S )AsyncAdapt_aiosqlite_ss_cursorr#   Tc                    s   t  j|i | d | _d S r   )superr   r#   )r   argkw	__class__r   r   r      s    z'AsyncAdapt_aiosqlite_ss_cursor.__init__c                 C   s$   | j d ur | | j   d | _ d S r   )r#   r   r   r   r   r   r   r      s    
z$AsyncAdapt_aiosqlite_ss_cursor.closec                 C   s   |  | j S r   )r   r#   r.   r   r   r   r   r.      s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchoneNc                 C   s"   |d u r| j }| | jj|dS )N)r9   )r   r   r#   r:   r8   r   r   r   r:      s    z(AsyncAdapt_aiosqlite_ss_cursor.fetchmanyc                 C   s   |  | j S r   )r   r#   r"   r   r   r   r   r"      s    z'AsyncAdapt_aiosqlite_ss_cursor.fetchall)N)r=   r>   r?   r@   r!   r   r   r.   r:   r"   __classcell__r   r   rE   r   rA      s   
rA   c                   @   st   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S )AsyncAdapt_aiosqlite_connection)dbapic                 C   s   || _ || _d S r   )rI   r   )r   rI   
connectionr   r   r   r      s    z(AsyncAdapt_aiosqlite_connection.__init__c                 C   s   | j jS r   )r   isolation_levelr   r   r   r   rK      s    z/AsyncAdapt_aiosqlite_connection.isolation_levelc              
   C   sv   dd }t || jj|}t  }| jj||f z| |W S  t	yp } z| 
| W Y d }~n
d }~0 0 d S )Nc                 S   s
   || _ d S r   )rK   )rJ   valuer   r   r   set_iso  s    z@AsyncAdapt_aiosqlite_connection.isolation_level.<locals>.set_iso)r   r   Z_connasyncioget_event_loopcreate_futureZ_tx
put_nowaitr   r$   r%   )r   rL   rM   functionfuturer(   r   r   r   rK      s    c              
   O   sP   z|  | jj|i | W n. tyJ } z| | W Y d }~n
d }~0 0 d S r   )r   r   create_functionr$   r%   )r   argsrD   r(   r   r   r   rT     s    z/AsyncAdapt_aiosqlite_connection.create_functionFc                 C   s   |rt | S t| S d S r   )rA   r   )r   r!   r   r   r   r     s    z&AsyncAdapt_aiosqlite_connection.cursorc                 O   s   |  | jj|i |S r   )r   r   r    )r   rU   rD   r   r   r   r      s    z'AsyncAdapt_aiosqlite_connection.executec              
   C   sH   z|  | j  W n. tyB } z| | W Y d }~n
d }~0 0 d S r   )r   r   rollbackr$   r%   r   r(   r   r   r   rV     s    z(AsyncAdapt_aiosqlite_connection.rollbackc              
   C   sH   z|  | j  W n. tyB } z| | W Y d }~n
d }~0 0 d S r   )r   r   commitr$   r%   rW   r   r   r   rX   $  s    z&AsyncAdapt_aiosqlite_connection.commitc              
   C   sX   z|  | j  W n> ty&   Y n. tyR } z| | W Y d }~n
d }~0 0 d S r   )r   r   r   
ValueErrorr$   r%   rW   r   r   r   r   *  s    	z%AsyncAdapt_aiosqlite_connection.closec                 C   s2   t |tr*|jd dkr*| jjd|n|d S )Nr   no active connection)
isinstancerY   rU   rI   sqliteOperationalErrorrW   r   r   r   r%   :  s    z1AsyncAdapt_aiosqlite_connection._handle_exceptionN)F)r=   r>   r?   staticmethodr   r   r@   r   propertyrK   setterrT   r   r    rV   rX   r   r%   r   r   r   r   rH      s   


rH   c                   @   s   e Zd ZdZeeZdS )'AsyncAdaptFallback_aiosqlite_connectionr   N)r=   r>   r?   r@   r^   r   r   r   r   r   r   ra   F  s   ra   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )AsyncAdapt_aiosqlite_dbapic                 C   s   || _ || _d| _|   d S )NZqmark)	aiosqliter\   Z
paramstyle_init_dbapi_attributes)r   rc   r\   r   r   r   r   M  s    z#AsyncAdapt_aiosqlite_dbapi.__init__c                 C   s^   dD ]}t | |t| j| qdD ]}t | |t| j| q"dD ]}t | |t| j| q@d S )N)ZDatabaseErrorErrorZIntegrityErrorZNotSupportedErrorr]   ZProgrammingErrorZsqlite_versionZsqlite_version_info)ZPARSE_COLNAMESZPARSE_DECLTYPES)Binary)setattrgetattrrc   r\   )r   namer   r   r   rd   S  s    
z1AsyncAdapt_aiosqlite_dbapi._init_dbapi_attributesc                 O   sn   | dd}| dd }|r,||i |}n| jj|i |}d|_t|r\t| t|S t| t	|S d S )Nasync_fallbackFZasync_creator_fnT)
poprc   connectdaemonr	   Zasboolra   r   rH   r   )r   rC   rD   rj   Z
creator_fnrJ   r   r   r   rl   f  s    
z"AsyncAdapt_aiosqlite_dbapi.connectN)r=   r>   r?   r   rd   rl   r   r   r   r   rb   L  s   rb   c                   @   s   e Zd Zdd ZdS ) SQLiteExecutionContext_aiosqlitec                 C   s   | j jddS )NT)r!   )Z_dbapi_connectionr   r   r   r   r   create_server_side_cursor~  s    z:SQLiteExecutionContext_aiosqlite.create_server_side_cursorN)r=   r>   r?   ro   r   r   r   r   rn   }  s   rn   c                       sP   e Zd ZdZdZdZdZeZe	dd Z
e	dd Z fddZd	d
 Z  ZS )SQLiteDialect_aiosqliterc   Tc                 C   s   t tdtdS )Nrc   sqlite3)rb   
__import__)clsr   r   r   import_dbapi  s    z$SQLiteDialect_aiosqlite.import_dbapic                 C   s   |  |rtjS tjS d S r   )Z_is_url_file_dbr   ZAsyncAdaptedQueuePoolZ
StaticPool)rs   urlr   r   r   get_pool_class  s    
z&SQLiteDialect_aiosqlite.get_pool_classc                    s.   t || jjrdt|v rdS t |||S )NrZ   T)r[   rI   r]   strrB   is_disconnect)r   erJ   r   rE   r   r   rx     s    
z%SQLiteDialect_aiosqlite.is_disconnectc                 C   s   |j S r   )r   )r   rJ   r   r   r   get_driver_connection  s    z-SQLiteDialect_aiosqlite.get_driver_connection)r=   r>   r?   ZdriverZsupports_statement_cacheis_asyncZsupports_server_side_cursorsrn   Zexecution_ctx_clsclassmethodrt   rv   rx   rz   rG   r   r   rE   r   rp     s   

rp   )__doc__rN   collectionsr   	functoolsr   baser   Zpysqliter    r   r	   Zenginer
   Zutil.concurrencyr   r   r   rA   rH   ra   rb   rn   rp   dialectr   r   r   r   <module>
   s$   _]W1#