Skip to content

Commit 301cd0f

Browse files
author
Louis Béranger
committed
feat: New parameter PRETTIFY_SQL
1 parent ea65261 commit 301cd0f

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

debug_toolbar/panels/sql/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from django.utils.html import escape
55
from sqlparse import tokens as T
66

7+
from debug_toolbar import settings as dt_settings
8+
79

810
class BoldKeywordFilter:
911
"""sqlparse filter to bold SQL keywords"""
@@ -31,7 +33,8 @@ def reformat_sql(sql, with_toggle=False):
3133

3234
def parse_sql(sql, aligned_indent=False):
3335
stack = sqlparse.engine.FilterStack()
34-
stack.enable_grouping()
36+
if dt_settings.get_config()["PRETTIFY_SQL"]:
37+
stack.enable_grouping()
3538
if aligned_indent:
3639
stack.stmtprocess.append(
3740
sqlparse.filters.AlignedIndentFilter(char="&nbsp;", n="<br/>")

debug_toolbar/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"SHOW_TEMPLATE_CONTEXT": True,
4242
"SKIP_TEMPLATE_PREFIXES": ("django/forms/widgets/", "admin/widgets/"),
4343
"SQL_WARNING_THRESHOLD": 500, # milliseconds
44+
"PRETTIFY_SQL": True,
4445
}
4546

4647

docs/configuration.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,40 @@ Panel options
221221
The SQL panel highlights queries that took more that this amount of time,
222222
in milliseconds, to execute.
223223

224+
* ``PRETTIFY_SQL``
225+
226+
Default: ``True``
227+
228+
Panel: SQL
229+
230+
Controls SQL token grouping.
231+
232+
Token grouping allows pretty print of similar tokens,
233+
like aligned indentation for every selected field.
234+
235+
When set to ``True``, it might cause render slowdowns
236+
when a view make long SQL textual queries.
237+
238+
**Without grouping**::
239+
240+
SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name"
241+
FROM "auth_user"
242+
WHERE "auth_user"."username" = '''test_username'''
243+
LIMIT 21
244+
245+
**With grouping**::
246+
247+
SELECT "auth_user"."id",
248+
"auth_user"."password",
249+
"auth_user"."last_login",
250+
"auth_user"."is_superuser",
251+
"auth_user"."username",
252+
"auth_user"."first_name",
253+
"auth_user"."last_name",
254+
FROM "auth_user"
255+
WHERE "auth_user"."username" = '''test_username'''
256+
LIMIT 21
257+
224258
Here's what a slightly customized toolbar configuration might look like::
225259

226260
# This example is unlikely to be appropriate for your project.

0 commit comments

Comments
 (0)