o
    `oi                     @   s   d dl mZmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ dd Zee	ed	d
 Zee	edd Zee	edd ZdS )    )HttpResponseBadRequestJsonResponse)render_to_string)csrf_exempt)render_with_toolbar_languagerequire_show_toolbar)SignedDataForm)SQLSelectFormc                 C   s2   | j dkr| jn| j}t|}| r| S dS )z2Unpack a signed data form, if invalid returns NoneGETN)methodr
   POSTr   is_validverified_data)requestdataZsigned_form r   f/var/www/html/kuke-dev/django-kuke/venv/lib/python3.10/site-packages/debug_toolbar/panels/sql/views.pyget_signed_data
   s
   r   c           
      C   s   t | }|s
tdS t|}| r\|jd }|jd }|j}||| dd |jD }| }W d   n1 s<w   Y  ||	 |jd ||jd d	}t
d
|}	td|	iS tdS )z.Returns the output of the SQL SELECT statementInvalid signatureraw_sqlparamsc                 S      g | ]}|d  qS r   r   .0dr   r   r   
<listcomp>"       zsql_select.<locals>.<listcomp>Ndurationaliasresultsqlr   headersr   z$debug_toolbar/panels/sql_select.htmlcontentForm errors)r   r   r	   r   cleaned_datacursorexecutedescriptionfetchallreformat_sqlr   r   )
r   r   formr"   r   r'   r#   r!   contextr$   r   r   r   
sql_select   s*   



r.   c                 C   s  t | }|s
tdS t|}| r|jd }|jd }|jj}|j9}|dkr2|d| | n|dkr@|d| | n	|d| | d	d
 |j	D }|
 }W d   n1 s_w   Y  || |jd ||jd d}	td|	}
td|
iS tdS )z8Returns the output of the SQL EXPLAIN on the given queryr   r   r   ZsqlitezEXPLAIN QUERY PLAN 
postgresqlzEXPLAIN ANALYZE zEXPLAIN c                 S   r   r   r   r   r   r   r   r   I   r   zsql_explain.<locals>.<listcomp>Nr   r   r    z%debug_toolbar/panels/sql_explain.htmlr$   r%   )r   r   r	   r   r&   
connectionvendorr'   r(   r)   r*   r+   r   r   )r   r   r,   r"   r   r1   r'   r#   r!   r-   r$   r   r   r   sql_explain1   s4   



r2   c              	   C   s  t | }|s
tdS t|}| r|jd }|jd }d}d}d}|j7}z#|d ||| |d |d dd	 |jD }| }W n t	yT   d
}Y nw W d   n1 s_w   Y  |||
 |jd ||jd d}	td|	}
td|
iS tdS )zJReturns the output of running the SQL and getting the profiling statisticsr   r   r   NzSET PROFILING=1zSET PROFILING=0aV  
                    SELECT *
                    FROM information_schema.profiling
                    WHERE query_id = (
                        SELECT query_id
                        FROM information_schema.profiling
                        ORDER BY query_id DESC
                        LIMIT 1
                    )
                    c                 S   r   r   r   r   r   r   r   r   {   r   zsql_profile.<locals>.<listcomp>zDProfiling is either not available or not supported by your database.r   r   )r!   result_errorr"   r   r#   r   z%debug_toolbar/panels/sql_profile.htmlr$   r%   )r   r   r	   r   r&   r'   r(   r)   r*   	Exceptionr+   r   r   )r   r   r,   r"   r   r!   r#   r3   r'   r-   r$   r   r   r   sql_profileX   sH   




r5   N)Zdjango.httpr   r   Zdjango.template.loaderr   Zdjango.views.decorators.csrfr   Zdebug_toolbar.decoratorsr   r   Zdebug_toolbar.formsr   Zdebug_toolbar.panels.sql.formsr	   r   r.   r2   r5   r   r   r   r   <module>   s&    	$