Skip to content

Commit

Permalink
Handle table comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Cima committed May 1, 2016
1 parent a30d3cf commit 152adbb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Model/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Table
*/
private $defaultCharset;

/**
* @var string
*/
private $comment;

/**
* @param string $name
*/
Expand Down Expand Up @@ -334,6 +339,22 @@ public function setDefaultCharset($defaultCharset)
$this->defaultCharset = $defaultCharset;
}

/**
* @return string
*/
public function getComment()
{
return $this->comment;
}

/**
* @param string $comment
*/
public function setComment($comment)
{
$this->comment = $comment;
}

/**
* @return string
*/
Expand Down Expand Up @@ -415,6 +436,10 @@ public function generateCreationScript($ignoreAutoIncrement = false, $sortKeys =
$tableOptions[] = sprintf('DEFAULT CHARSET=%s', $this->defaultCharset);
}

if ($this->comment) {
$tableOptions[] = sprintf('COMMENT \'%s\'', str_replace('\'','\'\'', $this->comment));
}

$implodedTableOptions = implode(' ', $tableOptions);

if (!empty($implodedTableOptions)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function parseTables($sqlScript)
$engine = $matches['engine'][$i];
$autoIncrement = $matches['autoIncrement'][$i];
$defaultCharset = $matches['defaultCharset'][$i];
$comment = $matches['comment'][$i];

$table = new Table($name);
$table->setDefinition(trim($definition));
Expand All @@ -70,6 +71,10 @@ public function parseTables($sqlScript)
$table->setDefaultCharset($defaultCharset);
}

if ($comment) {
$table->setComment(str_replace('\'\'', '\'', $comment));
}

$tables[$name] = $table;
}

Expand Down
2 changes: 2 additions & 0 deletions src/RegExpPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static function tables()
$pattern .= '(?:DEFAULT CHARSET=(?<defaultCharset>[^;\s]+))?\s*';
$pattern .= '|';
$pattern .= '(?:COLLATE=.+?)?\s*';
$pattern .= '|';
$pattern .= '(?:COMMENT \'(?<comment>([^\']|\'\')+)\')?\s*';
$pattern .= ')*';
$pattern .= ')(?:\/\*.+?\*\/)?\s*';
$pattern .= ';/';
Expand Down
10 changes: 10 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ public function testIsParsingDatabase()
$this->assertCount(19, $database->getTables());
}

public function testIsParsingTableComment()
{
$parser = new Parser();

$database = $parser->parseDatabase($this->getDatabaseFixture('sakila.sql'));

$this->assertInstanceOf(Database::class, $database);
$this->assertEquals('table\'s comment', $database->getTableByName('test')->getComment());
}

public function testIsGeneratingTableCreationScript()
{
$parser = new Parser();
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/sakila.sql
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ DROP TABLE IF EXISTS `test`;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`test1` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT 'table''s comment';
/*!40101 SET character_set_client = @saved_cs_client */;

--
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/sakila_new.sql
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ DROP TABLE IF EXISTS `test`;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`test1` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT 'table''s comment';
/*!40101 SET character_set_client = @saved_cs_client */;

--
Expand Down

0 comments on commit 152adbb

Please sign in to comment.