1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components\Parsers; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\Expression; |
8
|
|
|
use PhpMyAdmin\SqlParser\Components\RenameOperation; |
9
|
|
|
use PhpMyAdmin\SqlParser\Parseable; |
10
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
11
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
12
|
|
|
use PhpMyAdmin\SqlParser\TokenType; |
13
|
|
|
|
14
|
|
|
use function implode; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* `RENAME TABLE` keyword parser. |
18
|
|
|
*/ |
19
|
|
|
final class RenameOperations implements Parseable |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param Parser $parser the parser that serves as context |
23
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
24
|
|
|
* @param array<string, mixed> $options parameters for parsing |
25
|
|
|
* |
26
|
|
|
* @return RenameOperation[] |
27
|
|
|
*/ |
28
|
18 |
|
public static function parse(Parser $parser, TokensList $list, array $options = []): array |
29
|
|
|
{ |
30
|
18 |
|
$ret = []; |
31
|
|
|
|
32
|
18 |
|
$expr = new RenameOperation(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The state of the parser. |
36
|
|
|
* |
37
|
|
|
* Below are the states of the parser. |
38
|
|
|
* |
39
|
|
|
* 0 ---------------------[ old name ]--------------------> 1 |
40
|
|
|
* |
41
|
|
|
* 1 ------------------------[ TO ]-----------------------> 2 |
42
|
|
|
* |
43
|
|
|
* 2 ---------------------[ new name ]--------------------> 3 |
44
|
|
|
* |
45
|
|
|
* 3 ------------------------[ , ]------------------------> 0 |
46
|
|
|
* 3 -----------------------[ else ]----------------------> (END) |
47
|
|
|
*/ |
48
|
18 |
|
$state = 0; |
49
|
|
|
|
50
|
18 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
51
|
|
|
/** |
52
|
|
|
* Token parsed at this moment. |
53
|
|
|
*/ |
54
|
18 |
|
$token = $list->tokens[$list->idx]; |
55
|
|
|
|
56
|
|
|
// End of statement. |
57
|
18 |
|
if ($token->type === TokenType::Delimiter) { |
58
|
14 |
|
break; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Skipping whitespaces and comments. |
62
|
18 |
|
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) { |
63
|
14 |
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
18 |
|
if ($state === 0) { |
67
|
18 |
|
$expr->old = Expression::parse( |
68
|
18 |
|
$parser, |
69
|
18 |
|
$list, |
70
|
18 |
|
[ |
71
|
18 |
|
'breakOnAlias' => true, |
72
|
18 |
|
'parseField' => 'table', |
73
|
18 |
|
], |
74
|
18 |
|
); |
75
|
18 |
|
if (empty($expr->old)) { |
76
|
2 |
|
$parser->error('The old name of the table was expected.', $token); |
77
|
|
|
} |
78
|
|
|
|
79
|
18 |
|
$state = 1; |
80
|
16 |
|
} elseif ($state === 1) { |
81
|
16 |
|
if ($token->type !== TokenType::Keyword || $token->keyword !== 'TO') { |
82
|
2 |
|
$parser->error('Keyword "TO" was expected.', $token); |
83
|
2 |
|
break; |
84
|
|
|
} |
85
|
|
|
|
86
|
14 |
|
$state = 2; |
87
|
14 |
|
} elseif ($state === 2) { |
88
|
14 |
|
$expr->new = Expression::parse( |
89
|
14 |
|
$parser, |
90
|
14 |
|
$list, |
91
|
14 |
|
[ |
92
|
14 |
|
'breakOnAlias' => true, |
93
|
14 |
|
'parseField' => 'table', |
94
|
14 |
|
], |
95
|
14 |
|
); |
96
|
14 |
|
if (empty($expr->new)) { |
97
|
2 |
|
$parser->error('The new name of the table was expected.', $token); |
98
|
|
|
} |
99
|
|
|
|
100
|
14 |
|
$state = 3; |
101
|
|
|
} else { |
102
|
10 |
|
if (($token->type !== TokenType::Operator) || ($token->value !== ',')) { |
103
|
2 |
|
break; |
104
|
|
|
} |
105
|
|
|
|
106
|
8 |
|
$ret[] = $expr; |
107
|
8 |
|
$expr = new RenameOperation(); |
108
|
8 |
|
$state = 0; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
18 |
|
if ($state !== 3) { |
|
|
|
|
113
|
4 |
|
$parser->error('A rename operation was expected.', $list->tokens[$list->idx - 1]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Last iteration was not saved. |
117
|
18 |
|
if (! empty($expr->old)) { |
118
|
16 |
|
$ret[] = $expr; |
119
|
|
|
} |
120
|
|
|
|
121
|
18 |
|
--$list->idx; |
122
|
|
|
|
123
|
18 |
|
return $ret; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** @param RenameOperation[] $component the component to be built */ |
127
|
4 |
|
public static function buildAll(array $component): string |
128
|
|
|
{ |
129
|
4 |
|
return implode(', ', $component); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|