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