From aa034bb0c9f3757b919cfc89056380b84e66e1ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Fri, 18 Mar 2016 12:06:27 +0100 Subject: [PATCH] Update SQL parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - issue #12026 Fixed parsing of UNION SELECT with brackets Signed-off-by: Michal Čihař --- ChangeLog | 1 + .../sql-parser/src/Components/Condition.php | 3 + libraries/sql-parser/src/Utils/CLI.php | 120 ++++++++++++++++++ libraries/sql-parser/src/Utils/Formatter.php | 4 + 4 files changed, 128 insertions(+) create mode 100644 libraries/sql-parser/src/Utils/CLI.php diff --git a/ChangeLog b/ChangeLog index b4361ccd14..3e0a921d82 100644 --- a/ChangeLog +++ b/ChangeLog @@ -61,6 +61,7 @@ phpMyAdmin - ChangeLog - issue #12094 PHP Fatal error: Call to undefined function __() - issue #12098 Fix login after logout with http authentication - issue #12074 Fixed possible invalid SQL export +- issue #12026 Fixed parsing of UNION SELECT with brackets 4.5.5.1 (2016-02-29) - issue #11971 CREATE UNIQUE INDEX index type is not recognized by parser. diff --git a/libraries/sql-parser/src/Components/Condition.php b/libraries/sql-parser/src/Components/Condition.php index 65374ccbd2..b902121c84 100644 --- a/libraries/sql-parser/src/Components/Condition.php +++ b/libraries/sql-parser/src/Components/Condition.php @@ -183,6 +183,9 @@ class Condition extends Component if ($token->value === '(') { ++$brackets; } elseif ($token->value === ')') { + if ($brackets == 0) { + break; + } --$brackets; } } diff --git a/libraries/sql-parser/src/Utils/CLI.php b/libraries/sql-parser/src/Utils/CLI.php new file mode 100644 index 0000000000..306de04f2b --- /dev/null +++ b/libraries/sql-parser/src/Utils/CLI.php @@ -0,0 +1,120 @@ + + * @license http://opensource.org/licenses/GPL-2.0 GNU Public License + */ +class CLI +{ + public function mergeLongOpts(&$params, &$longopts) + { + foreach ($longopts as $value) { + $value = rtrim($value, ':'); + if (isset($params[$value])) { + $params[$value[0]] = $params[$value]; + } + } + } + + public function usageHighlight() + { + echo "Usage: highlight-query --query SQL [--format html|cli|text]\n"; + } + + public function parseHighlight() + { + $longopts = array('help', 'query:', 'format:'); + $params = getopt( + 'hq:f:', $longopts + ); + $this->mergeLongOpts($params, $longopts); + if (! isset($params['f'])) { + $params['f'] = 'cli'; + } + if (! in_array($params['f'], array('html', 'cli', 'text'))) { + echo "ERROR: Invalid value for format!\n"; + return false; + } + return $params; + } + + public function runHighlight() + { + $params = $this->parseHighlight(); + if ($params === false) { + return 1; + } + if (isset($params['h'])) { + $this->usageHighlight(); + 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->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; + } +} diff --git a/libraries/sql-parser/src/Utils/Formatter.php b/libraries/sql-parser/src/Utils/Formatter.php index 2ac7e3b4bd..56d4bcb437 100644 --- a/libraries/sql-parser/src/Utils/Formatter.php +++ b/libraries/sql-parser/src/Utils/Formatter.php @@ -408,6 +408,10 @@ class Formatter $prev = $curr; } + if ($this->options['type'] === 'cli') { + return $ret . "\e[0m"; + } + return $ret; }