
 Xf,                 @   sI  d  d l  m Z m Z m Z m Z m Z d  d l m Z Gd d   d e  Z d d d  Z	 d d d	  Z
 d d
 d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z Gd d   d e  Z d d  d! d"  Z d# d$   Z d d% d&  Z d S)'    )DEFAULT_DB_ALIASDatabaseErrorErrorProgrammingErrorconnections)ContextDecoratorc               @   s   e  Z d  Z d Z d S)TransactionManagementErrorzR
    This exception is thrown when transaction management is used improperly.
    N)__name__
__module____qualname____doc__ r   r   C/home/ubuntu/projects/ifolica/build/django/django/db/transaction.pyr      s   r   Nc             C   s   |  d k r t  }  n  t |  S)z
    Get a database connection by name, or the default database connection
    if no name is provided. This is a private API.
    N)r   r   )usingr   r   r   get_connection   s    	r   c             C   s   t  |   j   S)z6
    Get the autocommit status of the connection.
    )r   get_autocommit)r   r   r   r   r      s    r   c             C   s   t  |  j |   S)z6
    Set the autocommit status of the connection.
    )r   set_autocommit)Z
autocommitr   r   r   r   r      s    r   c             C   s   t  |   j   d S)z 
    Commits a transaction.
    N)r   commit)r   r   r   r   r   &   s    r   c             C   s   t  |   j   d S)z#
    Rolls back a transaction.
    N)r   rollback)r   r   r   r   r   -   s    r   c             C   s   t  |   j   S)z
    Creates a savepoint (if supported and required by the backend) inside the
    current transaction. Returns an identifier for the savepoint that will be
    used for the subsequent rollback or commit.
    )r   	savepoint)r   r   r   r   r   4   s    r   c             C   s   t  |  j |   d S)zq
    Rolls back the most recent savepoint (if one exists). Does nothing if
    savepoints are not supported.
    N)r   savepoint_rollback)sidr   r   r   r   r   =   s    r   c             C   s   t  |  j |   d S)zn
    Commits the most recent savepoint (if one exists). Does nothing if
    savepoints are not supported.
    N)r   savepoint_commit)r   r   r   r   r   r   E   s    r   c             C   s   t  |   j   d S)zR
    Resets the counter used to generate unique savepoint ids in this thread.
    N)r   clean_savepoints)r   r   r   r   r   M   s    r   c             C   s   t  |   j   S)zD
    Gets the "needs rollback" flag -- for *advanced use* only.
    )r   get_rollback)r   r   r   r   r   T   s    r   c             C   s   t  |  j |   S)a  
    Sets or unsets the "needs rollback" flag -- for *advanced use* only.

    When `rollback` is `True`, it triggers a rollback when exiting the
    innermost enclosing atomic block that has `savepoint=True` (that's the
    default). Use this to force a rollback without raising an exception.

    When `rollback` is `False`, it prevents such a rollback. Use this only
    after rolling back to a known-good state! Otherwise, you break the atomic
    block and data corruption may occur.
    )r   set_rollback)r   r   r   r   r   r   [   s    r   c             C   s   t  |  j |   d S)z
    Register `func` to be called when the current transaction is committed.
    If the current transaction is rolled back, `func` will not be called.
    N)r   	on_commit)funcr   r   r   r   r   j   s    r   c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	Atomica  
    This class guarantees the atomic execution of a given block.

    An instance can be used either as a decorator or as a context manager.

    When it's used as a decorator, __call__ wraps the execution of the
    decorated function in the instance itself, used as a context manager.

    When it's used as a context manager, __enter__ creates a transaction or a
    savepoint, depending on whether a transaction is already in progress, and
    __exit__ commits the transaction or releases the savepoint on normal exit,
    and rolls back the transaction or to the savepoint on exceptions.

    It's possible to disable the creation of savepoints if the goal is to
    ensure that some code runs within a transaction without creating overhead.

    A stack of savepoints identifiers is maintained as an attribute of the
    connection. None denotes the absence of a savepoint.

    This allows reentrancy even if the same AtomicWrapper is reused. For
    example, it's possible to define `oa = @atomic('other')` and use `@oa` or
    `with oa:` multiple times.

    Since database connections are thread-local, this is thread-safe.

    This is a private API.
    c             C   s   | |  _  | |  _ d  S)N)r   r   )selfr   r   r   r   r   __init__   s    	zAtomic.__init__c             C   s   t  |  j  } | j si d | _ d | _ | j   si | j j rQ t d   n  d | _ d | _ qi n  | j r |  j	 r | j r | j	   } | j
 j |  q | j
 j d   n | j d d d d | _ d  S)NTFzgYour database backend doesn't behave properly when autocommit is off. Turn it on before using 'atomic'.Z.force_begin_transaction_with_broken_autocommit)r   r   in_atomic_blockcommit_on_exitneeds_rollbackr   featuresZ"autocommits_when_autocommit_is_offr   r   savepoint_idsappendr   )r   
connectionr   r   r   r   	__enter__   s"    					zAtomic.__enter__c             C   s0  t  |  j  } | j r* | j j   } n	 d | _ z| j rB n| d  k r.| j r.| j r | d  k	 r+y | j |  Wq t k
 r y | j	 |  | j |  Wn t
 k
 r d | _ Yn X  Yq Xq+qy | j   Wqt k
 r*y | j   Wn t
 k
 r"| j   Yn X  YqXn d | _ | j r| d  k rXd | _ qy | j	 |  | j |  Wqt
 k
 rd | _ YqXn- y | j   Wn t
 k
 r| j   Yn XWd  | j s| j rd  | _ q+| j d  n5 | j r+| j r+| j rd  | _ q+d | _ n  Xd  S)NFT)r   r   r%   popr!   Zclosed_in_transactionr#   r   r   r   r   r   r   closer'   r   r"   )r   exc_type	exc_value	tracebackr'   r   r   r   r   __exit__   s`    									zAtomic.__exit__N)r	   r
   r   r   r    r(   r.   r   r   r   r   r   v   s   $r   Tc             C   s0   t  |   r t t |  |   St |  |  Sd  S)N)callabler   r   )r   r   r   r   r   atomic  s    r0   c             C   s9   y |  j  j |  Wn t k
 r4 | h |  _  Yn X|  S)N)_non_atomic_requestsaddAttributeError)viewr   r   r   r   r1     s
    r1   c                sB   t     r t   t  S  d  k r. t   n    f d d   Sd  S)Nc                s   t  |     S)N)r1   )r4   )r   r   r   <lambda>,  s    z%non_atomic_requests.<locals>.<lambda>)r/   r1   r   )r   r   )r   r   non_atomic_requests&  s
    	r6   )Z	django.dbr   r   r   r   r   Zdjango.utils.decoratorsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r0   r1   r6   r   r   r   r   <module>   s$   (
	
