diff --git a/src/Model/Table.php b/src/Model/Table.php index e1cdebd..f8b634a 100644 --- a/src/Model/Table.php +++ b/src/Model/Table.php @@ -59,6 +59,11 @@ class Table */ private $defaultCharset; + /** + * @var string + */ + private $comment; + /** * @param string $name */ @@ -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 */ @@ -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)) { diff --git a/src/Parser.php b/src/Parser.php index d5a5291..4a7ea75 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -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)); @@ -70,6 +71,10 @@ public function parseTables($sqlScript) $table->setDefaultCharset($defaultCharset); } + if ($comment) { + $table->setComment(str_replace('\'\'', '\'', $comment)); + } + $tables[$name] = $table; } diff --git a/src/RegExpPattern.php b/src/RegExpPattern.php index 295df48..b117611 100644 --- a/src/RegExpPattern.php +++ b/src/RegExpPattern.php @@ -41,6 +41,8 @@ public static function tables() $pattern .= '(?:DEFAULT CHARSET=(?[^;\s]+))?\s*'; $pattern .= '|'; $pattern .= '(?:COLLATE=.+?)?\s*'; + $pattern .= '|'; + $pattern .= '(?:COMMENT \'(?([^\']|\'\')+)\')?\s*'; $pattern .= ')*'; $pattern .= ')(?:\/\*.+?\*\/)?\s*'; $pattern .= ';/'; diff --git a/tests/ParserTest.php b/tests/ParserTest.php index dbb6acb..35ea9bc 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -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(); diff --git a/tests/fixtures/sakila.sql b/tests/fixtures/sakila.sql index e555973..692a8c6 100644 --- a/tests/fixtures/sakila.sql +++ b/tests/fixtures/sakila.sql @@ -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 */; -- diff --git a/tests/fixtures/sakila_new.sql b/tests/fixtures/sakila_new.sql index f902b89..a8ff2c7 100644 --- a/tests/fixtures/sakila_new.sql +++ b/tests/fixtures/sakila_new.sql @@ -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 */; --