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.