diff options
author | Jeremy Dorn <jeremy@jeremydorn.com> | 2013-03-07 18:26:59 -0800 |
---|---|---|
committer | Jeremy Dorn <jeremy@jeremydorn.com> | 2013-03-07 18:26:59 -0800 |
commit | 2724ddb7fb79851a557d868a688fc269960bff87 (patch) | |
tree | 40e791d5e1d485392ec095f51fdd0ce4acc82814 | |
parent | 17fdbb8bd358d226dc1a9691c2245f041d72e3d3 (diff) | |
download | sql-formatter-2724ddb7fb79851a557d868a688fc269960bff87.zip sql-formatter-2724ddb7fb79851a557d868a688fc269960bff87.tar.gz sql-formatter-2724ddb7fb79851a557d868a688fc269960bff87.tar.bz2 |
Fixed formatting bug with multiple boundary characters (e.g. ">=", "<>", etc.). Fixes #29
Improved code coverage in unit tests. Now everything is covered except the infinite loop catching code which I don't know how to test.
-rw-r--r-- | lib/SqlFormatter.php | 29 | ||||
-rw-r--r-- | tests/SqlFormatterTest.php | 12 | ||||
-rw-r--r-- | tests/format-highlight.html | 6 | ||||
-rw-r--r-- | tests/format.html | 6 | ||||
-rw-r--r-- | tests/highlight.html | 6 | ||||
-rw-r--r-- | tests/sql.sql | 4 |
6 files changed, 39 insertions, 24 deletions
diff --git a/lib/SqlFormatter.php b/lib/SqlFormatter.php index 6c4be7c..009b6df 100644 --- a/lib/SqlFormatter.php +++ b/lib/SqlFormatter.php @@ -171,24 +171,20 @@ class SqlFormatter // Quoted String if($string[0]==='"' || $string[0]==='\'' || $string[0]==='`') { + $return = array( + self::TOKEN_TYPE => ($string[0]==='`'? self::TOKEN_TYPE_BACKTICK_QUOTE : self::TOKEN_TYPE_QUOTE), + self::TOKEN_VALUE => $string + ); + // This checks for the following patterns: // 1. backtick quoted string using `` to escape // 2. double quoted string using "" or \" to escape // 3. single quoted string using '' or \' to escape if( preg_match('/^(((`[^`]*($|`))+)|(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)|((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+))/s', $string, $matches)) { - if($string[0]==='`') { - return array( - self::TOKEN_VALUE=>$matches[1], - self::TOKEN_TYPE=>self::TOKEN_TYPE_BACKTICK_QUOTE - ); - } - else { - return array( - self::TOKEN_VALUE=>$matches[1], - self::TOKEN_TYPE=>self::TOKEN_TYPE_QUOTE - ); - } + $return[self::TOKEN_VALUE] = $matches[1]; } + + return $return; } // Number @@ -511,6 +507,13 @@ class SqlFormatter $highlighted = preg_replace('/\s+/',' ',$highlighted); } } + + // Multiple boundary characters in a row should not have spaces between them (not including parentheses) + elseif($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BOUNDARY) { + if($tokens[$i-1][self::TOKEN_TYPE] === self::TOKEN_TYPE_BOUNDARY) { + $return = rtrim($return, ' '); + } + } // If the token shouldn't have a space before it if ($token[self::TOKEN_VALUE] === '.' || $token[self::TOKEN_VALUE] === ',' || $token[self::TOKEN_VALUE] === ';') { @@ -799,7 +802,7 @@ class SqlFormatter private static function output($string) { $string=trim($string); - if(!self::$use_pre){ + if(!self::$use_pre) { return $string; } return '<pre '.self::$pre_attributes.'>' . $string . '</pre>'; diff --git a/tests/SqlFormatterTest.php b/tests/SqlFormatterTest.php index 5cf65ad..2bd8f7f 100644 --- a/tests/SqlFormatterTest.php +++ b/tests/SqlFormatterTest.php @@ -23,6 +23,18 @@ class SqlFormatterTest extends PHPUnit_Framework_TestCase { $this->assertEquals(trim($html), trim(SqlFormatter::highlight($sql))); } + function testUsePre() { + SqlFormatter::$use_pre = false; + $actual = SqlFormatter::highlight("test"); + $expected = '<span style="color: #333;">test</span>'; + $this->assertEquals($actual,$expected); + + SqlFormatter::$use_pre = true; + $actual = SqlFormatter::highlight("test"); + $expected = '<pre style="color: black; background-color: white;"><span style="color: #333;">test</span></pre>'; + $this->assertEquals($actual,$expected); + } + function testSplitQuery() { $expected = array( "SELECT 'test' FROM MyTable;", diff --git a/tests/format-highlight.html b/tests/format-highlight.html index f64da33..0be035d 100644 --- a/tests/format-highlight.html +++ b/tests/format-highlight.html @@ -799,6 +799,8 @@ <span style="font-weight:bold;">WHERE</span> <span style="color: green;">1</span> <span >=</span> <span style="color: green;">2</span><span >;</span></pre> +<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #aaa;">-- This is a test</span></pre> + <pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #333;">Test</span> <span style="font-weight:bold;">FROM</span> @@ -808,6 +810,4 @@ <span style="background-color: red;">)</span><span style="font-weight:bold;">AND</span> ( ( (<span style="color: #333;">SomeOtherColumn</span> <span >=</span> <span style="color: green;">2</span>)<span >;</span> -<span style="background-color: red;">WARNING: unclosed parentheses or section</span></pre> - -<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #aaa;">-- This is a test</span></pre>
\ No newline at end of file +<span style="background-color: red;">WARNING: unclosed parentheses or section</span></pre>
\ No newline at end of file diff --git a/tests/format.html b/tests/format.html index 32514ce..e806f6d 100644 --- a/tests/format.html +++ b/tests/format.html @@ -799,6 +799,8 @@ FROM -- This is another comment WHERE 1 = 2; +SELECT -- This is a test + SELECT Test FROM @@ -807,6 +809,4 @@ WHERE (MyColumn = 1) ) AND ( ( - (SomeOtherColumn = 2); - -SELECT -- This is a test
\ No newline at end of file + (SomeOtherColumn = 2);
\ No newline at end of file diff --git a/tests/highlight.html b/tests/highlight.html index b18d77b..f5b3ea9 100644 --- a/tests/highlight.html +++ b/tests/highlight.html @@ -238,8 +238,8 @@ <span style="color: #aaa;">/* This is a block comment */</span> <span style="font-weight:bold;">WHERE</span> <span style="color: green;">1</span> <span >=</span> <span style="color: green;">2</span><span >;</span></pre> +<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #aaa;">-- This is a test</span></pre> + <pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #333;">Test</span> <span style="font-weight:bold;">FROM</span> <span style="color: #333;">Test</span> <span style="font-weight:bold;">WHERE</span> ( - <span style="color: #333;">MyColumn</span> <span >=</span> <span style="color: green;">1</span> )) <span style="font-weight:bold;">AND</span> ((( <span style="color: #333;">SomeOtherColumn</span> <span >=</span> <span style="color: green;">2</span>)<span >;</span></pre> - -<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #aaa;">-- This is a test</span></pre>
\ No newline at end of file + <span style="color: #333;">MyColumn</span> <span >=</span> <span style="color: green;">1</span> )) <span style="font-weight:bold;">AND</span> ((( <span style="color: #333;">SomeOtherColumn</span> <span >=</span> <span style="color: green;">2</span>)<span >;</span></pre>
\ No newline at end of file diff --git a/tests/sql.sql b/tests/sql.sql index ed92aa6..f1b358f 100644 --- a/tests/sql.sql +++ b/tests/sql.sql @@ -238,8 +238,8 @@ MyTable # One final comment /* This is a block comment */ WHERE 1 = 2; +SELECT -- This is a test + SELECT Test FROM Test WHERE ( MyColumn = 1 )) AND ((( SomeOtherColumn = 2); - -SELECT -- This is a test |