public function convertSqlToJob(SQLTokenIterator $tokens) { $tokens->seekTokenNum(SqlToken::T_DROP()); if ($tokens->getCurrentTokenNumber() !== SqlToken::T_DROP()) { throw new ErrorException("Tried to parse sql-drop when token-iterator does not point to T_DROP!"); } $dropJob = new DropStatement(); if ($tokens->seekTokenNum(SqlToken::T_TEMPORARY())) { $dropJob->setIsTemporary(true); } switch (true) { case $tokens->seekTokenNum(SqlToken::T_SCHEMA(), TokenIterator::NEXT): case $tokens->seekTokenNum(SqlToken::T_DATABASE()): $dropJob->setType(DropStatement::TYPE_DATABASE); break; case $tokens->seekTokenNum(SqlToken::T_TABLE()): $dropJob->setType(DropStatement::TYPE_TABLE); break; case $tokens->seekTokenNum(SqlToken::T_VIEW()): $dropJob->setType(DropStatement::TYPE_VIEW); break; } if ($tokens->seekTokenNum(SqlToken::T_IF())) { if (!$tokens->seekTokenNum(SqlToken::T_EXISTS())) { throw new MalformedSqlException("Malformed drop-statement (missing T_EXISTS after T_IF)!"); } $dropJob->setOnlyIfExist(true); } else { $dropJob->setOnlyIfExist(false); } do { if (!$this->valueParser->canParseTokens($tokens)) { throw new MalformedSqlException("Missing a subject to drop for T_DROP statement!", $tokens); } $subject = $this->valueParser->convertSqlToJob($tokens); $dropJob->addSubject($subject); } while ($tokens->seekTokenText(',')); if ($tokens->seekTokenNum(SqlToken::T_RESTRICT())) { $dropJob->setReferenceOption(ReferenceOption::RESTRICT()); if ($tokens->seekTokenNum(SqlToken::T_CASCADE())) { throw new MalformedSqlException("Conflicting T_RESTRICT with T_CASCADE!", $tokens); } } if ($tokens->seekTokenNum(SqlToken::T_CASCADE())) { $dropJob->setReferenceOption(ReferenceOption::CASCADE()); if ($tokens->seekTokenNum(SqlToken::T_RESTRICT())) { throw new MalformedSqlException("Conflicting T_RESTRICT with T_CASCADE!", $tokens); } } return $dropJob; }
/** * @return Create * @see Addiks\PHPSQL.SqlParser::convertSqlToJob() */ public function convertSqlToJob(SQLTokenIterator $tokens) { $tokens->seekTokenNum(SqlToken::T_CREATE()); if ($tokens->getCurrentTokenNumber() !== SqlToken::T_CREATE()) { throw new ErrorException("Tried to parse create-statement when token-iterator is not at T_CREATE!"); } switch (true) { case $tokens->seekTokenNum(SqlToken::T_SCHEMA(), TokenIterator::NEXT): case $tokens->seekTokenNum(SqlToken::T_DATABASE()): return $this->parseCreateDatabase($tokens); break; case $tokens->seekTokenNum(SqlToken::T_TABLE(), TokenIterator::NEXT, [SqlToken::T_TEMPORARY()]): return $this->parseCreateTable($tokens); break; case $tokens->seekTokenNum(SqlToken::T_INDEX(), TokenIterator::NEXT, [SqlToken::T_UNIQUE(), SqlToken::T_PRIMARY()]): return $this->parseCreateIndex($tokens); break; case $tokens->seekTokenNum(SqlToken::T_VIEW(), TokenIterator::NEXT): return $this->parseCreateView($tokens); break; default: throw new MalformedSqlException("Invalid type of create-statement!", $tokens); } }