Skip to content

Commit

Permalink
Adding support for table comments in Sqlite.
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Apr 12, 2019
1 parent c4d83a7 commit 177c2d3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -605,6 +613,11 @@ public function getInlineColumnCommentSQL($comment)
return '--' . str_replace("\n", "\n--", $comment) . "\n";
}

private function getInlineTableCommentSQL($comment)
{
return $this->getInlineColumnCommentSQL($comment);
}

/**
* {@inheritDoc}
*/
Expand Down
29 changes: 29 additions & 0 deletions lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 177c2d3

Please sign in to comment.