Exemple #1
0
 /**
  * Creates column object from tokens.
  * <p>
  * Current position should point to the name of the column.
  *
  * @param Tokenizer $tokenizer Tokens collection.
  *
  * @return Column
  * @throws NotSupportedException
  */
 public static function create(Tokenizer $tokenizer)
 {
     $columnName = $tokenizer->getCurrentToken()->text;
     $tokenizer->nextToken();
     $tokenizer->skipWhiteSpace();
     $token = $tokenizer->getCurrentToken();
     $columnType = $token->upper;
     if (!self::checkType($columnType)) {
         throw new NotSupportedException("column type expected but [" . $tokenizer->getCurrentToken()->text . "] found. line: " . $tokenizer->getCurrentToken()->line);
     }
     $column = new self($columnName);
     $column->type = $columnType;
     $level = $token->level;
     $lengthLevel = -1;
     $columnDefinition = '';
     do {
         if ($token->level == $level && $token->text == ',') {
             break;
         }
         if ($token->level < $level && $token->text == ')') {
             break;
         }
         $columnDefinition .= $token->text;
         if ($token->upper === 'NOT') {
             $column->nullable = false;
         } elseif ($token->upper === 'DEFAULT') {
             $column->default = false;
         } elseif ($column->default === false) {
             if ($token->type !== Token::T_WHITESPACE && $token->type !== Token::T_COMMENT) {
                 $column->default = $token->text;
             }
         }
         $token = $tokenizer->nextToken();
         //parentheses after type
         if ($lengthLevel == -1) {
             if ($token->text == '(') {
                 $lengthLevel = $token->level;
                 $column->length = '';
                 while (!$tokenizer->endOfInput()) {
                     $columnDefinition .= $token->text;
                     $token = $tokenizer->nextToken();
                     if ($token->level == $lengthLevel && $token->text == ')') {
                         break;
                     }
                     $column->length .= $token->text;
                 }
             } elseif ($token->type !== Token::T_WHITESPACE && $token->type !== Token::T_COMMENT) {
                 $lengthLevel = 0;
             }
         }
     } while (!$tokenizer->endOfInput());
     $column->setBody($columnDefinition);
     return $column;
 }
Exemple #2
0
 /**
  * Searches token collection for 'ON' keyword.
  * <p>
  * Advances current position on to next token skipping whitespace.
  *
  * @param Tokenizer $tokenizer Tokens collection.
  *
  * @return void
  * @throws NotSupportedException
  */
 public static function searchTableName(Tokenizer $tokenizer)
 {
     $lineToken = $tokenizer->getCurrentToken();
     while (!$tokenizer->endOfInput()) {
         if ($tokenizer->getCurrentToken()->upper === 'ON') {
             $tokenizer->nextToken();
             $tokenizer->skipWhiteSpace();
             return;
         }
         $tokenizer->nextToken();
     }
     throw new NotSupportedException('Trigger: table name not found. line: ' . $lineToken->line);
 }
Exemple #3
0
 /**
  * @param Tokenizer $tokenizer Statement tokens.
  *
  * @return void
  * @throws NotSupportedException
  */
 protected function executeCreateSequence(Tokenizer $tokenizer)
 {
     $tokenizer->skipWhiteSpace();
     $this->sequences->add(Sequence::create($tokenizer));
 }
Exemple #4
0
 /**
  * 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;
 }