diff options
author | Jeremy Dorn <jeremy@jeremydorn.com> | 2012-12-29 11:41:32 -0800 |
---|---|---|
committer | Jeremy Dorn <jeremy@jeremydorn.com> | 2012-12-29 11:41:32 -0800 |
commit | 7cc05be93fc352be9c551bf5a45c7b930f5d9e2a (patch) | |
tree | a795457685bcaf7001d0e969c15a3f978f12c5cb /lib/SqlFormatter.php | |
parent | 9aac5ea51a1c13af3e67a7d7ba7e528f94c52485 (diff) | |
download | sql-formatter-7cc05be93fc352be9c551bf5a45c7b930f5d9e2a.zip sql-formatter-7cc05be93fc352be9c551bf5a45c7b930f5d9e2a.tar.gz sql-formatter-7cc05be93fc352be9c551bf5a45c7b930f5d9e2a.tar.bz2 |
Fixed formatting bug when 2 special reserved words immediately followed each other.
Added set operators to special reserved list (UNION, UNION ALL, EXCEPT, and INTERSECT).
Updated test sql list to reduce the number of similar queries and better reflect real world usecases.
Improved output of the regression test to make debugging easier.
Diffstat (limited to 'lib/SqlFormatter.php')
-rw-r--r-- | lib/SqlFormatter.php | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/SqlFormatter.php b/lib/SqlFormatter.php index 3ef2002..a37d4cc 100644 --- a/lib/SqlFormatter.php +++ b/lib/SqlFormatter.php @@ -35,7 +35,7 @@ class SqlFormatter 'SERIALIZABLE', 'SESSION', 'SET', 'SHARE', 'SHOW', 'SHUTDOWN', 'SLAVE', 'SONAME', 'SOUNDS', 'SQL', 'SQL_CACHE', 'SQL_NO_CACHE', 'START', 'STARTING', 'STATUS', 'STOP', 'STORAGE', 'STRAIGHT_JOIN', 'STRING', 'SUPER', 'TABLE', 'TABLES', 'TEMPORARY', 'TERMINATED', 'THEN', 'TO', 'TRAILING', 'TRANSACTIONAL', - 'TRUNCATE', 'TYPE', 'TYPES', 'UNCOMMITTED', 'UNION', 'UNIQUE', 'UNLOCK', 'UNSIGNED', 'USAGE', 'USE', 'USING', 'VARIABLES', + 'TRUNCATE', 'TYPE', 'TYPES', 'UNCOMMITTED', 'UNIQUE', 'UNLOCK', 'UNSIGNED', 'USAGE', 'USE', 'USING', 'VARIABLES', 'VIEW', 'WHEN', 'WITH', 'WORK', 'WRITE', 'XOR', 'YEAR_MONTH' ); @@ -43,7 +43,7 @@ class SqlFormatter // These keywords will all be on their own line protected static $special_reserved = array( 'SELECT', 'FROM', 'WHERE', 'SET', 'ORDER BY', 'GROUP BY', 'LEFT JOIN', 'OUTER JOIN', 'INNER JOIN', 'RIGHT JOIN', 'JOIN', 'LIMIT', - 'VALUES', 'UPDATE', 'HAVING', 'ADD', 'AFTER', 'ALTER TABLE', 'DELETE FROM' + 'VALUES', 'UPDATE', 'HAVING', 'ADD', 'AFTER', 'ALTER TABLE', 'DELETE FROM', 'UNION ALL', 'UNION', 'EXCEPT', 'INTERSECT' ); // Punctuation that can be used as a boundary between other tokens @@ -479,6 +479,10 @@ class SqlFormatter if(!$added_newline) { $return .= "\n" . str_repeat($tab, $indent_level); } + // If we already added a newline, redo the indentation since it may be different now + else { + $return = rtrim($return,$tab).str_repeat($tab, $indent_level); + } // If the token may have extra whitespace if (strpos($token['token'],' ')!==false || strpos($token['token'],"\n")!==false || strpos($token['token'],"\t")!==false) { |