Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for column inline comments in SQLite #819

Merged
merged 1 commit into from
Jan 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,18 @@ public function getCommentOnColumnSQL($tableName, $columnName, $comment)
" IS " . $comment;
}

/**
* Returns the SQL to create inline comment on a column.
*
* @param string $comment
*
* @return string
*/
public function getInlineColumnCommentSQL($comment)
{
return "COMMENT " . $this->quoteStringLiteral($comment);
}

/**
* Returns the SQL used to create a table.
*
Expand Down Expand Up @@ -2211,7 +2223,7 @@ public function getColumnDeclarationSQL($name, array $field)
}

if ($this->supportsInlineColumnComments() && isset($field['comment']) && $field['comment'] !== '') {
$columnDef .= " COMMENT " . $this->quoteStringLiteral($field['comment']);
$columnDef .= ' ' . $this->getInlineColumnCommentSQL($field['comment']);
}

return $name . ' ' . $columnDef;
Expand Down
16 changes: 16 additions & 0 deletions lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,14 @@ public function supportsColumnCollation()
return true;
}

/**
* {@inheritDoc}
*/
public function supportsInlineColumnComments()
{
return true;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -567,6 +575,14 @@ public function getForUpdateSql()
return '';
}

/**
* {@inheritDoc}
*/
public function getInlineColumnCommentSQL($comment)
{
return '--'.str_replace("\n", "\n--", $comment)."\n";
}

/**
* {@inheritDoc}
*/
Expand Down
31 changes: 30 additions & 1 deletion lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\TextType;
use Doctrine\DBAL\Types\Type;

/**
* Sqlite SchemaManager.
Expand Down Expand Up @@ -118,6 +119,7 @@ public function listTableForeignKeys($table, $database = null)
if ( ! empty($tableForeignKeys)) {
$createSql = $this->_conn->fetchAll("SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = 'table' AND name = '$table'");
$createSql = isset($createSql[0]['sql']) ? $createSql[0]['sql'] : '';

if (preg_match_all('#
(?:CONSTRAINT\s+([^\s]+)\s+)?
(?:FOREIGN\s+KEY[^\)]+\)\s*)?
Expand Down Expand Up @@ -168,6 +170,7 @@ protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
// fetch primary
$stmt = $this->_conn->executeQuery("PRAGMA TABLE_INFO ('$tableName')");
$indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);

usort($indexArray, function($a, $b) {
if ($a['pk'] == $b['pk']) {
return $a['cid'] - $b['cid'];
Expand Down Expand Up @@ -248,7 +251,7 @@ protected function _getPortableTableColumnList($table, $database, $tableColumns)
}
}

// inspect column collation
// inspect column collation and comments
$createSql = $this->_conn->fetchAll("SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = 'table' AND name = '$table'");
$createSql = isset($createSql[0]['sql']) ? $createSql[0]['sql'] : '';

Expand All @@ -258,6 +261,17 @@ protected function _getPortableTableColumnList($table, $database, $tableColumns)
if ($type instanceof StringType || $type instanceof TextType) {
$column->setPlatformOption('collation', $this->parseColumnCollationFromSQL($columnName, $createSql) ?: 'BINARY');
}

$comment = $this->parseColumnCommentFromSQL($columnName, $createSql);
if (false !== $comment) {
$type = $this->extractDoctrineTypeFromComment($comment, null);
if (null !== $type) {
$column->setType(Type::getType($type));
$comment = $this->removeDoctrineTypeFromComment($comment, $type);
}

$column->setComment($comment);
}
}

return $list;
Expand Down Expand Up @@ -436,4 +450,19 @@ private function parseColumnCollationFromSQL($column, $sql)

return false;
}

private function parseColumnCommentFromSQL($column, $sql)
{
if (preg_match(
'{[\s(,](?:'.preg_quote($this->_platform->quoteSingleIdentifier($column)).'|'.preg_quote($column).')
(?:\(.*?\)|[^,(])*?,?((?:\s*--[^\n]*\n?)+)
}isx', $sql, $match
)) {
$comment = preg_replace('{^\s*--}m', '', rtrim($match[1], "\n"));

return '' === $comment ? false : $comment;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ public function testGetColumnComment()
$this->assertEquals('This is a comment', $columns['id']->getComment());

$tableDiff = new \Doctrine\DBAL\Schema\TableDiff('column_comment_test');
$tableDiff->fromTable = $table;
$tableDiff->changedColumns['id'] = new \Doctrine\DBAL\Schema\ColumnDiff(
'id', new \Doctrine\DBAL\Schema\Column(
'id', \Doctrine\DBAL\Types\Type::getType('integer'), array('primary' => true)
Expand Down Expand Up @@ -1043,7 +1044,6 @@ public function testAlterColumnComment($comment1, $expectedComment1, $comment2,
$offlineTable->addColumn('comment2', 'integer', array('comment' => $comment2));
$offlineTable->addColumn('no_comment1', 'integer');
$offlineTable->addColumn('no_comment2', 'integer');

$this->_sm->dropAndCreateTable($offlineTable);

$onlineTable = $this->_sm->listTableDetails("alter_column_comment_test");
Expand Down
24 changes: 20 additions & 4 deletions tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,11 +523,20 @@ protected function getQuotedAlterTableRenameColumnSQL()
return array(
'CREATE TEMPORARY TABLE __temp__mytable AS SELECT unquoted1, unquoted2, unquoted3, "create", "table", "select", "quoted1", "quoted2", "quoted3" FROM mytable',
'DROP TABLE mytable',
'CREATE TABLE mytable (unquoted INTEGER NOT NULL, "where" INTEGER NOT NULL, "foo" INTEGER NOT NULL, reserved_keyword INTEGER NOT NULL, "from" INTEGER NOT NULL, "bar" INTEGER NOT NULL, quoted INTEGER NOT NULL, "and" INTEGER NOT NULL, "baz" INTEGER NOT NULL)',
'CREATE TABLE mytable (unquoted INTEGER NOT NULL --Unquoted 1
, "where" INTEGER NOT NULL --Unquoted 2
, "foo" INTEGER NOT NULL --Unquoted 3
, reserved_keyword INTEGER NOT NULL --Reserved keyword 1
, "from" INTEGER NOT NULL --Reserved keyword 2
, "bar" INTEGER NOT NULL --Reserved keyword 3
, quoted INTEGER NOT NULL --Quoted 1
, "and" INTEGER NOT NULL --Quoted 2
, "baz" INTEGER NOT NULL --Quoted 3
)',
'INSERT INTO mytable (unquoted, "where", "foo", reserved_keyword, "from", "bar", quoted, "and", "baz") SELECT unquoted1, unquoted2, unquoted3, "create", "table", "select", "quoted1", "quoted2", "quoted3" FROM __temp__mytable',
'DROP TABLE __temp__mytable',
);
}
}

/**
* {@inheritdoc}
Expand All @@ -537,7 +546,13 @@ protected function getQuotedAlterTableChangeColumnLengthSQL()
return array(
'CREATE TEMPORARY TABLE __temp__mytable AS SELECT unquoted1, unquoted2, unquoted3, "create", "table", "select" FROM mytable',
'DROP TABLE mytable',
'CREATE TABLE mytable (unquoted1 VARCHAR(255) NOT NULL, unquoted2 VARCHAR(255) NOT NULL, unquoted3 VARCHAR(255) NOT NULL, "create" VARCHAR(255) NOT NULL, "table" VARCHAR(255) NOT NULL, "select" VARCHAR(255) NOT NULL)',
'CREATE TABLE mytable (unquoted1 VARCHAR(255) NOT NULL --Unquoted 1
, unquoted2 VARCHAR(255) NOT NULL --Unquoted 2
, unquoted3 VARCHAR(255) NOT NULL --Unquoted 3
, "create" VARCHAR(255) NOT NULL --Reserved keyword 1
, "table" VARCHAR(255) NOT NULL --Reserved keyword 2
, "select" VARCHAR(255) NOT NULL --Reserved keyword 3
)',
'INSERT INTO mytable (unquoted1, unquoted2, unquoted3, "create", "table", "select") SELECT unquoted1, unquoted2, unquoted3, "create", "table", "select" FROM __temp__mytable',
'DROP TABLE __temp__mytable',
);
Expand Down Expand Up @@ -581,7 +596,8 @@ public function getAlterTableRenameColumnSQL()
return array(
'CREATE TEMPORARY TABLE __temp__foo AS SELECT bar FROM foo',
'DROP TABLE foo',
'CREATE TABLE foo (baz INTEGER DEFAULT 666 NOT NULL)',
'CREATE TABLE foo (baz INTEGER DEFAULT 666 NOT NULL --rename test
)',
'INSERT INTO foo (baz) SELECT bar FROM __temp__foo',
'DROP TABLE __temp__foo',
);
Expand Down