Skip to content

Commit 3f3debd

Browse files
committed
Merge #421 - Fix #418 - ALTER EVENT statement with DEFINER=user modifier fails to be parsed
Pull-request: #421 Fixes: #418 Signed-off-by: William Desportes <williamdes@wdes.fr>
2 parents 4d09f0e + e9cf491 commit 3f3debd

File tree

7 files changed

+3279
-6
lines changed

7 files changed

+3279
-6
lines changed

src/Statements/AlterStatement.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpMyAdmin\SqlParser\TokensList;
1414

1515
use function implode;
16+
use function trim;
1617

1718
/**
1819
* `ALTER` statement.
@@ -43,7 +44,11 @@ class AlterStatement extends Statement
4344
'ONLINE' => 1,
4445
'OFFLINE' => 1,
4546
'IGNORE' => 2,
46-
47+
// `DEFINER` is also used for `ALTER EVENT`
48+
'DEFINER' => [
49+
2,
50+
'expr=',
51+
],
4752
'DATABASE' => 3,
4853
'EVENT' => 3,
4954
'FUNCTION' => 3,
@@ -139,8 +144,10 @@ public function build()
139144
$tmp[] = $altered::build($altered);
140145
}
141146

142-
return 'ALTER ' . OptionsArray::build($this->options)
147+
return trim(
148+
'ALTER ' . OptionsArray::build($this->options)
143149
. ' ' . Expression::build($this->table)
144-
. ' ' . implode(', ', $tmp);
150+
. ' ' . implode(', ', $tmp)
151+
);
145152
}
146153
}

tests/Builder/AlterStatementTest.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,37 @@ public function testBuilder(): void
2121
$this->assertEquals($query, $stmt->build());
2222
}
2323

24+
public function testBuilderWithComments(): void
25+
{
26+
$query = 'ALTER /* comment */ TABLE `actor` ' .
27+
'ADD PRIMARY KEY (`actor_id`), -- comment at the end of the line' . "\n" .
28+
'ADD KEY `idx_actor_last_name` (`last_name`) -- and that is the last comment.';
29+
30+
$expectedQuery = 'ALTER TABLE `actor` ' .
31+
'ADD PRIMARY KEY (`actor_id`), ' .
32+
'ADD KEY `idx_actor_last_name` (`last_name`)';
33+
34+
$parser = new Parser($query);
35+
$stmt = $parser->statements[0];
36+
37+
$this->assertEquals($expectedQuery, $stmt->build());
38+
}
39+
40+
public function testBuilderWithCommentsOnOptions(): void
41+
{
42+
$query = 'ALTER EVENT `myEvent` /* comment */ ' .
43+
'ON SCHEDULE -- Comment at the end of the line' . "\n" .
44+
'AT "2023-01-01 01:23:45"';
45+
46+
$expectedQuery = 'ALTER EVENT `myEvent` ' .
47+
'ON SCHEDULE AT "2023-01-01 01:23:45"';
48+
49+
$parser = new Parser($query);
50+
$stmt = $parser->statements[0];
51+
52+
$this->assertEquals($expectedQuery, $stmt->build());
53+
}
54+
2455
public function testBuilderCompressed(): void
2556
{
2657
$query = 'ALTER TABLE `user` CHANGE `message` `message` TEXT COMPRESSED';
@@ -34,7 +65,7 @@ public function testBuilderPartitions(): void
3465
$parser = new Parser('ALTER TABLE t1 PARTITION BY HASH(id) PARTITIONS 8');
3566
$stmt = $parser->statements[0];
3667

37-
$this->assertEquals('ALTER TABLE t1 PARTITION BY HASH(id) PARTITIONS 8 ', $stmt->build());
68+
$this->assertEquals('ALTER TABLE t1 PARTITION BY HASH(id) PARTITIONS 8', $stmt->build());
3869

3970
$parser = new Parser('ALTER TABLE t1 ADD PARTITION (PARTITION p3 VALUES LESS THAN (2002))');
4071
$stmt = $parser->statements[0];
@@ -50,16 +81,24 @@ public function testBuilderPartitions(): void
5081
$stmt = $parser->statements[0];
5182

5283
$this->assertEquals(
53-
'ALTER TABLE p PARTITION BY LINEAR KEY ALGORITHM=2 (id) PARTITIONS 32 ',
84+
'ALTER TABLE p PARTITION BY LINEAR KEY ALGORITHM=2 (id) PARTITIONS 32',
5485
$stmt->build()
5586
);
5687

5788
$parser = new Parser('ALTER TABLE t1 DROP PARTITION p0, p1;');
5889
$stmt = $parser->statements[0];
5990

6091
$this->assertEquals(
61-
'ALTER TABLE t1 DROP PARTITION p0, p1 ',
92+
'ALTER TABLE t1 DROP PARTITION p0, p1',
6293
$stmt->build()
6394
);
6495
}
96+
97+
public function testBuilderEventWithDefiner(): void
98+
{
99+
$query = 'ALTER DEFINER=user EVENT myEvent ENABLE';
100+
$parser = new Parser($query);
101+
$stmt = $parser->statements[0];
102+
$this->assertEquals($query, $stmt->build());
103+
}
65104
}

tests/Parser/AlterStatementTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public function alterProvider(): array
7676
['parser/parseAlterEventOnScheduleEvery4'],
7777
['parser/parseAlterEventOnScheduleEvery5'],
7878
['parser/parseAlterEventOnScheduleEvery6'],
79+
['parser/parseAlterEventWithDefiner'],
80+
['parser/parseAlterEventWithOtherDefiners'],
7981
];
8082
}
8183
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER DEFINER = user EVENT my_event ENABLE;

0 commit comments

Comments
 (0)