
    ]j                        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 	 d dlmZ  G d d	e      Z G d
 de      Z e       Zej,                  Zd Zd Z G d d      Z G d d      Zy# e$ r dZY Mw xY w)    N)local)time)	force_str)settings)	get_stackget_template_infotidy_stacktrace)Jsonc                       e Zd ZdZy)SQLQueryTriggeredz+Thrown when template panel triggers a queryN)__name__
__module____qualname____doc__     K/root/env/lib/python3.12/site-packages/debug_toolbar/panels/sql/tracking.pyr   r      s    5r   r   c                   (    e Zd Zd Zed        Zd Zy)ThreadLocalStatec                     d| _         y )NTenabledselfs    r   __init__zThreadLocalState.__init__   s	    r   c                 2    | j                   rt        S t        S N)r   NormalCursorWrapperExceptionCursorWrapperr   s    r   WrapperzThreadLocalState.Wrapper   s    <<&&%%r   c                     || _         y r   r   )r   vs     r   	recordingzThreadLocalState.recording!   s	    r   N)r   r   r   r   propertyr    r#   r   r   r   r   r      s      & &
r   r   c                      t         d      s> j                   _         j                   _         fd} fd}| _        | _        |S y )N_djdt_cursorc                  R    t         j                   j                  | i |      S r   )stater    r&   argskwargs
connectionpanels     r   cursorzwrap_cursor.<locals>.cursor.   s/     =='
''88*e r   c                  R    t         j                   j                  | i |      S r   )r(   r    _djdt_chunked_cursorr)   s     r   chunked_cursorz#wrap_cursor.<locals>.chunked_cursor9   s-    ==/
//@@*e r   )hasattrr.   r&   r1   r0   )r,   r-   r.   r1   s   ``  r   wrap_cursorr3   )   sO    :~.","3"3
*4*C*C
'			
 #
$2
!- /r   c                 *    t        | d      r| `| `| `y y )Nr&   )r2   r&   r.   r1   )r,   s    r   unwrap_cursorr5   C   s#    z>*#% +r   c                       e Zd ZdZd Zd Zy)r   z_
    Wraps a cursor and raises an exception on any operation.
    Used in Templates panel.
    c                      y r   r   r   r.   dbloggers       r   r   zExceptionCursorWrapper.__init__P   s    r   c                     t               r   )r   r   attrs     r   __getattr__z"ExceptionCursorWrapper.__getattr__S   s    !!r   N)r   r   r   r   r   r>   r   r   r   r   r   J   s    
"r   r   c                   \    e Zd ZdZd Zd Zd Zd Zd ZddZ	dd	Z
d
 Zd Zd Zd Zd Zy)r   z*
    Wraps a cursor and logs queries.
    c                 .    || _         || _        || _        y r   )r.   r9   r:   r8   s       r   r   zNormalCursorWrapper.__init__\   s    r   c                 b    t        |t              rd|j                  dd      z  S t        |      S )Nz'%s''z'')
isinstancestrreplacerepr)r   elements     r   _quote_exprzNormalCursorWrapper._quote_exprc   s,    gs#GOOC666= r   c                     |s|S t        |t              r3|j                         D ci c]  \  }}|| j                  |       c}}S |D cg c]  }| j                  |       c}S c c}}w c c}w r   )rC   dictitemsrH   )r   paramskeyvalueps        r   _quote_paramsz!NormalCursorWrapper._quote_paramsi   sd    Mfd#CI<<>RZS%C))%00RR-34  #44 S4s   A'A-c                    t         r+t        |t               r|j                  |j                        S t        |t        t
        f      r|D cg c]  }| j                  |       c}S t        |t              r3|j                         D ci c]  \  }}|| j                  |       c}}S t        j                  t        j                  t        j                  f}	 t        |t        ||             S c c}w c c}}w # t        $ r Y yw xY w)N)strings_onlyz(encoded string))PostgresJsonrC   dumpsadaptedtuplelist_decoderJ   rK   datetimedater   r   UnicodeDecodeError)r   paramrG   rM   rN   CONVERT_TYPESs         r   rX   zNormalCursorWrapper._decodep   s    Jul;;;u}}--eeT]+9>?gDLL)?? eT"?D{{}MeCe,,MM "**HMM8==I	&UZ}5U1UVV @ N " 	&%	&s   C2
C7C= =	D	D	c                    t               }	  |||      t               }||z
  dz  }t        j                         d   rt        t	        t                           }ng }d}	 t        j                  | j                  |            }t               }	t        | j                  dd      }
| j                  j                  }t        |dd      }t        |      }||
| j                  j                  j!                  | j"                  || j%                  |            ||||||||t        j                         d   kD  |j'                         j)                         j+                  d	      |	d
}|dk(  rT	 |j,                  }|j1                  | j2                  j5                  |
      |j7                         ||j8                  d        | j2                  j:                  di | S # t        $ r Y bw xY w# |j.                  $ r d}Y w xY w# t               }||z
  dz  }t        j                         d   rt        t	        t                           }ng }d}	 t        j                  | j                  |            }n# t        $ r Y nw xY wt               }	t        | j                  dd      }
| j                  j                  }t        |dd      }t        |      }||
| j                  j                  j!                  | j"                  || j%                  |            ||||||||t        j                         d   kD  |j'                         j)                         j+                  d	      |	d
}|dk(  rl	 |j,                  }n# |j.                  $ r d}Y nw xY w|j1                  | j2                  j5                  |
      |j7                         ||j8                  d        | j2                  j:                  di | w xY w)Ni  ENABLE_STACKTRACES aliasdefaultvendorunknownSQL_WARNING_THRESHOLDselect)rc   ra   sqldurationraw_sqlrL   
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   jsonrT   rX   	TypeErrorr   getattrr9   r,   rD   opslast_executed_queryr.   rP   lowerstrip
startswithisolation_levelInternalErrorupdater:   get_transaction_idget_transaction_statusru   record)r   methodrg   rL   rl   rm   rh   rk   _paramsrp   ra   connrc   rt   s                 r   _recordzNormalCursorWrapper._record   s   V
<	)#v&I!J.$6H%%'(<=,Xik-BC

G**T\\&%9: ./MDGGWi8E77%%DT8Y7F c(C !ww{{66KKd&8&8&@ %!$((&#k&<&<&>?V&WW YY[..0;;HE!.F$ %* $ 4 4I $(KK$B$B5$I(,(C(C(E%.$(MM	 DKK((a  H )) * )I*[ I!J.$6H%%'(<=,Xik-BC

G**T\\&%9: -/MDGGWi8E77%%DT8Y7F c(C !ww{{66KKd&8&8&@ %!$((&#k&<&<&>?V&WW YY[..0;;HE!.F$ %* $ 4 4I)) * )I*$(KK$B$B5$I(,(C(C(E%.$(MM	 DKK((sz   H  $G)G)	G&%G&)G=<G= AO.$I10O.1	I=:O.<I==C(O.&M32O.3NO.NA'O.Nc                 P    | j                  | j                  j                  ||      S r   )r   r.   callproc)r   procnamerL   s      r   r   zNormalCursorWrapper.callproc   s    ||DKK00(FCCr   c                 P    | j                  | j                  j                  ||      S r   )r   r.   execute)r   rg   rL   s      r   r   zNormalCursorWrapper.execute   s    ||DKK//f==r   c                 P    | j                  | j                  j                  ||      S r   )r   r.   executemany)r   rg   
param_lists      r   r   zNormalCursorWrapper.executemany   s    ||DKK33S*EEr   c                 .    t        | j                  |      S r   )r{   r.   r<   s     r   r>   zNormalCursorWrapper.__getattr__   s    t{{D))r   c                 ,    t        | j                        S r   )iterr.   r   s    r   __iter__zNormalCursorWrapper.__iter__   s    DKK  r   c                     | S r   r   r   s    r   	__enter__zNormalCursorWrapper.__enter__   s    r   c                 $    | j                          y r   )close)r   typerN   	tracebacks       r   __exit__zNormalCursorWrapper.__exit__   s    

r   r   )r   r   r   r   r   rH   rP   rX   r   r   r   r   r>   r   r   r   r   r   r   r   r   W   sF    !5&$>)@D>F*!r   r   )rY   ry   	threadingr   r   django.utils.encodingr   debug_toolbarr   rv   debug_toolbar.utilsr   r   r	   psycopg2._jsonr
   rS   ImportError	Exceptionr   r   r(   r#   r3   r5   r   r   r   r   r   <module>r      s        + 1 M M3
		 	u  	OO	4&
" 
"~ ~U  Ls   A6 6B ?B 