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 fb9d2b3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
Expand Up @@ -23,6 +23,7 @@
use function strlen;
use function strpos;
use function strtolower;
use function trim;

/**
* The SqlitePlatform class describes the specifics and dialects of the SQLite
Expand Down Expand Up @@ -332,7 +333,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 +614,11 @@ public function getInlineColumnCommentSQL($comment)
return '--' . str_replace("\n", "\n--", $comment) . "\n";
}

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

/**
* {@inheritDoc}
*/
Expand Down
34 changes: 34 additions & 0 deletions lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php
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,24 @@ private function getCreateTableSQL(string $table) : ?string
[$table]
) ?: null;
}

/**
* @param string $tableName
*
* @return Table
*/
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 fb9d2b3

Please sign in to comment.