2008-09-16 20:12:07 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Format a SQL Query for better readable output in HTML or Plaintext.
|
|
|
|
* Its a simple string parser, not a full tokenizer - so formatting
|
|
|
|
* is not aware of the SQL syntax. This means we have to be conservative
|
|
|
|
* with modifying the SQL string.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-09-16 20:12:07 +02:00
|
|
|
* @subpackage parsers
|
|
|
|
* @author Ingo Schommer, Silverstripe Ltd. (<firstname>@silverstripe.com)
|
|
|
|
*/
|
|
|
|
class SQLFormatter extends Object {
|
|
|
|
|
|
|
|
protected static $newline_before_tokens = array(
|
|
|
|
'SELECT',
|
|
|
|
'UPDATE',
|
|
|
|
'INSERT',
|
|
|
|
'DELETE',
|
|
|
|
'FROM',
|
|
|
|
'INNER JOIN',
|
|
|
|
'FULL JOIN',
|
|
|
|
'LEFT JOIN',
|
|
|
|
'RIGHT JOIN',
|
|
|
|
'WHERE',
|
|
|
|
'ORDER BY',
|
|
|
|
'GROUP BY',
|
|
|
|
'LIMIT',
|
|
|
|
);
|
|
|
|
|
|
|
|
public function formatPlain($sql) {
|
|
|
|
$sql = $this->addNewlines($sql, false);
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function formatHTML($sql) {
|
|
|
|
$sql = $this->addNewlines($sql, true);
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Newlines for tokens defined in $newline_before_tokens.
|
|
|
|
* Case-sensitive, only applies to uppercase SQL to avoid
|
|
|
|
* messing with possible content fragments in the query.
|
|
|
|
*/
|
|
|
|
protected function addNewlines($sql, $useHtmlFormatting = false) {
|
2009-11-21 02:43:54 +01:00
|
|
|
$eol = PHP_EOL;
|
2008-09-16 20:12:07 +02:00
|
|
|
foreach(self::$newline_before_tokens as $token) {
|
2009-11-21 02:43:54 +01:00
|
|
|
$breakToken = ($useHtmlFormatting) ? "<br />$eol" : $eol;
|
2008-09-16 20:12:07 +02:00
|
|
|
$sql = preg_replace('/[^\n](' . $token . ')/', $breakToken . '$1', $sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|