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