Skip to content
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

SQL query formatting improvements #1752

Merged
merged 10 commits into from
Apr 9, 2023
Merged

Commits on Mar 27, 2023

  1. Remove unhelpful comments

    living180 committed Mar 27, 2023
    Configuration menu
    Copy the full SHA
    9448bdb View commit details
    Browse the repository at this point in the history

Commits on Mar 30, 2023

  1. Use Python's built-in html.escape()

    Because the token values escaped by BoldKeywordFilter are simply
    intermediate values and are not directly included in HTML templates,
    use Python's html.escape() instead of django.utils.html.escape() to
    eliminate the overhead of converting the token values to SafeString.
    
    Also pass quote=False when calling escape() since the token values will
    not be used in quoted attributes.
    living180 committed Mar 30, 2023
    Configuration menu
    Copy the full SHA
    ee98c58 View commit details
    Browse the repository at this point in the history
  2. Replace sqlparse.filters.SerializerUnicode() usage

    sqlparse's SerializerUnicode filter does a bunch of fancy whitespace
    processing which isn't needed because the resulting string will just be
    inserted into HTML.  Replace with a simple EscapedStringSerializer that
    does nothing but convert the Statement to a properly-escaped string.
    
    In the process stop the escaping within BoldKeywordFilter to have a
    cleaner separation of concerns:  BoldKeywordFilter now only handles
    marking up keywords as bold, while escaping is explicitly handled by the
    EscapedStringSerializer.
    living180 committed Mar 30, 2023
    Configuration menu
    Copy the full SHA
    cd9a189 View commit details
    Browse the repository at this point in the history
  3. Replace select-list elision implementation

    Instead of using a regex to elide the select list in the simplified
    representation of an SQL query, use an sqlparse filter to elide the
    select list as a preprocessing step.  The result ends up being about 10%
    faster.
    living180 committed Mar 30, 2023
    Configuration menu
    Copy the full SHA
    496c97d View commit details
    Browse the repository at this point in the history
  4. Use better heuristic for select list elision

    Instead of only eliding select lists longer than 12 characters, now only
    elide select lists that contain a dot (from a column expression like
    `table_name`.`column_name`).  The motivation for this is that as of
    Django 1.10, using .count() on a queryset generates
        SELECT COUNT(*) AS `__count` FROM ...
    instead of
        SELECT COUNT(*) FROM ...
    queries.  This change prevents the new form from being elided.
    living180 committed Mar 30, 2023
    Configuration menu
    Copy the full SHA
    3b881fb View commit details
    Browse the repository at this point in the history

Commits on Apr 2, 2023

  1. Only elide top-level select lists

    If a query has subselects in its WHERE clause, do not elide the select
    lists in those subselects.
    living180 committed Apr 2, 2023
    Configuration menu
    Copy the full SHA
    ef9cfbb View commit details
    Browse the repository at this point in the history
  2. Apply BoldKeywordFilter after AlignedIndentFilter

    The "<strong>" tokens inserted by the BoldKeywordFilter were causing the
    AlignedIndentFilter to apply excessive indentation to queries which used
    CASE statements.  Fix by rewriting BoldIndentFilter as a statement
    filter rather than a preprocess filter, and applying after
    AlignedIndentFilter.
    living180 committed Apr 2, 2023
    Configuration menu
    Copy the full SHA
    f45e136 View commit details
    Browse the repository at this point in the history
  3. Only enable SQL grouping for AlignedIndentFilter

    When formatting SQL statements using sqparse, grouping only affects the
    output when AlignedIndentFilter is applied.
    living180 committed Apr 2, 2023
    Configuration menu
    Copy the full SHA
    255efeb View commit details
    Browse the repository at this point in the history
  4. Eliminate intermediate _parse_sql() method

    By using a settings_changed signal receiver to clear the query caching,
    the parse_sql() and _parse_sql() functions can be merged and the check
    for the "PRETTIFY_SQL" setting can be moved back inside the
    get_filter_stack() function.
    living180 committed Apr 2, 2023
    Configuration menu
    Copy the full SHA
    3751812 View commit details
    Browse the repository at this point in the history
  5. Amend change log

    living180 committed Apr 2, 2023
    Configuration menu
    Copy the full SHA
    e34ec83 View commit details
    Browse the repository at this point in the history