Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from __future__ import absolute_import, unicode_literals
  2. from django.http import HttpResponseBadRequest
  3. from django.template.response import SimpleTemplateResponse
  4. from django.views.decorators.csrf import csrf_exempt
  5. from debug_toolbar.decorators import require_show_toolbar
  6. from debug_toolbar.panels.sql.forms import SQLSelectForm
  7. @csrf_exempt
  8. @require_show_toolbar
  9. def sql_select(request):
  10. """Returns the output of the SQL SELECT statement"""
  11. form = SQLSelectForm(request.POST or None)
  12. if form.is_valid():
  13. sql = form.cleaned_data['raw_sql']
  14. params = form.cleaned_data['params']
  15. cursor = form.cursor
  16. cursor.execute(sql, params)
  17. headers = [d[0] for d in cursor.description]
  18. result = cursor.fetchall()
  19. cursor.close()
  20. context = {
  21. 'result': result,
  22. 'sql': form.reformat_sql(),
  23. 'duration': form.cleaned_data['duration'],
  24. 'headers': headers,
  25. 'alias': form.cleaned_data['alias'],
  26. }
  27. # Using SimpleTemplateResponse avoids running global context processors.
  28. return SimpleTemplateResponse('debug_toolbar/panels/sql_select.html', context)
  29. return HttpResponseBadRequest('Form errors')
  30. @csrf_exempt
  31. @require_show_toolbar
  32. def sql_explain(request):
  33. """Returns the output of the SQL EXPLAIN on the given query"""
  34. form = SQLSelectForm(request.POST or None)
  35. if form.is_valid():
  36. sql = form.cleaned_data['raw_sql']
  37. params = form.cleaned_data['params']
  38. vendor = form.connection.vendor
  39. cursor = form.cursor
  40. if vendor == 'sqlite':
  41. # SQLite's EXPLAIN dumps the low-level opcodes generated for a query;
  42. # EXPLAIN QUERY PLAN dumps a more human-readable summary
  43. # See https://www.sqlite.org/lang_explain.html for details
  44. cursor.execute("EXPLAIN QUERY PLAN %s" % (sql,), params)
  45. elif vendor == 'postgresql':
  46. cursor.execute("EXPLAIN ANALYZE %s" % (sql,), params)
  47. else:
  48. cursor.execute("EXPLAIN %s" % (sql,), params)
  49. headers = [d[0] for d in cursor.description]
  50. result = cursor.fetchall()
  51. cursor.close()
  52. context = {
  53. 'result': result,
  54. 'sql': form.reformat_sql(),
  55. 'duration': form.cleaned_data['duration'],
  56. 'headers': headers,
  57. 'alias': form.cleaned_data['alias'],
  58. }
  59. # Using SimpleTemplateResponse avoids running global context processors.
  60. return SimpleTemplateResponse('debug_toolbar/panels/sql_explain.html', context)
  61. return HttpResponseBadRequest('Form errors')
  62. @csrf_exempt
  63. @require_show_toolbar
  64. def sql_profile(request):
  65. """Returns the output of running the SQL and getting the profiling statistics"""
  66. form = SQLSelectForm(request.POST or None)
  67. if form.is_valid():
  68. sql = form.cleaned_data['raw_sql']
  69. params = form.cleaned_data['params']
  70. cursor = form.cursor
  71. result = None
  72. headers = None
  73. result_error = None
  74. try:
  75. cursor.execute("SET PROFILING=1") # Enable profiling
  76. cursor.execute(sql, params) # Execute SELECT
  77. cursor.execute("SET PROFILING=0") # Disable profiling
  78. # The Query ID should always be 1 here but I'll subselect to get
  79. # the last one just in case...
  80. cursor.execute("""
  81. SELECT *
  82. FROM information_schema.profiling
  83. WHERE query_id = (
  84. SELECT query_id
  85. FROM information_schema.profiling
  86. ORDER BY query_id DESC
  87. LIMIT 1
  88. )
  89. """)
  90. headers = [d[0] for d in cursor.description]
  91. result = cursor.fetchall()
  92. except Exception:
  93. result_error = "Profiling is either not available or not supported by your database."
  94. cursor.close()
  95. context = {
  96. 'result': result,
  97. 'result_error': result_error,
  98. 'sql': form.reformat_sql(),
  99. 'duration': form.cleaned_data['duration'],
  100. 'headers': headers,
  101. 'alias': form.cleaned_data['alias'],
  102. }
  103. # Using SimpleTemplateResponse avoids running global context processors.
  104. return SimpleTemplateResponse('debug_toolbar/panels/sql_profile.html', context)
  105. return HttpResponseBadRequest('Form errors')