o
    ѷ6i                     @  sb   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ G dd	 d	ZdS )
    )annotationsN)wraps)TracebackType)Any)connections)DEFAULT_DB_ALIASc                   @  sZ   e Zd ZdZef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 S )&override_mysql_variablesa7  
    Based on Django's override_settings, but for connection settings. Give a
    connection alias in using and variable=value pairs to save on that
    connection. Keeps the old values MySQL-side using session variables of the
    form @overridden_X.

    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.
    usingstroptions	str | intreturnNonec                 K  s,   || _ || _t jddd d | _d S )N-    )dbr   uuiduuid1hexreplaceprefix)selfr	   r    r   \/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/django_mysql/test/utils.py__init__   s    z!override_mysql_variables.__init__c                 C     |    d S N)enable)r   r   r   r   	__enter__   s   z"override_mysql_variables.__enter__exc_typetype[BaseException] | None	exc_valueBaseException | Noneexc_tracebackTracebackType | Nonec                 C  r   r   )disable)r   r    r"   r$   r   r   r   __exit__!   s   z!override_mysql_variables.__exit__	test_funcr   c                   s\   ddl m} ttr t|std jj 	 S t
d
 fdd	}|S )Nr   )TestCasez${} only works with TestCase classes.argsr   kwargsr   c                    s6     | i |W  d    S 1 sw   Y  d S r   r   )r*   r+   r   r(   r   r   inner9   s   $z0override_mysql_variables.__call__.<locals>.inner)r*   r   r+   r   r   r   )unittestr)   
isinstancetype
issubclass	TypeErrorformat	__class____name__
wrap_classr   )r   r(   r)   r-   r   r,   r   __call__)   s   


z!override_mysql_variables.__call__klass	type[Any]c                 C  sX   d| j i| j}t|D ]}|dsqt||}| jdi ||}t||| qd S )Nr	   test_r   )r   r   dir
startswithgetattrr4   setattr)r   r8   r+   namemethodwrappedr   r   r   r6   @   s   

z#override_mysql_variables.wrap_classc                 C  sb   t | j   }| j D ]\}}|dj| j|d|f qW d    d S 1 s*w   Y  d S )NziSET @overridden_{prefix}_{name} = @@{name},
                           @@{name} = %s
                    )r   r?   )r   r   cursorr   itemsexecuter3   r   )r   rB   keyvaluer   r   r   r   L   s   "zoverride_mysql_variables.enablec                 C  sV   t | j  }| jD ]}|dj|| jd qW d    d S 1 s$w   Y  d S )Nz~SET @@{name} = @overridden_{prefix}_{name},
                           @overridden_{prefix}_{name} = NULL
                    )r?   r   )r   r   rB   r   rD   r3   r   )r   rB   rE   r   r   r   r&   X   s   
"z override_mysql_variables.disableN)r	   r
   r   r   r   r   )r   r   )r    r!   r"   r#   r$   r%   r   r   )r(   r   r   r   )r8   r9   r   r   )r5   
__module____qualname____doc__r   r   r   r'   r7   r6   r   r&   r   r   r   r   r      s    




r   )
__future__r   r   	functoolsr   typesr   typingr   	django.dbr   django.db.utilsr   r   r   r   r   r   <module>   s    