add more highlights to SyntaxExtension

* add more keywords to highlight
* add highlight on query comments
* add highlight on table names
This commit is contained in:
Toni Uebernickel 2012-02-09 15:51:30 +01:00
parent 33fbd28b39
commit 034eb3703f
2 changed files with 67 additions and 3 deletions

View file

@ -29,6 +29,17 @@
<style type="text/css">
.SQLKeyword {
color: blue;
white-space: nowrap;
}
.SQLName {
color: #464646;
white-space: nowrap;
}
.SQLInfo, .SQLComment {
color: gray;
display: block;
font-size: 0.9em;
margin: 3px 0;
}
</style>
@ -47,7 +58,7 @@
<tr>
<td>
<code>{{ query.sql|format_sql }}</code>
<div style="color: gray;font-size: 0.9em;">Time: {{ query.time }} - Memory: {{ query.memory }}</div>
<div class="SQLInfo">Time: {{ query.time }} - Memory: {{ query.memory }}</div>
</td>
</tr>
{% endfor %}

View file

@ -33,9 +33,62 @@ class SyntaxExtension extends \Twig_Extension
public function formatSQL($sql)
{
$sql = preg_replace('/\b(UPDATE|SET|SELECT|FROM|AS|LIMIT|ASC|COUNT|DESC|WHERE|LEFT JOIN|INNER JOIN|RIGHT JOIN|ORDER BY|GROUP BY|IN|LIKE|DISTINCT|DELETE|INSERT|INTO|VALUES|ON|AND|OR)\b/', '<span class="SQLKeyword">\\1</span>', $sql);
// list of keywords to prepend a newline in output
$newlines = array(
'FROM',
'(((FULL|LEFT|RIGHT)? ?(OUTER|INNER)?|CROSS|NATURAL)? JOIN)',
'VALUES',
'WHERE',
'ORDER BY',
'GROUP BY',
'HAVING',
'LIMIT',
);
$sql = preg_replace('/\b(FROM|WHERE|INNER JOIN|LEFT JOIN|RIGHT JOIN|ORDER BY|GROUP BY)\b/', '<br />\\1', $sql);
// list of keywords to highlight
$keywords = array_merge($newlines, array(
// base
'SELECT', 'UPDATE', 'DELETE', 'INSERT', 'REPLACE',
'SET',
'INTO',
'AS',
'DISTINCT',
// most used methods
'COUNT',
'AVG',
'MIN',
'MAX',
// joins
'ON', 'USING',
// where clause
'(IS (NOT)?)?NULL',
'(NOT )?IN',
'(NOT )?I?LIKE',
'AND', 'OR', 'XOR',
'BETWEEN',
// order, group, limit ..
'ASC',
'DESC',
'OFFSET',
));
$sql = preg_replace(array(
'/\b('.implode('|', $newlines).')\b/',
'/\b('.implode('|', $keywords).')\b/',
'/(\/\*.*\*\/)/',
'/(`[^`.]*`)/',
'/(([0-9a-zA-Z$_]+)\.([0-9a-zA-Z$_]+))/',
), array(
'<br />\\1',
'<span class="SQLKeyword">\\1</span>',
'<span class="SQLComment">\\1</span>',
'<span class="SQLName">\\1</span>',
'<span class="SQLName">\\1</span>',
), $sql);
return $sql;
}