SqlParser\Components\AlterOperation::parse PHP 메소드

parse() 공개 정적인 메소드

public static parse ( Parser $parser, TokensList $list, array $options = [] ) : AlterOperation
$parser SqlParser\Parser The parser that serves as context.
$list SqlParser\TokensList The list of tokens that are being parsed.
$options array Parameters for parsing.
리턴 AlterOperation
    public static function parse(Parser $parser, TokensList $list, array $options = array())
    {
        $ret = new AlterOperation();
        /**
         * Counts brackets.
         *
         * @var int $brackets
         */
        $brackets = 0;
        /**
         * The state of the parser.
         *
         * Below are the states of the parser.
         *
         *      0 ---------------------[ options ]---------------------> 1
         *
         *      1 ----------------------[ field ]----------------------> 2
         *
         *      2 -------------------------[ , ]-----------------------> 0
         *
         * @var int $state
         */
        $state = 0;
        for (; $list->idx < $list->count; ++$list->idx) {
            /**
             * Token parsed at this moment.
             *
             * @var Token $token
             */
            $token = $list->tokens[$list->idx];
            // End of statement.
            if ($token->type === Token::TYPE_DELIMITER) {
                break;
            }
            // Skipping comments.
            if ($token->type === Token::TYPE_COMMENT) {
                continue;
            }
            // Skipping whitespaces.
            if ($token->type === Token::TYPE_WHITESPACE) {
                if ($state === 2) {
                    // When parsing the unknown part, the whitespaces are
                    // included to not break anything.
                    $ret->unknown[] = $token;
                }
                continue;
            }
            if ($state === 0) {
                $ret->options = OptionsArray::parse($parser, $list, $options);
                if ($ret->options->has('AS')) {
                    for (; $list->idx < $list->count; ++$list->idx) {
                        if ($list->tokens[$list->idx]->type === Token::TYPE_DELIMITER) {
                            break;
                        }
                        $ret->unknown[] = $list->tokens[$list->idx];
                    }
                    break;
                }
                $state = 1;
            } elseif ($state === 1) {
                $ret->field = Expression::parse($parser, $list, array('breakOnAlias' => true, 'parseField' => 'column'));
                if ($ret->field === null) {
                    // No field was read. We go back one token so the next
                    // iteration will parse the same token, but in state 2.
                    --$list->idx;
                }
                $state = 2;
            } elseif ($state === 2) {
                if ($token->type === Token::TYPE_OPERATOR) {
                    if ($token->value === '(') {
                        ++$brackets;
                    } elseif ($token->value === ')') {
                        --$brackets;
                    } elseif ($token->value === ',' && $brackets === 0) {
                        break;
                    }
                } elseif (!empty(Parser::$STATEMENT_PARSERS[$token->value])) {
                    // We have reached the end of ALTER operation and suddenly found
                    // a start to new statement, but have not find a delimiter between them
                    if (!($token->value == 'SET' && $list->tokens[$list->idx - 1]->value == 'CHARACTER')) {
                        $parser->error(__('A new statement was found, but no delimiter between it and the previous one.'), $token);
                        break;
                    }
                }
                $ret->unknown[] = $token;
            }
        }
        if ($ret->options->isEmpty()) {
            $parser->error(__('Unrecognized alter operation.'), $list->tokens[$list->idx]);
        }
        --$list->idx;
        return $ret;
    }

Usage Example

예제 #1
0
 /**
  * @param Parser     $parser The instance that requests parsing.
  * @param TokensList $list   The list of tokens to be parsed.
  *
  * @return void
  */
 public function parse(Parser $parser, TokensList $list)
 {
     ++$list->idx;
     // Skipping `ALTER`.
     $this->options = OptionsArray::parse($parser, $list, static::$OPTIONS);
     // Skipping `TABLE`.
     $list->getNextOfTypeAndValue(Token::TYPE_KEYWORD, 'TABLE');
     // Parsing affected table.
     $this->table = Expression::parse($parser, $list, array('noAlias' => true, 'noBrackets' => true));
     ++$list->idx;
     // Skipping field.
     /**
      * The state of the parser.
      *
      * Below are the states of the parser.
      *
      *      0 -----------------[ alter operation ]-----------------> 1
      *
      *      1 -------------------------[ , ]-----------------------> 0
      *
      * @var int $state
      */
     $state = 0;
     for (; $list->idx < $list->count; ++$list->idx) {
         /**
          * Token parsed at this moment.
          *
          * @var Token $token
          */
         $token = $list->tokens[$list->idx];
         // End of statement.
         if ($token->type === Token::TYPE_DELIMITER) {
             break;
         }
         // Skipping whitespaces and comments.
         if ($token->type === Token::TYPE_WHITESPACE || $token->type === Token::TYPE_COMMENT) {
             continue;
         }
         if ($state === 0) {
             $this->altered[] = AlterOperation::parse($parser, $list);
             $state = 1;
         } elseif ($state === 1) {
             if ($token->type === Token::TYPE_OPERATOR && $token->value === ',') {
                 $state = 0;
             }
         }
     }
 }
All Usage Examples Of SqlParser\Components\AlterOperation::parse
AlterOperation