
    Xf0                         d dl mZmZ d dlmZmZmZmZmZ  G d de      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edd       ZddZ G d de      ZddZd ZddZy)    )ContextDecoratorcontextmanager)DEFAULT_DB_ALIASDatabaseErrorErrorProgrammingErrorconnectionsc                       e Zd ZdZy)TransactionManagementErrorz*Transaction management is used improperly.N)__name__
__module____qualname____doc__     <D:\switchATM\venv\Lib\site-packages\django/db/transaction.pyr   r      s    4r   r   Nc                 $    | t         } t        |    S )z
    Get a database connection by name, or the default database connection
    if no name is provided. This is a private API.
    )r   r	   usings    r   get_connectionr      s    
 } ur   c                 4    t        |       j                         S )z,Get the autocommit status of the connection.)r   get_autocommitr   s    r   r   r      s    % //11r   c                 6    t        |      j                  |       S )z,Set the autocommit status of the connection.)r   set_autocommit)
autocommitr   s     r   r   r   !   s    % //
;;r   c                 6    t        |       j                          y)zCommit a transaction.N)r   commitr   s    r   r   r   &   s    5  "r   c                 6    t        |       j                          y)zRoll back a transaction.N)r   rollbackr   s    r   r   r   +   s    5""$r   c                 4    t        |       j                         S )z
    Create a savepoint (if supported and required by the backend) inside the
    current transaction. Return an identifier for the savepoint that will be
    used for the subsequent rollback or commit.
    )r   	savepointr   s    r   r!   r!   0   s     % **,,r   c                 8    t        |      j                  |        y)zn
    Roll back the most recent savepoint (if one exists). Do nothing if
    savepoints are not supported.
    N)r   savepoint_rollbacksidr   s     r   r#   r#   9   s    
 5,,S1r   c                 8    t        |      j                  |        y)zk
    Commit the most recent savepoint (if one exists). Do nothing if
    savepoints are not supported.
    N)r   savepoint_commitr$   s     r   r'   r'   A   s    
 5**3/r   c                 6    t        |       j                          y)zQ
    Reset the counter used to generate unique savepoint ids in this thread.
    N)r   clean_savepointsr   s    r   r)   r)   I   s     5**,r   c                 4    t        |       j                         S )z9Get the "needs rollback" flag -- for *advanced use* only.)r   get_rollbackr   s    r   r+   r+   P   s    % --//r   c                 6    t        |      j                  |       S )a  
    Set or unset the "needs rollback" flag -- for *advanced use* only.

    When `rollback` is `True`, trigger 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`, prevent 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   s     r   r-   r-   U   s     % --h77r   c              #      K   	 d y# t         $ r+}t        |       }|j                  rd|_        ||_         d}~ww xY ww)ab  
    Internal low-level utility to mark a transaction as "needs rollback" when
    an exception is raised while not enforcing the enclosed block to be in a
    transaction. This is needed by Model.save() and friends to avoid starting a
    transaction when in autocommit mode and a single query is executed.

    It's equivalent to:

        connection = get_connection(using)
        if connection.get_autocommit():
            yield
        else:
            with transaction.atomic(using=using, savepoint=False):
                yield

    but it uses low-level utilities to avoid performance overhead.
    NT)	Exceptionr   in_atomic_blockneeds_rollbackrollback_exc)r   exc
connections      r   mark_for_rollback_on_errorr5   d   sC     & #E*
%%(,J%&)J#s   A 	 A 	=&8=A c                 :    t        |      j                  | |       y)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   robusts      r   r7   r7      s    
 5##D&1r   c                   "    e Zd ZdZd Zd Zd Zy)Atomica  
    Guarantee 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.

    An atomic block can be tagged as durable. In this case, raise a
    RuntimeError if it's nested within another atomic block. This guarantees
    that database changes in a durable block are committed to the database when
    the block exists without error.

    This is a private API.
    c                 <    || _         || _        || _        d| _        y NF)r   r!   durable_from_testcase)selfr   r!   r>   s       r   __init__zAtomic.__init__   s    
"#r   c                 r   t        | j                        }| j                  r0|j                  r$|j                  d   j                  st        d      |j                  s,d|_        d|_        |j                         sd|_        d|_        |j                  r`| j                  r8|j                  s,|j                         }|j                  j                  |       n6|j                  j                  d        n|j                  dd       d|_        |j                  r|j                  j                  |        y y )NzDA durable atomic block cannot be nested within another atomic block.TF).force_begin_transaction_with_broken_autocommit)r   r   r>   atomic_blocksr?   RuntimeErrorr0   commit_on_exitr1   r   r!   savepoint_idsappendr   )r@   r4   r%   s      r   	__enter__zAtomic.__enter__   s   #DJJ/
 LL((,,R0??   ))(,J%(-J%,,. .2
*,1
)%%
 ~~j&?&? **,((//4((//5%%d &  *.J&%%$$++D1 &r   c                 4   t        | j                        }|j                  r|j                  j	                          |j
                  r|j
                  j	                         }nd|_        	 |j                  rn|?|j                  s3|j                  rw	 |j                  |       nd	 |j                          nRd|_        |j                  r.d|_        n5	 |j                  |       |j                  |       n	 |j                          |j                  s&|j                  rd |_        y |j!                  d       y |j
                  s)|j"                  s|j                  rd |_        y d|_        y y y # t        $ r; 	 |j                  |       |j                  |        # t        $ r
 d|_        Y  w xY ww xY w# t        $ r2 	 |j                           # t        $ r |j                          Y  w xY ww xY w# t        $ r
 d|_        Y w xY w# t        $ r |j                          Y w xY w# |j                  s&|j                  rd |_        w |j!                  d       w |j
                  s)|j"                  s|j                  rd |_        w d|_        w w w xY w)NFT)r   r   r0   rE   poprH   closed_in_transactionr1   r'   r   r#   r   r   r   closer4   r   rG   )r@   exc_type	exc_value	tracebackr4   r%   s         r   __exit__zAtomic.__exit__   s   #DJJ/
%%$$((*##**..0C */J&M	7// !**C*C--"&77<	"))+ -2
)-- {48
1	=&99#> '77<+"++- --33,0J)--d3--j6O6O33,0J)16J.	 7P-{  - "	A * = =c B !+ ; ;C @ " $) A =A
 9!A"  ) /&//1
 	  % / ',,.	/.  % = 9=J5	= ! + #((*+ --33,0J)--d3--j6O6O33,0J)16J.	 7P-s   +)H. E3 &H. (F: 8H. "G8 9H. ;H 3	F7="F F7 F30F72F33F77H. :	G5GG5G1.G50G11G55H. 8HH. 
HH. H+'H. *H++H. .A)JN)r   r   r   r   rA   rJ   rR   r   r   r   r;   r;      s    B$(2TY7r   r;   c                 `    t        |       r t        t        ||      |       S t        | ||      S N)callabler;   r   )r   r!   r>   s      r   atomicrV   <  s3     ;v&	7;EBB eY00r   c                 n    	 | j                   j                  |       | S # t        $ r |h| _         Y | S w xY wrT   )_non_atomic_requestsaddAttributeErrorviewr   s     r   rX   rX   F  sB    ,!!%%e, K  ,%*G!K,s    44c                 T     t               rt         t              S  t          fdS )Nc                     t        |       S rT   )rX   r[   s    r   <lambda>z%non_atomic_requests.<locals>.<lambda>T  s    0u= r   )rU   rX   r   r   s   `r   non_atomic_requestsr`   N  s(    #E+;<<=$E==r   rT   r=   )NTF)
contextlibr   r   	django.dbr   r   r   r   r	   r   r   r   r   r   r   r!   r#   r'   r)   r+   r-   r5   r7   r;   rV   rX   r`   r   r   r   <module>rc      s    7 	!1 	2
<
#
%
-20-0
8  82k7 k7\1>r   