/
SQLFormatter.php
68 lines (58 loc) · 1.62 KB
/
SQLFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace SilverStripe\View\Parsers;
use SilverStripe\Core\Injector\Injectable;
/**
* 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.
*
* @author Ingo Schommer, Silverstripe Ltd. (<firstname>@silverstripe.com)
*/
class SQLFormatter
{
use Injectable;
protected static $newline_before_tokens = [
'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.
*
* @param string $sql
* @param bool $useHtmlFormatting
* @return string
*/
protected function addNewlines($sql, $useHtmlFormatting = false)
{
$eol = PHP_EOL;
foreach (self::$newline_before_tokens as $token) {
$breakToken = ($useHtmlFormatting) ? "<br />$eol" : $eol;
$sql = preg_replace('/[^\n](' . $token . ')/', $breakToken . '$1', $sql ?? '');
}
return $sql;
}
}