@@ -15,11 +15,11 @@ def sql_select(request):
15
15
if form .is_valid ():
16
16
sql = form .cleaned_data ["raw_sql" ]
17
17
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
+
23
23
context = {
24
24
"result" : result ,
25
25
"sql" : form .reformat_sql (),
@@ -42,21 +42,19 @@ def sql_explain(request):
42
42
sql = form .cleaned_data ["raw_sql" ]
43
43
params = form .cleaned_data ["params" ]
44
44
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 ()
56
57
57
- headers = [d [0 ] for d in cursor .description ]
58
- result = cursor .fetchall ()
59
- cursor .close ()
60
58
context = {
61
59
"result" : result ,
62
60
"sql" : form .reformat_sql (),
@@ -78,35 +76,36 @@ def sql_profile(request):
78
76
if form .is_valid ():
79
77
sql = form .cleaned_data ["raw_sql" ]
80
78
params = form .cleaned_data ["params" ]
81
- cursor = form .cursor
82
79
result = None
83
80
headers = None
84
81
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
+
110
109
context = {
111
110
"result" : result ,
112
111
"result_error" : result_error ,
0 commit comments