コード例 #1
0
ファイル: CreateSqlParser.php プロジェクト: addiks/phpsql
 /**
  * This parses a CREATE INDEX statement.
  */
 protected function parseCreateIndex(SQLTokenIterator $tokens)
 {
     /* @var $entity CreateIndexStatement */
     $entity = new CreateIndexStatement();
     ### FLAGS
     if ($tokens->isToken(SqlToken::T_UNIQUE(), TokenIterator::PREVIOUS, [SqlToken::T_PRIMARY()])) {
         $entity->setIsUnique(true);
     }
     if ($tokens->isToken(SqlToken::T_PRIMARY(), TokenIterator::PREVIOUS, [SqlToken::T_UNIQUE()])) {
         $entity->setIsPrimary(true);
     }
     ### NAME
     if (!$tokens->seekTokenNum(T_STRING)) {
         throw new MalformedSqlException("Missing valid index-name!", $tokens);
     }
     $entity->setName($tokens->getCurrentTokenString());
     ### USING
     if ($tokens->seekTokenNum(SqlToken::T_USING())) {
         switch (true) {
             // TODO: R-TREE index not implemented yet!
             case $tokens->seekTokenNum(SqlToken::T_RTREE()):
             case $tokens->seekTokenNum(SqlToken::T_BTREE()):
                 $entity->setIndexType(IndexType::BTREE());
                 break;
             case $tokens->seekTokenNum(SqlToken::T_HASH()):
                 $entity->setIndexType(IndexType::HASH());
                 break;
             default:
                 throw new MalformedSqlException("Invalid index-type specified!", $tokens);
         }
     }
     ### TABLE
     if (!$tokens->seekTokenNum(SqlToken::T_ON())) {
         throw new MalformedSqlException("Missing T_ON for CREATE INDEX statement!", $tokens);
     }
     if (!$this->tableParser->canParseTokens($tokens)) {
         throw new MalformedSqlException("Missing valid table-specifier for CREATE INDEX statement!", $tokens);
     }
     $entity->setTable($this->tableParser->convertSqlToJob($tokens));
     ### COLUMNS
     if (!$tokens->seekTokenText('(')) {
         throw new MalformedSqlException("Missing beginning parenthesis holding columns in CREATE INDEX statement!", $tokens);
     }
     do {
         if (!$this->columnParser->canParseTokens($tokens)) {
             throw new MalformedSqlException("Missing valid column-specifier in CREATE INDEX statement!", $tokens);
         }
         $column = $this->columnParser->convertSqlToJob($tokens);
         $length = null;
         if ($tokens->seekTokenText('(')) {
             if (!$this->valueParser->canParseTokens($tokens)) {
                 throw new MalformedSqlException("Missing valid column-length in CREATE INDEX statement!", $tokens);
             }
             $length = $this->valueParser->convertSqlToJob($tokens);
             if (!$tokens->seekTokenText(')')) {
                 throw new MalformedSqlException("Missing closing parenthesis holding column-length in CREATE INDEX statement!", $tokens);
             }
         }
         $direction = null;
         if ($tokens->seekTokenNum(SqlToken::T_ASC())) {
             $direction = SqlToken::T_ASC();
         } elseif ($tokens->seekTokenNum(SqlToken::T_DESC())) {
             $direction = SqlToken::T_DESC();
         }
         $entity->addColumn($column, $length, $direction);
     } while ($tokens->seekTokenText(','));
     if (!$tokens->seekTokenText(')')) {
         throw new MalformedSqlException("Missing closing parenthesis holding columns in CREATE INDEX statement!", $tokens);
     }
     ### WITH PARSER
     if ($tokens->seekTokenNum(SqlToken::T_WITH())) {
         if (!$tokens->seekTokenNum(SqlToken::T_PARSER())) {
             throw new MalformedSqlException("Missing T_PARSER after T_WITH in CREATE INDEX statement!", $tokens);
         }
         if (!$tokens->seekTokenNum(T_STRING)) {
             throw new MalformedSqlException("Missing valid parser name after WITH PARSER in CREATE INDEX statement!", $tokens);
         }
         $entity->setParser($tokens->getCurrentTokenString());
     }
     return $entity;
 }