Skip to content

Fix #430 - ALTER TABLE … RENAME COLUMN … TO … is not understood by the parser/linter #432

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

Merged
merged 2 commits into from
Mar 13, 2023
Merged
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
21 changes: 19 additions & 2 deletions src/Components/AlterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function in_array;
use function is_numeric;
use function is_string;
use function trim;

/**
* Parses an alter operation.
Expand Down Expand Up @@ -381,6 +382,13 @@ public static function parse(Parser $parser, TokensList $list, array $options =
--$list->idx;
}

// If the operation is a RENAME COLUMN, now we have detected the field to rename, we need to parse
// again the options to get the new name of the column.
if ($ret->options->has('RENAME') && $ret->options->has('COLUMN')) {
$nextOptions = OptionsArray::parse($parser, $list, $options);
$ret->options->merge($nextOptions);
}

$state = 2;
} elseif ($state === 2) {
if (is_string($token->value) || is_numeric($token->value)) {
Expand Down Expand Up @@ -485,18 +493,27 @@ public static function parse(Parser $parser, TokensList $list, array $options =
*/
public static function build($component, array $options = [])
{
// Specific case of RENAME COLUMN that insert the field between 2 options.
$afterFieldsOptions = new OptionsArray();
if ($component->options->has('RENAME') && $component->options->has('COLUMN')) {
$afterFieldsOptions = clone $component->options;
$afterFieldsOptions->remove('RENAME');
$afterFieldsOptions->remove('COLUMN');
$component->options->remove('TO');
}

$ret = $component->options . ' ';
if (isset($component->field) && ($component->field !== '')) {
$ret .= $component->field . ' ';
}

$ret .= TokensList::build($component->unknown);
$ret .= $afterFieldsOptions . TokensList::build($component->unknown);

if (isset($component->partitions)) {
$ret .= PartitionDefinition::build($component->partitions);
}

return $ret;
return trim($ret);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions tests/Builder/AlterStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpMyAdmin\SqlParser\Tests\Builder;

use Generator;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Tests\TestCase;

Expand Down Expand Up @@ -113,4 +114,44 @@ public function testBuilderEventWithDefiner(): void
$stmt = $parser->statements[0];
$this->assertEquals($query, $stmt->build());
}

/**
* @return Generator<string, array{string}>
*/
public static function provideBuilderForRenameColumn(): Generator
{
$query = 'ALTER TABLE myTable RENAME COLUMN a TO b';

yield 'Single RENAME COLUMN' => [$query];

$query = 'ALTER TABLE myTable RENAME COLUMN a TO b, RENAME COLUMN b TO a';

yield 'Multiple RENAME COLUMN' => [$query];

$query = 'ALTER TABLE myTable ' .
'RENAME COLUMN a TO b, ' .
'RENAME COLUMN b TO a, ' .
'RENAME INDEX oldIndex TO newIndex, ' .
'RENAME TO newTable';

yield 'Mixed RENAME COLUMN + RENAME INDEX + RENAME table' => [$query];

$query = 'ALTER TABLE myTable ' .
'RENAME TO newTable, ' .
'RENAME INDEX oldIndex TO newIndex, ' .
'RENAME COLUMN b TO a, ' .
'RENAME COLUMN a TO b';

yield 'Mixed RENAME table + RENAME INDEX + RENAME COLUMNS' => [$query];
}

/**
* @dataProvider provideBuilderForRenameColumn
*/
public function testBuilderRenameColumn(string $query): void
{
$parser = new Parser($query);
$stmt = $parser->statements[0];
$this->assertEquals($query, $stmt->build());
}
}
2 changes: 2 additions & 0 deletions tests/Parser/AlterStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public function alterProvider(): array
['parser/parseAlterEventOnScheduleEvery6'],
['parser/parseAlterEventWithDefiner'],
['parser/parseAlterEventWithOtherDefiners'],
['parser/parseAlterRenameColumn'],
['parser/parseAlterRenameColumns'],
];
}
}
1 change: 1 addition & 0 deletions tests/data/parser/parseAlterRenameColumn.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE myTable RENAME COLUMN foo TO bar;
248 changes: 248 additions & 0 deletions tests/data/parser/parseAlterRenameColumn.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
{
"query": "ALTER TABLE myTable RENAME COLUMN foo TO bar;\n",
"lexer": {
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
"str": "ALTER TABLE myTable RENAME COLUMN foo TO bar;\n",
"len": 46,
"last": 46,
"list": {
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
"tokens": [
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "ALTER",
"value": "ALTER",
"keyword": "ALTER",
"type": 1,
"flags": 3,
"position": 0
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 5
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "TABLE",
"value": "TABLE",
"keyword": "TABLE",
"type": 1,
"flags": 3,
"position": 6
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 11
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "myTable",
"value": "myTable",
"keyword": null,
"type": 0,
"flags": 0,
"position": 12
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 19
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "RENAME",
"value": "RENAME",
"keyword": "RENAME",
"type": 1,
"flags": 3,
"position": 20
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 26
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "COLUMN",
"value": "COLUMN",
"keyword": "COLUMN",
"type": 1,
"flags": 3,
"position": 27
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 33
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "foo",
"value": "foo",
"keyword": null,
"type": 0,
"flags": 0,
"position": 34
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 37
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "TO",
"value": "TO",
"keyword": "TO",
"type": 1,
"flags": 3,
"position": 38
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": " ",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 40
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "bar",
"value": "bar",
"keyword": null,
"type": 0,
"flags": 0,
"position": 41
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": ";",
"value": ";",
"keyword": null,
"type": 9,
"flags": 0,
"position": 44
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": "\n",
"value": " ",
"keyword": null,
"type": 3,
"flags": 0,
"position": 45
},
{
"@type": "PhpMyAdmin\\SqlParser\\Token",
"token": null,
"value": null,
"keyword": null,
"type": 9,
"flags": 0,
"position": null
}
],
"count": 18,
"idx": 18
},
"delimiter": ";",
"delimiterLen": 1,
"strict": false,
"errors": []
},
"parser": {
"@type": "PhpMyAdmin\\SqlParser\\Parser",
"list": {
"@type": "@1"
},
"statements": [
{
"@type": "PhpMyAdmin\\SqlParser\\Statements\\AlterStatement",
"table": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
"database": null,
"table": "myTable",
"column": null,
"expr": "myTable",
"alias": null,
"function": null,
"subquery": null
},
"altered": [
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation",
"options": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
"options": [
"RENAME",
"COLUMN",
{
"name": "TO",
"equals": false,
"expr": "bar",
"value": "bar"
}
]
},
"field": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
"database": null,
"table": null,
"column": "foo",
"expr": "foo",
"alias": null,
"function": null,
"subquery": null
},
"partitions": null,
"unknown": []
}
],
"options": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
"options": {
"3": "TABLE"
}
},
"first": 0,
"last": 15
}
],
"brackets": 0,
"strict": false,
"errors": []
},
"errors": {
"lexer": [],
"parser": []
}
}
4 changes: 4 additions & 0 deletions tests/data/parser/parseAlterRenameColumns.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE myTable RENAME COLUMN a TO b,
RENAME COLUMN b TO c,
RENAME COLUMN c TO d,
RENAME COLUMN d TO a;
Loading