ó
ù¢TQc           @   sO  d  Z  d d l m Z d d l m Z m Z d e f d „  ƒ  YZ d d „ Z	 e
 d d „ Z d d „ Z d d	 „ Z d d
 „ Z d d „ Z d d „ Z d d „ Z e
 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 e f d „  ƒ  YZ d „  Z d d „ Z d d „ Z d d „ Z d S(   s+  
This module implements a transaction manager that can be used to define
transaction handling in a request or view function. It is used by transaction
control middleware and decorators.

The transaction manager can be in managed or in auto state. Auto state means the
system is using a commit-on-save strategy (actually it's more like
commit-on-change). As soon as the .save() or .delete() (or related) methods are
called, a commit is made.

Managed transactions don't do those commits, but will need some kind of manual
or implicit commits or rollbacks.
iÿÿÿÿ(   t   wraps(   t   connectionst   DEFAULT_DB_ALIASt   TransactionManagementErrorc           B   s   e  Z d  Z RS(   s^   
    This exception is thrown when something bad happens with transaction
    management.
    (   t   __name__t
   __module__t   __doc__(    (    (    s.   ../Django//lib/python/django/db/transaction.pyR      s   c         C   s-   |  d k r t }  n  t |  } | j ƒ  d S(   sz  
    Roll back any ongoing transactions and clean the transaction management
    state of the connection.

    This method is to be used only in cases where using balanced
    leave_transaction_management() calls isn't possible. For example after a
    request has finished, the transaction state isn't known, yet the connection
    must be cleaned up for the next request.
    N(   t   NoneR   R   t   abort(   t   usingt
   connection(    (    s.   ../Django//lib/python/django/db/transaction.pyR      s    
	
c         C   s0   | d k r t } n  t | } | j |  ƒ d S(   s‚  
    Enters transaction management for a running thread. It must be balanced with
    the appropriate leave_transaction_management call, since the actual state is
    managed as a stack.

    The state and dirty flag are carried over from the surrounding block or
    from the settings, if there is no surrounding block (dirty is always false
    when no current block is running).
    N(   R   R   R   t   enter_transaction_management(   t   managedR	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   *   s    
	
c         C   s-   |  d k r t }  n  t |  } | j ƒ  d S(   sÝ   
    Leaves transaction management for a running thread. A dirty flag is carried
    over to the surrounding block, as a commit will commit all changes, even
    those from outside. (Commits are on connection level.)
    N(   R   R   R   t   leave_transaction_management(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   9   s    	
c         C   s)   |  d k r t }  n  t |  } | j ƒ  S(   s^   
    Returns True if the current transaction requires a commit for changes to
    happen.
    N(   R   R   R   t   is_dirty(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   D   s    	
c         C   s-   |  d k r t }  n  t |  } | j ƒ  d S(   s¾   
    Sets a dirty flag for the current thread and code streak. This can be used
    to decide in a managed block of code to decide whether there are open
    changes waiting for commit.
    N(   R   R   R   t	   set_dirty(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   N   s    	
c         C   s-   |  d k r t }  n  t |  } | j ƒ  d S(   s¹   
    Resets a dirty flag for the current thread and code streak. This can be used
    to decide in a managed block of code to decide whether a commit or rollback
    should happen.
    N(   R   R   R   t	   set_clean(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   Y   s    	
c         C   s-   |  d  k r t }  n  t |  } | j ƒ  d  S(   N(   R   R   R   t   clean_savepoints(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   d   s    	
c         C   s)   |  d k r t }  n  t |  } | j ƒ  S(   sO   
    Checks whether the transaction manager is in manual or in auto state.
    N(   R   R   R   t
   is_managed(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   j   s    	
c         C   s0   | d k r t } n  t | } | j |  ƒ d S(   sõ   
    Puts the transaction manager into a manual state: managed transactions have
    to be committed explicitly by the user. If you switch off transaction
    management and there is a pending commit/rollback, the data will be
    commited.
    N(   R   R   R   R   (   t   flagR	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   s   s    	
c         C   s-   |  d k r t }  n  t |  } | j ƒ  d S(   sK   
    Commits changes if the system is not in managed transaction mode.
    N(   R   R   R   t   commit_unless_managed(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR      s    	
c         C   s-   |  d k r t }  n  t |  } | j ƒ  d S(   sN   
    Rolls back changes if the system is not in managed transaction mode.
    N(   R   R   R   t   rollback_unless_managed(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   ˆ   s    	
c         C   s-   |  d k r t }  n  t |  } | j ƒ  d S(   s;   
    Does the commit itself and resets the dirty flag.
    N(   R   R   R   t   commit(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   ‘   s    	
c         C   s-   |  d k r t }  n  t |  } | j ƒ  d S(   sK   
    This function does the rollback itself and resets the dirty flag.
    N(   R   R   R   t   rollback(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   š   s    	
c         C   s)   |  d k r t }  n  t |  } | j ƒ  S(   sÑ   
    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.
    N(   R   R   R   t	   savepoint(   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   £   s    	
c         C   s0   | d k r t } n  t | } | j |  ƒ d S(   sq   
    Rolls back the most recent savepoint (if one exists). Does nothing if
    savepoints are not supported.
    N(   R   R   R   t   savepoint_rollback(   t   sidR	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   ®   s    	
c         C   s0   | d k r t } n  t | } | j |  ƒ d S(   sn   
    Commits the most recent savepoint (if one exists). Does nothing if
    savepoints are not supported.
    N(   R   R   R   t   savepoint_commit(   R   R	   R
   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   ¸   s    	
t   Transactionc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s¯  
    Acts as either a decorator, or a context manager.  If it's a decorator it
    takes a function and returns a wrapped function.  If it's a contextmanager
    it's used with the ``with`` statement.  In either event entering/exiting
    are called before and after, respectively, the function/block is executed.

    autocommit, commit_on_success, and commit_manually contain the
    implementations of entering and exiting.
    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   enteringt   exitingR	   (   t   selfR   R   R	   (    (    s.   ../Django//lib/python/django/db/transaction.pyt   __init__Ð   s    		c         C   s   |  j  |  j ƒ d  S(   N(   R   R	   (   R   (    (    s.   ../Django//lib/python/django/db/transaction.pyt	   __enter__Õ   s    c         C   s   |  j  | |  j ƒ d  S(   N(   R   R	   (   R   t   exc_typet	   exc_valuet	   traceback(    (    s.   ../Django//lib/python/django/db/transaction.pyt   __exit__Ø   s    c            s"   t  ˆ ƒ ‡  ‡ f d †  ƒ } | S(   Nc             s   ˆ   ˆ |  | Ž  SWd  QXd  S(   N(    (   t   argst   kwargs(   R   t   func(    s.   ../Django//lib/python/django/db/transaction.pyt   innerÜ   s    (   R    (   R   R(   R)   (    (   R   R(   s.   ../Django//lib/python/django/db/transaction.pyt   __call__Û   s    (   R   R   R   R    R!   R%   R*   (    (    (    s.   ../Django//lib/python/django/db/transaction.pyR   Æ   s
   				c         C   sG   | d k r t } n  t | ƒ r7 t |  | t ƒ | ƒ St |  | | ƒ S(   sø  
    Takes 3 things, an entering function (what to do to start this block of
    transaction management), an exiting function (what to do to end it, on both
    success and failure, and using which can be: None, indiciating using is
    DEFAULT_DB_ALIAS, a callable, indicating that using is DEFAULT_DB_ALIAS and
    to return the function already wrapped.

    Returns either a Transaction objects, which is both a decorator and a
    context manager, or a wrapped function, if using is a callable.
    N(   R   R   t   callableR   (   R   R   R	   (    (    s.   ../Django//lib/python/django/db/transaction.pyt   _transaction_funcâ   s
    	c         C   s"   d „  } d „  } t  | | |  ƒ S(   só   
    Decorator that activates commit on save. This is Django's default behavior;
    this decorator is useful if you globally activated transaction management in
    your settings file and want the default behavior in some view functions.
    c         S   s'   t  d t d |  ƒ t t d |  ƒd  S(   NR   R	   (   R   t   FalseR   (   R	   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   ý   s    c         S   s   t  d | ƒ d  S(   NR	   (   R   (   R#   R	   (    (    s.   ../Django//lib/python/django/db/transaction.pyR     s    (   R,   (   R	   R   R   (    (    s.   ../Django//lib/python/django/db/transaction.pyt
   autocommit÷   s    		c         C   s"   d „  } d „  } t  | | |  ƒ S(   s  
    This decorator activates commit on response. This way, if the view function
    runs successfully, a commit is made; if the viewfunc produces an exception,
    a rollback is made. This is one of the most common ways to do transaction
    control in Web apps.
    c         S   s!   t  d |  ƒ t t d |  ƒd  S(   NR	   (   R   R   t   True(   R	   (    (    s.   ../Django//lib/python/django/db/transaction.pyR     s    c         S   s„   zo |  d  k	 r1 t d | ƒ rn t d | ƒ qn n= t d | ƒ rn y t d | ƒ Wqn t d | ƒ ‚  qn Xn  Wd  t d | ƒ Xd  S(   NR	   (   R   R   R   R   R   (   R#   R	   (    (    s.   ../Django//lib/python/django/db/transaction.pyR     s    (   R,   (   R	   R   R   (    (    s.   ../Django//lib/python/django/db/transaction.pyt   commit_on_success  s    		c         C   s"   d „  } d „  } t  | | |  ƒ S(   sô   
    Decorator that activates manual transaction control. It just disables
    automatic transaction control and doesn't do any commit/rollback of its
    own -- it's up to the user to call the commit and rollback functions
    themselves.
    c         S   s!   t  d |  ƒ t t d |  ƒd  S(   NR	   (   R   R   R/   (   R	   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   )  s    c         S   s   t  d | ƒ d  S(   NR	   (   R   (   R#   R	   (    (    s.   ../Django//lib/python/django/db/transaction.pyR   -  s    (   R,   (   R	   R   R   (    (    s.   ../Django//lib/python/django/db/transaction.pyt   commit_manually"  s    		N(    R   t	   functoolsR    t	   django.dbR   R   t	   ExceptionR   R   R   R/   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   objectR   R,   R.   R0   R1   (    (    (    s.   ../Django//lib/python/django/db/transaction.pyt   <module>   s0   
					
	