/** * Creates index object from tokens. * <p> * If parameter $indexName is not passed then current position should point to the name of the index. * * @param Tokenizer $tokenizer Tokens collection. * @param boolean $unique Uniqueness flag. * @param string $indexName Optional name of the index. * * @return Index * @throws NotSupportedException */ public static function create(Tokenizer $tokenizer, $unique = false, $indexName = '') { if (!$indexName) { if ($tokenizer->getCurrentToken()->text !== '(') { $indexName = $tokenizer->getCurrentToken()->text; $tokenizer->nextToken(); $tokenizer->skipWhiteSpace(); } } if ($tokenizer->testUpperText('ON')) { $tokenizer->skipWhiteSpace(); /** @noinspection PhpUnusedLocalVariableInspection */ $tableName = $tokenizer->getCurrentToken()->text; $tokenizer->nextToken(); $tokenizer->skipWhiteSpace(); } $index = new self($indexName, $unique); if ($tokenizer->testText('(')) { $tokenizer->skipWhiteSpace(); $token = $tokenizer->getCurrentToken(); $level = $token->level; $column = ''; do { if ($token->text === ',') { $index->addColumn($column); $column = ''; } else { $column .= $token->text; } $token = $tokenizer->nextToken(); } while (!$tokenizer->endOfInput() && $token->level >= $level); if ($column) { $index->addColumn($column); } if (!$tokenizer->testText(')')) { throw new NotSupportedException("')' expected. line:" . $tokenizer->getCurrentToken()->line); } } else { throw new NotSupportedException("'(' expected. line:" . $tokenizer->getCurrentToken()->line); } return $index; }
/** * Creates table object from tokens. * <p> * Current position should point to the name of the sequence or 'if not exists' clause. * * @param Tokenizer $tokenizer Tokens collection. * * @return Table * @throws NotSupportedException */ public static function create(Tokenizer $tokenizer) { $tokenizer->skipWhiteSpace(); if ($tokenizer->testUpperText('IF')) { $tokenizer->skipWhiteSpace(); if ($tokenizer->testUpperText('NOT')) { $tokenizer->skipWhiteSpace(); } if ($tokenizer->testUpperText('EXISTS')) { $tokenizer->skipWhiteSpace(); } } $table = new Table($tokenizer->getCurrentToken()->text); $tokenizer->nextToken(); $tokenizer->skipWhiteSpace(); if ($tokenizer->testText('(')) { $tokenizer->skipWhiteSpace(); $token = $tokenizer->getCurrentToken(); $level = $token->level; do { if ($tokenizer->testUpperText('INDEX') || $tokenizer->testUpperText('KEY')) { $tokenizer->skipWhiteSpace(); $table->createIndex($tokenizer, false); } elseif ($tokenizer->testUpperText('UNIQUE')) { $tokenizer->skipWhiteSpace(); if ($tokenizer->testUpperText('KEY')) { $tokenizer->skipWhiteSpace(); } elseif ($tokenizer->testUpperText('INDEX')) { $tokenizer->skipWhiteSpace(); } $table->createIndex($tokenizer, true); } elseif ($tokenizer->testUpperText('PRIMARY')) { $tokenizer->skipWhiteSpace(); if (!$tokenizer->testUpperText('KEY')) { throw new NotSupportedException("'KEY' expected. line:" . $tokenizer->getCurrentToken()->line); } $tokenizer->skipWhiteSpace(); $table->createIndex($tokenizer, true); } elseif ($tokenizer->testUpperText('CONSTRAINT')) { $tokenizer->skipWhiteSpace(); $constraintName = $tokenizer->getCurrentToken()->text; $tokenizer->nextToken(); $tokenizer->skipWhiteSpace(); if ($tokenizer->testUpperText('PRIMARY') || $tokenizer->testUpperText('UNIQUE')) { $tokenizer->putBack(); $table->createConstraint($tokenizer, $constraintName); } elseif ($tokenizer->testUpperText('FOREIGN')) { $tokenizer->putBack(); $table->createConstraint($tokenizer, $constraintName); } else { throw new NotSupportedException("'PRIMARY KEY' expected. line:" . $tokenizer->getCurrentToken()->line); } } elseif ($tokenizer->testUpperText(')')) { break; } else { $table->createColumn($tokenizer); } $tokenizer->skipWhiteSpace(); $token = $tokenizer->getCurrentToken(); if ($token->level == $level && $token->text == ',') { $token = $tokenizer->nextToken(); } elseif ($token->level < $level && $token->text == ')') { $tokenizer->nextToken(); break; } else { throw new NotSupportedException("',' or ')' expected. line:" . $token->line); } $tokenizer->skipWhiteSpace(); } while (!$tokenizer->endOfInput() && $token->level >= $level); $suffix = ''; while (!$tokenizer->endOfInput()) { $suffix .= $tokenizer->getCurrentToken()->text; $tokenizer->nextToken(); } if ($suffix) { $table->setBody($suffix); } } else { throw new NotSupportedException("'(' expected. line:" . $tokenizer->getCurrentToken()->line); } return $table; }