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