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 /lib | |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/SqlFormatter.php | 29 |
1 files changed, 16 insertions, 13 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>'; |