Skip to content

Commit

Permalink
Fix #447 Support TABLE statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Tithugues committed Jun 5, 2023
1 parent 5ac8e6f commit b9c52b3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down
84 changes: 84 additions & 0 deletions src/Statements/TableStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\SqlParser\Statements;

use PhpMyAdmin\SqlParser\Components\ArrayObj;
use PhpMyAdmin\SqlParser\Components\Condition;
use PhpMyAdmin\SqlParser\Components\Expression;
use PhpMyAdmin\SqlParser\Components\FunctionCall;
use PhpMyAdmin\SqlParser\Components\GroupKeyword;
use PhpMyAdmin\SqlParser\Components\IndexHint;
use PhpMyAdmin\SqlParser\Components\IntoKeyword;
use PhpMyAdmin\SqlParser\Components\JoinKeyword;
use PhpMyAdmin\SqlParser\Components\Limit;
use PhpMyAdmin\SqlParser\Components\OptionsArray;
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
use PhpMyAdmin\SqlParser\Statement;

/**
* `TABLE` statement.
*
* TABLE table_references
* [ORDER BY {col_name | expr | position}
* [ASC | DESC], ...]
* [LIMIT row_count [OFFSET offset]]
*/
class TableStatement extends Statement
{
/**
* The clauses of this statement, in order.
*
* @see Statement::$CLAUSES
*
* @var array<string, array<int, int|string>>
* @psalm-var array<string, array{non-empty-string, (1|2|3)}>
*/
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 = [];
}

0 comments on commit b9c52b3

Please sign in to comment.