Ejemplo n.º 1
0
 public function convertSqlToJob(SQLTokenIterator $tokens)
 {
     $tokens->seekTokenNum(SqlToken::T_INSERT());
     if ($tokens->getCurrentTokenNumber() !== SqlToken::T_INSERT()) {
         throw new ErrorException("Tried to parse INSERT statement when token-iterator is not at INSERT!");
     }
     $dataChange = new InsertDataChange();
     $insertJob = new InsertStatement();
     switch (true) {
         case $tokens->seekTokenNum(SqlToken::T_LOW_PRIORITY()):
             $insertJob->setPriority(Priority::LOW_PRIORITY());
             break;
         case $tokens->seekTokenNum(SqlToken::T_DELAYED()):
             $insertJob->setPriority(Priority::DELAYED());
             break;
         case $tokens->seekTokenNum(SqlToken::T_HIGH_PRIORITY()):
             $insertJob->setPriority(Priority::HIGH_PRIORITY());
             break;
     }
     if ($tokens->seekTokenNum(SqlToken::T_IGNORE())) {
         $insertJob->setDoIgnoreErrors(true);
     }
     if (!$tokens->seekTokenNum(SqlToken::T_INTO())) {
         throw new MalformedSqlException("Missing INTO after INSERT for INSERT INTO statement!", $tokens);
     }
     if (!$this->tableParser->canParseTokens($tokens)) {
         throw new MalformedSqlException("Missing table-specifier for INSERT INTO statement!", $tokens);
     }
     $insertJob->setTable($this->tableParser->convertSqlToJob($tokens));
     if ($tokens->seekTokenText('(')) {
         do {
             if (!$this->columnParser->canParseTokens($tokens)) {
                 throw new MalformedSqlException("Missing valid column name in column selection for INSERT INTO statement!", $tokens);
             }
             $insertJob->addColumnSelection($this->columnParser->convertSqlToJob($tokens));
         } while ($tokens->seekTokenText(','));
         if (!$tokens->seekTokenText(')')) {
             throw new MalformedSqlException("Missing closing parenthesis after column-selection for INSERT INTO statement!");
         }
         if ($tokens->seekTokenNum(SqlToken::T_VALUES())) {
             do {
                 if (!$tokens->seekTokenText('(')) {
                     throw new MalformedSqlException("Missing begin parenthesis in value definiton for INSERT INTO statement!", $tokens);
                 }
                 $dataRow = array();
                 do {
                     switch (true) {
                         case $this->valueParser->canParseTokens($tokens):
                             $dataRow[] = $this->valueParser->convertSqlToJob($tokens);
                             break;
                         default:
                             throw new MalformedSqlException("Invalid value in value-defintion for INSERT INTO statement!", $tokens);
                     }
                 } while ($tokens->seekTokenText(','));
                 if (!$tokens->seekTokenText(')')) {
                     throw new MalformedSqlException("Missing ending parenthesis in value definiton for INSERT INTO statement!", $tokens);
                 }
                 $insertJob->addDataSourceValuesRow($dataRow);
             } while ($tokens->seekTokenText(','));
         } elseif ($this->selectParser->canParseTokens($tokens)) {
             $insertJob->setDataSourceSelect($this->selectParser->convertSqlToJob($tokens));
         } else {
             throw new MalformedSqlException("Invalid data-source-definiton (VALUES or SELECT) in INSERT INTO statement!", $tokens);
         }
     } elseif ($tokens->seekTokenNum(SqlToken::T_SET())) {
         do {
             if (!$this->columnParser->canParseTokens($tokens)) {
                 throw new MalformedSqlException("Missing column specifier for INSERT INTO SET statement!", $tokens);
             }
             $dataChange->setColumn($this->columnParser->convertSqlToJob($tokens));
             if (!$tokens->seekTokenText('=')) {
                 throw new MalformedSqlException("Missing '=' in INSERT INTO SET statement!", $tokens);
             }
             switch (true) {
                 case $this->valueParser->canParseTokens($tokens):
                     $dataChange->setValue($this->valueParser->convertSqlToJob($tokens));
                     break;
                 default:
                     throw new MalformedSqlException("Invalid value for INSERT INTO SET statement!", $tokens);
             }
             $insertJob->addColumnSetValue(clone $dataChange);
         } while ($tokens->seekTokenText(','));
     } elseif ($this->selectParser->canParseTokens($tokens)) {
         $insertJob->setDataSourceSelect($this->selectParser->convertSqlToJob($tokens));
     } else {
         throw new MalformedSqlException("Invalid column-selection for INSERT INTO statement!", $tokens);
     }
     if ($tokens->seekTokenNum(SqlToken::T_ON())) {
         if (!$tokens->seekTokenNum(SqlToken::T_DUPLICATE())) {
             throw new MalformedSqlException("Missing DUPLICATE in INSERT INTO ON DUPLICATE KEY UPDATE statement!", $tokens);
         }
         if (!$tokens->seekTokenNum(SqlToken::T_KEY())) {
             throw new MalformedSqlException("Missing KEY in INSERT INTO ON DUPLICATE KEY UPDATE statement!", $tokens);
         }
         if (!$tokens->seekTokenNum(SqlToken::T_UPDATE())) {
             throw new MalformedSqlException("Missing UPDATE in INSERT INTO ON DUPLICATE KEY UPDATE statement!", $tokens);
         }
         do {
             if (!$this->columnParser->canParseTokens($tokens)) {
                 throw new MalformedSqlException("Missing column specifier for INSERT INTO ON DUPLICATE KEY UPDATE statement!", $tokens);
             }
             $dataChange->setColumn($this->columnParser->convertSqlToJob($tokens));
             if (!$tokens->seekTokenText('=')) {
                 throw new MalformedSqlException("Missing '=' in INSERT INTO ON DUPLICATE KEY UPDATE statement!", $tokens);
             }
             switch (true) {
                 case $valueParser->canParseTokens($tokens):
                     $dataChange->setValue($valueParser->convertSqlToJob($tokens));
                     break;
                 default:
                     throw new MalformedSqlException("Invalid value for INSERT INTO ON DUPLICATE KEY UPDATE statement!", $tokens);
             }
             $insertJob->addOnDuplicateDataChange(clone $dataChange);
         } while ($tokens->seekTokenText(','));
     }
     return $insertJob;
 }