Skip to content

Commit bb01d8a

Browse files
committed
Add bin/sql-parser executable file
- Related to #517 Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 4e288e8 commit bb01d8a

File tree

5 files changed

+87
-23
lines changed

5 files changed

+87
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [5.9.x] - YYYY-MM-DD
44

5+
- Add `bin/sql-parser` executable file (#517)
6+
57
## [5.8.2] - 2023-09-19
68

79
- Fix a regression with the ALTER operation (#511)

bin/sql-parser

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

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"bin": [
5050
"bin/highlight-query",
5151
"bin/lint-query",
52+
"bin/sql-parser",
5253
"bin/tokenize-query"
5354
],
5455
"autoload": {

phpstan-baseline.neon

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ parameters:
292292

293293
-
294294
message: "#^Cannot access offset 'expr' on mixed\\.$#"
295-
count: 4
295+
count: 2
296296
path: src/Components/OptionsArray.php
297297

298298
-
@@ -307,12 +307,12 @@ parameters:
307307

308308
-
309309
message: "#^Cannot access offset 1 on mixed\\.$#"
310-
count: 6
310+
count: 2
311311
path: src/Components/OptionsArray.php
312312

313313
-
314314
message: "#^Cannot access offset 2 on mixed\\.$#"
315-
count: 2
315+
count: 1
316316
path: src/Components/OptionsArray.php
317317

318318
-
@@ -847,7 +847,7 @@ parameters:
847847

848848
-
849849
message: "#^Cannot access offset 'c' on mixed\\.$#"
850-
count: 2
850+
count: 1
851851
path: src/Utils/CLI.php
852852

853853
-
@@ -862,7 +862,7 @@ parameters:
862862

863863
-
864864
message: "#^Cannot access offset 'q' on mixed\\.$#"
865-
count: 12
865+
count: 9
866866
path: src/Utils/CLI.php
867867

868868
-

src/Utils/CLI.php

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@
2424
*/
2525
class CLI
2626
{
27+
public function run(): int
28+
{
29+
$params = $this->getopt('', ['lint', 'highlight', 'tokenize']);
30+
if ($params !== false) {
31+
if (isset($params['lint'])) {
32+
return $this->runLint(false);
33+
}
34+
35+
if (isset($params['highlight'])) {
36+
return $this->runHighlight(false);
37+
}
38+
39+
if (isset($params['tokenize'])) {
40+
return $this->runTokenize(false);
41+
}
42+
}
43+
44+
$this->usageLint(false);
45+
$this->usageHighlight(false);
46+
$this->usageTokenize(false);
47+
48+
return 1;
49+
}
50+
2751
/**
2852
* @param string[]|false[] $params
2953
* @param string[] $longopts
@@ -45,10 +69,12 @@ public function mergeLongOpts(&$params, &$longopts)
4569
/**
4670
* @return void
4771
*/
48-
public function usageHighlight()
72+
public function usageHighlight(bool $isStandalone = true)
4973
{
50-
echo "Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]\n";
51-
echo " cat file.sql | highlight-query\n";
74+
$command = $isStandalone ? 'highlight-query' : 'sql-parser --highlight';
75+
76+
echo 'Usage: ' . $command . ' --query SQL [--format html|cli|text] [--ansi]' . "\n";
77+
echo ' cat file.sql | ' . $command . "\n";
5278
}
5379

5480
/**
@@ -95,15 +121,15 @@ public function parseHighlight()
95121
/**
96122
* @return int
97123
*/
98-
public function runHighlight()
124+
public function runHighlight(bool $isStandalone = true)
99125
{
100126
$params = $this->parseHighlight();
101127
if ($params === false) {
102128
return 1;
103129
}
104130

105131
if (isset($params['h'])) {
106-
$this->usageHighlight();
132+
$this->usageHighlight($isStandalone);
107133

108134
return 0;
109135
}
@@ -131,18 +157,20 @@ public function runHighlight()
131157
}
132158

133159
echo "ERROR: Missing parameters!\n";
134-
$this->usageHighlight();
160+
$this->usageHighlight($isStandalone);
135161

136162
return 1;
137163
}
138164

139165
/**
140166
* @return void
141167
*/
142-
public function usageLint()
168+
public function usageLint(bool $isStandalone = true)
143169
{
144-
echo "Usage: lint-query --query SQL [--ansi]\n";
145-
echo " cat file.sql | lint-query\n";
170+
$command = $isStandalone ? 'lint-query' : 'sql-parser --lint';
171+
172+
echo 'Usage: ' . $command . ' --query SQL [--ansi]' . "\n";
173+
echo ' cat file.sql | ' . $command . "\n";
146174
}
147175

148176
/**
@@ -165,15 +193,15 @@ public function parseLint()
165193
/**
166194
* @return int
167195
*/
168-
public function runLint()
196+
public function runLint(bool $isStandalone = true)
169197
{
170198
$params = $this->parseLint();
171199
if ($params === false) {
172200
return 1;
173201
}
174202

175203
if (isset($params['h'])) {
176-
$this->usageLint();
204+
$this->usageLint($isStandalone);
177205

178206
return 0;
179207
}
@@ -210,18 +238,20 @@ public function runLint()
210238
}
211239

212240
echo "ERROR: Missing parameters!\n";
213-
$this->usageLint();
241+
$this->usageLint($isStandalone);
214242

215243
return 1;
216244
}
217245

218246
/**
219247
* @return void
220248
*/
221-
public function usageTokenize()
249+
public function usageTokenize(bool $isStandalone = true)
222250
{
223-
echo "Usage: tokenize-query --query SQL [--ansi]\n";
224-
echo " cat file.sql | tokenize-query\n";
251+
$command = $isStandalone ? 'tokenize-query' : 'sql-parser --tokenize';
252+
253+
echo 'Usage: ' . $command . ' --query SQL [--ansi]' . "\n";
254+
echo ' cat file.sql | ' . $command . "\n";
225255
}
226256

227257
/**
@@ -243,15 +273,15 @@ public function parseTokenize()
243273
/**
244274
* @return int
245275
*/
246-
public function runTokenize()
276+
public function runTokenize(bool $isStandalone = true)
247277
{
248278
$params = $this->parseTokenize();
249279
if ($params === false) {
250280
return 1;
251281
}
252282

253283
if (isset($params['h'])) {
254-
$this->usageTokenize();
284+
$this->usageTokenize($isStandalone);
255285

256286
return 0;
257287
}
@@ -287,7 +317,7 @@ public function runTokenize()
287317
}
288318

289319
echo "ERROR: Missing parameters!\n";
290-
$this->usageTokenize();
320+
$this->usageTokenize($isStandalone);
291321

292322
return 1;
293323
}

0 commit comments

Comments
 (0)