
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
U
    SW[!                     @   s  d dl Z d dlZd dlmZ d dlmZ zd dlmZ W n  ek
rX   d dlmZ Y nX z0d dlZd dlm	Z
 d dlmZ d dlmZ W n  ek
r   d Z
 ZZY nX d dlmZ e dZG dd	 d	eZG d
d deZdZdZG dd deZG dd deZG dd deZdS )    NEvent)Thread)Queue)Greenlet)SqliteExtDatabasezpeewee.sqliteqc                   @   s   e Zd ZdS )ResultTimeoutN)__name__
__module____qualname__ r   r   D/opt/alt/python38/lib64/python3.8/site-packages/playhouse/sqliteq.pyr      s   r   c                   @   s|   e Zd ZdZdd ZdddZdddZd	d
 Zdd ZeZ	e
dd Ze
dd Ze
dd Zdd Zdd Zdd ZdS )AsyncCursor)	sqlparamscommittimeout_event_cursor_exc_idx_rowsc                 C   s:   || _ || _|| _|| _|| _d  | _ | _ | _| _d S N)	r   r   r   r   r   r   r   r   r   )selfeventr   r   r   r   r   r   r   __init__    s    zAsyncCursor.__init__Nc                 C   s6   || _ || _d| _|d kr"| ng | _| j  | S )Nr   )r   r   r   fetchallr   r   set)r   cursorexcr   r   r   
set_result(   s    
zAsyncCursor.set_resultc                 C   s@   |d k	r|n| j }| jj|ds,|r,td| jd k	r<| jd S )N)r   zresults not ready, timed out.)r   r   waitr   r   )r   r   r   r   r   _wait0   s
    
zAsyncCursor._waitc                 C   s   |    | jd k	r| j| S r   )r"   r   _execr   r   r   r   __iter__7   s    
zAsyncCursor.__iter__c                 C   s@   z| j | j }W n tk
r(   tY nX |  jd7  _|S d S )N   )r   r   
IndexErrorStopIteration)r   objr   r   r   next=   s    
zAsyncCursor.nextc                 C   s   |    | jjS r   )r"   r   	lastrowidr$   r   r   r   r+   G   s    zAsyncCursor.lastrowidc                 C   s   |    | jjS r   )r"   r   rowcountr$   r   r   r   r,   L   s    zAsyncCursor.rowcountc                 C   s   | j jS r   )r   descriptionr$   r   r   r   r-   Q   s    zAsyncCursor.descriptionc                 C   s   | j   d S r   )r   closer$   r   r   r   r.   U   s    zAsyncCursor.closec                 C   s   t | S r   )listr$   r   r   r   r   X   s    zAsyncCursor.fetchallc                 C   s.   |    z
t| W S  tk
r(   Y d S X d S r   )r"   r*   r(   r$   r   r   r   fetchone[   s
    
zAsyncCursor.fetchone)N)N)r	   r
   r   	__slots__r   r    r"   r%   r*   __next__propertyr+   r,   r-   r.   r   r0   r   r   r   r   r      s    




r   zthreadlocals cannot be set to True when using the Sqlite thread / queue database. All queries are serialized through a single connection, so allowing multiple threads to connect defeats the purpose of this database.zSQLite must be configured to use the WAL journal mode when using this feature. WAL mode allows one or more readers to continue reading while another connection writes to the database.c                       sr   e Zd Zd f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d Zdd Z  ZS )SqliteQueueDatabaseFr&   Nc                    s   | drttd|d< d|d< | |dd |dd }	tt| }
|
j| _|
j	|f|d|	i| || _
|| _|| _d| _| ||| _|   | j
r|   d S )NZthreadlocalsFZcheck_same_threadjournal_modepragmasT)get
ValueErrorTHREADLOCAL_ERROR_MESSAGE_validate_journal_modepopsuperr4   execute_sql!_SqliteQueueDatabase__execute_sqlr   Z
_autostart_results_timeout_num_readers_is_stoppedget_thread_impl_thread_helper_create_queues_and_workersstart)r   Zdatabase
use_geventZ	autostartZreadersqueue_max_sizeZresults_timeoutargskwargsr6   ZParent	__class__r   r   r   o   s&    



zSqliteQueueDatabase.__init__c                 C   s   |rt S tS r   )GreenletHelperThreadHelper)r   rF   r   r   r   rB      s    z#SqliteQueueDatabase.get_thread_implc                 C   sh   |r|  dkrtt|r^tdd |D }|dd  dkrJttdd |D dg S dgS d S )Nwalc                 s   s   | ]\}}|  |fV  qd S r   )lower.0kvr   r   r   	<genexpr>   s     z=SqliteQueueDatabase._validate_journal_mode.<locals>.<genexpr>r5   c                 S   s    g | ]\}}|d kr||fqS )r5   r   rP   r   r   r   
<listcomp>   s    z>SqliteQueueDatabase._validate_journal_mode.<locals>.<listcomp>)r5   rN   )rO   r8   WAL_MODE_ERROR_MESSAGEdictr7   )r   r5   r6   Zpdictr   r   r   r:      s    z*SqliteQueueDatabase._validate_journal_modec                    sP    j   _ j   _ j j  j _ fddt jD  _	d S )Nc                    s   g | ]} j  jqS r   )rC   thread_read_queue)rQ   _r   targetr   r   rU      s   zBSqliteQueueDatabase._create_queues_and_workers.<locals>.<listcomp>)
rC   queue_write_queuerY   _run_worker_looprX   _writerranger@   _readersr$   r   r[   r   rD      s    z.SqliteQueueDatabase._create_queues_and_workersc                 C   s<   |  }|tkrtd d S td|j | | q d S )Nzworker shutting down.zreceived query %s)r7   r(   loggerinfodebugr   _process_execution)r   r]   async_cursorr   r   r   r_      s    
z$SqliteQueueDatabase._run_worker_loopc              
   C   sP   z|  |j|j|j}W n& tk
r> } zd }W 5 d }~X Y nX d }|||S r   )r>   r   r   r   	Exceptionr    )r   rg   r   r   r   r   r   rf      s    z&SqliteQueueDatabase._process_executionc                 C   s   | j  | j fS r   )r^   ZqsizerY   r$   r   r   r   
queue_size   s    zSqliteQueueDatabase.queue_sizeTc                 C   sD   t | j ||||d kr| jn|d}|r0| jn| j}|| |S )N)r   r   r   r   r   )r   rC   r   r?   r^   rY   put)r   r   r   Zrequire_commitr   r   r]   r   r   r   r=      s    
zSqliteQueueDatabase.execute_sqlc              	   C   sb   | j R | jsW 5 Q R  dS | j  | jD ]}|  q,td d| _W 5 Q R  dS Q R X d S )NFzworkers started.T)
_conn_lockrA   r`   rE   rb   rc   rd   )r   readerr   r   r   rE      s    



zSqliteQueueDatabase.startc              	   C   s   t d | jf | jr&W 5 Q R  dS | jt | jD ]}| jt q8| j	
  | jD ]}|
  qZW 5 Q R  dS Q R X d S )Nzenvironment stop requested.FT)rc   re   rk   rA   r^   rj   r(   rb   rY   r`   join)r   rZ   rl   r   r   r   stop   s    




zSqliteQueueDatabase.stopc              
   C   s$   | j  | jW  5 Q R  S Q R X d S r   )rk   rA   r$   r   r   r   
is_stopped   s    zSqliteQueueDatabase.is_stopped)FFr&   NN)NN)NTN)r	   r
   r   r   rB   r:   rD   r_   rf   ri   r=   rE   rn   ro   __classcell__r   r   rJ   r   r4   n   s       !
	


r4   c                   @   s4   e Zd ZdZdddZdd ZdddZd	d
 ZdS )rM   rG   Nc                 C   s
   || _ d S r   rq   )r   rG   r   r   r   r      s    zThreadHelper.__init__c                 C   s   t  S r   r   r$   r   r   r   r          zThreadHelper.eventc                 C   s    |d k	r|n| j }t|pddS Nr   )maxsize)rG   r   r   Zmax_sizer   r   r   r]      s    zThreadHelper.queuec                 O   s   t |||d}d|_|S )N)r\   rH   rI   T)r   daemon)r   fnrH   rI   rX   r   r   r   rX      s    zThreadHelper.thread)N)N)r	   r
   r   r1   r   r   r]   rX   r   r   r   r   rM      s
   

rM   c                   @   s*   e Zd ZdZdd Zd	ddZdd ZdS )
rL   rq   c                 C   s   t  S r   )GEventr$   r   r   r   r      rr   zGreenletHelper.eventNc                 C   s    |d k	r|n| j }t|pddS rs   )rG   GQueueru   r   r   r   r]      s    zGreenletHelper.queuec                    s    fdd}t |f||S )Nc                     s   t    | |S r   )geventsleep)arR   rw   r   r   wrap  s    z#GreenletHelper.thread.<locals>.wrap)GThread)r   rw   rH   rI   r~   r   r}   r   rX     s    zGreenletHelper.thread)N)r	   r
   r   r1   r   r]   rX   r   r   r   r   rL      s   
rL   )Zloggingweakref	threadingr   r   r   ImportErrorr]   rz   r   r   Zgevent.eventrx   Zgevent.queuery   Zplayhouse.sqlite_extr   Z	getLoggerrc   rh   r   objectr   r9   rV   r4   rM   rL   r   r   r   r   <module>   s.   
G{