o
    ðT·j+  ã                   @   s^  d Z ddlZddlT 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 i de“de“de“de“de“de“de“de“de“de“de“de“de“de“de“de“de“eeeeeeeeeedœ
¥¡ G dd„ deƒZ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(e
ƒZ	G d)d*„ d*eƒZdS )+an  
Peewee integration with APSW, "another python sqlite wrapper".

Project page: https://rogerbinns.github.io/apsw/

APSW is a really neat library that provides a thin wrapper on top of SQLite's
C interface.

Here are just a few reasons to use APSW, taken from the documentation:

* APSW gives all functionality of SQLite, including virtual tables, virtual
  file system, blob i/o, backups and file control.
* Connections can be shared across threads without any additional locking.
* Transactions are managed explicitly by your code.
* APSW can handle nested transactions.
* Unicode is handled correctly.
* APSW is faster.
é    N)Ú*)Ú
EXCEPTIONS)Ú__exception_wrapper__)ÚBooleanField)Ú	DateField)ÚDateTimeField)ÚDecimalField)Ú	TimeFieldÚSQLErrorÚ	BusyErrorÚLockedErrorÚReadOnlyErrorÚIOErrorÚ	FullErrorÚCantOpenErrorÚSchemaChangeErrorÚInterruptErrorÚ
AbortErrorÚPermissionsErrorÚProtocolErrorÚ
EmptyErrorÚExtensionLoadingErrorÚCorruptErrorÚNotADBErrorÚ	AuthError)
ÚFormatErrorÚTooBigErrorÚMismatchErrorÚMisuseErrorÚ
RangeErrorÚNotFoundErrorÚBindingsErrorÚConnectionClosedErrorÚCursorClosedErrorÚThreadingViolationErrorc                       s´   e Zd Zedd„ e ¡  d¡D ƒƒZ‡ fdd„Zdd„ Z	dd	„ Z
d
d„ Z‡ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Z‡  ZS )$ÚAPSWDatabasec                 c   s   � | ]}t |ƒV  qd S ©N)Úint)Ú.0Úi© r*   úU/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/playhouse/apsw_ext.pyÚ	<genexpr>@   s   € zAPSWDatabase.<genexpr>Ú.c                    s"   i | _ tt| ƒj|fi |¤Ž d S r&   )Ú_modulesÚsuperr%   Ú__init__)ÚselfÚdatabaseÚkwargs©Ú	__class__r*   r+   r0   B   s   zAPSWDatabase.__init__c                 C   s*   || j |< |  ¡ s|  ¡  ||¡ d S d S r&   )r.   Ú	is_closedÚ
connectionÚcreatemodule)r1   Úmod_nameÚmod_instr*   r*   r+   Úregister_moduleF   s   
ÿzAPSWDatabase.register_modulec                 C   s   | j |= d S r&   )r.   )r1   r9   r*   r*   r+   Úunregister_moduleK   s   zAPSWDatabase.unregister_modulec                 C   sV   t j| jfi | j¤Ž}| jd ur| t| jd ƒ¡ z|  |¡ W |S    | ¡  ‚ )Niè  )	ÚapswÚ
Connectionr2   Úconnect_paramsÚ_timeoutÚsetbusytimeoutr'   Ú_add_conn_hooksÚclose©r1   Úconnr*   r*   r+   Ú_connectN   s   
ýzAPSWDatabase._connectc                    s   t t| ƒ |¡ |  |¡ d S r&   )r/   r%   rB   Ú_load_modulesrD   r4   r*   r+   rB   Y   s   zAPSWDatabase._add_conn_hooksc                 C   s$   | j  ¡ D ]
\}}| ||¡ q|S r&   )r.   Úitemsr8   )r1   rE   r9   r:   r*   r*   r+   rG   ]   s   zAPSWDatabase._load_modulesc                 C   s4   | j  ¡ D ]\}\}}|fdd„}| ||¡ qd S )Nc                 S   s   | ƒ | j | jfS r&   )ÚstepÚfinalize©Úklassr*   r*   r+   Úmake_aggregated   s   z5APSWDatabase._load_aggregates.<locals>.make_aggregate)Ú_aggregatesrH   Úcreateaggregatefunction)r1   rE   ÚnamerL   Ú
num_paramsrM   r*   r*   r+   Ú_load_aggregatesb   s   ýzAPSWDatabase._load_aggregatesc                 C   s$   | j  ¡ D ]
\}}| ||¡ qd S r&   )Ú_collationsrH   Úcreatecollation)r1   rE   rP   Úfnr*   r*   r+   Ú_load_collationsh   s   ÿzAPSWDatabase._load_collationsc                 C   sB   | j  ¡ D ]\}\}}}|r|fnd}|j|||g|¢R Ž  qd S )Nr*   )Ú
_functionsrH   Úcreatescalarfunction)r1   rE   rP   rU   rQ   ÚdeterministicÚargsr*   r*   r+   Ú_load_functionsl   s   þzAPSWDatabase._load_functionsc                 C   s6   | j  ¡ D ]\}\}}|fdd„}| |||¡ qd S )Nc                 S   s   | ƒ | j | j| j| jfS r&   )rI   rJ   ÚvalueÚinverserK   r*   r*   r+   Úmake_windows   s   ÿz8APSWDatabase._load_window_functions.<locals>.make_window)Ú_window_functionsrH   Úcreate_window_function)r1   rE   rP   rL   rQ   r^   r*   r*   r+   Ú_load_window_functionsq   s   üz#APSWDatabase._load_window_functionsc                 C   s$   |  d¡ | jD ]}| |¡ qd S ©NT)ÚenableloadextensionÚ_extensionsÚloadextension)r1   rE   Ú	extensionr*   r*   r+   Ú_load_extensionsx   s   

ÿzAPSWDatabase._load_extensionsc                 C   s8   | j  |¡ |  ¡ s|  ¡ }| d¡ | |¡ d S d S rb   )rd   Úaddr6   r7   rc   re   )r1   rf   rE   r*   r*   r+   Úload_extension}   s   
ýzAPSWDatabase.load_extensionc                 C   s
   |j  ¡ S r&   )r7   Úlast_insert_rowid©r1   Úcursorr*   r*   r+   Ú_last_insert_rowid„   s   
zAPSWDatabase._last_insert_rowidc                 C   s,   z|j  ¡ W S  ty   |jj  ¡  Y S w r&   )r7   ÚchangesÚAttributeErrorrl   rk   r*   r*   r+   Úrows_affected‡   s
   ÿzAPSWDatabase.rows_affectedc                 C   óh   |   ¡ rtdƒ‚t�  |  ¡ }|j ¡ r	 W d   ƒ dS | d¡ W d   ƒ dS 1 s-w   Y  dS )Nz,Cannot commit, database connection not open.Fzcommit;T©r6   ÚInterfaceErrorr   rl   r7   ÚgetautocommitÚexecute©r1   Úcursr*   r*   r+   Úcommit�   ó   
ý
üûzAPSWDatabase.commitc                 C   rq   )Nz.Cannot rollback, database connection not open.Fz	rollback;Trr   rv   r*   r*   r+   Úrollback˜   ry   zAPSWDatabase.rollback)Ú__name__Ú
__module__Ú__qualname__Útupler=   ÚsqlitelibversionÚsplitÚserver_versionr0   r;   r<   rF   rB   rG   rR   rV   r[   ra   rg   ri   rm   rp   rx   rz   Ú__classcell__r*   r*   r4   r+   r%   ?   s$    r%   c                 C   s   |d urt |ƒS d S r&   )Ústr)ÚsÚvr*   r*   r+   Únh¤   s   ÿr†   c                       s   e Zd Z‡ fdd„Z‡  ZS )r   c                    s(   t t| ƒ |¡}|d ur|rdpdS d S )Né   r   )r/   r   Údb_value)r1   r…   r4   r*   r+   rˆ   ©   s   ÿzBooleanField.db_value)r{   r|   r}   rˆ   r‚   r*   r*   r4   r+   r   ¨   s    r   c                   @   ó   e Zd ZeZdS )r   N©r{   r|   r}   r†   rˆ   r*   r*   r*   r+   r   ®   ó    r   c                   @   r‰   )r	   NrŠ   r*   r*   r*   r+   r	   ±   r‹   r	   c                   @   r‰   )r   NrŠ   r*   r*   r*   r+   r   ´   r‹   r   c                   @   r‰   )r   NrŠ   r*   r*   r*   r+   r   ·   r‹   r   )Ú__doc__r=   Úpeeweer   r   r   Ú_BooleanFieldr   Ú
_DateFieldr   Ú_DateTimeFieldr   Ú_DecimalFieldr	   Ú
_TimeFieldÚupdateÚOperationalErrorÚDatabaseErrorÚ	DataErrorÚIntegrityErrorrs   ÚInternalErrorÚProgrammingErrorÚSqliteDatabaser%   r†   r*   r*   r*   r+   Ú<module>   s~    ÿþýüûúùø	÷
öõôóòñðï
åe