62 foreach ($this->splitStatements($tokenizer, $delimiter) as $statement)
64 $this->executeStatement($statement);
76 protected function splitStatements(
Tokenizer $tokenizer, $delimiter =
';')
85 foreach ($tokenizer->
getTokens() as $token)
88 $token->text === $delimiter
90 && mb_strpos($prevToken->text,
"\n") !==
false
97 mb_strpos($token->text,
"\n") !==
false
99 && $prevToken->text === $delimiter
102 array_pop($result[$index]);
104 $result[$index] = [];
108 $result[$index][] = $token;
113 foreach ($result as $i => $tokens)
129 protected function executeStatement(Tokenizer $tokenizer)
132 $tokenizer->resetState();
133 $tokenizer->skipWhiteSpace();
134 if ($tokenizer->testUpperText(
'CREATE'))
138 elseif ($tokenizer->testUpperText(
'INSERT'))
142 elseif ($tokenizer->testUpperText(
'SET'))
146 elseif ($tokenizer->testUpperText(
'ALTER'))
148 $this->executeAlter($tokenizer);
150 elseif ($tokenizer->testUpperText(
'IF'))
152 $tokenizer->skipWhiteSpace();
153 if ($tokenizer->testUpperText(
'OBJECT_ID'))
155 while (!$tokenizer->endOfInput())
157 if ($tokenizer->nextToken()->upper ===
'CREATE')
162 $tokenizer->nextToken();
163 $tokenizer->skipWhiteSpace();
164 if ($tokenizer->testUpperText(
'TABLE'))
170 throw new NotSupportedException(
"'CREATE TABLE' expected. line:" . $tokenizer->getCurrentToken()->line);
173 elseif ($tokenizer->testUpperText(
'NOT'))
175 $tokenizer->skipWhiteSpace();
176 if ($tokenizer->testUpperText(
'EXISTS'))
178 while (!$tokenizer->endOfInput())
180 if ($tokenizer->nextToken()->upper ===
'CREATE')
185 $tokenizer->nextToken();
186 $tokenizer->skipWhiteSpace();
188 if ($tokenizer->testUpperText(
'UNIQUE'))
191 $tokenizer->skipWhiteSpace();
198 if ($tokenizer->testUpperText(
'INDEX'))
200 $this->executeCreateIndex($tokenizer, $unique);
204 throw new NotSupportedException(
"'CREATE INDEX' expected. line:" . $tokenizer->getCurrentToken()->line);
209 throw new NotSupportedException(
"'NOT EXISTS' expected. line:" . $tokenizer->getCurrentToken()->line);
214 throw new NotSupportedException(
"'OBJECT_ID' expected. line:" . $tokenizer->getCurrentToken()->line);
217 elseif (!$tokenizer->endOfInput())
219 throw new NotSupportedException(
"'CREATE' expected. line:" . $tokenizer->getCurrentToken()->line);
231 $tokenizer->skipWhiteSpace();
232 if ($tokenizer->testUpperText(
'OR'))
234 $tokenizer->skipWhiteSpace();
235 if ($tokenizer->testUpperText(
'REPLACE'))
237 $tokenizer->skipWhiteSpace();
241 throw new NotSupportedException(
"'OR REPLACE' expected. line:" . $tokenizer->getCurrentToken()->line);
245 if ($tokenizer->testUpperText(
'TABLE'))
249 elseif ($tokenizer->testUpperText(
'INDEX'))
251 $this->executeCreateIndex($tokenizer,
false);
253 elseif ($tokenizer->testUpperText(
'UNIQUE'))
255 $tokenizer->skipWhiteSpace();
256 if ($tokenizer->testUpperText(
'INDEX'))
258 $tokenizer->skipWhiteSpace();
261 $this->executeCreateIndex($tokenizer,
true);
263 elseif ($tokenizer->testUpperText(
'FULLTEXT'))
265 $tokenizer->skipWhiteSpace();
266 if ($tokenizer->testUpperText(
'INDEX'))
268 $tokenizer->skipWhiteSpace();
271 $this->executeCreateIndex($tokenizer,
false,
true);
273 elseif ($tokenizer->testUpperText(
'TRIGGER'))
275 $this->executeCreateTrigger($tokenizer);
278 $tokenizer->testUpperText(
'PROCEDURE')
279 || $tokenizer->testUpperText(
'FUNCTION')
280 || $tokenizer->testUpperText(
'TYPE')
285 elseif ($tokenizer->testUpperText(
'SEQUENCE'))
291 throw new NotSupportedException(
'TABLE|INDEX|UNIQUE|TRIGGER|PROCEDURE|FUNCTION|TYPE|SEQUENCE expected. line:' . $tokenizer->getCurrentToken()->line);
301 protected function executeAlter(
Tokenizer $tokenizer)
303 $tokenizer->skipWhiteSpace();
304 if ($tokenizer->testUpperText(
'TABLE'))
306 $tokenizer->skipWhiteSpace();
307 $tableName = $tokenizer->getCurrentToken()->text;
309 $table = $this->tables->search($tableName);
312 throw new NotSupportedException(
"Table [${tableName}] not found. line: " . $tokenizer->getCurrentToken()->line);
314 $tokenizer->nextToken();
315 $tokenizer->skipWhiteSpace();
316 if ($tokenizer->testUpperText(
'ADD'))
318 $tokenizer->skipWhiteSpace();
319 if ($tokenizer->testUpperText(
'CONSTRAINT'))
321 $tokenizer->skipWhiteSpace();
322 $table->createConstraint($tokenizer);
325 elseif ($tokenizer->testUpperText(
'MODIFY'))
327 $tokenizer->skipWhiteSpace();
328 $table->modifyColumn($tokenizer);
330 elseif ($tokenizer->testUpperText(
'NOCHECK') || $tokenizer->testUpperText(
'CHECK'))
334 elseif ($tokenizer->testUpperText(
'DISABLE') || $tokenizer->testUpperText(
'ENABLE'))
340 throw new NotSupportedException(
"'ADD' expected. line:" . $tokenizer->getCurrentToken()->line);
345 throw new NotSupportedException(
"'TABLE' expected. line:" . $tokenizer->getCurrentToken()->line);
357 $tokenizer->skipWhiteSpace();
369 protected function executeCreateIndex(
Tokenizer $tokenizer, $unique, $fulltext =
false)
371 $tokenizer->skipWhiteSpace();
376 $tableName = $tokenizer->getCurrentToken()->text;
379 $table = $this->tables->search($tableName);
382 $table =
new Table($tableName);
383 $this->tables->add($table);
388 $table->createIndex($tokenizer, $unique, $fulltext);
397 protected function executeCreateTrigger(Tokenizer $tokenizer)
399 $tokenizer->skipWhiteSpace();
401 $tokenizer->setBookmark();
404 $tableName = $tokenizer->getCurrentToken()->text;
407 $table = $this->tables->search($tableName);
410 throw new NotSupportedException(
"Table [${tableName}] not found. line: " . $tokenizer->getCurrentToken()->line);
413 $tokenizer->restoreBookmark();
415 $table->createTrigger($tokenizer);
438 $tokenizer->skipWhiteSpace();