From bb01d8a62334922c5673e918661f1ace6b192433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 16 Jan 2024 22:12:10 -0300 Subject: [PATCH] Add bin/sql-parser executable file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Related to #517 Signed-off-by: MaurĂ­cio Meneghini Fauth --- CHANGELOG.md | 2 ++ bin/sql-parser | 31 ++++++++++++++++++++ composer.json | 1 + phpstan-baseline.neon | 10 +++---- src/Utils/CLI.php | 66 +++++++++++++++++++++++++++++++------------ 5 files changed, 87 insertions(+), 23 deletions(-) create mode 100755 bin/sql-parser diff --git a/CHANGELOG.md b/CHANGELOG.md index 5143ab76d..f30bedb9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/bin/sql-parser b/bin/sql-parser new file mode 100755 index 000000000..7850f75a8 --- /dev/null +++ b/bin/sql-parser @@ -0,0 +1,31 @@ +#!/usr/bin/env php +run()); diff --git a/composer.json b/composer.json index 15b94e51a..88b322122 100644 --- a/composer.json +++ b/composer.json @@ -49,6 +49,7 @@ "bin": [ "bin/highlight-query", "bin/lint-query", + "bin/sql-parser", "bin/tokenize-query" ], "autoload": { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index cb9ed9efc..419731eec 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -292,7 +292,7 @@ parameters: - message: "#^Cannot access offset 'expr' on mixed\\.$#" - count: 4 + count: 2 path: src/Components/OptionsArray.php - @@ -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 - @@ -847,7 +847,7 @@ parameters: - message: "#^Cannot access offset 'c' on mixed\\.$#" - count: 2 + count: 1 path: src/Utils/CLI.php - @@ -862,7 +862,7 @@ parameters: - message: "#^Cannot access offset 'q' on mixed\\.$#" - count: 12 + count: 9 path: src/Utils/CLI.php - diff --git a/src/Utils/CLI.php b/src/Utils/CLI.php index f4c148861..99e237b0e 100644 --- a/src/Utils/CLI.php +++ b/src/Utils/CLI.php @@ -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 @@ -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"; } /** @@ -95,7 +121,7 @@ public function parseHighlight() /** * @return int */ - public function runHighlight() + public function runHighlight(bool $isStandalone = true) { $params = $this->parseHighlight(); if ($params === false) { @@ -103,7 +129,7 @@ public function runHighlight() } if (isset($params['h'])) { - $this->usageHighlight(); + $this->usageHighlight($isStandalone); return 0; } @@ -131,7 +157,7 @@ public function runHighlight() } echo "ERROR: Missing parameters!\n"; - $this->usageHighlight(); + $this->usageHighlight($isStandalone); return 1; } @@ -139,10 +165,12 @@ public function runHighlight() /** * @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"; } /** @@ -165,7 +193,7 @@ public function parseLint() /** * @return int */ - public function runLint() + public function runLint(bool $isStandalone = true) { $params = $this->parseLint(); if ($params === false) { @@ -173,7 +201,7 @@ public function runLint() } if (isset($params['h'])) { - $this->usageLint(); + $this->usageLint($isStandalone); return 0; } @@ -210,7 +238,7 @@ public function runLint() } echo "ERROR: Missing parameters!\n"; - $this->usageLint(); + $this->usageLint($isStandalone); return 1; } @@ -218,10 +246,12 @@ public function runLint() /** * @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"; } /** @@ -243,7 +273,7 @@ public function parseTokenize() /** * @return int */ - public function runTokenize() + public function runTokenize(bool $isStandalone = true) { $params = $this->parseTokenize(); if ($params === false) { @@ -251,7 +281,7 @@ public function runTokenize() } if (isset($params['h'])) { - $this->usageTokenize(); + $this->usageTokenize($isStandalone); return 0; } @@ -287,7 +317,7 @@ public function runTokenize() } echo "ERROR: Missing parameters!\n"; - $this->usageTokenize(); + $this->usageTokenize($isStandalone); return 1; }