Skip to content

Display indented SQL queries in expanded views. #1116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion debug_toolbar/panels/sql/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def clean_hash(self):
return hash

def reformat_sql(self):
return reformat_sql(self.cleaned_data["sql"])
return reformat_sql(self.cleaned_data["sql"], with_toggle=False)

def make_hash(self, data):
m = hmac.new(key=force_bytes(settings.SECRET_KEY), digestmod=hashlib.sha1)
Expand Down
2 changes: 1 addition & 1 deletion debug_toolbar/panels/sql/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def duplicate_key(query):
query["form"] = SQLSelectForm(auto_id=None, initial=copy(query))

if query["sql"]:
query["sql"] = reformat_sql(query["sql"])
query["sql"] = reformat_sql(query["sql"], with_toggle=True)
query["rgb_color"] = self._databases[alias]["rgb_color"]
try:
query["width_ratio"] = (query["duration"] / self._sql_time) * 100
Expand Down
30 changes: 20 additions & 10 deletions debug_toolbar/panels/sql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,32 @@ def process(self, stream):
yield T.Text, "</strong>"


def reformat_sql(sql):
def reformat_sql(sql, with_toggle=False):
formatted = parse_sql(sql, aligned_indent=True)
if not with_toggle:
return formatted
simple = simplify(parse_sql(sql, aligned_indent=False))
uncollapsed = '<span class="djDebugUncollapsed" href="#">{}</span>'.format(simple)
collapsed = '<span class="djDebugCollapsed" href="#">{}</span>'.format(formatted)
return collapsed + uncollapsed


def parse_sql(sql, aligned_indent=False):
stack = sqlparse.engine.FilterStack()
stack.enable_grouping()
if aligned_indent:
stack.stmtprocess.append(
sqlparse.filters.AlignedIndentFilter(char="&nbsp;", n="<br/>")
)
stack.preprocess.append(BoldKeywordFilter()) # add our custom filter
stack.postprocess.append(sqlparse.filters.SerializerUnicode()) # tokens -> strings
return swap_fields("".join(stack.run(sql)))
return "".join(stack.run(sql))


def swap_fields(sql):
def simplify(sql):
expr = r"SELECT</strong> (...........*?) <strong>FROM"
subs = (
r"SELECT</strong> "
r'<span class="djDebugUncollapsed" href="#">&#8226;&#8226;&#8226;</span> '
r'<span class="djDebugCollapsed" href="#">\1</span> '
r"<strong>FROM"
)
return re.sub(expr, subs, sql)
sub = r"SELECT</strong> &#8226;&#8226;&#8226; <strong>FROM"
return re.sub(expr, sub, sql)


def contrasting_color_generator():
Expand Down