Skip to content

Commit 5114843

Browse files
committed
Add utility to tokenize query
It's mostly useful for debugging changes in parser. See #147. Signed-off-by: Michal Čihař <michal@cihar.com>
1 parent f3f36d2 commit 5114843

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ Command line utility to lint SQL query:
3939
./vendor/bin/lint-query --query "SELECT 1"
4040
```
4141

42+
Command line utility to tokenize SQL query:
43+
44+
```sh
45+
./vendor/bin/tokenize-query --query "SELECT 1"
46+
```
47+
4248
### Formatting SQL query
4349

4450
```php

bin/tokenize-query

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$files = array(
5+
__DIR__ . "/../vendor/autoload.php",
6+
__DIR__ . "/../../vendor/autoload.php",
7+
__DIR__ . "/../../../autoload.php",
8+
"vendor/autoload.php"
9+
);
10+
11+
$found = false;
12+
foreach ($files as $file) {
13+
if (file_exists($file)) {
14+
require_once $file;
15+
$found = true;
16+
break;
17+
}
18+
}
19+
20+
if (!$found) {
21+
die(
22+
"You need to set up the project dependencies using the following commands:" . PHP_EOL .
23+
"curl -sS https://getcomposer.org/installer | php" . PHP_EOL .
24+
"php composer.phar install" . PHP_EOL
25+
);
26+
}
27+
28+
$cli = new PhpMyAdmin\SqlParser\Utils\CLI();
29+
exit($cli->runTokenize());

src/Utils/CLI.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,54 @@ public function runLint()
130130

131131
return 1;
132132
}
133+
134+
public function usageTokenize()
135+
{
136+
echo "Usage: tokenize-query --query SQL\n";
137+
}
138+
139+
public function parseTokenize()
140+
{
141+
$longopts = array('help', 'query:');
142+
$params = $this->getopt(
143+
'hq:', $longopts
144+
);
145+
$this->mergeLongOpts($params, $longopts);
146+
147+
return $params;
148+
}
149+
150+
public function runTokenize()
151+
{
152+
$params = $this->parseTokenize();
153+
if ($params === false) {
154+
return 1;
155+
}
156+
if (isset($params['h'])) {
157+
$this->usageTokenize();
158+
159+
return 0;
160+
}
161+
if (isset($params['q'])) {
162+
$lexer = new Lexer($params['q'], false);
163+
foreach ($lexer->list->tokens as $idx => $token) {
164+
echo '[TOKEN ', $idx, "]\n";
165+
echo 'Type = ', $token->type, "\n";
166+
echo 'Flags = ', $token->flags, "\n";
167+
echo 'Value = ';
168+
var_export($token->value);
169+
echo "\n";
170+
echo 'Token = ';
171+
var_export($token->token);
172+
echo "\n";
173+
echo "\n";
174+
}
175+
176+
return 0;
177+
}
178+
echo "ERROR: Missing parameters!\n";
179+
$this->usageTokenize();
180+
181+
return 1;
182+
}
133183
}

tests/Utils/CLITest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,57 @@ public function lintParams()
142142
),
143143
);
144144
}
145+
146+
/**
147+
* @dataProvider tokenizeParams
148+
*
149+
* @param mixed $getopt
150+
* @param mixed $output
151+
* @param mixed $result
152+
*/
153+
public function testRunTokenize($getopt, $output, $result)
154+
{
155+
$cli = $this->getCLI($getopt);
156+
$this->expectOutputString($output);
157+
$this->assertEquals($result, $cli->runTokenize());
158+
}
159+
160+
public function tokenizeParams()
161+
{
162+
$result = (
163+
"[TOKEN 0]\nType = 1\nFlags = 3\nValue = 'SELECT'\nToken = 'SELECT'\n\n"
164+
. "[TOKEN 1]\nType = 3\nFlags = 0\nValue = ' '\nToken = ' '\n\n"
165+
. "[TOKEN 2]\nType = 6\nFlags = 0\nValue = 1\nToken = '1'\n\n"
166+
. "[TOKEN 3]\nType = 9\nFlags = 0\nValue = NULL\nToken = NULL\n\n"
167+
);
168+
169+
return array(
170+
array(
171+
array('q' => 'SELECT 1'),
172+
$result,
173+
0,
174+
),
175+
array(
176+
array('query' => 'SELECT 1'),
177+
$result,
178+
0,
179+
),
180+
array(
181+
array('h' => true),
182+
'Usage: tokenize-query --query SQL' . "\n",
183+
0,
184+
),
185+
array(
186+
array(),
187+
'ERROR: Missing parameters!' . "\n" .
188+
'Usage: tokenize-query --query SQL' . "\n",
189+
1,
190+
),
191+
array(
192+
false,
193+
'',
194+
1,
195+
),
196+
);
197+
}
145198
}

0 commit comments

Comments
 (0)