a
    ތxd                     @   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yz   dZY n0 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   Y/var/www/html/Ranjet/env/lib/python3.9/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__   s    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!   s    zThreadLocalState.recordingN)r   r   r   r   propertyr   r   r   r   r   r   r      s   
r   c                    sJ   t  dsF j _ j _ fdd} fdd}| _| _|S d S )N_djdt_cursorc                     s   t  j| i | S r   )stater   r   argskwargs
connectionpanelr   r   cursor.   s    zwrap_cursor.<locals>.cursorc                     s   t  j| i | S 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 )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   s    zExceptionCursorWrapper.__init__c                 C   s
   t  d S r   )r
   r   attrr   r   r   __getattr__S   s    z"ExceptionCursorWrapper.__getattr__N)r   r   r   r   r   r1   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-   r.   r,   r   r   r   r   \   s    zNormalCursorWrapper.__init__c                 C   s&   t |trd|dd S t|S d 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                    s   i | ]\}}|  |qS r   r8   .0keyvaluer   r   r   
<dictcomp>m       z5NormalCursorWrapper._quote_params.<locals>.<dictcomp>c                    s   g | ]}  |qS r   r9   )r;   pr   r   r   
<listcomp>n   r?   z5NormalCursorWrapper._quote_params.<locals>.<listcomp>)r3   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rZ fdd| D S tjtj	tj
f}zt|t|| dW S  ty   Y dS 0 d S )Nc                    s   g | ]}  |qS r   _decode)r;   r7   r   r   r   rA   u   r?   z/NormalCursorWrapper._decode.<locals>.<listcomp>c                    s   i | ]\}}|  |qS r   rF   r:   r   r   r   r>   y   r?   z/NormalCursorWrapper._decode.<locals>.<dictcomp>)Zstrings_onlyz(encoded string))PostgresJsonr3   dumpsZadaptedtuplelistrB   rC   datetimedater   r   UnicodeDecodeError)r   paramZCONVERT_TYPESr   r   r   rG   p   s    
zNormalCursorWrapper._decodec                 C   s  t  }zh|||W t  }|| d }t d rBttt }ng }d}zt| |}W n t	yp   Y n0 t
 }	t| jdd}
| jj}t|dd}t|}||
| jj| j|| |||||||||t d k|  d	|	d
}|dkrDz
|j}W n |jy    d}Y n0 || j|
| ||jd | jjf i | S g }d}zt| |}W n t	y   Y n0 t
 }	t| jdd}
| jj}t|dd}t|}||
| jj| j|| |||||||||t d k|  d	|	d
}|dkr\z
|j}W n |jy8   d}Y n0 || j|
| ||jd | jjf i | nHt  }|| d }t d rttt }ng }d}zt| |}W n t	y   Y n0 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0 || j|
| ||jd | jjf i | 0 d S )Ni  ZENABLE_STACKTRACES aliasdefaultvendorunknownZSQL_WARNING_THRESHOLDselect)rS   rQ   sqldurationZraw_sqlrD   Z
raw_params
stacktrace
start_time	stop_timeZis_slowZ	is_selecttemplate_info
postgresql)Ztrans_idZtrans_status	iso_levelencoding)r   dt_settingsZ
get_configr   reversedr   jsonrI   rG   	TypeErrorr   getattrr-   r$   r4   opsZlast_executed_queryr&   rE   lowerstrip
startswithZisolation_levelZInternalErrorupdater.   Zget_transaction_idZget_transaction_statusr^   record)r   methodrV   rD   rY   rZ   rW   rX   _paramsr[   rQ   connrS   r]   r   r   r   _record   s   





    









zNormalCursorWrapper._recordNc                 C   s   |  | jj||S r   )rm   r&   callproc)r   ZprocnamerD   r   r   r   rn      s    zNormalCursorWrapper.callprocc                 C   s   |  | jj||S r   )rm   r&   execute)r   rV   rD   r   r   r   ro      s    zNormalCursorWrapper.executec                 C   s   |  | jj||S r   )rm   r&   executemany)r   rV   Z
param_listr   r   r   rp      s    zNormalCursorWrapper.executemanyc                 C   s   t | j|S r   )rc   r&   r/   r   r   r   r1      s    zNormalCursorWrapper.__getattr__c                 C   s
   t | jS r   )iterr&   r   r   r   r   __iter__   s    zNormalCursorWrapper.__iter__c                 C   s   | S r   r   r   r   r   r   	__enter__   s    zNormalCursorWrapper.__enter__c                 C   s   |    d S r   )close)r   typer=   	tracebackr   r   r   __exit__   s    zNormalCursorWrapper.__exit__)N)N)r   r   r   r   r   r8   rE   rG   rm   rn   ro   rp   r1   rr   rs   rw   r   r   r   r   r   W   s   @

r   )rL   ra   	threadingr   r   Zdjango.utils.encodingr   Zdebug_toolbarr   r_   Zdebug_toolbar.utilsr   r   r   Zpsycopg2._jsonr	   rH   ImportError	Exceptionr
   r   r   r   r*   r+   r   r   r   r   r   r   <module>   s$   
