Skip to content

Commit

Permalink
Recognize question mark as parameter, add formatter for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
srsbiz committed Apr 19, 2019
1 parent 0eb16ef commit 3adf389
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public static function isSymbol($str)
return Token::FLAG_SYMBOL_VARIABLE;
} elseif ($str[0] === '`') {
return Token::FLAG_SYMBOL_BACKTICK;
} elseif ($str[0] === ':') {
} elseif ($str[0] === ':' || $str[0] === '?') {
return Token::FLAG_SYMBOL_PARAMETER;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ public function parseSymbol()
$flags |= Token::FLAG_SYMBOL_SYSTEM;
}
} elseif ($flags & Token::FLAG_SYMBOL_PARAMETER) {
if ($this->last + 1 < $this->len) {
if ('?' !== $token && $this->last + 1 < $this->len) {
++$this->last;
}
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/Utils/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ protected function getDefaultFormats()
'cli' => "\x1b[91m",
'function' => '',
),
array(
'type' => Token::TYPE_SYMBOL,
'flags' => Token::FLAG_SYMBOL_PARAMETER,
'html' => 'class="sql-parameter"',
'cli' => "\x1b[31m",
'function' => '',
),
array(
'type' => Token::TYPE_SYMBOL,
'flags' => 0,
Expand Down
6 changes: 6 additions & 0 deletions tests/Lexer/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public function testExtractSymbol()

$tok = new Token('@`foo`', Token::TYPE_SYMBOL, Token::FLAG_SYMBOL_VARIABLE);
$this->assertEquals($tok->value, 'foo');

$tok = new Token(':foo', Token::TYPE_SYMBOL, Token::FLAG_SYMBOL_PARAMETER);
$this->assertEquals($tok->value, 'foo');

$tok = new Token('?', Token::TYPE_SYMBOL, Token::FLAG_SYMBOL_PARAMETER);
$this->assertEquals($tok->value, '?');
}

public function testInlineToken()
Expand Down
42 changes: 42 additions & 0 deletions tests/Utils/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,48 @@ public function formatQueries()
"\x1b[0m",
'html' => '<span class="sql-reserved">JOIN</span> tbl2 <span class="sql-reserved">ON</span> c1 = c2',
),
'named param' => array(
'query' => 'select * from tbl where col = :param',
'text' => 'SELECT' . "\n" .
' *' . "\n" .
'FROM' . "\n" .
' tbl' . "\n" .
'WHERE' . "\n" .
' col = :param',
'cli' => "\x1b[35mSELECT" . "\n" .
" \x1b[39m*" . "\n" .
"\x1b[35mFROM" . "\n" .
" \x1b[39mtbl" . "\n" .
"\x1b[35mWHERE" . "\n" .
" \x1b[39mcol = \x1b[31m:param" . "\x1b[0m",
'html' => '<span class="sql-reserved">SELECT</span>' . '<br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;*' . '<br/>' .
'<span class="sql-reserved">FROM</span>' . '<br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;tbl' . '<br/>' .
'<span class="sql-reserved">WHERE</span>' . '<br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;col = <span class="sql-parameter">:param</span>',
),
'anon param' => array(
'query' => 'select * from tbl where col = ?',
'text' => 'SELECT' . "\n" .
' *' . "\n" .
'FROM' . "\n" .
' tbl' . "\n" .
'WHERE' . "\n" .
' col = ?',
'cli' => "\x1b[35mSELECT" . "\n" .
" \x1b[39m*" . "\n" .
"\x1b[35mFROM" . "\n" .
" \x1b[39mtbl" . "\n" .
"\x1b[35mWHERE" . "\n" .
" \x1b[39mcol = \x1b[31m?" . "\x1b[0m",
'html' => '<span class="sql-reserved">SELECT</span>' . '<br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;*' . '<br/>' .
'<span class="sql-reserved">FROM</span>' . '<br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;tbl' . '<br/>' .
'<span class="sql-reserved">WHERE</span>' . '<br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;col = <span class="sql-parameter">?</span>',
),
);
}
}

0 comments on commit 3adf389

Please sign in to comment.