o
    gr(                     @   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   a/var/www/html/bloggers_ems/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test	testcases_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   )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   r1   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   r4   r   r   r   r   r   a   s    r   c                       sd   e Zd ZdZdd Zd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                 C   s"   t |trd|dd S t|S )Nz'%s''z'')r"   strreplacerepr)r/   elementr   r   r   _quote_exprp   s   
zNormalCursorMixin._quote_exprc                    sZ   |s|S t |tr fdd| D S t |tr$t fdd|D S  fdd|D S )Nc                       i | ]
\}}|  |qS r   r:   .0keyvaluer/   r   r   
<dictcomp>z       z3NormalCursorMixin._quote_params.<locals>.<dictcomp>c                 3   s    | ]}  |V  qd S r   r<   r>   prA   r   r   	<genexpr>|   s    z2NormalCursorMixin._quote_params.<locals>.<genexpr>c                       g | ]}  |qS r   r<   rD   rA   r   r   
<listcomp>}       z3NormalCursorMixin._quote_params.<locals>.<listcomp>)r"   dictitemstuple)r/   paramsr   rA   r   _quote_paramsv   s   

zNormalCursorMixin._quote_paramsc                    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                    rG   r   _decode)r>   r9   rA   r   r   rH      rI   z-NormalCursorMixin._decode.<locals>.<listcomp>c                    r;   r   rQ   r=   rA   r   r   rB      rC   z-NormalCursorMixin._decode.<locals>.<dictcomp>)strings_onlyz(encoded string))PostgresJsonr"   r)   dumpsrO   rP   rL   listrJ   rK   datetimedatetimer   UnicodeDecodeError)r/   paramCONVERT_TYPESr   rA   r   rR      s   


zNormalCursorMixin._decodec                 C   s<   d| j _z| j j| j|| |W | j| j _S | j| j _w )z0Get the last executed query from the connection.N)r0   r   opslast_executed_queryr   rN   r   r/   sqlrM   r   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aliasr`   durationraw_sqlrM   
raw_params
stacktracetemplate_infounknown)trans_idtrans_status	iso_levelr   )r0   rg   rf   r    infotransaction_statusr   
contextlibsuppress	TypeErrorjsonrU   rR   r   r"   r6   bytesdecode	as_stringra   r   isolation_levelInternalErrorr   r   current_transaction_idnew_transaction_idupdaterecord)r/   methodr`   rM   rg   rf   conninitial_conn_status
start_time	stop_timerh   _paramsrl   r   rp   final_conn_statusrn   r   r   r   _record   s   



	




	zNormalCursorMixin._recordNc                       |  t j||S r   )r   r-   callproc)r/   procnamerM   r1   r   r   r         zNormalCursorMixin.callprocc                    r   r   )r   r-   executer_   r1   r   r   r      r   zNormalCursorMixin.executec                    r   r   )r   r-   executemany)r/   r`   
param_listr1   r   r   r      r   zNormalCursorMixin.executemanyr   )r
   r   r   r   r:   rN   rR   ra   r   r   r   r   r2   r   r   r1   r   r   k   s    	Rr   )"rs   contextvarsrW   rv   rY   r   django.test.testcasesr%   django.utils.encodingr   debug_toolbar.utilsr   r   psycopgtypesJsonbrT   pqTransactionStatusINTRANSr   ImportErrorpsycopg2._jsonr   psycopg2.extensions
ContextVarr   	Exceptionr	   r*   r   r#   r   r   r   r   r   r   <module>   s:    
0
