o
    `oi(&                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlm	Z	 d dl
mZmZ zd dlZejjjZejjjZW n% ey]   zd dlmZ d dlmZ W n eyZ   dZdZY nw Y nw ejddd	ZG d
d deZdd Zdd ZG dd dZG dd deZ G dd deZ!dS )    N)perf_counter)	force_str)get_stack_traceget_template_info)Json)STATUS_IN_TRANSACTIONzdebug-toolbar-allow-sqlT)defaultc                   @   s   e Zd ZdZdS )SQLQueryTriggeredz+Thrown when template panel triggers a queryN)__name__
__module____qualname____doc__ r   r   i/var/www/html/kuke-dev/django-kuke/venv/lib/python3.10/site-packages/debug_toolbar/panels/sql/tracking.pyr	      s    r	   c                    sb   t  jtjjjrd S t ds/ j _ j _	d  _
 fdd} fdd}| _| _d S d S )N_djdt_cursorc                     sH    j } j| i |}|d u r|S t rtnt}t|j||j |S N)	_djdt_loggerr   	allow_sqlgetNormalCursorMixinExceptionCursorMixinpatch_cursor_wrapper_with_mixin	__class__cursorargskwargsloggerr   mixin
connectionr   r   r   3   s   
zwrap_cursor.<locals>.cursorc                     sR    j } j| i |}|d ur't|ts't rtnt}t|j	||j
 |S |S r   )r   _djdt_chunked_cursor
isinstanceDjDTCursorWrapperMixinr   r   r   r   r   r   r   r   r   r   r   chunked_cursorC   s   
z#wrap_cursor.<locals>.chunked_cursor)r"   r   djangotestZ	testcasesZ_DatabaseFailurehasattrr   r$   r!   r   )r    r   r$   r   r   r   wrap_cursor#   s   	

r(   c                 C   s   G dd d|| }|S )Nc                   @   s   e Zd ZdS )z:patch_cursor_wrapper_with_mixin.<locals>.DjDTCursorWrapperN)r
   r   r   r   r   r   r   DjDTCursorWrapperT   s    r)   r   )Zbase_wrapperr   r)   r   r   r   r   S   s   r   c                       s   e Zd Z fddZ  ZS )r#   c                    s   t  || || _d S r   )super__init__r   )selfr   dbr   r   r   r   r+   [   s   
zDjDTCursorWrapperMixin.__init__)r
   r   r   r+   __classcell__r   r   r.   r   r#   Z   s    r#   c                   @   s   e Zd ZdZdd ZdS )r   z_
    Wraps a cursor and raises an exception on any operation.
    Used in Templates panel.
    c                 C   s   t  r   )r	   )r,   attrr   r   r   __getattr__g   s   z ExceptionCursorMixin.__getattr__N)r
   r   r   r   r1   r   r   r   r   r   a   s    r   c                       sT   e Zd ZdZdd Zdd Zdd Zd fd	d
	Zd fdd	Z fddZ	  Z
S )r   z*
    Wraps a cursor and logs queries.
    c                    s   t rt|t rt|dr||jS t|dr||jS t|ttfr- fdd|D S t|tr= fdd|	 D S t
j
t
jt
jf}zt|t|| dW S  tyZ   Y dS w )	Nobjadaptedc                    s   g | ]}  |qS r   _decode).0elementr,   r   r   
<listcomp>{   s    z-NormalCursorMixin._decode.<locals>.<listcomp>c                    s   i | ]
\}}|  |qS r   r4   )r6   keyvaluer8   r   r   
<dictcomp>   s    z-NormalCursorMixin._decode.<locals>.<dictcomp>)Zstrings_onlyz(encoded string))PostgresJsonr"   r'   dumpsr2   r3   tuplelistdictitemsdatetimedatetimer   UnicodeDecodeError)r,   paramZCONVERT_TYPESr   r8   r   r5   p   s   


zNormalCursorMixin._decodec                 C   s6   d| j _z| j j| j||W | j| j _S | j| j _w )z0Get the last executed query from the connection.N)r-   r   opsZlast_executed_queryr   r   r,   sqlparamsr   r   r   _last_executed_query   s   z&NormalCursorMixin._last_executed_queryc                 C   s  | j j}| j j}|dkr| j j}|jj}t }z|||W t }	|	| d }
d}tt	 t
| |}W d    n1 s@w   Y  t }|dkrbt|tsbt|tr\|d}n
||}nt|}||| |||
|||tdd|d	}|dkrz|j}W n |jy   d}Y nw |jj}|tkr|tkr| j|}n	| j|}nd }|||jj|d	 | jjd
i | S t }	|	| d }
d}tt	 t
| |}W d    n1 sw   Y  t }|dkrt|tst|tr|d}n
||}nt|}||| |||
|||tdd|d	}|dkr`z|j}W n |jy6   d}Y nw |jj}|tkrS|tkrL| j|}n	| j|}nd }|||jj|d	 | jjd
i | w )N
postgresqli   zutf-8   )skip)	vendoraliasrJ   durationZraw_sqlrK   Z
raw_paramsZ
stacktracetemplate_infounknown)trans_idZtrans_status	iso_levelr   )r-   rR   rQ   r    infoZtransaction_statusr   
contextlibsuppress	TypeErrorjsonr>   r5   r   r"   strbytesdecode	as_stringrL   r   Zisolation_levelZInternalErrorr   r   Zcurrent_transaction_idZnew_transaction_idupdaterecord)r,   methodrJ   rK   rR   rQ   connZinitial_conn_status
start_time	stop_timerS   _paramsrT   r   rW   Zfinal_conn_statusrV   r   r   r   _record   s   



	




	zNormalCursorMixin._recordNc                       |  t j||S r   )rh   r*   callproc)r,   ZprocnamerK   r.   r   r   rj         zNormalCursorMixin.callprocc                    ri   r   )rh   r*   executerI   r.   r   r   rl      rk   zNormalCursorMixin.executec                    ri   r   )rh   r*   executemany)r,   rJ   Z
param_listr.   r   r   rm      rk   zNormalCursorMixin.executemanyr   )r
   r   r   r   r5   rL   rh   rj   rl   rm   r/   r   r   r.   r   r   k   s    Rr   )"rY   ZcontextvarsrC   r\   rE   r   Zdjango.test.testcasesr%   Zdjango.utils.encodingr   Zdebug_toolbar.utilsr   r   ZpsycopgtypesZJsonbr=   ZpqZTransactionStatusZINTRANSr   ImportErrorZpsycopg2._jsonr   Zpsycopg2.extensionsZ
ContextVarr   	Exceptionr	   r(   r   r#   r   r   r   r   r   r   <module>   s:    
0
