Add debug methods in dbtng to print readable SQL for debug

Created on 22 July 2012, over 12 years ago
Updated 17 January 2025, 5 days ago

As the title says:
I sometimes want to print out the SQL produced by a dynamic query for debug.
However, what I get is a huge string that I need to paste into a text editor and manually add linebreaks and indentation to make it readable.

A new method on SelectQuery (and maybe other classes) could return a nicely formatted SQL string. There could be an option to replace placeholders with values or not.

This would have no effect on the usual ->__toString() used internally.
Although, there could be a setting to always use human-readable query formatting. This would also make the typical SQL error messages more readable.

I don't know if we have a convention for readable SQL. All I could find is
SQL coding conventions
SQL coding standards and whitespace (groups.drupal.org, 2008)

Maybe there are also some external references.

For my taste, readable SQL looks like this:

SELECT
  alias1.field1,
  alias2.field1,
FROM
  table1 alias1
  LEFT JOIN table2 alias2 ON
    alias1.x = alias2.y
  INNER JOIN ...
WHERE
  alias1.z = 5  AND
  alias2.z = 7  AND
  (
    another condition OR
    yet another condition
  )
ORDER BY
  alias1.foo ASC,
  alias2.bar DESC

(nested AND/OR conditions are a beast to format in any nice way, I don't have a clean proposal for that)

Atm the most straight-forward I can think of is really a dedicated method on SelectQuery. This would have to be in core.
A less straightforward solution would be a query formatter in devel module, which would not have direct access to the protected variables of SelectQuery.

✨ Feature request
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

database system

Created by

πŸ‡©πŸ‡ͺGermany donquixote

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for sharing your idea for improving Drupal.

    We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024