Skip to content

Commit

Permalink
Add bin/sql-parser executable file
Browse files Browse the repository at this point in the history
- Related to #517

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jan 17, 2024
1 parent 4e288e8 commit 38959b6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

- Add `bin/sql-parser` executable file (#517)

## [5.8.2] - 2023-09-19

- Fix a regression with the ALTER operation (#511)
Expand Down
31 changes: 31 additions & 0 deletions bin/sql-parser
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

$files = [
__DIR__ . "/../vendor/autoload.php",
__DIR__ . "/../../vendor/autoload.php",
__DIR__ . "/../../../autoload.php",
"vendor/autoload.php"
];

$found = false;
foreach ($files as $file) {
if (file_exists($file)) {
require_once $file;
$found = true;
break;
}
}

if (! $found) {
die(
"You need to set up the project dependencies using the following commands:" . PHP_EOL .
"curl -sS https://getcomposer.org/installer | php" . PHP_EOL .
"php composer.phar install" . PHP_EOL
);
}

$cli = new PhpMyAdmin\SqlParser\Utils\CLI();
exit($cli->run());
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"bin": [
"bin/highlight-query",
"bin/lint-query",
"bin/sql-parser",
"bin/tokenize-query"
],
"autoload": {
Expand Down
66 changes: 48 additions & 18 deletions src/Utils/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@
*/
class CLI
{
public function run(): int

Check warning on line 27 in src/Utils/CLI.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/CLI.php#L27

Added line #L27 was not covered by tests
{
$params = $this->getopt('', ['lint', 'highlight', 'tokenize']);
if ($params !== false) {
if (isset($params['lint'])) {
return $this->runLint(false);

Check warning on line 32 in src/Utils/CLI.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/CLI.php#L29-L32

Added lines #L29 - L32 were not covered by tests
}

if (isset($params['highlight'])) {
return $this->runHighlight(false);

Check warning on line 36 in src/Utils/CLI.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/CLI.php#L35-L36

Added lines #L35 - L36 were not covered by tests
}

if (isset($params['tokenize'])) {
return $this->runTokenize(false);

Check warning on line 40 in src/Utils/CLI.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/CLI.php#L39-L40

Added lines #L39 - L40 were not covered by tests
}
}

$this->usageLint(false);
$this->usageHighlight(false);
$this->usageTokenize(false);

Check warning on line 46 in src/Utils/CLI.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/CLI.php#L44-L46

Added lines #L44 - L46 were not covered by tests

return 1;

Check warning on line 48 in src/Utils/CLI.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/CLI.php#L48

Added line #L48 was not covered by tests
}

/**
* @param string[]|false[] $params
* @param string[] $longopts
Expand All @@ -45,10 +69,12 @@ public function mergeLongOpts(&$params, &$longopts)
/**
* @return void
*/
public function usageHighlight()
public function usageHighlight(bool $isStandalone = true)
{
echo "Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]\n";
echo " cat file.sql | highlight-query\n";
$command = $isStandalone ? 'highlight-query' : 'sql-parser --highlight';

echo 'Usage: ' . $command . ' --query SQL [--format html|cli|text] [--ansi]' . "\n";
echo ' cat file.sql | ' . $command . "\n";
}

/**
Expand Down Expand Up @@ -95,15 +121,15 @@ public function parseHighlight()
/**
* @return int
*/
public function runHighlight()
public function runHighlight(bool $isStandalone = true)
{
$params = $this->parseHighlight();
if ($params === false) {
return 1;
}

if (isset($params['h'])) {
$this->usageHighlight();
$this->usageHighlight($isStandalone);

return 0;
}
Expand Down Expand Up @@ -131,18 +157,20 @@ public function runHighlight()
}

echo "ERROR: Missing parameters!\n";
$this->usageHighlight();
$this->usageHighlight($isStandalone);

return 1;
}

/**
* @return void
*/
public function usageLint()
public function usageLint(bool $isStandalone = true)
{
echo "Usage: lint-query --query SQL [--ansi]\n";
echo " cat file.sql | lint-query\n";
$command = $isStandalone ? 'lint-query' : 'sql-parser --lint';

echo 'Usage: ' . $command . ' --query SQL [--ansi]' . "\n";
echo ' cat file.sql | ' . $command . "\n";
}

/**
Expand All @@ -165,15 +193,15 @@ public function parseLint()
/**
* @return int
*/
public function runLint()
public function runLint(bool $isStandalone = true)
{
$params = $this->parseLint();
if ($params === false) {
return 1;
}

if (isset($params['h'])) {
$this->usageLint();
$this->usageLint($isStandalone);

return 0;
}
Expand Down Expand Up @@ -210,18 +238,20 @@ public function runLint()
}

echo "ERROR: Missing parameters!\n";
$this->usageLint();
$this->usageLint($isStandalone);

return 1;
}

/**
* @return void
*/
public function usageTokenize()
public function usageTokenize(bool $isStandalone = true)
{
echo "Usage: tokenize-query --query SQL [--ansi]\n";
echo " cat file.sql | tokenize-query\n";
$command = $isStandalone ? 'tokenize-query' : 'sql-parser --tokenize';

echo 'Usage: ' . $command . ' --query SQL [--ansi]' . "\n";
echo ' cat file.sql | ' . $command . "\n";
}

/**
Expand All @@ -243,15 +273,15 @@ public function parseTokenize()
/**
* @return int
*/
public function runTokenize()
public function runTokenize(bool $isStandalone = true)
{
$params = $this->parseTokenize();
if ($params === false) {
return 1;
}

if (isset($params['h'])) {
$this->usageTokenize();
$this->usageTokenize($isStandalone);

return 0;
}
Expand Down Expand Up @@ -287,7 +317,7 @@ public function runTokenize()
}

echo "ERROR: Missing parameters!\n";
$this->usageTokenize();
$this->usageTokenize($isStandalone);

return 1;
}
Expand Down

0 comments on commit 38959b6

Please sign in to comment.