Commit a4dfce29 authored by Martin Hasoň's avatar Martin Hasoň

Added support for inline comments for SQLite

parent 2241ee4b
......@@ -1602,6 +1602,18 @@ abstract class AbstractPlatform
" 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.
*
......@@ -2211,7 +2223,7 @@ abstract class AbstractPlatform
}
if ($this->supportsInlineColumnComments() && isset($field['comment']) && $field['comment'] !== '') {
$columnDef .= " COMMENT " . $this->quoteStringLiteral($field['comment']);
$columnDef .= ' ' . $this->getInlineColumnCommentSQL($field['comment']);
}
return $name . ' ' . $columnDef;
......
......@@ -492,6 +492,14 @@ class SqlitePlatform extends AbstractPlatform
return true;
}
/**
* {@inheritDoc}
*/
public function supportsInlineColumnComments()
{
return true;
}
/**
* {@inheritDoc}
*/
......@@ -567,6 +575,14 @@ class SqlitePlatform extends AbstractPlatform
return '';
}
/**
* {@inheritDoc}
*/
public function getInlineColumnCommentSQL($comment)
{
return '--'.str_replace("\n", "\n--", $comment)."\n";
}
/**
* {@inheritDoc}
*/
......
......@@ -22,6 +22,7 @@ namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\TextType;
use Doctrine\DBAL\Types\Type;
/**
* Sqlite SchemaManager.
......@@ -118,6 +119,7 @@ class SqliteSchemaManager extends AbstractSchemaManager
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*)?
......@@ -168,6 +170,7 @@ class SqliteSchemaManager extends AbstractSchemaManager
// 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'];
......@@ -248,7 +251,7 @@ class SqliteSchemaManager extends AbstractSchemaManager
}
}
// 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'] : '';
......@@ -258,6 +261,17 @@ class SqliteSchemaManager extends AbstractSchemaManager
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;
......@@ -436,4 +450,19 @@ class SqliteSchemaManager extends AbstractSchemaManager
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;
}
}
......@@ -691,6 +691,7 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$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)
......@@ -1043,7 +1044,6 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$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");
......
......@@ -523,11 +523,20 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
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}
......@@ -537,7 +546,13 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
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',
);
......@@ -581,7 +596,8 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
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',
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment