B
    þ¦†\Š  ã               @   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||Ž d| _d S )Nr   )Úsuperr   Ú__init__Ú_TLConnection__opencount)ÚselfÚargÚkw)Ú	__class__© ú<lib/python3.7/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 )Nr   )r	   r   Ú
ConnectionÚclose)r
   r   r   r   r      s    
zTLConnection.closec             C   s   d| _ tj | ¡ d S )Nr   )r	   r   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:`.Connection` object in order to invoke statements.c                s"   t t| ƒj||Ž tj ¡ | _d S )N)r   r   r   r   Ú	threadingÚlocalÚ_connections)r
   ÚargsÚkwargs)r   r   r   r   1   s    	zTLEngine.__init__c             K   s   | j f |ŽS )N)Ú_contextual_connect)r
   r   r   r   r   Úcontextual_connect=   s    zTLEngine.contextual_connectc             K   s\   t | jdƒsd }n
| j ¡ }|d ks*|jrT| j| |  | jj|¡f|Ž}t 	|¡| j_| 
¡ S )NÚconn)Úhasattrr   r    ÚclosedÚ_tl_connection_clsZ_wrap_pool_connectZpoolZconnectÚweakrefÚrefr   )r
   r   Ú
connectionr   r   r   r   @   s    
zTLEngine._contextual_connectNc             C   s2   t | jdƒsg | j_| jj |  ¡ j|d¡ | S )NÚtrans)Úxid)r!   r   r'   Úappendr   Úbegin_twophase)r
   r(   r   r   r   r*   R   s
    zTLEngine.begin_twophasec             C   s.   t | jdƒsg | j_| jj |  ¡  ¡ ¡ | S )Nr'   )r!   r   r'   r)   r   Úbegin_nested)r
   r   r   r   r+   Z   s
    zTLEngine.begin_nestedc             C   s.   t | jdƒsg | j_| jj |  ¡  ¡ ¡ | S )Nr'   )r!   r   r'   r)   r   Úbegin)r
   r   r   r   r,   b   s    zTLEngine.beginc             C   s   | S )Nr   )r
   r   r   r   Ú	__enter__h   s    zTLEngine.__enter__c             C   s   |d kr|   ¡  n|  ¡  d S )N)ÚcommitÚrollback)r
   Ztype_ÚvalueÚ	tracebackr   r   r   Ú__exit__k   s    
zTLEngine.__exit__c             C   s,   t | jdƒr| jjsd S | jjd  ¡  d S )Nr'   éÿÿÿÿ)r!   r   r'   Úprepare)r
   r   r   r   r4   q   s    zTLEngine.preparec             C   s2   t | jdƒr| jjsd S | jj d¡}| ¡  d S )Nr'   r3   )r!   r   r'   Úpopr.   )r
   r'   r   r   r   r.   y   s
    zTLEngine.commitc             C   s2   t | jdƒr| jjsd S | jj d¡}| ¡  d S )Nr'   r3   )r!   r   r'   r5   r/   )r
   r'   r   r   r   r/   ‚   s
    zTLEngine.rollbackc                s   t j ¡ | _tt| ƒ ¡  d S )N)r   r   r   r   r   r   Údispose)r
   )r   r   r   r6   ‹   s    zTLEngine.disposec             C   s(   t | jdƒ p&| j ¡ d kp&| j ¡ jS )Nr    )r!   r   r    r"   )r
   r   r   r   r"      s    zTLEngine.closedc             C   s6   | j s2|  ¡  ¡  | j ¡ }| ¡  | j`g | j_d S )N)r"   r   r   r   r    r   r'   )r
   r&   r   r   r   r   —   s    
zTLEngine.closec             C   s
   d| j  S )NzTLEngine(%r))Zurl)r
   r   r   r   Ú__repr__Ÿ   s    zTLEngine.__repr__)N)r   r   r   Ú__doc__r   r#   r   Z
deprecatedr   r   r   r*   r+   r,   r-   r2   r4   r.   r/   r6   Úpropertyr"   r   r7   r   r   r   )r   r   r   )   s&   

		r   )	r8   r$   Ú r   r   r   r   ZEnginer   r   r   r   r   Ú<module>   s
   