o
    [h                     @   sL   d Z ddlZddlmZ ddlmZ G dd dejZG d	d
 d
ejZdS )a8  Provides a thread-local transactional wrapper around the root Engine class.

The ``threadlocal`` module is invoked when using the
``strategy="threadlocal"`` flag with :func:`~sqlalchemy.engine.create_engine`.
This module is semi-private and is invoked automatically when the threadlocal
engine strategy is used.
    N   )base   )utilc                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	TLConnectionc                    s    t t| j|i | d| _d S Nr   )superr   __init___TLConnection__opencount)selfargkw	__class__ /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/engine/threadlocal.pyr	      s   
zTLConnection.__init__c                 C   s   |  j d7  _ | S Nr   )r
   r   r   r   r   _increment_connect   s   zTLConnection._increment_connectc                 C   s(   | j dkrtj|  |  j d8  _ d S r   r
   r   
Connectioncloser   r   r   r   r      s   
zTLConnection.closec                 C   s   d| _ tj|  d S r   r   r   r   r   r   _force_close$   s   zTLConnection._force_close)__name__
__module____qualname__r	   r   r   r   __classcell__r   r   r   r   r      s
    r   c                       s   e Zd ZdZeZedd fd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 Zdd Zdd Z fddZedd Zdd  Zd!d" Z  ZS )$TLEnginezPAn Engine that includes support for thread-local managed
    transactions.

    z1.3a!  The 'threadlocal' engine strategy is deprecated, and will be removed in a future release.  The strategy is no longer relevant to modern usage patterns (including that of the ORM :class:`.Session` object) which make use of a :class:`_engine.Connection` object in order to invoke statements.c                    s&   t t| j|i | tj | _d S N)r   r   r	   r   	threadinglocal_connections)r   argskwargsr   r   r   r	   1   s   
zTLEngine.__init__c                 K   s   | j di |S )Nr   )_contextual_connect)r   r   r   r   r   contextual_connect>   s   zTLEngine.contextual_connectc                 K   s`   t | jds	d }n| j }|d u s|jr,| j| | | jj|fi |}t	|| j_|
 S Nconn)hasattrr!   r'   closed_tl_connection_clsZ_wrap_pool_connectpoolconnectweakrefrefr   )r   r   
connectionr   r   r   r$   A   s   
zTLEngine._contextual_connectNc                 C   s2   t | jds
g | j_| jj|  j|d | S )Ntrans)xid)r(   r!   r0   appendr$   begin_twophase)r   r1   r   r   r   r3   S   s   zTLEngine.begin_twophasec                 C   .   t | jds
g | j_| jj|    | S Nr0   )r(   r!   r0   r2   r$   begin_nestedr   r   r   r   r6   [   s   
zTLEngine.begin_nestedc                 C   r4   r5   )r(   r!   r0   r2   r$   beginr   r   r   r   r7   c   s   zTLEngine.beginc                 C   s   | S r   r   r   r   r   r   	__enter__i   s   zTLEngine.__enter__c                 C   s    |d u r
|    d S |   d S r   )commitrollback)r   type_value	tracebackr   r   r   __exit__l   s   zTLEngine.__exit__c                 C   s,   t | jdr
| jjsd S | jjd   d S Nr0   )r(   r!   r0   preparer   r   r   r   rA   r   s   
zTLEngine.preparec                 C   2   t | jdr
| jjsd S | jjd}|  d S r?   )r(   r!   r0   popr9   r   r0   r   r   r   r9   z      
zTLEngine.commitc                 C   rB   r?   )r(   r!   r0   rC   r:   rD   r   r   r   r:      rE   zTLEngine.rollbackc                    s   t j | _tt|   d S r   )r   r   r    r!   r   r   disposer   r   r   r   rF      s   zTLEngine.disposec                 C   s(   t | jd p| j d u p| j jS r&   )r(   r!   r'   r)   r   r   r   r   r)      s
   
zTLEngine.closedc                 C   s:   | j s|    | j }|  | j`g | j_d S d S r   )r)   r$   r   r!   r'   r   r0   )r   r/   r   r   r   r      s   
zTLEngine.closec                 C   s
   d| j  S )NzTLEngine(%r))urlr   r   r   r   __repr__   s   
zTLEngine.__repr__r   )r   r   r   __doc__r   r*   r   
deprecatedr	   r%   r$   r3   r6   r7   r8   r>   rA   r9   r:   rF   propertyr)   r   rH   r   r   r   r   r   r   )   s.    	
		
r   )	rI   r-    r   r   r   r   ZEnginer   r   r   r   r   <module>   s   