Skip to content

Commit 8859d66

Browse files
committed
Fix formatting of negative numbers (fixes #49).
Improve phpunit code coverage.
1 parent 96effdc commit 8859d66

File tree

7 files changed

+116
-17
lines changed

7 files changed

+116
-17
lines changed

lib/SqlFormatter.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @copyright 2013 Jeremy Dorn
1010
* @license http://opensource.org/licenses/MIT
1111
* @link http://github.com/jdorn/sql-formatter
12-
* @version 1.2.15
12+
* @version 1.2.16
1313
*/
1414
class SqlFormatter
1515
{
@@ -241,33 +241,29 @@ protected static function getNextToken($string, $previous = null)
241241
self::TOKEN_VALUE => self::getQuotedString($string)
242242
);
243243

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-
249244
return $return;
250245
}
251246

252247
// User-defined Variable
253248
if ($string[0] === '@' && isset($string[1])) {
249+
$ret = array(
250+
self::TOKEN_VALUE => null,
251+
self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE
252+
);
253+
254254
// If the variable name is quoted
255255
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));
260257
}
261258
// Non-quoted variable name
262259
else {
263260
preg_match('/^(@[a-zA-Z0-9\._\$]+)/',$string,$matches);
264261
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];
269263
}
270264
}
265+
266+
if($ret[self::TOKEN_VALUE] !== null) return $ret;
271267
}
272268

273269
// Number (decimal, binary, or hex)
@@ -335,15 +331,17 @@ protected static function getNextToken($string, $previous = null)
335331

336332
protected static function getQuotedString($string)
337333
{
334+
$ret = null;
335+
338336
// This checks for the following patterns:
339337
// 1. backtick quoted string using `` to escape
340338
// 2. double quoted string using "" or \" to escape
341339
// 3. single quoted string using '' or \' to escape
342340
if ( preg_match('/^(((`[^`]*($|`))+)|(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)|((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+))/s', $string, $matches)) {
343-
return $matches[1];
341+
$ret = $matches[1];
344342
}
345-
346-
return null;
343+
344+
return $ret;
347345
}
348346

349347
/**
@@ -696,6 +694,14 @@ public static function format($string, $highlight=true)
696694
if ($token[self::TOKEN_VALUE] === '(' || $token[self::TOKEN_VALUE] === '.') {
697695
$return = rtrim($return,' ');
698696
}
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+
}
699705
}
700706

701707
// If there are unmatched parentheses

tests/clihighlight.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,29 @@
778778
a in (1, 2, 3, 4, 5)
779779
and b = 5;
780780

781+
SELECT
782+
count - 50
783+
WHERE
784+
a - 50 = b
785+
WHERE
786+
1
787+
and -50
788+
WHERE
789+
-50 = a
790+
WHERE
791+
a = -50
792+
WHERE
793+
1
794+
/*test*/
795+
-50
796+
WHERE
797+
1
798+
and -50;
799+
800+
SELECT
801+
@
802+
and b;
803+
781804
SELECT
782805
@"weird variable name";
783806

tests/compress.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868

6969
SELECT * LIMIT 1; SELECT a,b,c,d FROM e LIMIT 1, 2; SELECT 1,2,3 WHERE a in (1,2,3,4,5) and b=5;
7070

71+
SELECT count - 50 WHERE a-50 = b WHERE 1 and - 50 WHERE -50 = a WHERE a = -50 WHERE 1 - 50 WHERE 1 and -50;
72+
73+
SELECT @ and b;
74+
7175
SELECT @"weird variable name";
7276

7377
SELECT "no closing quote

tests/format-highlight.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,29 @@
778778
<span style="color: #333;">a</span> <span style="font-weight:bold;">in</span> (<span style="color: green;">1</span><span >,</span> <span style="color: green;">2</span><span >,</span> <span style="color: green;">3</span><span >,</span> <span style="color: green;">4</span><span >,</span> <span style="color: green;">5</span>)
779779
<span style="font-weight:bold;">and</span> <span style="color: #333;">b</span> <span >=</span> <span style="color: green;">5</span><span >;</span></pre>
780780

781+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
782+
<span style="color: #333;">count</span> <span >-</span> <span style="color: green;">50</span>
783+
<span style="font-weight:bold;">WHERE</span>
784+
<span style="color: #333;">a</span> <span >-</span> <span style="color: green;">50</span> <span >=</span> <span style="color: #333;">b</span>
785+
<span style="font-weight:bold;">WHERE</span>
786+
<span style="color: green;">1</span>
787+
<span style="font-weight:bold;">and</span> <span >-</span><span style="color: green;">50</span>
788+
<span style="font-weight:bold;">WHERE</span>
789+
<span >-</span><span style="color: green;">50</span> <span >=</span> <span style="color: #333;">a</span>
790+
<span style="font-weight:bold;">WHERE</span>
791+
<span style="color: #333;">a</span> <span >=</span> <span >-</span><span style="color: green;">50</span>
792+
<span style="font-weight:bold;">WHERE</span>
793+
<span style="color: green;">1</span>
794+
<span style="color: #aaa;">/*test*/</span>
795+
<span >-</span><span style="color: green;">50</span>
796+
<span style="font-weight:bold;">WHERE</span>
797+
<span style="color: green;">1</span>
798+
<span style="font-weight:bold;">and</span> <span >-</span><span style="color: green;">50</span><span >;</span></pre>
799+
800+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
801+
<span style="color: #333;">@</span>
802+
<span style="font-weight:bold;">and</span> <span style="color: #333;">b</span><span >;</span></pre>
803+
781804
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
782805
<span style="color: orange;">@&quot;weird variable name&quot;</span><span >;</span></pre>
783806

tests/format.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,29 @@
777777
a in (1, 2, 3, 4, 5)
778778
and b = 5;
779779

780+
SELECT
781+
count - 50
782+
WHERE
783+
a - 50 = b
784+
WHERE
785+
1
786+
and -50
787+
WHERE
788+
-50 = a
789+
WHERE
790+
a = -50
791+
WHERE
792+
1
793+
/*test*/
794+
-50
795+
WHERE
796+
1
797+
and -50;
798+
799+
SELECT
800+
@
801+
and b;
802+
780803
SELECT
781804
@"weird variable name";
782805

tests/highlight.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@
246246

247247
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span >*</span> <span style="font-weight:bold;">LIMIT</span> <span style="color: green;">1</span><span >;</span> <span style="font-weight:bold;">SELECT</span> <span style="color: #333;">a</span><span >,</span><span style="color: #333;">b</span><span >,</span><span style="color: #333;">c</span><span >,</span><span style="color: #333;">d</span> <span style="font-weight:bold;">FROM</span> <span style="color: #333;">e</span> <span style="font-weight:bold;">LIMIT</span> <span style="color: green;">1</span><span >,</span> <span style="color: green;">2</span><span >;</span> <span style="font-weight:bold;">SELECT</span> <span style="color: green;">1</span><span >,</span><span style="color: green;">2</span><span >,</span><span style="color: green;">3</span> <span style="font-weight:bold;">WHERE</span> <span style="color: #333;">a</span> <span style="font-weight:bold;">in</span> (<span style="color: green;">1</span><span >,</span><span style="color: green;">2</span><span >,</span><span style="color: green;">3</span><span >,</span><span style="color: green;">4</span><span >,</span><span style="color: green;">5</span>) <span style="font-weight:bold;">and</span> <span style="color: #333;">b</span><span >=</span><span style="color: green;">5</span><span >;</span></pre>
248248

249+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #333;">count</span> <span >-</span> <span style="color: green;">50</span>
250+
<span style="font-weight:bold;">WHERE</span> <span style="color: #333;">a</span><span >-</span><span style="color: green;">50</span> <span >=</span> <span style="color: #333;">b</span>
251+
<span style="font-weight:bold;">WHERE</span> <span style="color: green;">1</span> <span style="font-weight:bold;">and</span> <span >-</span> <span style="color: green;">50</span>
252+
<span style="font-weight:bold;">WHERE</span> <span >-</span><span style="color: green;">50</span> <span >=</span> <span style="color: #333;">a</span>
253+
<span style="font-weight:bold;">WHERE</span> <span style="color: #333;">a</span> <span >=</span> <span >-</span><span style="color: green;">50</span>
254+
<span style="font-weight:bold;">WHERE</span> <span style="color: green;">1</span> <span style="color: #aaa;">/*test*/</span> <span >-</span> <span style="color: green;">50</span>
255+
<span style="font-weight:bold;">WHERE</span> <span style="color: green;">1</span> <span style="font-weight:bold;">and</span> <span >-</span><span style="color: green;">50</span><span >;</span></pre>
256+
257+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #333;">@</span> <span style="font-weight:bold;">and</span> <span style="color: #333;">b</span><span >;</span></pre>
258+
249259
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: orange;">@&quot;weird variable name&quot;</span><span >;</span></pre>
250260

251261
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: blue;">&quot;no closing quote

tests/sql.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ SELECT Test FROM Test WHERE
246246

247247
SELECT * LIMIT 1; SELECT a,b,c,d FROM e LIMIT 1, 2; SELECT 1,2,3 WHERE a in (1,2,3,4,5) and b=5;
248248

249+
SELECT count - 50
250+
WHERE a-50 = b
251+
WHERE 1 and - 50
252+
WHERE -50 = a
253+
WHERE a = -50
254+
WHERE 1 /*test*/ - 50
255+
WHERE 1 and -50;
256+
257+
SELECT @ and b;
258+
249259
SELECT @"weird variable name";
250260

251261
SELECT "no closing quote

0 commit comments

Comments
 (0)