From c4b0efe72b0902fffb9abd25c6ec7280f410c475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jakes=CC=8C?= Date: Wed, 31 Jul 2019 15:13:50 +0200 Subject: [PATCH] Add simple formatting for select queries, wrap long SELECT clauses [MAILPOET-2239] --- lib/Tracy/DoctrinePanel/DoctrinePanel.php | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/Tracy/DoctrinePanel/DoctrinePanel.php b/lib/Tracy/DoctrinePanel/DoctrinePanel.php index 2802acd1ed..d2eacf0a77 100644 --- a/lib/Tracy/DoctrinePanel/DoctrinePanel.php +++ b/lib/Tracy/DoctrinePanel/DoctrinePanel.php @@ -45,6 +45,37 @@ class DoctrinePanel implements IBarPanel { } protected function formatSql($sql) { + // format SELECT queries a bit (wrap long select clauses, wrap lines on some keywords) + preg_match('/^(SELECT\s+)(.*?)(\s+FROM\s+)(.*?)$/iu', trim($sql), $matches); + if (count($matches) >= 5) { + // if SELECT clause over 50 chars, make it wrappable + $select_html = mb_strlen($matches[2]) > 50 ? ($matches[1] . ' + ... +
' . $matches[2] . '
+ ') : ($matches[1] . $matches[2]); + $from_keyword = $matches[3]; + $rest = $matches[4]; + + // try to match & indent WHERE clause + $where_html = ''; + preg_match('/^(.*)(\s+WHERE\s+)(.*?)$/iu', $rest, $matches); + if (count($matches) >= 4) { + $where_html = $matches[1] . '
' . $matches[2]; + $rest = $matches[3]; + } + + // try to match & indent ORDER BY/GROUP BY/LIMIT/OFFSET + $end_html = ''; + preg_match('/^(.*?)(\s+(?:ORDER\s+BY|GROUP\s+BY|LIMIT|OFFSET)\s+)(.*)$/iu', $rest, $matches); + if (count($matches) >= 4) { + $end_html = $matches[1] . '
' . $matches[2]; + $rest = $matches[3]; + } + + $sql = $select_html . '
' . $from_keyword . $where_html . $end_html . $rest; + } + + // highlight keywords $keywords = new MySQLKeywords(); $tokens = preg_split('/(\s+)/', $sql, -1, PREG_SPLIT_DELIM_CAPTURE); $output = '';