From b9c52b3ff662c3ec7c9392c7d6a0b9144f41d00b Mon Sep 17 00:00:00 2001 From: Hugues Peccatte Date: Mon, 5 Jun 2023 15:08:35 +0200 Subject: [PATCH] Fix #447 Support TABLE statement --- src/Parser.php | 23 ++++++--- src/Statements/TableStatement.php | 84 +++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 src/Statements/TableStatement.php diff --git a/src/Parser.php b/src/Parser.php index 759d58dcd..4cdc1dbef 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -6,6 +6,7 @@ use PhpMyAdmin\SqlParser\Exceptions\ParserException; use PhpMyAdmin\SqlParser\Statements\SelectStatement; +use PhpMyAdmin\SqlParser\Statements\TableStatement; use PhpMyAdmin\SqlParser\Statements\TransactionStatement; use function is_string; @@ -70,6 +71,7 @@ class Parser extends Core 'LOAD DATA' => 'PhpMyAdmin\\SqlParser\\Statements\\LoadStatement', 'REPLACE' => 'PhpMyAdmin\\SqlParser\\Statements\\ReplaceStatement', 'SELECT' => 'PhpMyAdmin\\SqlParser\\Statements\\SelectStatement', + 'TABLE' => 'PhpMyAdmin\\SqlParser\\Statements\\TableStatement', 'UPDATE' => 'PhpMyAdmin\\SqlParser\\Statements\\UpdateStatement', 'WITH' => 'PhpMyAdmin\\SqlParser\\Statements\\WithStatement', @@ -310,6 +312,11 @@ class Parser extends Core 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', 'field' => 'expr', ], + 'TABLE' => [ + 'class' => 'PhpMyAdmin\\SqlParser\\Components\\ExpressionArray', + 'field' => 'tables', + 'options' => ['parseField' => 'table'], + ], 'TRUNCATE' => [ 'class' => 'PhpMyAdmin\\SqlParser\\Components\\Expression', 'field' => 'table', @@ -534,26 +541,28 @@ public function parse() // Handles unions. if ( ! empty($unionType) - && ($lastStatement instanceof SelectStatement) - && ($statement instanceof SelectStatement) + && ( + ($lastStatement instanceof SelectStatement && $statement instanceof SelectStatement) + || ($lastStatement instanceof TableStatement && $statement instanceof TableStatement) + ) ) { /* - * This SELECT statement. + * This SELECT|TABLE statement. * - * @var SelectStatement $statement + * @var SelectStatement|TableStatement $statement */ /* - * Last SELECT statement. + * Last SELECT|TABLE statement. * - * @var SelectStatement $lastStatement + * @var SelectStatement|TableStatement $lastStatement */ $lastStatement->union[] = [ $unionType, $statement, ]; - // if there are no no delimiting brackets, the `ORDER` and + // if there are no delimiting brackets, the `ORDER` and // `LIMIT` keywords actually belong to the first statement. $lastStatement->order = $statement->order; $lastStatement->limit = $statement->limit; diff --git a/src/Statements/TableStatement.php b/src/Statements/TableStatement.php new file mode 100644 index 000000000..337dc3b91 --- /dev/null +++ b/src/Statements/TableStatement.php @@ -0,0 +1,84 @@ +> + * @psalm-var array + */ + public static $CLAUSES = [ + 'TABLE' => [ + 'TABLE', + 3, + ], + 'ORDER BY' => [ + 'ORDER BY', + 3, + ], + 'LIMIT' => [ + 'LIMIT', + 3, + ], + 'UNION' => [ + 'UNION', + 1, + ], + ]; + + /** + * Tables used as sources for this statement. + * + * @var Expression[] + */ + public $from = []; + + /** + * Specifies the order of the rows in the result set. + * + * @var OrderKeyword[]|null + */ + public $order; + + /** + * Conditions used for limiting the size of the result set. + * + * @var Limit|null + */ + public $limit; + + /** + * Unions. + * + * @var TableStatement[] + */ + public $union = []; +}