silverstripe-framework/parsers/SQLFormatter.php
Ingo Schommer c57ce5f1a4 FEATURE Formatting MySQL error messages with newlines through new SQLFormatter class (used in MySQLDatabase)
ENHANCEMENT Using CliDebugView to report errors on ajax requests (with plaintext output)
ENHANCEMENT Removed "ERROR:" prefix hack for ajax error responses - clientside evaluation should inspect HTTP status codes instead

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@62467 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-16 18:12:07 +00:00

58 lines
1.3 KiB
PHP

<?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.
*
* @package sapphire
* @subpackage parsers
* @author Ingo Schommer, Silverstripe Ltd. (<firstname>@silverstripe.com)
* @usedby Database->databaseError()
*/
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) {
foreach(self::$newline_before_tokens as $token) {
$breakToken = ($useHtmlFormatting) ? "<br />\n" : "\n";
$sql = preg_replace('/[^\n](' . $token . ')/', $breakToken . '$1', $sql);
}
return $sql;
}
}
?>