U
    µ©Þa-  ã                   @   sò   d dl mZmZ d dlmZmZmZmZmZ G d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d0dd„Zd1dd„Zd2dd„Zd3dd„Zed4dd„ƒZd5dd„ZG d d!„ d!eƒZd6d#d$„Zd%d&„ Zd7d'd(„ZdS )8é    )ÚContextDecoratorÚcontextmanager)ÚDEFAULT_DB_ALIASÚDatabaseErrorÚErrorÚProgrammingErrorÚconnectionsc                   @   s   e Zd ZdZdS )ÚTransactionManagementErrorz*Transaction management is used improperly.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú„/home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/django/db/transaction.pyr	      s   r	   Nc                 C   s   | dkr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.
    N)r   r   ©Úusingr   r   r   Úget_connection   s    r   c                 C   s   t | ƒ ¡ S )z,Get the autocommit status of the connection.)r   Úget_autocommitr   r   r   r   r      s    r   c                 C   s   t |ƒ | ¡S )z,Set the autocommit status of the connection.)r   Úset_autocommit)Z
autocommitr   r   r   r   r      s    r   c                 C   s   t | ƒ ¡  dS )zCommit a transaction.N)r   Úcommitr   r   r   r   r   !   s    r   c                 C   s   t | ƒ ¡  dS )zRoll back a transaction.N)r   Úrollbackr   r   r   r   r   &   s    r   c                 C   s   t | ƒ ¡ 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   r   r   r   r   +   s    r   c                 C   s   t |ƒ | ¡ dS )zn
    Roll back the most recent savepoint (if one exists). Do nothing if
    savepoints are not supported.
    N)r   Úsavepoint_rollback©Úsidr   r   r   r   r   4   s    r   c                 C   s   t |ƒ | ¡ dS )zk
    Commit the most recent savepoint (if one exists). Do nothing if
    savepoints are not supported.
    N)r   Úsavepoint_commitr   r   r   r   r   <   s    r   c                 C   s   t | ƒ ¡  dS )zQ
    Reset the counter used to generate unique savepoint ids in this thread.
    N)r   Úclean_savepointsr   r   r   r   r   D   s    r   c                 C   s   t | ƒ ¡ S )z9Get the "needs rollback" flag -- for *advanced use* only.)r   Úget_rollbackr   r   r   r   r   K   s    r   c                 C   s   t |ƒ | ¡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   r   r   r   r   P   s    r   c                 c   s:   z
dV  W n* t k
r4   t| ƒ}|jr.d|_‚ Y nX dS )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)r   Ú
connectionr   r   r   Úmark_for_rollback_on_error_   s    
r#   c                 C   s   t |ƒ | ¡ 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$   {   s    r$   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú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.

    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s0d|_d|_| ¡ s0d|_d|_|jrf| jrX|jsX| ¡ }|j |¡ qz|j d ¡ n|j	ddd d|_d S )NTF)Z.force_begin_transaction_with_broken_autocommit)
r   r   r    Úcommit_on_exitr!   r   r   Úsavepoint_idsÚappendr   )r)   r"   r   r   r   r   Ú	__enter__¨   s    
zAtomic.__enter__c                 C   sÐ  t | jƒ}|jr|j ¡ }nd|_zZ|jr0nL|d krø|j	sø|jr¨|d k	röz| 
|¡ W qö tk
r¤   z| |¡ | 
|¡ W n tk
rœ   d|_	Y nX ‚ Y qöX nNz| ¡  W n@ tk
rô   z| ¡  W n tk
rì   | ¡  Y nX ‚ Y nX n„d|_	|jrP|d krd|_	n6z| |¡ | 
|¡ W n tk
rL   d|_	Y nX n,z| ¡  W n tk
rz   | ¡  Y nX W 5 |js¤|jr˜d |_n
| d¡ n&|jsÊ|jsÊ|jrÄd |_nd|_X d S )NFT)r   r   r,   Úpopr    Zclosed_in_transactionr"   r   r+   r!   r   r   r   r   r   r   Úclose)r)   Úexc_typeÚ	exc_valueÚ	tracebackr"   r   r   r   r   Ú__exit__Ä   s`    





zAtomic.__exit__N)r
   r   r   r   r*   r.   r4   r   r   r   r   r&   ‡   s   r&   Tc                 C   s$   t | ƒrtt|ƒ| ƒS t| |ƒS d S r'   )Úcallabler&   r   r(   r   r   r   Úatomic  s    r6   c                 C   s2   z| j  |¡ W n tk
r,   |h| _ Y nX | S r'   )Ú_non_atomic_requestsÚaddÚAttributeError)Úviewr   r   r   r   r7   '  s
    r7   c                    s.   t ˆ ƒrtˆ tƒS ˆ d krt‰ ‡ fdd„S d S )Nc                    s
   t | ˆ ƒS r'   )r7   )r:   r   r   r   Ú<lambda>5  ó    z%non_atomic_requests.<locals>.<lambda>)r5   r7   r   r   r   r   r   Únon_atomic_requests/  s
    
r=   )N)N)N)N)N)N)N)N)N)N)N)N)N)NT)N)Ú
contextlibr   r   Z	django.dbr   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r#   r$   r&   r6   r7   r=   r   r   r   r   Ú<module>   s*   






	





 

