o
    Mf                     @   s   d dl 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mZmZ zd dlmZ W n ey=   dZY nw G dd	 d	eZG d
d deZe ZejZdd Zdd ZG dd dZG dd dZdS )    N)local)time)	force_str)settings)	get_stackget_template_infotidy_stacktrace)Jsonc                   @   s   e Zd ZdZdS )SQLQueryTriggeredz+Thrown when template panel triggers a queryN)__name__
__module____qualname____doc__ r   r   c/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/debug_toolbar/panels/sql/tracking.pyr
      s    r
   c                   @   s(   e Zd Zdd Zedd Zdd ZdS )ThreadLocalStatec                 C   s
   d| _ d S )NTenabledselfr   r   r   __init__      
zThreadLocalState.__init__c                 C   s   | j rtS tS N)r   NormalCursorWrapperExceptionCursorWrapperr   r   r   r   Wrapper   s   zThreadLocalState.Wrapperc                 C   s
   || _ d S r   r   )r   vr   r   r   	recording!   r   zThreadLocalState.recordingN)r   r   r   r   propertyr   r   r   r   r   r   r      s
    
r   c                    sJ   t  ds# j _ j _ fdd} fdd}| _| _|S d S )N_djdt_cursorc                        t  j| i | S r   )stater   r   argskwargs
connectionpanelr   r   cursor.   s   zwrap_cursor.<locals>.cursorc                     r    r   )r!   r   _djdt_chunked_cursorr"   r%   r   r   chunked_cursor9   s   z#wrap_cursor.<locals>.chunked_cursor)hasattrr(   r   r*   r)   )r&   r'   r(   r*   r   r%   r   wrap_cursor)   s   
r,   c                 C   s   t | dr| `| `| `d S d S )Nr   )r+   r   r(   r*   )r&   r   r   r   unwrap_cursorC   s
   
r-   c                   @   s    e Zd ZdZd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   d S r   r   r   r(   dbloggerr   r   r   r   P      zExceptionCursorWrapper.__init__c                 C   s   t  r   )r
   r   attrr   r   r   __getattr__S   s   z"ExceptionCursorWrapper.__getattr__N)r   r   r   r   r   r4   r   r   r   r   r   J   s    r   c                   @   st   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdddZ	dddZ
dd Zdd Zdd Zdd Zdd ZdS )r   z*
    Wraps a cursor and logs queries.
    c                 C   s   || _ || _|| _d S r   )r(   r/   r0   r.   r   r   r   r   \   s   
zNormalCursorWrapper.__init__c                 C   s"   t |trd|dd S t|S )Nz'%s''z'')
isinstancestrreplacerepr)r   elementr   r   r   _quote_exprc   s   
zNormalCursorWrapper._quote_exprc                    s:   |s|S t |tr fdd| D S  fdd|D S )Nc                       i | ]
\}}|  |qS r   r;   .0keyvaluer   r   r   
<dictcomp>m       z5NormalCursorWrapper._quote_params.<locals>.<dictcomp>c                       g | ]}  |qS r   r=   )r?   pr   r   r   
<listcomp>n       z5NormalCursorWrapper._quote_params.<locals>.<listcomp>)r6   dictitems)r   paramsr   r   r   _quote_paramsi   s
   
z!NormalCursorWrapper._quote_paramsc                    s   t rt|t 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yJ   Y dS w )Nc                    rD   r   _decode)r?   r:   r   r   r   rF   u   rG   z/NormalCursorWrapper._decode.<locals>.<listcomp>c                    r<   r   rL   r>   r   r   r   rB   y   rC   z/NormalCursorWrapper._decode.<locals>.<dictcomp>)strings_onlyz(encoded string))PostgresJsonr6   dumpsadaptedtuplelistrH   rI   datetimedater   r   UnicodeDecodeError)r   paramCONVERT_TYPESr   r   r   rM   p   s   
zNormalCursorWrapper._decodec                 C   s  t  }z|||W t  }|| d }t d r ttt }ng }d}z
t| |}W n	 t	y7   Y nw t
 }	t| jdd}
| jj}t|dd}t|}||
| jj| j|| |||||||||t d k|  d	|	d
}|dkrz|j}W n |jy   d}Y nw || j|
| ||jd | jjdi | S t  }|| d }t d rttt }ng }d}z
t| |}W n	 t	y   Y nw t
 }	t| jdd}
| jj}t|dd}t|}||
| jj| j|| |||||||||t d k|  d	|	d
}|dkrAz|j}W n |jy/   d}Y nw || j|
| ||jd | jjdi | w )Ni  ENABLE_STACKTRACES aliasdefaultvendorunknownSQL_WARNING_THRESHOLDselect)r]   r[   sqldurationraw_sqlrJ   
raw_params
stacktrace
start_time	stop_timeis_slow	is_selecttemplate_info
postgresql)trans_idtrans_status	iso_levelencodingr   )r   dt_settings
get_configr   reversedr   jsonrP   rM   	TypeErrorr   getattrr/   r&   r7   opslast_executed_queryr(   rK   lowerstrip
startswithisolation_levelInternalErrorupdater0   get_transaction_idget_transaction_statusro   record)r   methodra   rJ   rf   rg   rb   re   _paramsrj   r[   connr]   rn   r   r   r   _record   s   







zNormalCursorWrapper._recordNc                 C      |  | jj||S r   )r   r(   callproc)r   procnamerJ   r   r   r   r         zNormalCursorWrapper.callprocc                 C   r   r   )r   r(   execute)r   ra   rJ   r   r   r   r      r   zNormalCursorWrapper.executec                 C   r   r   )r   r(   executemany)r   ra   
param_listr   r   r   r      r   zNormalCursorWrapper.executemanyc                 C   s   t | j|S r   )ru   r(   r2   r   r   r   r4         zNormalCursorWrapper.__getattr__c                 C   s
   t | jS r   )iterr(   r   r   r   r   __iter__   r   zNormalCursorWrapper.__iter__c                 C   s   | S r   r   r   r   r   r   	__enter__   r1   zNormalCursorWrapper.__enter__c                 C   s   |    d S r   )close)r   typerA   	tracebackr   r   r   __exit__   r   zNormalCursorWrapper.__exit__r   )r   r   r   r   r   r;   rK   rM   r   r   r   r   r4   r   r   r   r   r   r   r   r   W   s    
@
r   )rT   rs   	threadingr   r   django.utils.encodingr   debug_toolbarr   rp   debug_toolbar.utilsr   r   r   psycopg2._jsonr	   rO   ImportError	Exceptionr
   r   r!   r   r,   r-   r   r   r   r   r   r   <module>   s(    