a
    ތxd                     @   st   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 eedd Zeedd	 Zeed
d ZdS )    )HttpResponseBadRequestJsonResponse)render_to_string)csrf_exempt)require_show_toolbar)SQLSelectFormc           	      C   s   t | jp
d}| r|jd }|jd }|j4}||| dd |jD }| }W d   n1 sj0    Y  || |jd ||jd d}t	d	|}t
d
|iS tdS )z.Returns the output of the SQL SELECT statementNraw_sqlparamsc                 S   s   g | ]}|d  qS r    .0dr   r   V/var/www/html/Ranjet/env/lib/python3.9/site-packages/debug_toolbar/panels/sql/views.py
<listcomp>       zsql_select.<locals>.<listcomp>durationaliasresultsqlr   headersr   z$debug_toolbar/panels/sql_select.htmlcontentForm errors)r   POSTis_validcleaned_datacursorexecutedescriptionfetchallreformat_sqlr   r   r   )	requestformr   r	   r   r   r   contextr   r   r   r   
sql_select	   s"    

&
r%   c           
      C   s   t | jp
d}| r|jd }|jd }|jj}|jr}|dkrV|d|| n.|dkrr|d|| n|d|| d	d
 |j	D }|
 }W d   n1 s0    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 queryNr   r	   ZsqlitezEXPLAIN QUERY PLAN {}
postgresqlzEXPLAIN ANALYZE {}z
EXPLAIN {}c                 S   s   g | ]}|d  qS r
   r   r   r   r   r   r   7   r   zsql_explain.<locals>.<listcomp>r   r   r   z%debug_toolbar/panels/sql_explain.htmlr   r   )r   r   r   r   
connectionvendorr   r   formatr   r    r!   r   r   r   )
r"   r#   r   r	   r(   r   r   r   r$   r   r   r   r   sql_explain#   s,    

&
r*   c           
   	   C   s   t | jp
d}| r|jd }|jd }d}d}d}|jn}zF|d ||| |d |d dd |jD }| }W n ty   d	}Y n0 W d   n1 s0    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 statisticsNr   r	   z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   s   g | ]}|d  qS r
   r   r   r   r   r   r   e   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    	Exceptionr!   r   r   r   )
r"   r#   r   r	   r   r   r+   r   r$   r   r   r   r   sql_profileF   s<    



&
r-   N)Zdjango.httpr   r   Zdjango.template.loaderr   Zdjango.views.decorators.csrfr   Zdebug_toolbar.decoratorsr   Zdebug_toolbar.panels.sql.formsr   r%   r*   r-   r   r   r   r   <module>   s   !