Skip to content

Commit 2823f3b

Browse files
committed
Update out files + fix ALTER TABLE … RENAME COLUMN operation.
1 parent 29d9ff7 commit 2823f3b

File tree

4 files changed

+103
-69
lines changed

4 files changed

+103
-69
lines changed

src/Components/AlterOperation.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function in_array;
1414
use function is_numeric;
1515
use function is_string;
16+
use function trim;
1617

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

385+
// If the operation is a RENAME COLUMN, now we have detected the field to rename, we need to parse
386+
// again the options to get the new name of the column.
387+
if ($ret->options->has('RENAME') && $ret->options->has('COLUMN')) {
388+
$nextOptions = OptionsArray::parse($parser, $list, $options);
389+
$ret->options->merge($nextOptions);
390+
}
391+
384392
$state = 2;
385393
} elseif ($state === 2) {
386394
if (is_string($token->value) || is_numeric($token->value)) {
@@ -485,18 +493,27 @@ public static function parse(Parser $parser, TokensList $list, array $options =
485493
*/
486494
public static function build($component, array $options = [])
487495
{
496+
// Specific case of RENAME COLUMN that insert the field between 2 options.
497+
$afterFieldsOptions = new OptionsArray();
498+
if ($component->options->has('RENAME') && $component->options->has('COLUMN')) {
499+
$afterFieldsOptions = clone $component->options;
500+
$afterFieldsOptions->remove('RENAME');
501+
$afterFieldsOptions->remove('COLUMN');
502+
$component->options->remove('TO');
503+
}
504+
488505
$ret = $component->options . ' ';
489506
if (isset($component->field) && ($component->field !== '')) {
490507
$ret .= $component->field . ' ';
491508
}
492509

493-
$ret .= TokensList::build($component->unknown);
510+
$ret .= $afterFieldsOptions . TokensList::build($component->unknown);
494511

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

499-
return $ret;
516+
return trim($ret);
500517
}
501518

502519
/**

tests/Builder/AlterStatementTest.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpMyAdmin\SqlParser\Tests\Builder;
66

7+
use Generator;
78
use PhpMyAdmin\SqlParser\Parser;
89
use PhpMyAdmin\SqlParser\Tests\TestCase;
910

@@ -114,18 +115,41 @@ public function testBuilderEventWithDefiner(): void
114115
$this->assertEquals($query, $stmt->build());
115116
}
116117

117-
118-
public function testBuilderRenameColumn(): void
118+
/**
119+
* @return Generator<string, array{string}>
120+
*/
121+
public static function provideBuilderForRenameColumn(): Generator
119122
{
120-
$query = 'ALTER TABLE myTable RENAME COLUMN a TO b;';
121-
$parser = new Parser($query);
122-
$stmt = $parser->statements[0];
123-
$this->assertEquals($query, $stmt->build());
123+
$query = 'ALTER TABLE myTable RENAME COLUMN a TO b';
124+
125+
yield 'Single RENAME COLUMN' => [$query];
126+
127+
$query = 'ALTER TABLE myTable RENAME COLUMN a TO b, RENAME COLUMN b TO a';
128+
129+
yield 'Multiple RENAME COLUMN' => [$query];
130+
131+
$query = 'ALTER TABLE myTable ' .
132+
'RENAME COLUMN a TO b, ' .
133+
'RENAME COLUMN b TO a, ' .
134+
'RENAME INDEX oldIndex TO newIndex, ' .
135+
'RENAME TO newTable';
136+
137+
yield 'Mixed RENAME COLUMN + RENAME INDEX + RENAME table' => [$query];
138+
139+
$query = 'ALTER TABLE myTable ' .
140+
'RENAME TO newTable, ' .
141+
'RENAME INDEX oldIndex TO newIndex, ' .
142+
'RENAME COLUMN b TO a, ' .
143+
'RENAME COLUMN a TO b';
144+
145+
yield 'Mixed RENAME table + RENAME INDEX + RENAME COLUMNS' => [$query];
124146
}
125147

126-
public function testBuilderRenameColumns(): void
148+
/**
149+
* @dataProvider provideBuilderForRenameColumn
150+
*/
151+
public function testBuilderRenameColumn(string $query): void
127152
{
128-
$query = 'ALTER TABLE myTable RENAME COLUMN a TO b, RENAME COLUMN b TO a;';
129153
$parser = new Parser($query);
130154
$stmt = $parser->statements[0];
131155
$this->assertEquals($query, $stmt->build());

tests/data/parser/parseAlterRenameColumn.out

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,16 @@
202202
"@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation",
203203
"options": {
204204
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
205-
"options": {
206-
"1": "RENAME",
207-
"2": "COLUMN"
208-
}
205+
"options": [
206+
"RENAME",
207+
"COLUMN",
208+
{
209+
"name": "TO",
210+
"equals": false,
211+
"expr": "bar",
212+
"value": "bar"
213+
}
214+
]
209215
},
210216
"field": {
211217
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
@@ -237,14 +243,6 @@
237243
},
238244
"errors": {
239245
"lexer": [],
240-
"parser": [
241-
[
242-
"Missing comma before start of a new alter operation.",
243-
{
244-
"@type": "@14"
245-
},
246-
0
247-
]
248-
]
246+
"parser": []
249247
}
250248
}

tests/data/parser/parseAlterRenameColumns.out

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,16 @@
499499
"@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation",
500500
"options": {
501501
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
502-
"options": {
503-
"1": "RENAME",
504-
"2": "COLUMN"
505-
}
502+
"options": [
503+
"RENAME",
504+
"COLUMN",
505+
{
506+
"name": "TO",
507+
"equals": false,
508+
"expr": "b",
509+
"value": "b"
510+
}
511+
]
506512
},
507513
"field": {
508514
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
@@ -521,10 +527,16 @@
521527
"@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation",
522528
"options": {
523529
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
524-
"options": {
525-
"1": "RENAME",
526-
"2": "COLUMN"
527-
}
530+
"options": [
531+
"RENAME",
532+
"COLUMN",
533+
{
534+
"name": "TO",
535+
"equals": false,
536+
"expr": "c",
537+
"value": "c"
538+
}
539+
]
528540
},
529541
"field": {
530542
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
@@ -543,10 +555,16 @@
543555
"@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation",
544556
"options": {
545557
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
546-
"options": {
547-
"1": "RENAME",
548-
"2": "COLUMN"
549-
}
558+
"options": [
559+
"RENAME",
560+
"COLUMN",
561+
{
562+
"name": "TO",
563+
"equals": false,
564+
"expr": "d",
565+
"value": "d"
566+
}
567+
]
550568
},
551569
"field": {
552570
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
@@ -565,10 +583,16 @@
565583
"@type": "PhpMyAdmin\\SqlParser\\Components\\AlterOperation",
566584
"options": {
567585
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
568-
"options": {
569-
"1": "RENAME",
570-
"2": "COLUMN"
571-
}
586+
"options": [
587+
"RENAME",
588+
"COLUMN",
589+
{
590+
"name": "TO",
591+
"equals": false,
592+
"expr": "a",
593+
"value": "a"
594+
}
595+
]
572596
},
573597
"field": {
574598
"@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
@@ -600,35 +624,6 @@
600624
},
601625
"errors": {
602626
"lexer": [],
603-
"parser": [
604-
[
605-
"Missing comma before start of a new alter operation.",
606-
{
607-
"@type": "@14"
608-
},
609-
0
610-
],
611-
[
612-
"Missing comma before start of a new alter operation.",
613-
{
614-
"@type": "@25"
615-
},
616-
0
617-
],
618-
[
619-
"Missing comma before start of a new alter operation.",
620-
{
621-
"@type": "@36"
622-
},
623-
0
624-
],
625-
[
626-
"Missing comma before start of a new alter operation.",
627-
{
628-
"@type": "@47"
629-
},
630-
0
631-
]
632-
]
627+
"parser": []
633628
}
634629
}

0 commit comments

Comments
 (0)