Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #447 - Support TABLE statement #475

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
76 changes: 76 additions & 0 deletions src/Statements/TableStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\SqlParser\Statements;

use PhpMyAdmin\SqlParser\Components\Expression;
use PhpMyAdmin\SqlParser\Components\Limit;
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 = [];
}
50 changes: 40 additions & 10 deletions tests/data/parser/parseTable1.out
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,51 @@
"list": {
"@type": "@1"
},
"statements": [],
"statements": [
{
"@type": "PhpMyAdmin\\SqlParser\\Statements\\TableStatement",
"from": [],
"order": [
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
"expr": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
"database": null,
"table": "fo",
"column": "uuid",
"expr": "`fo`.`uuid`",
"alias": null,
"function": null,
"subquery": null
},
"type": "ASC"
}
],
"limit": null,
"union": [],
"options": null,
"first": 0,
"last": 10,
"tables": [
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
"database": null,
"table": "fo",
"column": null,
"expr": "`fo`",
"alias": null,
"function": null,
"subquery": null
}
]
}
],
"brackets": 0,
"strict": false,
"errors": []
},
"errors": {
"lexer": [],
"parser": [
[
"Unrecognized statement type.",
{
"@type": "@2"
},
0
]
]
"parser": []
}
}