diff --git a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php index b7382ec08b..42baf560ea 100644 --- a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php @@ -332,7 +332,15 @@ protected function _getCreateTableSQL($name, array $columns, array $options = [] } } - $query = ['CREATE TABLE ' . $name . ' (' . $queryFields . ')']; + // Comment + $tableComment = ''; + if (isset($options['comment'])) { + $comment = trim($options['comment'], " '"); + + $tableComment = $this->getInlineTableCommentSQL($comment); + } + + $query = ['CREATE TABLE ' . $name . ' ' . $tableComment . '(' . $queryFields . ')']; if (isset($options['alter']) && $options['alter'] === true) { return $query; @@ -605,6 +613,11 @@ public function getInlineColumnCommentSQL($comment) return '--' . str_replace("\n", "\n--", $comment) . "\n"; } + private function getInlineTableCommentSQL($comment) + { + return $this->getInlineColumnCommentSQL($comment); + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php index e819d963f0..d58bc0e5e1 100644 --- a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php @@ -466,6 +466,20 @@ private function parseColumnCollationFromSQL(string $column, string $sql) : ?str return $match[1]; } + private function parseTableCommentFromSQL(string $table, string $sql) : ?string + { + $pattern = '{[\s]*CREATE TABLE(?:\W"' . preg_quote($this->_platform->quoteSingleIdentifier($table)) . '"\W|\W' . preg_quote($table) + . '\W)(?:\(.*?\)|[^,(])*?,?((?:(?!\n))(?:\s*--[^\n]*\n?)+)}i'; + + if (preg_match($pattern, $sql, $match) !== 1) { + return null; + } + + $comment = preg_replace('{^\s*--}m', '', rtrim($match[1], "\n")); + + return $comment === '' ? null : $comment; + } + private function parseColumnCommentFromSQL(string $column, string $sql) : ?string { $pattern = '{[\s(,](?:\W' . preg_quote($this->_platform->quoteSingleIdentifier($column)) . '\W|\W' . preg_quote($column) @@ -499,4 +513,19 @@ private function getCreateTableSQL(string $table) : ?string [$table] ) ?: null; } + + public function listTableDetails($tableName) + { + $table = parent::listTableDetails($tableName); + + $tableCreateSql = $this->getCreateTableSQL($tableName); + + $comment = $this->parseTableCommentFromSQL($tableName, $tableCreateSql); + + if (!empty($comment)) { + $table->addOption('comment', $comment); + } + + return $table; + } }