o
    Mf                     @   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 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)require_show_toolbarsigned_data_view)SQLSelectFormc           
      C   s   t |}| rR|jd }|jd }|j}||| dd |jD }| }W d   n1 s2w   Y  || |jd ||jd d}td	|}	t	d
|	iS t
dS )z.Returns the output of the SQL SELECT statementraw_sqlparamsc                 S      g | ]}|d  qS r    .0dr   r   `/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/debug_toolbar/panels/sql/views.py
<listcomp>       zsql_select.<locals>.<listcomp>Ndurationaliasresultsqlr   headersr   z$debug_toolbar/panels/sql_select.htmlcontentForm errors)r   is_validcleaned_datacursorexecutedescriptionfetchallreformat_sqlr   r   r   )
requestverified_dataformr   r
   r   r   r   contextr   r   r   r   
sql_select	   s$   



r'   c                 C   s   t |}| ru|jd }|jd }|jj}|j9}|dkr(|d|| n|dkr6|d|| n	|d|| dd	 |jD }|	 }W d
   n1 sUw   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
   sqlitezEXPLAIN QUERY PLAN {}
postgresqlzEXPLAIN ANALYZE {}z
EXPLAIN {}c                 S   r   r   r   r   r   r   r   r   9   r   zsql_explain.<locals>.<listcomp>Nr   r   r   z%debug_toolbar/panels/sql_explain.htmlr   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   r   sql_explain$   s.   



r-   c              	   C   s   t |}| rv|jd }|jd }d}d}d}|j7}z#|d ||| |d |d dd |jD }| }W n tyJ   d	}Y nw W d   n1 sUw   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
   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   h   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!   	Exceptionr"   r   r   r   )r#   r$   r%   r   r
   r   r   r.   r   r&   r   r   r   r   sql_profileH   sB   




r0   N)django.httpr   r   django.template.loaderr   django.views.decorators.csrfr   debug_toolbar.decoratorsr   r   debug_toolbar.panels.sql.formsr   r'   r-   r0   r   r   r   r   <module>   s"    !