SqlParser\Components\PartitionDefinition::parse PHP Method

parse() public static method

public static parse ( Parser $parser, TokensList $list, array $options = [] ) : PartitionDefinition
$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.
return PartitionDefinition
    public static function parse(Parser $parser, TokensList $list, array $options = array())
    {
        $ret = new PartitionDefinition();
        /**
         * The state of the parser.
         *
         * Below are the states of the parser.
         *
         *      0 -------------[ PARTITION | SUBPARTITION ]------------> 1
         *
         *      1 -----------------------[ name ]----------------------> 2
         *
         *      2 ----------------------[ VALUES ]---------------------> 3
         *
         *      3 ---------------------[ LESS THAN ]-------------------> 4
         *      3 ------------------------[ IN ]-----------------------> 4
         *
         *      4 -----------------------[ expr ]----------------------> 5
         *
         *      5 ----------------------[ options ]--------------------> 6
         *
         *      6 ------------------[ subpartitions ]------------------> (END)
         *
         * @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) {
                $ret->isSubpartition = $token->type === Token::TYPE_KEYWORD && $token->value === 'SUBPARTITION';
                $state = 1;
            } elseif ($state === 1) {
                $ret->name = $token->value;
                // Looking ahead for a 'VALUES' keyword.
                $idx = $list->idx;
                $list->getNext();
                $nextToken = $list->getNext();
                $list->idx = $idx;
                $state = $nextToken->type === Token::TYPE_KEYWORD && $nextToken->value === 'VALUES' ? 2 : 5;
            } elseif ($state === 2) {
                $state = 3;
            } elseif ($state === 3) {
                $ret->type = $token->value;
                $state = 4;
            } elseif ($state === 4) {
                if ($token->value === 'MAXVALUE') {
                    $ret->expr = $token->value;
                } else {
                    $ret->expr = Expression::parse($parser, $list, array('parenthesesDelimited' => true, 'breakOnAlias' => true));
                }
                $state = 5;
            } elseif ($state === 5) {
                $ret->options = OptionsArray::parse($parser, $list, static::$OPTIONS);
                $state = 6;
            } elseif ($state === 6) {
                if ($token->type === Token::TYPE_OPERATOR && $token->value === '(') {
                    $ret->subpartitions = ArrayObj::parse($parser, $list, array('type' => 'SqlParser\\Components\\PartitionDefinition'));
                    ++$list->idx;
                }
                break;
            }
        }
        --$list->idx;
        return $ret;
    }

Usage Example

 public function testParse()
 {
     $component = PartitionDefinition::parse(new Parser(), $this->getTokensList('PARTITION p0 VALUES LESS THAN(1990)'));
     $this->assertFalse($component->isSubpartition);
     $this->assertEquals('p0', $component->name);
     $this->assertEquals('LESS THAN', $component->type);
     $this->assertEquals('(1990)', $component->expr->expr);
 }
PartitionDefinition