î
à^Q\   ã               @   s²   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
 d  d l m Z e e
 d d „  ƒ ƒ Z e e
 d	 d
 „  ƒ ƒ Z e e
 d d „  ƒ ƒ Z d S)é    )Úabsolute_importÚunicode_literals)ÚHttpResponseBadRequest)ÚSimpleTemplateResponse)Úcsrf_exempt)Úrequire_show_toolbar)ÚSQLSelectFormc             C   sÔ   t  |  j p d ƒ } | j ƒ  rÊ | j d } | j d } | j } | j | | ƒ d d „  | j Dƒ } | j ƒ  } | j ƒ  i | d 6| j	 ƒ  d 6| j d d 6| d	 6| j d
 d
 6} t
 d | ƒ St d ƒ S)z.Returns the output of the SQL SELECT statementNÚraw_sqlÚparamsc             S   s   g  |  ] } | d  ‘ q S)r   © )Ú.0Údr   r   úR/var/www/dbchiro/venv/build/django-debug-toolbar/debug_toolbar/panels/sql/views.pyú
<listcomp>   s   	 zsql_select.<locals>.<listcomp>ÚresultÚsqlÚdurationÚheadersÚaliasz$debug_toolbar/panels/sql_select.htmlzForm errors)r   ÚPOSTÚis_validÚcleaned_dataÚcursorÚexecuteÚdescriptionÚfetchallÚcloseÚreformat_sqlr   r   )ÚrequestÚformr   r
   r   r   r   Úcontextr   r   r   Ú
sql_select   s"    	
r!   c       	      C   s3  t  |  j p d ƒ } | j ƒ  r)| j d } | j d } | j j } | j } | d k rv | j d | f | ƒ n= | d k rœ | j d | f | ƒ n | j d | f | ƒ d	 d
 „  | j Dƒ } | j	 ƒ  } | j
 ƒ  i | d 6| j ƒ  d 6| j d d 6| d 6| j d d 6} t d | ƒ St d ƒ S)z8Returns the output of the SQL EXPLAIN on the given queryNr	   r
   ZsqlitezEXPLAIN QUERY PLAN %sZ
postgresqlzEXPLAIN ANALYZE %sz
EXPLAIN %sc             S   s   g  |  ] } | d  ‘ q S)r   r   )r   r   r   r   r   r   ;   s   	 zsql_explain.<locals>.<listcomp>r   r   r   r   r   z%debug_toolbar/panels/sql_explain.htmlzForm errors)r   r   r   r   Ú
connectionÚvendorr   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   s3  t  |  j p d ƒ } | j ƒ  r)| j d } | j d } | j } d } d } d } y] | j d ƒ | j | | ƒ | j d ƒ | j d ƒ d d „  | j Dƒ } | j ƒ  } Wn t k
 rÍ d	 } Yn X| j	 ƒ  i | d
 6| d 6| j
 ƒ  d 6| j d d 6| d 6| j d d 6} t d | ƒ St d ƒ S)zJReturns the output of running the SQL and getting the profiling statisticsNr	   r
   zSET PROFILING=1zSET PROFILING=0zÓ
  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   g   s   	 zsql_profile.<locals>.<listcomp>zDProfiling is either not available or not supported by your database.r   Úresult_errorr   r   r   r   z%debug_toolbar/panels/sql_profile.htmlzForm errors)r   r   r   r   r   r   r   r   Ú	Exceptionr   r   r   r   )	r   r   r   r
   r   r   r   r%   r    r   r   r   Úsql_profileJ   s8    		
r'   N)Ú
__future__r   r   Zdjango.httpr   Zdjango.template.responser   Zdjango.views.decorators.csrfr   Zdebug_toolbar.decoratorsr   Zdebug_toolbar.panels.sql.formsr   r!   r$   r'   r   r   r   r   Ú<module>   s   $