Skip to content

Commit def9af1

Browse files
committed
Open database cursor using a context manager
Using a context manager ensures the cursor is always closed at a deterministic time, even in the event of a failure.
1 parent c7c44d9 commit def9af1

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

debug_toolbar/panels/sql/views.py

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ def sql_select(request):
1515
if form.is_valid():
1616
sql = form.cleaned_data["raw_sql"]
1717
params = form.cleaned_data["params"]
18-
cursor = form.cursor
19-
cursor.execute(sql, params)
20-
headers = [d[0] for d in cursor.description]
21-
result = cursor.fetchall()
22-
cursor.close()
18+
with form.cursor as cursor:
19+
cursor.execute(sql, params)
20+
headers = [d[0] for d in cursor.description]
21+
result = cursor.fetchall()
22+
2323
context = {
2424
"result": result,
2525
"sql": form.reformat_sql(),
@@ -42,21 +42,19 @@ def sql_explain(request):
4242
sql = form.cleaned_data["raw_sql"]
4343
params = form.cleaned_data["params"]
4444
vendor = form.connection.vendor
45-
cursor = form.cursor
46-
47-
if vendor == "sqlite":
48-
# SQLite's EXPLAIN dumps the low-level opcodes generated for a query;
49-
# EXPLAIN QUERY PLAN dumps a more human-readable summary
50-
# See https://www.sqlite.org/lang_explain.html for details
51-
cursor.execute("EXPLAIN QUERY PLAN {}".format(sql), params)
52-
elif vendor == "postgresql":
53-
cursor.execute("EXPLAIN ANALYZE {}".format(sql), params)
54-
else:
55-
cursor.execute("EXPLAIN {}".format(sql), params)
45+
with form.cursor as cursor:
46+
if vendor == "sqlite":
47+
# SQLite's EXPLAIN dumps the low-level opcodes generated for a query;
48+
# EXPLAIN QUERY PLAN dumps a more human-readable summary
49+
# See https://www.sqlite.org/lang_explain.html for details
50+
cursor.execute("EXPLAIN QUERY PLAN {}".format(sql), params)
51+
elif vendor == "postgresql":
52+
cursor.execute("EXPLAIN ANALYZE {}".format(sql), params)
53+
else:
54+
cursor.execute("EXPLAIN {}".format(sql), params)
55+
headers = [d[0] for d in cursor.description]
56+
result = cursor.fetchall()
5657

57-
headers = [d[0] for d in cursor.description]
58-
result = cursor.fetchall()
59-
cursor.close()
6058
context = {
6159
"result": result,
6260
"sql": form.reformat_sql(),
@@ -78,35 +76,36 @@ def sql_profile(request):
7876
if form.is_valid():
7977
sql = form.cleaned_data["raw_sql"]
8078
params = form.cleaned_data["params"]
81-
cursor = form.cursor
8279
result = None
8380
headers = None
8481
result_error = None
85-
try:
86-
cursor.execute("SET PROFILING=1") # Enable profiling
87-
cursor.execute(sql, params) # Execute SELECT
88-
cursor.execute("SET PROFILING=0") # Disable profiling
89-
# The Query ID should always be 1 here but I'll subselect to get
90-
# the last one just in case...
91-
cursor.execute(
92-
"""
93-
SELECT *
94-
FROM information_schema.profiling
95-
WHERE query_id = (
96-
SELECT query_id
97-
FROM information_schema.profiling
98-
ORDER BY query_id DESC
99-
LIMIT 1
100-
)
101-
"""
102-
)
103-
headers = [d[0] for d in cursor.description]
104-
result = cursor.fetchall()
105-
except Exception:
106-
result_error = (
107-
"Profiling is either not available or not supported by your database."
108-
)
109-
cursor.close()
82+
with form.cursor as cursor:
83+
try:
84+
cursor.execute("SET PROFILING=1") # Enable profiling
85+
cursor.execute(sql, params) # Execute SELECT
86+
cursor.execute("SET PROFILING=0") # Disable profiling
87+
# The Query ID should always be 1 here but I'll subselect to get
88+
# the last one just in case...
89+
cursor.execute(
90+
"""
91+
SELECT *
92+
FROM information_schema.profiling
93+
WHERE query_id = (
94+
SELECT query_id
95+
FROM information_schema.profiling
96+
ORDER BY query_id DESC
97+
LIMIT 1
98+
)
99+
"""
100+
)
101+
headers = [d[0] for d in cursor.description]
102+
result = cursor.fetchall()
103+
except Exception:
104+
result_error = (
105+
"Profiling is either not available or not supported by your "
106+
"database."
107+
)
108+
110109
context = {
111110
"result": result,
112111
"result_error": result_error,

0 commit comments

Comments
 (0)