Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bin/sql-parser executable file #537

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 5 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ parameters:

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

-
Expand All @@ -307,12 +307,12 @@ parameters:

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

-
message: "#^Cannot access offset 2 on mixed\\.$#"
count: 2
count: 1
path: src/Components/OptionsArray.php

-
Expand Down Expand Up @@ -847,7 +847,7 @@ parameters:

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

-
Expand All @@ -862,7 +862,7 @@ parameters:

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

-
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
{
$params = $this->getopt('', ['lint', 'highlight', 'tokenize']);
if ($params !== false) {
if (isset($params['lint'])) {
return $this->runLint(false);
}

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

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

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

return 1;
}

/**
* @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
Loading