diff options
author | Michal Čihař <michal@cihar.com> | 2016-03-18 11:37:18 +0100 |
---|---|---|
committer | Michal Čihař <michal@cihar.com> | 2016-03-18 11:37:18 +0100 |
commit | 8956cfb15641a19aeba8b3f040124284a39e8b89 (patch) | |
tree | 3cabd04558c2af6c39916daef3e68c1239b8b8fb /src | |
parent | 1053b3a81c302fc87db52f1f5d9ae4191a3bc0f6 (diff) | |
download | sql-parser-8956cfb15641a19aeba8b3f040124284a39e8b89.zip sql-parser-8956cfb15641a19aeba8b3f040124284a39e8b89.tar.gz sql-parser-8956cfb15641a19aeba8b3f040124284a39e8b89.tar.bz2 |
Add command line query linter
Signed-off-by: Michal Čihař <michal@cihar.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/Utils/CLI.php | 59 |
1 files changed, 52 insertions, 7 deletions
diff --git a/src/Utils/CLI.php b/src/Utils/CLI.php index b654016..306de04 100644 --- a/src/Utils/CLI.php +++ b/src/Utils/CLI.php @@ -8,6 +8,9 @@ */ namespace SqlParser\Utils; +use SqlParser\Parser; +use SqlParser\Lexer; + /** * CLI interface * @@ -19,11 +22,6 @@ namespace SqlParser\Utils; */ class CLI { - public function usage() - { - echo "Usage: highlight-query --query SQL [--format html|cli|text]\n"; - } - public function mergeLongOpts(&$params, &$longopts) { foreach ($longopts as $value) { @@ -34,6 +32,11 @@ class CLI } } + public function usageHighlight() + { + echo "Usage: highlight-query --query SQL [--format html|cli|text]\n"; + } + public function parseHighlight() { $longopts = array('help', 'query:', 'format:'); @@ -58,7 +61,7 @@ class CLI return 1; } if (isset($params['h'])) { - $this->usage(); + $this->usageHighlight(); return 0; } if (isset($params['q'])) { @@ -69,7 +72,49 @@ class CLI return 0; } echo "ERROR: Missing parameters!\n"; - $this->usage(); + $this->usageHighlight(); + return 1; + } + + public function usageLint() + { + echo "Usage: lint-query --query SQL\n"; + } + + public function parseLint() + { + $longopts = array('help', 'query:'); + $params = getopt( + 'hq:', $longopts + ); + $this->mergeLongOpts($params, $longopts); + return $params; + } + + public function runLint() + { + $params = $this->parseLint(); + if ($params === false) { + return 1; + } + if (isset($params['h'])) { + $this->usageLint(); + return 0; + } + if (isset($params['q'])) { + $lexer = new Lexer($params['q'], false); + $parser = new Parser($lexer->list); + $errors = Error::get(array($lexer, $parser)); + if (count($errors) == 0) { + return 0; + } + $output = Error::format($errors); + echo implode("\n", $output); + echo "\n"; + return 10; + } + echo "ERROR: Missing parameters!\n"; + $this->usageLint(); return 1; } } |