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