Skip to content

Commit fb9d2b3

Browse files
committed
Adding support for table comments in Sqlite.
1 parent c4d83a7 commit fb9d2b3

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/Doctrine/DBAL/Platforms/SqlitePlatform.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function strlen;
2424
use function strpos;
2525
use function strtolower;
26+
use function trim;
2627

2728
/**
2829
* The SqlitePlatform class describes the specifics and dialects of the SQLite
@@ -332,7 +333,15 @@ protected function _getCreateTableSQL($name, array $columns, array $options = []
332333
}
333334
}
334335

335-
$query = ['CREATE TABLE ' . $name . ' (' . $queryFields . ')'];
336+
// Comment
337+
$tableComment = '';
338+
if (isset($options['comment'])) {
339+
$comment = trim($options['comment'], " '");
340+
341+
$tableComment = $this->getInlineTableCommentSQL($comment);
342+
}
343+
344+
$query = ['CREATE TABLE ' . $name . ' ' . $tableComment . '(' . $queryFields . ')'];
336345

337346
if (isset($options['alter']) && $options['alter'] === true) {
338347
return $query;
@@ -605,6 +614,11 @@ public function getInlineColumnCommentSQL($comment)
605614
return '--' . str_replace("\n", "\n--", $comment) . "\n";
606615
}
607616

617+
private function getInlineTableCommentSQL($comment)
618+
{
619+
return $this->getInlineColumnCommentSQL($comment);
620+
}
621+
608622
/**
609623
* {@inheritDoc}
610624
*/

lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,20 @@ private function parseColumnCollationFromSQL(string $column, string $sql) : ?str
466466
return $match[1];
467467
}
468468

469+
private function parseTableCommentFromSQL(string $table, string $sql) : ?string
470+
{
471+
$pattern = '{[\s]*CREATE TABLE(?:\W"' . preg_quote($this->_platform->quoteSingleIdentifier($table)) . '"\W|\W' . preg_quote($table)
472+
. '\W)(?:\(.*?\)|[^,(])*?,?((?:(?!\n))(?:\s*--[^\n]*\n?)+)}i';
473+
474+
if (preg_match($pattern, $sql, $match) !== 1) {
475+
return null;
476+
}
477+
478+
$comment = preg_replace('{^\s*--}m', '', rtrim($match[1], "\n"));
479+
480+
return $comment === '' ? null : $comment;
481+
}
482+
469483
private function parseColumnCommentFromSQL(string $column, string $sql) : ?string
470484
{
471485
$pattern = '{[\s(,](?:\W' . preg_quote($this->_platform->quoteSingleIdentifier($column)) . '\W|\W' . preg_quote($column)
@@ -499,4 +513,24 @@ private function getCreateTableSQL(string $table) : ?string
499513
[$table]
500514
) ?: null;
501515
}
516+
517+
/**
518+
* @param string $tableName
519+
*
520+
* @return Table
521+
*/
522+
public function listTableDetails($tableName)
523+
{
524+
$table = parent::listTableDetails($tableName);
525+
526+
$tableCreateSql = $this->getCreateTableSQL($tableName) ?? '';
527+
528+
$comment = $this->parseTableCommentFromSQL($tableName, $tableCreateSql);
529+
530+
if (! empty($comment)) {
531+
$table->addOption('comment', $comment);
532+
}
533+
534+
return $table;
535+
}
502536
}

0 commit comments

Comments
 (0)