LazyRecord\TableParser\SqliteTableDefinitionParser::parse PHP Method

parse() public method

public parse ( )
    public function parse()
    {
        $tableDef = new stdClass();
        $this->skipSpaces();
        $keyword = $this->tryParseKeyword(['CREATE']);
        $this->skipSpaces();
        if ($this->tryParseKeyword(['TEMPORARY', 'TEMP'])) {
            $tableDef->temporary = true;
        }
        $this->skipSpaces();
        $this->tryParseKeyword(['TABLE']);
        $this->skipSpaces();
        if ($this->tryParseKeyword(['IF']) && $this->tryParseKeyword(['NOT']) && $this->tryParseKeyword(['EXISTS'])) {
            $tableDef->ifNotExists = true;
        }
        $tableName = $this->tryParseIdentifier();
        $tableDef->tableName = $tableName->val;
        $this->advance('(');
        $tableDef->columns = $this->parseColumnDefinitions();
        $this->advance(')');
        return $tableDef;
    }

Usage Example

 /**
  * @see https://github.com/c9s/LazyRecord/issues/94
  */
 public function testForIssue94()
 {
     $parser = new SqliteTableDefinitionParser('CREATE TABLE foo (`col4` text DEFAULT \'123\\\'\'\')');
     $def = $parser->parse();
     $this->assertNotNull($def);
     $this->assertEquals('foo', $def->tableName);
     $this->assertCount(1, $def->columns->columns);
     $this->assertEquals('col4', $def->columns->columns[0]->name);
     $this->assertEquals('TEXT', $def->columns->columns[0]->type);
     $this->assertEquals("123\\'", $def->columns->columns[0]->default);
 }