Ejemplo n.º 1
0
 public function canParseTokens(SQLTokenIterator $tokens, &$skipChecks = 0)
 {
     $previousIndex = $tokens->getIndex();
     $result = is_int($tokens->isTokenNum(SqlToken::T_LIKE(), TokenIterator::NEXT, [SqlToken::T_NOT()])) || is_int($tokens->isTokenNum(SqlToken::T_LIKE(), TokenIterator::CURRENT, [SqlToken::T_NOT()]));
     $tokens->seekIndex($previousIndex);
     return $result;
 }
Ejemplo n.º 2
0
 public function convertSqlToJob(SQLTokenIterator $tokens)
 {
     $tokens->seekTokenNum(SqlToken::T_CASE());
     if ($tokens->getCurrentTokenNumber() !== SqlToken::T_CASE()) {
         throw new ErrorException("Tried to parse CASE statement when token-iterator is not at CASE!");
     }
     $caseJob = new CaseJob();
     if (!$tokens->isTokenNum(SqlToken::T_WHEN()) && $this->valueParser->canParseTokens($tokens)) {
         $caseJob->setCaseValue($this->valueParser->convertSqlToJob($tokens));
     }
     do {
         if (!$tokens->seekTokenNum(SqlToken::T_WHEN())) {
             throw new MalformedSqlException("Missing WHEN in CASE statement!", $tokens);
         }
         if (!$this->valueParser->canParseTokens($tokens)) {
             throw new MalformedSqlException("Missing valid when-value in CASE statement!", $tokens);
         }
         $whenValue = $this->valueParser->convertSqlToJob($tokens);
         if (!$tokens->seekTokenNum(SqlToken::T_THEN())) {
             throw new MalformedSqlException("Missing THEN in CASE statement!", $tokens);
         }
         switch (true) {
             case $this->valueParser->canParseTokens($tokens):
                 $thenExpression = $this->valueParser->convertSqlToJob($tokens);
                 break;
             default:
                 throw new MalformedSqlException("Missing valid THEN statement for CASE statement!", $tokens);
         }
         $caseJob->addWhenThenStatement($whenValue, $thenExpression);
     } while ($tokens->isTokenNum(SqlToken::T_WHEN()));
     if ($tokens->seekTokenNum(SqlToken::T_ELSE())) {
         switch (true) {
             case $this->valueParser->canParseTokens($tokens):
                 $caseJob->setElseStatement($this->valueParser->convertSqlToJob($tokens));
                 break;
             default:
                 throw new MalformedSqlException("Missing valid THEN statement for CASE statement!", $tokens);
         }
     }
     if (!$tokens->seekTokenNum(SqlToken::T_END())) {
         throw new MalformedSqlException("Missing END at the end of for CASE statement!", $tokens);
     }
     $tokens->seekTokenNum(SqlToken::T_CASE());
     return $caseJob;
 }
Ejemplo n.º 3
0
 public function convertSqlToJob(SQLTokenIterator $tokens, &$checkFlags = 0)
 {
     if (!$tokens->seekTokenNum(SqlToken::T_IN(), TokenIterator::NEXT, [SqlToken::T_NOT()])) {
         throw new ErrorException("Missing IN after string when tried to parse IN-condition! (was 'canParseTokens' not used?)");
     }
     $enumConditionJob = new EnumConditionJob();
     $enumConditionJob->setIsNegated($tokens->isTokenNum(SqlToken::T_NOT(), TokenIterator::PREVIOUS));
     if (!$tokens->seekTokenText('(')) {
         throw new MalformedSqlException("Missing beginning parenthesis for IN condition!", $tokens);
     }
     do {
         if (!$this->valueParser->canParseTokens($tokens)) {
             throw new MalformedSqlException("Missing valid value in value-listing for IN condition!", $tokens);
         }
         $enumConditionJob->addValue($this->valueParser->convertSqlToJob($tokens));
     } while ($tokens->seekTokenText(','));
     if (!$tokens->seekTokenText(')')) {
         throw new MalformedSqlException("Missing ending parenthesis for IN condition!", $tokens);
     }
     return $enumConditionJob;
 }
Ejemplo n.º 4
0
 public function canParseTokens(SQLTokenIterator $tokens)
 {
     return is_int($tokens->isTokenNum(SqlToken::T_COMMIT(), TokenIterator::NEXT));
 }
Ejemplo n.º 5
0
 public function canParseTokens(SQLTokenIterator $tokens)
 {
     return is_int($tokens->isTokenNum(SqlToken::T_ALTER(), TokenIterator::CURRENT)) || is_int($tokens->isTokenNum(SqlToken::T_ALTER(), TokenIterator::NEXT));
 }
Ejemplo n.º 6
0
 public function canParseTokens(SQLTokenIterator $tokens)
 {
     return is_int($tokens->isTokenNum(SqlToken::T_START(), TokenIterator::CURRENT)) && is_int($tokens->isTokenNum(SqlToken::T_TRANSACTION(), TokenIterator::NEXT, [SqlToken::T_START()]));
 }
Ejemplo n.º 7
0
 /**
  * @param SQLTokenIterator $tokens
  * @return Operator
  */
 protected function parseCondition(SQLTokenIterator $tokens)
 {
     switch (true) {
         case $tokens->seekTokenText('='):
             return Operator::OP_EQUAL();
         case $tokens->seekTokenText('<=>'):
             return Operator::OP_EQUAL_NULLSAFE();
         case $tokens->seekTokenText('!='):
             return Operator::OP_NOT_EQUAL();
         case $tokens->seekTokenText('<>'):
             return Operator::OP_LESSERGREATER();
         case $tokens->seekTokenText('<='):
             return Operator::OP_LESSEREQUAL();
         case $tokens->seekTokenText('<'):
             return Operator::OP_LESSER();
         case $tokens->seekTokenText('>='):
             return Operator::OP_GREATEREQUAL();
         case $tokens->seekTokenText('>'):
             return Operator::OP_GREATER();
         case $tokens->seekTokenText('+'):
             return Operator::OP_ADDITION();
         case $tokens->seekTokenText('-'):
             return Operator::OP_SUBTRACTION();
         case $tokens->seekTokenText('*'):
             return Operator::OP_MULTIPLICATION();
         case $tokens->seekTokenText('/'):
             return Operator::OP_DIVISION();
         case $tokens->seekTokenNum(SqlToken::T_IS()):
             if ($tokens->seekTokenNum(SqlToken::T_NOT())) {
                 return Operator::OP_IS_NOT();
             } else {
                 return Operator::OP_IS();
             }
             break;
         case $tokens->seekTokenNum(SqlToken::T_AND()):
             return Operator::OP_AND();
         case $tokens->seekTokenNum(SqlToken::T_OR()):
             return Operator::OP_OR();
         case $tokens->seekTokenNum(SqlToken::T_BETWEEN(), TokenIterator::NEXT, SqlToken::T_NOT()):
             if ($tokens->isTokenNum(SqlToken::T_NOT(), TokenIterator::PREVIOUS)) {
                 return Operator::OP_NOT_BETWEEN();
             } else {
                 return Operator::OP_BETWEEN();
             }
         default:
             return null;
     }
 }
Ejemplo n.º 8
0
 public function canParseTokens(SQLTokenIterator $tokens)
 {
     return is_int($tokens->isTokenNum(T_STRING));
 }
Ejemplo n.º 9
0
 public function canParseTokens(SQLTokenIterator $tokens)
 {
     switch (true) {
         case is_int($tokens->isTokenNum(SqlToken::T_DEFAULT())):
         case is_int($tokens->isTokenNum(SqlToken::T_NULL())):
         case is_int($tokens->isTokenNum(SqlToken::T_FALSE())):
         case is_int($tokens->isTokenNum(SqlToken::T_TRUE())):
         case is_int($tokens->isTokenNum(SqlToken::T_CURRENT_TIMESTAMP())):
         case is_int($tokens->isTokenNum(SqlToken::T_CURRENT_DATE())):
         case is_int($tokens->isTokenNum(SqlToken::T_CURRENT_TIME())):
         case is_int($tokens->isTokenNum(SqlToken::T_CURRENT_USER())):
         case is_int($tokens->isTokenNum(T_NUM_STRING)):
         case is_int($tokens->isTokenNum(T_CONSTANT_ENCAPSED_STRING)):
         case is_int($tokens->isTokenNum(T_VARIABLE)):
         case $this->parenthesisParser->canParseTokens($tokens):
         case $this->functionParser->canParseTokens($tokens):
         case $this->columnParser->canParseTokens($tokens):
         case $this->caseParser->canParseTokens($tokens):
             return true;
         default:
             return false;
     }
 }
Ejemplo n.º 10
0
 protected function parseJoinOperator(SQLTokenIterator $tokens)
 {
     $joinData = array('isInner' => false, 'isRight' => false);
     if ($tokens->seekTokenText(',')) {
         return $joinData;
     }
     if (!$tokens->isTokenNum(SqlToken::T_JOIN(), TokenIterator::NEXT, [SqlToken::T_LEFT(), SqlToken::T_RIGHT(), SqlToken::T_INNER(), SqlToken::T_OUTER(), SqlToken::T_CROSS()])) {
         return null;
     }
     while (true) {
         switch (true) {
             case $tokens->seekTokenNum(SqlToken::T_RIGHT()):
                 $joinData['isRight'] = true;
                 break;
             case $tokens->seekTokenNum(SqlToken::T_LEFT()):
                 $joinData['isRight'] = false;
                 break;
             case $tokens->seekTokenNum(SqlToken::T_INNER()):
                 $joinData['isInner'] = true;
                 break;
             case $tokens->seekTokenNum(SqlToken::T_CROSS()):
             case $tokens->seekTokenNum(SqlToken::T_OUTER()):
                 $joinData['isInner'] = false;
                 break;
             case $tokens->seekTokenNum(SqlToken::T_JOIN()):
                 return $joinData;
             default:
                 throw new MalformedSqlException("Invalid JOIN definition!");
         }
     }
 }
Ejemplo n.º 11
0
 /**
  * This parses a CREATE TABLE statement.
  *
  * @param SQLTokenIterator $tokens
  * @throws MalformedSql
  */
 protected function parseCreateTable(SQLTokenIterator $tokens)
 {
     $createTableJob = new CreateTableStatement();
     $createTableJob->setIsTemporaryTable(is_int($tokens->isTokenNum(SqlToken::T_TEMPORARY(), TokenIterator::PREVIOUS)));
     # [IF NOT EXISTS]:
     if ($tokens->seekTokenNum(SqlToken::T_IF())) {
         if (!$tokens->seekTokenNum(SqlToken::T_NOT()) || !$tokens->seekTokenNum(SqlToken::T_EXISTS())) {
             throw new MalformedSqlException("Invalid create-database statement (invalid 'IF NOT EXISTS')!", $tokens);
         }
         $createTableJob->setIfNotExists(true);
     } else {
         $createTableJob->setIfNotExists(false);
     }
     # NAME
     if ($tokens->seekTokenNum(T_STRING)) {
         $createTableJob->setName($tokens->getCurrentTokenString());
     } elseif ($this->valueParser->canParseTokens($tokens)) {
         $createTableJob->setName($this->valueParser->convertSqlToJob($tokens));
     } else {
         throw new MalformedSqlException("Missing name of table to create!", $tokens);
     }
     # COLUMN DEFINITION
     $checkEndParenthesis = $tokens->seekTokenText('(');
     # LIKE other table?
     if ($tokens->seekTokenNum(SqlToken::T_LIKE())) {
         if (!$this->tableParser->canParseTokens($tokens)) {
             throw new MalformedSqlException("Missing valid table-specifier for 'CREATE TABLE LIKE' statement!", $tokens);
         }
         $createTableJob->setLikeTable($this->tableParser->convertSqlToJob($tokens));
     } elseif ($this->selectParser->canParseTokens($tokens)) {
         $createTableJob->setFromSelectStatement($this->selectParser->convertSqlToJob($tokens));
         # normal column definition
     } else {
         do {
             switch (true) {
                 # normal column definition
                 case $this->columnDefinitonParser->canParseTokens($tokens):
                     $createTableJob->addColumnDefinition($this->columnDefinitonParser->convertSqlToJob($tokens));
                     break;
                     # [CONSTRAINT [$keyName]] PRIMARY KEY [$keyType] ($column[, $column, ...])
                 # [CONSTRAINT [$keyName]] PRIMARY KEY [$keyType] ($column[, $column, ...])
                 case $tokens->seekTokenNum(SqlToken::T_PRIMARY(), TokenIterator::NEXT, [T_STRING, SqlToken::T_CONSTRAINT()]):
                     $indexJob = new IndexPart();
                     $indexJob->setIsPrimary(true);
                     if ($tokens->isTokenNum(SqlToken::T_CONSTRAINT(), TokenIterator::PREVIOUS, [T_STRING]) && $tokens->seekTokenNum(T_STRING, TokenIterator::PREVIOUS)) {
                         $indexJob->setContraintSymbol($tokens->getPreviousTokenString());
                         $tokens->seekTokenNum(SqlToken::T_PRIMARY());
                     }
                     $indexJob->setName("PRIMARY");
                     if (!$tokens->seekTokenNum(SqlToken::T_KEY())) {
                         throw new MalformedSqlException("Missing T_KEY for PRIMARY KEY constraint in create-table statement!", $tokens);
                     }
                     # define index type (BTREE, HASH, ...)
                     if ($tokens->seekTokenNum(T_STRING)) {
                         $indexJob->setType(IndexType::factory($tokens->getCurrentTokenString()));
                     } else {
                         $indexJob->setType(IndexType::BTREE());
                     }
                     # columns in index
                     if ($tokens->seekTokenText('(')) {
                         do {
                             if (!$this->columnParser->canParseTokens($tokens)) {
                                 throw new MalformedSqlException("Invalid column in column-list for defining index!", $tokens);
                             }
                             $indexJob->addColumn($this->columnParser->convertSqlToJob($tokens));
                         } while ($tokens->seekTokenText(','));
                         if (!$tokens->seekTokenText(')')) {
                             throw new MalformedSqlException("Missing closing parenthesis at column-list for index!", $tokens);
                         }
                     }
                     $createTableJob->addIndex($indexJob);
                     break;
                     # KEY|INDEX [index_name] [index_type] (index_col_name,...)
                 # KEY|INDEX [index_name] [index_type] (index_col_name,...)
                 case $tokens->seekTokenNum(SqlToken::T_INDEX()):
                 case $tokens->seekTokenNUm(SqlToken::T_KEY()):
                     /* @var $indexJob IndexPart */
                     $indexJob = new IndexPart();
                     if ($tokens->seekTokenNum(T_STRING)) {
                         $indexJob->setName($tokens->getCurrentTokenString());
                     } else {
                         $indexJob->setName(null);
                         # first column name is used
                     }
                     # define index type (BTREE, HASH, ...)
                     if ($tokens->seekTokenNum(T_STRING)) {
                         $indexJob->setType(IndexType::factory($tokens->getCurrentTokenString()));
                     } else {
                         $indexJob->setType(IndexType::BTREE());
                     }
                     # columns in index
                     if ($tokens->seekTokenText('(')) {
                         do {
                             if (!$this->columnParser->canParseTokens($tokens)) {
                                 throw new MalformedSqlException("Invalid column in column-list for defining index!", $tokens);
                             }
                             $indexJob->addColumn($this->columnParser->convertSqlToJob($tokens));
                         } while ($tokens->seekTokenText(','));
                         if (!$tokens->seekTokenText(')')) {
                             throw new MalformedSqlException("Missing closing parenthesis at column-list for index!", $tokens);
                         }
                     }
                     $createTableJob->addIndex($indexJob);
                     break;
                     # [CONSTRAINT [symbol]] UNIQUE|FULLTEXT|SPATIAL [INDEX] [index_name] [index_type] (index_col_name,...)
                 # [CONSTRAINT [symbol]] UNIQUE|FULLTEXT|SPATIAL [INDEX] [index_name] [index_type] (index_col_name,...)
                 case $tokens->seekTokenNum(SqlToken::T_UNIQUE(), TokenIterator::NEXT, [SqlToken::T_CONSTRAINT(), T_STRING]):
                 case $tokens->seekTokenNum(SqlToken::T_FULLTEXT(), TokenIterator::NEXT, [SqlToken::T_CONSTRAINT(), T_STRING]):
                 case $tokens->seekTokenNum(SqlToken::T_SPATIAL(), TokenIterator::NEXT, [SqlToken::T_CONSTRAINT(), T_STRING]):
                     /* @var $indexJob IndexPart */
                     $indexJob = new IndexPart();
                     switch ($tokens->getCurrentTokenNumber()) {
                         case SqlToken::T_UNIQUE():
                             $indexJob->setIsUnique(true);
                             break;
                         case SqlToken::T_FULLTEXT():
                             $indexJob->setIsFullText(true);
                             break;
                         case SqlToken::T_SPATIAL():
                             $indexJob->setIsSpatial(true);
                             break;
                     }
                     if ($tokens->isTokenNum(SqlToken::T_CONSTRAINT(), TokenIterator::PREVIOUS, [T_STRING]) && $tokens->seekTokenNum(T_STRING, TokenIterator::PREVIOUS)) {
                         $indexJob->setContraintSymbol($tokens->getPreviousTokenString());
                         $tokens->seekTokenNum(SqlToken::T_PRIMARY());
                     }
                     $tokens->seekTokenNum(SqlToken::T_KEY());
                     $tokens->seekTokenNum(SqlToken::T_INDEX());
                     if ($tokens->seekTokenNum(T_STRING)) {
                         $indexJob->setName($tokens->getCurrentTokenString());
                     } else {
                         $indexJob->setName(null);
                         # first column name is used
                     }
                     # define index type (BTREE, HASH, ...)
                     if ($tokens->seekTokenNum(T_STRING)) {
                         $indexJob->setType(IndexType::factory($tokens->getCurrentTokenString()));
                     } else {
                         $indexJob->setType(IndexType::BTREE());
                     }
                     # columns in index
                     if ($tokens->seekTokenText('(')) {
                         do {
                             if (!$this->columnParser->canParseTokens($tokens)) {
                                 throw new MalformedSqlException("Invalid column in column-list for defining index!", $tokens);
                             }
                             $indexJob->addColumn($this->columnParser->convertSqlToJob($tokens));
                         } while ($tokens->seekTokenText(','));
                         if (!$tokens->seekTokenText(')')) {
                             throw new MalformedSqlException("Missing closing parenthesis at column-list for index!", $tokens);
                         }
                     }
                     $createTableJob->addIndex($indexJob);
                     break;
                     # [CONSTRAINT [$symbol]] FOREIGN KEY [$name] ($column[, $column, ...]) [$reference]
                 # [CONSTRAINT [$symbol]] FOREIGN KEY [$name] ($column[, $column, ...]) [$reference]
                 case $tokens->seekTokenNum(SqlToken::T_FOREIGN(), TokenIterator::NEXT, [T_STRING, SqlToken::T_CONSTRAINT()]):
                     /* @var $indexJob IndexPart */
                     $indexJob = new IndexPart();
                     if ($tokens->isTokenNum(SqlToken::T_CONSTRAINT(), TokenIterator::PREVIOUS, [T_STRING]) && $tokens->seekTokenNum(T_STRING, TokenIterator::PREVIOUS)) {
                         $indexJob->setContraintSymbol($tokens->getCurrentTokenString());
                         $tokens->seekTokenNum(SqlToken::T_FOREIGN());
                     }
                     if (!$tokens->seekTokenNum(SqlToken::T_KEY())) {
                         throw new MalformedSqlException("Missing T_KEY after T_FOREIGN in constraint-definition!", $tokens);
                     }
                     if ($tokens->seekTokenNum(T_STRING)) {
                         $indexJob->setName($tokens->getCurrentTokenString());
                     } else {
                         $indexJob->setName(null);
                         # first column name is used
                     }
                     # columns in index
                     if ($tokens->seekTokenText('(')) {
                         do {
                             if (!$this->columnParser->canParseTokens($tokens)) {
                                 throw new MalformedSqlException("Invalid column in column-list for defining index!", $tokens);
                             }
                             $indexJob->addColumn($this->columnParser->convertSqlToJob($tokens));
                         } while ($tokens->seekTokenText(','));
                         if (!$tokens->seekTokenText(')')) {
                             throw new MalformedSqlException("Missing closing parenthesis at column-list for index!", $tokens);
                         }
                     }
                     if (!$tokens->seekTokenNum(SqlToken::T_REFERENCES())) {
                         throw new MalformedSqlException("Missing reference-definition in foreign-constraint-definition!", $tokens);
                     }
                     if (!$this->tableParser->canParseTokens($tokens)) {
                         throw new MalformedSqlException("Missing table-definition in foreign-constraint-definition!", $tokens);
                     }
                     $fkTable = $this->tableParser->convertSqlToJob($tokens);
                     # columns in index
                     if ($tokens->seekTokenText('(')) {
                         do {
                             if (!$this->columnParser->canParseTokens($tokens)) {
                                 throw new MalformedSqlException("Invalid column in column-list for defining index!", $tokens);
                             }
                             $fkColumn = $this->columnParser->convertSqlToJob($tokens);
                             $fkColumn = ColumnSpecifier::factory($fkTable . '.' . $fkColumn->getColumn());
                             $indexJob->addForeignKey($fkColumn);
                         } while ($tokens->seekTokenText(','));
                         if (!$tokens->seekTokenText(')')) {
                             throw new MalformedSqlException("Missing closing parenthesis at column-list for index!", $tokens);
                         }
                     }
                     if ($tokens->seekTokenNum(SqlToken::T_MATCH())) {
                         switch (true) {
                             case $tokens->seekTokenNum(SqlToken::T_FULL()):
                                 $indexJob->setForeignKeyMatchType(MatchType::FULL());
                                 break;
                             case $tokens->seekTokenNum(SqlToken::T_PARTIAL()):
                                 $indexJob->setForeignKeyMatchType(MatchType::PARTIAL());
                                 break;
                             case $tokens->seekTokenNum(SqlToken::T_SIMPLE()):
                                 $indexJob->setForeignKeyMatchType(MatchType::SIMPLE());
                                 break;
                             default:
                                 throw new MalformedSqlException("Invalid match parameter for foreign key!", $tokens);
                         }
                     }
                     while ($tokens->seekTokenNum(SqlToken::T_ON())) {
                         switch (true) {
                             case $tokens->seekTokenNum(SqlToken::T_DELETE()):
                                 switch (true) {
                                     case $tokens->seekTokenNum(SqlToken::T_RESTRICT()):
                                         $option = ReferenceOption::RESTRICT();
                                         break;
                                     case $tokens->seekTokenNum(SqlToken::T_CASCADE()):
                                         $option = ReferenceOption::CASCADE();
                                         break;
                                     case $tokens->seekTokenNum(SqlToken::T_SET()) && $tokens->seekTokenNum(SqlToken::T_NULL()):
                                         $option = ReferenceOption::SET_NULL();
                                         break;
                                     case $tokens->seekTokenNum(SqlToken::T_NO()) && $tokens->seekTokenText("ACTION"):
                                         $option = ReferenceOption::NO_ACTION();
                                         break;
                                     default:
                                         throw new MalformedSqlException("Invalid reference-option for foreign key ON DELETE option!", $tokens);
                                 }
                                 $indexJob->setForeignKeyOnDeleteReferenceOption($option);
                                 break;
                             case $tokens->seekTokenNum(SqlToken::T_UPDATE()):
                                 switch (true) {
                                     case $tokens->seekTokenNum(SqlToken::T_RESTRICT()):
                                         $option = ReferenceOption::RESTRICT();
                                         break;
                                     case $tokens->seekTokenNum(SqlToken::T_CASCADE()):
                                         $option = ReferenceOption::CASCADE();
                                         break;
                                     case $tokens->seekTokenNum(SqlToken::T_SET()) && $tokens->seekTokenNum(SqlToken::T_NULL()):
                                         $option = ReferenceOption::SET_NULL();
                                         break;
                                     case $tokens->seekTokenNum(SqlToken::T_NO()) && $tokens->seekTokenText("ACTION"):
                                         $option = ReferenceOption::NO_ACTION();
                                         break;
                                     default:
                                         throw new MalformedSqlException("Invalid reference-option for foreign key ON UPDATE option!", $tokens);
                                 }
                                 $indexJob->setForeignKeyOnUpdateReferenceOption($option);
                                 break;
                             default:
                                 throw new MalformedSqlException("Invalid ON event for foreign key (allowed are UPDATE and DELETE)!", $tokens);
                         }
                     }
                     $createTableJob->addIndex($indexJob);
                     break;
                     # CHECK (expression)
                 # CHECK (expression)
                 case $tokens->seekTokenNum(SqlToken::T_CHECK()):
                     if (!$this->conditionParser->canParseTokens($tokens)) {
                         throw new MalformedSqlException("Invalid CHECK condition statement!", $tokens);
                     }
                     $createTableJob->addCheck($this->conditionParser->convertSqlToJob($tokens));
                     break;
                 default:
                     throw new MalformedSqlException("Invalid definition in CREATE TABLE statement!", $tokens);
             }
         } while ($tokens->seekTokenText(','));
     }
     if ($checkEndParenthesis && !$tokens->seekTokenText(')')) {
         throw new MalformedSqlException("Missing closing parenthesis at end of table-definition!", $tokens);
     }
     ### TABLE OPTIONS
     while (true) {
         switch (true) {
             case $tokens->seekTokenNum(SqlToken::T_ENGINE()):
             case $tokens->seekTokenNum(SqlToken::T_TYPE()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_STRING)) {
                     throw new MalformedSqlException("Missing T_STRING after T_ENGINE!", $tokens);
                 }
                 $createTableJob->setEngine(Engine::factory($tokens->getCurrentTokenString()));
                 break;
             case $tokens->seekTokenNum(SqlToken::T_AUTO_INCREMENT()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_NUM_STRING)) {
                     throw new MalformedSqlException("Missing number-string for T_AUTO_INCREMENT!", $tokens);
                 }
                 $createTableJob->setAutoIncrement((int) $tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_AVG_ROW_LENGTH()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_NUM_STRING)) {
                     throw new MalformedSqlException("Missing number-string for T_AVG_ROW_LENGTH!", $tokens);
                 }
                 $createTableJob->setAverageRowLength((int) $tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_CHARACTER(), TokenIterator::NEXT, [SqlToken::T_DEFAULT()]):
                 if (!$tokens->seekTokenNum(SqlToken::T_SET())) {
                     throw new MalformedSqlException("Missing SET after CHARACTER keyword!", $tokens);
                 }
             case $tokens->seekTokenNum(SqlToken::T_CHARSET(), TokenIterator::NEXT, [SqlToken::T_DEFAULT()]):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING) && !$tokens->seekTokenNum(T_STRING)) {
                     throw new MalformedSqlException("Missing string for CHARACTER SET!", $tokens);
                 }
                 $createTableJob->setCharacterSet($tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_COLLATE()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING) && !$tokens->seekTokenNum(T_STRING)) {
                     throw new MalformedSqlException("Missing string for COLLATE!", $tokens);
                 }
                 $createTableJob->setCollate($tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_CHECKSUM()):
                 $tokens->seekTokenText('=');
                 switch (true) {
                     case $tokens->seekTokenText('0'):
                         $createTableJob->setUseChecksum(false);
                         break;
                     case $tokens->seekTokenText('1'):
                         $createTableJob->setUseChecksum(true);
                         break;
                     default:
                         throw new MalformedSqlException("Invalid value for CHECKSUM! (only 0 or 1 allowed!)", $tokens);
                 }
                 break;
             case $tokens->seekTokenNum(SqlToken::T_COMMENT()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING)) {
                     throw new MalformedSqlException("Missing encapsed string for comment!", $tokens);
                 }
                 $createTableJob->setComment($tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_CONNECTION()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING)) {
                     throw new MalformedSqlException("Missing encapsed string for connection-string!", $tokens);
                 }
                 $createTableJob->setConnectString($tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_MAX_ROWS()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_NUM_STRING)) {
                     throw new MalformedSqlException("Missing number-string for MAX_ROWS!", $tokens);
                 }
                 $createTableJob->setMaximumRows((int) $tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_MIN_ROWS()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_NUM_STRING)) {
                     throw new MalformedSqlException("Missing number-string for MIN_ROWS!", $tokens);
                 }
                 $createTableJob->setMinimumRows((int) $tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_PACK_KEYS()):
                 $tokens->seekTokenText('=');
                 switch (true) {
                     case $tokens->seekTokenText('DEFAULT'):
                     case $tokens->seekTokenText('0'):
                         $createTableJob->setDelayKeyWrite(false);
                         break;
                     case $tokens->seekTokenText('1'):
                         $createTableJob->setDelayKeyWrite(true);
                         break;
                     default:
                         throw new MalformedSqlException("Invalid value for PACK_KEYS! (only DEFAULT, 0 or 1 allowed!)", $tokens);
                 }
                 break;
             case $tokens->seekTokenNum(SqlToken::T_PASSWORD()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING)) {
                     throw new MalformedSqlException("Missing encapsed string for password!", $tokens);
                 }
                 $createTableJob->setPassword($tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_DELAY_KEY_WRITE()):
                 $tokens->seekTokenText('=');
                 switch (true) {
                     case $tokens->seekTokenText('0'):
                         $createTableJob->setDelayKeyWrite(false);
                         break;
                     case $tokens->seekTokenText('1'):
                         $createTableJob->setDelayKeyWrite(true);
                         break;
                     default:
                         throw new MalformedSqlException("Invalid value for DELAY_KEY_WRITE! (only 0 or 1 allowed!)", $tokens);
                 }
                 break;
             case $tokens->seekTokenNum(SqlToken::T_ROW_FORMAT()):
                 $tokens->seekTokenText('=');
                 $keyword = $tokens->getExclusiveTokenString();
                 $rowFormat = RowFormat::factory($keyword);
                 $tokens->seekIndex($tokens->getExclusiveTokenIndex());
                 $createTableJob->setRowFormat($rowFormat);
                 break;
             case $tokens->seekTokenNum(SqlToken::T_UNION()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenText('(')) {
                     throw new MalformedSqlException("Missing opening parenthesis for union-table-definition!", $tokens);
                 }
                 do {
                     if (!$this->tableParser->canParseTokens($tokens)) {
                         throw new MalformedSqlException("Invalid table in table-list for defining union tables!", $tokens);
                     }
                     $createTableJob->addUnionTable($this->tableParser->convertSqlToJob($tokens));
                 } while ($tokens->seekTokenText(','));
                 if (!$tokens->seekTokenText(')')) {
                     throw new MalformedSqlException("Missing closing parenthesis at union-table-list!", $tokens);
                 }
                 break;
             case $tokens->seekTokenNum(SqlToken::T_INSERT_METHOD()):
                 $tokens->seekTokenText('=');
                 switch (true) {
                     case $tokens->seekTokenNum(SqlToken::T_NO()):
                         $createTableJob->setInsertMethod(InsertMethod::NO());
                         break;
                     case $tokens->seekTokenNum(SqlToken::T_FIRST()):
                         $createTableJob->setInsertMethod(InsertMethod::FIRST());
                         break;
                     case $tokens->seekTokenNum(SqlToken::T_LAST()):
                         $createTableJob->setInsertMethod(InsertMethod::LAST());
                         break;
                     default:
                         throw new MalformedSqlException("Invalid value given for insert-method (allowed are NO, FIRST and LAST)!");
                 }
                 break;
             case $tokens->seekTokenNum(SqlToken::T_DATA()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(SqlToken::T_DIRECTORY())) {
                     throw new MalformedSqlException("Missing T_DIRECTORY after T_DATA for data-directory!", $tokens);
                 }
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING)) {
                     throw new MalformedSqlException("Missing encapsed string for comment!", $tokens);
                 }
                 $createTableJob->setDataDirectory($tokens->getCurrentTokenString());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_INDEX()):
                 $tokens->seekTokenText('=');
                 if (!$tokens->seekTokenNum(SqlToken::T_DIRECTORY())) {
                     throw new MalformedSqlException("Missing T_DIRECTORY after T_INDEX for index-directory!", $tokens);
                 }
                 if (!$tokens->seekTokenNum(T_CONSTANT_ENCAPSED_STRING)) {
                     throw new MalformedSqlException("Missing encapsed string for comment!", $tokens);
                 }
                 $createTableJob->setIndexDirectory($tokens->getCurrentTokenString());
                 break;
             default:
                 break 2;
         }
     }
     return $createTableJob;
 }