Permalink
Browse files

Merge pull request #104 from havvg/feature/syntax-extension

some love for the SyntaxExtension
  • Loading branch information...
2 parents 33fbd28 + 034eb37 commit 0dc2bd7e78d0692690061499c2c042babc8b0116 @willdurand willdurand committed Feb 10, 2012
Showing with 67 additions and 3 deletions.
  1. +12 −1 Resources/views/Collector/propel.html.twig
  2. +55 −2 Twig/Extension/SyntaxExtension.php
@@ -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 %}
@@ -33,9 +33,62 @@ public function getName()
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',
+ );
+
+ // 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('/\b(FROM|WHERE|INNER JOIN|LEFT JOIN|RIGHT JOIN|ORDER BY|GROUP BY)\b/', '<br />\\1', $sql);
+ $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;
}

0 comments on commit 0dc2bd7

Please sign in to comment.