a
    Ig                     @   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   Z/var/www/html/django/DPS/env/lib/python3.9/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4}||| dd |jD }| }W d   n1 sx0    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   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r}|dkrd|d| | n.|dkr|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 queryr   r   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   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   r0   r&   r"   r    r,   r#   r   r   r   sql_explain1   s2    

&
r1   c              	   C   s
  t | }|stdS t|}| 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 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   s   g | ]}|d  qS 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"   r2   r&   r,   r#   r   r   r   sql_profileX   sB    




&
r4   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-   r1   r4   r   r   r   r   <module>   s$   	$