o
    NK&h/                  	   @   s@  d Z ddlZddlZddlZddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ ddlmZmZmZmZmZ dd	lmZ d
dlmZ d
dlmZ g dZedZ eed  Z!dej"deddfddZ#dej"de$ddfddZ%e& Z'G dd deZ(ddddee)ef deej* dede(fdd Z+dS )!z*
Core implementation of aiosqlite proxies
    N)AsyncIterator	GeneratorIterable)partial)Path)EmptyQueueSimpleQueue)Thread)AnyCallableLiteralOptionalUnion)warn   )contextmanager)Cursor)connect
Connectionr   	aiosqlite)DEFERRED	IMMEDIATE	EXCLUSIVEfutresultreturnc                 C      |   s| | dS dS )z9Set the result of a future if it hasn't been set already.N)done
set_result)r   r    r    A/var/www/html/venv/lib/python3.10/site-packages/aiosqlite/core.pyr         r   ec                 C   r   )z<Set the exception of a future if it hasn't been set already.N)r   set_exception)r   r#   r    r    r!   r$   $   r"   r$   c                       sb  e Zd Z	d`deg ejf dedeej	 ddf fddZ
dd	 Zedejfd
dZdededeej fddZdededeej fddZdaddZdd ZdbddZdeedd f fddZdbddZdaddZedefddZdad d!Zdad"d#Zdad$d%Z e	d`dedeee  defd&d'Z!e	d`dedeee  deej fd(d)Z"e	d`dedeee  deej fd*d+Z#ededeee  defd,d-Z$ed.edefd/d0Z%dad1d2Z&	3dcd4ed5ed6ed7e'ddf
d8d9Z(ede'fd:d;Z)edee fd<d=Z*e*j+d>e,ddfd?d=Z*edee- fd@dAZ.e.j+dBee- ddfdCdAZ.edee/gef fdDdEZ0e0j+dBee/gef ddfdFdEZ0edefdGdHZ1d>e'ddfdIdJZ2dKefdLdMZ3dNeg ee f dOeddfdPdQZ4dNeddfdRdSZ5de6e fdTdUZ7dVddWdXdYdZe8d ejf d[ed\eeeeegdf  d4ed]e9ddfd^d_Z:  Z;S )dr   N	connectoriter_chunk_sizeloopr   c                    sD   t    d| _d | _|| _t | _|| _|d ur tdt	 d S d S )NTz8aiosqlite.Connection no longer uses the `loop` parameter)
super__init___running_connection
_connectorr	   _tx_iter_chunk_sizer   DeprecationWarning)selfr%   r&   r'   	__class__r    r!   r)   .   s   
zConnection.__init__c                 C   s   d| _ | jt d S )NF)r*   r-   
put_nowait_STOP_RUNNING_SENTINELr0   r    r    r!   _stop_runningA   s   zConnection._stop_runningc                 C   s   | j d u r	td| j S )Nzno active connection)r+   
ValueErrorr5   r    r    r!   _connF   s   
zConnection._connsql
parametersc                 C   s    | j ||}|d | S )NzSELECT last_insert_rowid())r8   executefetchoner0   r9   r:   cursorr    r    r!   _execute_insertM   s   
zConnection._execute_insertc                 C   s   | j ||}| S N)r8   r;   fetchallr=   r    r    r!   _execute_fetchallR   s   zConnection._execute_fetchallc              
   C   s   	 | j  }|tu rdS |\}}ztd| | }td| | t|| W n! tyK } ztd| | t	|| W Y d}~nd}~ww q)zV
        Execute function calls on a separate thread.

        :meta private:
        Tzexecuting %szoperation %s completedzreturning exception %sN)
r-   getr4   LOGdebugget_loopcall_soon_threadsafer   BaseExceptionr$   )r0   tx_itemfuturefunctionr   r#   r    r    r!   runV   s    
zConnection.runc                    sR   | j r| jstdt|g|R i |}t  }| j||f |I dH S )z8Queue a function with the given arguments for execution.zConnection closedN)	r*   r+   r7   r   asyncioget_event_loopcreate_futurer-   r3   )r0   fnargskwargsrK   rJ   r    r    r!   _executep   s   
zConnection._executec                    s`   | j du r.zt  }| j|| jf |I dH | _ W | S  ty-   |   d| _  w | S )z&Connect to the actual sqlite database.N)	r+   rM   rN   rO   r-   r3   r,   rH   r6   )r0   rJ   r    r    r!   _connect|   s   
zConnection._connectc                 C   s   |    |   S r@   )startrT   	__await__r5   r    r    r!   rV      s   zConnection.__await__c                    s   | I d H S r@   r    r5   r    r    r!   
__aenter__   s   
zConnection.__aenter__c                    s   |   I d H  d S r@   )close)r0   exc_typeexc_valexc_tbr    r    r!   	__aexit__   s   zConnection.__aexit__c                    s   t | | | jjI dH S )z<Create an aiosqlite cursor wrapping a sqlite3 cursor object.N)r   rS   r8   r>   r5   r    r    r!   r>      s   zConnection.cursorc                       |  | jjI dH  dS )zCommit the current transaction.N)rS   r8   commitr5   r    r    r!   r^         zConnection.commitc                    r]   )z"Roll back the current transaction.N)rS   r8   rollbackr5   r    r    r!   r`      r_   zConnection.rollbackc                    sj   | j du rdS z$z| | jjI dH  W n ty"   td  w W |   d| _ dS |   d| _ w )z9Complete queued queries/cursors and close the connection.Nz+exception occurred while closing connection)r+   rS   r8   rX   	ExceptionrD   infor6   r5   r    r    r!   rX      s   


zConnection.closec                    s0   |du rg }|  | jj||I dH }t| |S )z6Helper to create a cursor and execute the given query.N)rS   r8   r;   r   r=   r    r    r!   r;      s
   
zConnection.executec                    $   |du rg }|  | j||I dH S )z/Helper to insert and get the last_insert_rowid.N)rS   r?   r0   r9   r:   r    r    r!   execute_insert      zConnection.execute_insertc                    rc   )z2Helper to execute a query and return all the data.N)rS   rB   rd   r    r    r!   execute_fetchall   rf   zConnection.execute_fetchallc                    s$   |  | jj||I dH }t| |S )z;Helper to create a cursor and execute the given multiquery.N)rS   r8   executemanyr   r=   r    r    r!   rh      s   
zConnection.executemany
sql_scriptc                    s"   |  | jj|I dH }t| |S )z4Helper to create a cursor and execute a user script.N)rS   r8   executescriptr   )r0   ri   r>   r    r    r!   rj      s   
zConnection.executescriptc                    s   | j  S )zInterrupt pending queries.)r8   	interruptr5   r    r    r!   rk      s   
zConnection.interruptFname
num_paramsfuncdeterministicc                    s$   | j | jj||||dI dH  dS )a/  
        Create user-defined function that can be later used
        within SQL statements. Must be run within the same thread
        that query executions take place so instead of executing directly
        against the connection, we defer this to `run` function.

        If ``deterministic`` is true, the created function is marked as deterministic,
        which allows SQLite to perform additional optimizations. This flag is supported
        by SQLite 3.8.3 or higher, ``NotSupportedError`` will be raised if used with
        older versions.
        )ro   N)rS   r8   create_function)r0   rl   rm   rn   ro   r    r    r!   rp      s   zConnection.create_functionc                 C      | j jS r@   )r8   in_transactionr5   r    r    r!   rr         zConnection.in_transactionc                 C   rq   r@   r8   isolation_levelr5   r    r    r!   ru      rs   zConnection.isolation_levelvaluec                 C      || j _d S r@   rt   r0   rv   r    r    r!   ru         c                 C   rq   r@   r8   row_factoryr5   r    r    r!   r{      rs   zConnection.row_factoryfactoryc                 C   rw   r@   rz   r0   r|   r    r    r!   r{     ry   c                 C   rq   r@   r8   text_factoryr5   r    r    r!   r     rs   zConnection.text_factoryc                 C   rw   r@   r~   r}   r    r    r!   r     ry   c                 C   rq   r@   )r8   total_changesr5   r    r    r!   r     rs   zConnection.total_changesc                       |  | jj|I d H  d S r@   )rS   r8   enable_load_extensionrx   r    r    r!   r        z Connection.enable_load_extensionpathc                    r   r@   )rS   r8   load_extension)r0   r   r    r    r!   r     r   zConnection.load_extensionhandlernc                    s   |  | jj||I d H  d S r@   )rS   r8   set_progress_handler)r0   r   r   r    r    r!   r     s   zConnection.set_progress_handlerc                    r   r@   )rS   r8   set_trace_callback)r0   r   r    r    r!   r     r   zConnection.set_trace_callbackc                   s   t    fdd}|}t|}	 z  }|du r!W n"|V  W n tyA   | r7td Y nt	dI dH  Y nw q|I dH  dS )z
        Return an async iterator to dump the database in SQL text format.

        Example::

            async for line in db.iterdump():
                ...

        c                     sP   zj  D ]}  |  q d  W d S  ty'   td  d   w )Nzexception while dumping db)r8   iterdumpr3   ra   rD   	exception)line
dump_queuer0   r    r!   dumper.  s   

z#Connection.iterdump.<locals>.dumperTNziterdump completed unexpectedlyg{Gz?)
r   rS   rM   ensure_future
get_nowaitr   r   rD   warningsleep)r0   r   r   taskr   r    r   r!   r   "  s&   




zConnection.iterdumpr   maing      ?pagesprogressrl   r   targetr   r   r   c                   s6   t |tr	|j}| j| jj|||||dI dH  dS )z
        Make a backup of the current database to the target database.

        Takes either a standard sqlite3 or aiosqlite Connection object as the target.
        r   N)
isinstancer   r8   rS   backup)r0   r   r   r   rl   r   r    r    r!   r   L  s   
zConnection.backupr@   )r   N)r   r   )F)<__name__
__module____qualname__r   sqlite3r   intr   rM   AbstractEventLoopr)   r6   propertyr8   strr   Rowr?   r   rB   rL   rS   rT   r   rV   rW   r\   r   r   r>   r^   r`   rX   r;   re   rg   rh   rj   rk   boolrp   rr   ru   setterIsolationLeveltyper{   bytesr   r   r   r   r   r   r   r   r   floatr   __classcell__r    r    r1   r!   r   -   s    







	





.r   @   )r&   r'   databaser'   rR   c                   s2   |dur	t dt dtjf fdd}t||S )z<Create and return a connection proxy to the sqlite database.Nz7aiosqlite.connect() no longer uses the `loop` parameterr   c                     s@   t  tr } nt  tr d} nt } tj| fi S )Nzutf-8)r   r   r   decoder   r   )locr   rR   r    r!   r%   v  s   

zconnect.<locals>.connector)r   r/   r   r   )r   r&   r'   rR   r%   r    r   r!   r   g  s   	

r   ),__doc__rM   loggingr   collections.abcr   r   r   	functoolsr   pathlibr   queuer   r   r	   	threadingr
   typingr   r   r   r   r   warningsr   contextr   r>   r   __all__	getLoggerrD   r   Futurer   rH   r$   objectr4   r   r   r   r   r    r    r    r!   <module>   sB   
  ?
