|
9 | 9 | * @copyright 2013 Jeremy Dorn |
10 | 10 | * @license http://opensource.org/licenses/MIT |
11 | 11 | * @link http://github.com/jdorn/sql-formatter |
12 | | - * @version 1.2.15 |
| 12 | + * @version 1.2.16 |
13 | 13 | */ |
14 | 14 | class SqlFormatter |
15 | 15 | { |
@@ -241,33 +241,29 @@ protected static function getNextToken($string, $previous = null) |
241 | 241 | self::TOKEN_VALUE => self::getQuotedString($string) |
242 | 242 | ); |
243 | 243 |
|
244 | | - // If a quote was opened, but doesn't have a closing quote, return the remaining string |
245 | | - if ($return[self::TOKEN_VALUE] === null) { |
246 | | - $return[self::TOKEN_VALUE] = $string; |
247 | | - } |
248 | | - |
249 | 244 | return $return; |
250 | 245 | } |
251 | 246 |
|
252 | 247 | // User-defined Variable |
253 | 248 | if ($string[0] === '@' && isset($string[1])) { |
| 249 | + $ret = array( |
| 250 | + self::TOKEN_VALUE => null, |
| 251 | + self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE |
| 252 | + ); |
| 253 | + |
254 | 254 | // If the variable name is quoted |
255 | 255 | if ($string[1]==='"' || $string[1]==='\'' || $string[1]==='`') { |
256 | | - return array( |
257 | | - self::TOKEN_VALUE => '@'.self::getQuotedString(substr($string,1)), |
258 | | - self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE |
259 | | - ); |
| 256 | + $ret[self::TOKEN_VALUE] = '@'.self::getQuotedString(substr($string,1)); |
260 | 257 | } |
261 | 258 | // Non-quoted variable name |
262 | 259 | else { |
263 | 260 | preg_match('/^(@[a-zA-Z0-9\._\$]+)/',$string,$matches); |
264 | 261 | if ($matches) { |
265 | | - return array( |
266 | | - self::TOKEN_VALUE => $matches[1], |
267 | | - self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE |
268 | | - ); |
| 262 | + $ret[self::TOKEN_VALUE] = $matches[1]; |
269 | 263 | } |
270 | 264 | } |
| 265 | + |
| 266 | + if($ret[self::TOKEN_VALUE] !== null) return $ret; |
271 | 267 | } |
272 | 268 |
|
273 | 269 | // Number (decimal, binary, or hex) |
@@ -335,15 +331,17 @@ protected static function getNextToken($string, $previous = null) |
335 | 331 |
|
336 | 332 | protected static function getQuotedString($string) |
337 | 333 | { |
| 334 | + $ret = null; |
| 335 | + |
338 | 336 | // This checks for the following patterns: |
339 | 337 | // 1. backtick quoted string using `` to escape |
340 | 338 | // 2. double quoted string using "" or \" to escape |
341 | 339 | // 3. single quoted string using '' or \' to escape |
342 | 340 | if ( preg_match('/^(((`[^`]*($|`))+)|(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)|((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+))/s', $string, $matches)) { |
343 | | - return $matches[1]; |
| 341 | + $ret = $matches[1]; |
344 | 342 | } |
345 | | - |
346 | | - return null; |
| 343 | + |
| 344 | + return $ret; |
347 | 345 | } |
348 | 346 |
|
349 | 347 | /** |
@@ -696,6 +694,14 @@ public static function format($string, $highlight=true) |
696 | 694 | if ($token[self::TOKEN_VALUE] === '(' || $token[self::TOKEN_VALUE] === '.') { |
697 | 695 | $return = rtrim($return,' '); |
698 | 696 | } |
| 697 | + |
| 698 | + // If this is the "-" of a negative number, it shouldn't have a space after it |
| 699 | + if($token[self::TOKEN_VALUE] === '-' && isset($tokens[$i+1]) && $tokens[$i+1][self::TOKEN_TYPE] === self::TOKEN_TYPE_NUMBER && isset($tokens[$i-1])) { |
| 700 | + $prev = $tokens[$i-1][self::TOKEN_TYPE]; |
| 701 | + if($prev !== self::TOKEN_TYPE_QUOTE && $prev !== self::TOKEN_TYPE_BACKTICK_QUOTE && $prev !== self::TOKEN_TYPE_WORD && $prev !== self::TOKEN_TYPE_NUMBER) { |
| 702 | + $return = rtrim($return,' '); |
| 703 | + } |
| 704 | + } |
699 | 705 | } |
700 | 706 |
|
701 | 707 | // If there are unmatched parentheses |
|
0 commit comments