diff options
author | Michal Čihař <michal@cihar.com> | 2016-03-18 11:16:02 +0100 |
---|---|---|
committer | Michal Čihař <michal@cihar.com> | 2016-03-18 11:16:02 +0100 |
commit | c258b56f669e13c041a754c8ca476b4ea8366e91 (patch) | |
tree | 6e04fbf11ce2532f36b9eb4dc96ac59bbf8336ca | |
parent | bc0fc19c67dab8759224d6323b477ea825843470 (diff) | |
download | sql-parser-c258b56f669e13c041a754c8ca476b4ea8366e91.zip sql-parser-c258b56f669e13c041a754c8ca476b4ea8366e91.tar.gz sql-parser-c258b56f669e13c041a754c8ca476b4ea8366e91.tar.bz2 |
Add command line script for highlighting SQL query
Signed-off-by: Michal Čihař <michal@cihar.com>
-rw-r--r-- | README.md | 8 | ||||
-rwxr-xr-x | bin/highlight-query | 29 | ||||
-rw-r--r-- | composer.json | 3 | ||||
-rw-r--r-- | src/Utils/CLI.php | 69 |
4 files changed, 109 insertions, 0 deletions
@@ -19,6 +19,14 @@ composer require phpmyadmin/sql-parser ## Usage +### Command line utility + +The package install command line utility to syntax highlight SQL query: + +```sh +./vendor/bin/highlight-query --query "SELECT 1" +``` + ### Formatting SQL query ```php diff --git a/bin/highlight-query b/bin/highlight-query new file mode 100755 index 0000000..fe916d9 --- /dev/null +++ b/bin/highlight-query @@ -0,0 +1,29 @@ +#!/usr/bin/env php +<?php + +$files = array( + __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 -s http://getcomposer.org/installer | php" . PHP_EOL . + "php composer.phar install" . PHP_EOL + ); +} + +$cli = new SqlParser\Utils\CLI(); +exit($cli->run()); diff --git a/composer.json b/composer.json index 6fb37ec..329cb16 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,9 @@ "phpunit/php-code-coverage": "~2.0 || ~3.0", "phpunit/phpunit": "~4.8 || ~5.1" }, + "bin": [ + "bin/highlight-query" + ], "autoload": { "psr-4": { "SqlParser\\": "src" diff --git a/src/Utils/CLI.php b/src/Utils/CLI.php new file mode 100644 index 0000000..77da9ac --- /dev/null +++ b/src/Utils/CLI.php @@ -0,0 +1,69 @@ +<?php + +/** + * CLI interface + * + * @package SqlParser + * @subpackage Utils + */ +namespace SqlParser\Utils; + +/** + * CLI interface + * + * @category Exceptions + * @package SqlParser + * @subpackage Utils + * @author Michal Čihař <michal@cihar.com> + * @license http://opensource.org/licenses/GPL-2.0 GNU Public License + */ +class CLI +{ + public function usage() + { + echo "Usage: highlight-query --query SQL [--format html|cli|text]\n"; + } + + public function parse() + { + $longopts = array('help', 'query:', 'format:'); + $params = getopt( + 'hq:f:', $longopts + ); + foreach ($longopts as $value) { + $value = rtrim($value, ':'); + if (isset($params[$value])) { + $params[$value[0]] = $params[$value]; + } + } + if (! isset($params['f'])) { + $params['f'] = 'cli'; + } + if (! in_array($params['f'], array('html', 'cli', 'text'))) { + return false; + } + return $params; + } + + public function run() + { + $params = $this->parse(); + if ($params === false) { + return 1; + } + if (isset($params['h'])) { + $this->usage(); + return 0; + } + if (isset($params['q'])) { + echo Formatter::format( + $params['q'], array('type' => $params['f']) + ); + echo "\n"; + return 0; + } + echo "ERROR: Missing parameters!\n"; + $this->usage(); + return 1; + } +} |