Skip to content

Commit

Permalink
Fix formatting of negative numbers (fixes #49).
Browse files Browse the repository at this point in the history
Improve phpunit code coverage.
  • Loading branch information
jdorn committed Nov 27, 2013
1 parent 96effdc commit 8859d66
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 17 deletions.
40 changes: 23 additions & 17 deletions lib/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @copyright 2013 Jeremy Dorn
* @license http://opensource.org/licenses/MIT
* @link http://github.com/jdorn/sql-formatter
* @version 1.2.15
* @version 1.2.16
*/
class SqlFormatter
{
Expand Down Expand Up @@ -241,33 +241,29 @@ protected static function getNextToken($string, $previous = null)
self::TOKEN_VALUE => self::getQuotedString($string)
);

// If a quote was opened, but doesn't have a closing quote, return the remaining string
if ($return[self::TOKEN_VALUE] === null) {
$return[self::TOKEN_VALUE] = $string;
}

return $return;
}

// User-defined Variable
if ($string[0] === '@' && isset($string[1])) {
$ret = array(
self::TOKEN_VALUE => null,
self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE
);

// If the variable name is quoted
if ($string[1]==='"' || $string[1]==='\'' || $string[1]==='`') {
return array(
self::TOKEN_VALUE => '@'.self::getQuotedString(substr($string,1)),
self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE
);
$ret[self::TOKEN_VALUE] = '@'.self::getQuotedString(substr($string,1));
}
// Non-quoted variable name
else {
preg_match('/^(@[a-zA-Z0-9\._\$]+)/',$string,$matches);
if ($matches) {
return array(
self::TOKEN_VALUE => $matches[1],
self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE
);
$ret[self::TOKEN_VALUE] = $matches[1];
}
}

if($ret[self::TOKEN_VALUE] !== null) return $ret;
}

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

protected static function getQuotedString($string)
{
$ret = null;

// 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)) {
return $matches[1];
$ret = $matches[1];
}

return null;
return $ret;
}

/**
Expand Down Expand Up @@ -696,6 +694,14 @@ public static function format($string, $highlight=true)
if ($token[self::TOKEN_VALUE] === '(' || $token[self::TOKEN_VALUE] === '.') {
$return = rtrim($return,' ');
}

// If this is the "-" of a negative number, it shouldn't have a space after it
if($token[self::TOKEN_VALUE] === '-' && isset($tokens[$i+1]) && $tokens[$i+1][self::TOKEN_TYPE] === self::TOKEN_TYPE_NUMBER && isset($tokens[$i-1])) {
$prev = $tokens[$i-1][self::TOKEN_TYPE];
if($prev !== self::TOKEN_TYPE_QUOTE && $prev !== self::TOKEN_TYPE_BACKTICK_QUOTE && $prev !== self::TOKEN_TYPE_WORD && $prev !== self::TOKEN_TYPE_NUMBER) {
$return = rtrim($return,' ');
}
}
}

// If there are unmatched parentheses
Expand Down
23 changes: 23 additions & 0 deletions tests/clihighlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,29 @@
a in (1, 2, 3, 4, 5)
and b = 5;

SELECT
count - 50
WHERE
a - 50 = b
WHERE
1
and -50
WHERE
-50 = a
WHERE
a = -50
WHERE
1
/*test*/
-50
WHERE
1
and -50;

SELECT
@
and b;

SELECT
@"weird variable name";

Expand Down
4 changes: 4 additions & 0 deletions tests/compress.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@

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;

SELECT count - 50 WHERE a-50 = b WHERE 1 and - 50 WHERE -50 = a WHERE a = -50 WHERE 1 - 50 WHERE 1 and -50;

SELECT @ and b;

SELECT @"weird variable name";

SELECT "no closing quote
23 changes: 23 additions & 0 deletions tests/format-highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,29 @@
<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>

<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>
<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>
<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 style="font-weight:bold;">WHERE</span>
<span >-</span><span style="color: green;">50</span> <span >=</span> <span style="color: #333;">a</span>
<span style="font-weight:bold;">WHERE</span>
<span style="color: #333;">a</span> <span >=</span> <span >-</span><span style="color: green;">50</span>
<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>
<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>

<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>

<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>

Expand Down
23 changes: 23 additions & 0 deletions tests/format.html
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,29 @@
a in (1, 2, 3, 4, 5)
and b = 5;

SELECT
count - 50
WHERE
a - 50 = b
WHERE
1
and -50
WHERE
-50 = a
WHERE
a = -50
WHERE
1
/*test*/
-50
WHERE
1
and -50;

SELECT
@
and b;

SELECT
@"weird variable name";

Expand Down
10 changes: 10 additions & 0 deletions tests/highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@

<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>

<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>
<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>
<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 style="font-weight:bold;">WHERE</span> <span >-</span><span style="color: green;">50</span> <span >=</span> <span style="color: #333;">a</span>
<span style="font-weight:bold;">WHERE</span> <span style="color: #333;">a</span> <span >=</span> <span >-</span><span style="color: green;">50</span>
<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>
<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>

<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>

<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>

<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: blue;">&quot;no closing quote
Expand Down
10 changes: 10 additions & 0 deletions tests/sql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ SELECT Test FROM Test WHERE

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;

SELECT count - 50
WHERE a-50 = b
WHERE 1 and - 50
WHERE -50 = a
WHERE a = -50
WHERE 1 /*test*/ - 50
WHERE 1 and -50;

SELECT @ and b;

SELECT @"weird variable name";

SELECT "no closing quote

0 comments on commit 8859d66

Please sign in to comment.