Commit b94e0091 authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-42] Add support for inline and COMMENT ON syntax for MySQL, PostgreSQL and Oracle.

parent 45975b07
...@@ -797,6 +797,7 @@ abstract class AbstractPlatform ...@@ -797,6 +797,7 @@ abstract class AbstractPlatform
$columnData['default'] = $column->getDefault(); $columnData['default'] = $column->getDefault();
$columnData['columnDefinition'] = $column->getColumnDefinition(); $columnData['columnDefinition'] = $column->getColumnDefinition();
$columnData['autoincrement'] = $column->getAutoincrement(); $columnData['autoincrement'] = $column->getAutoincrement();
$columnData['comment'] = $column->getComment();
if(in_array($column->getName(), $options['primary'])) { if(in_array($column->getName(), $options['primary'])) {
$columnData['primary'] = true; $columnData['primary'] = true;
...@@ -812,7 +813,20 @@ abstract class AbstractPlatform ...@@ -812,7 +813,20 @@ abstract class AbstractPlatform
} }
} }
return $this->_getCreateTableSQL($tableName, $columns, $options); $sql = $this->_getCreateTableSQL($tableName, $columns, $options);
if ($this->supportsCommentOnStatement()) {
foreach ($table->getColumns() AS $column) {
if ($column->getComment()) {
$sql[] = $this->getCommentOnColumnSQL($tableName, $column->getName(), $column->getComment());
}
}
}
return $sql;
}
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
return "COMMENT ON " . $tableName . "." . $columnName . " IS '" . $comment . "'";
} }
/** /**
...@@ -1142,6 +1156,10 @@ abstract class AbstractPlatform ...@@ -1142,6 +1156,10 @@ abstract class AbstractPlatform
$columnDef = $typeDecl . $charset . $default . $notnull . $unique . $check . $collation; $columnDef = $typeDecl . $charset . $default . $notnull . $unique . $check . $collation;
} }
if ($this->supportsInlineColumnComments() && isset($field['comment']) && $field['comment']) {
$columnDef .= " COMMENT '" . $field['comment'] . "'";
}
return $name . ' ' . $columnDef; return $name . ' ' . $columnDef;
} }
...@@ -1880,6 +1898,26 @@ abstract class AbstractPlatform ...@@ -1880,6 +1898,26 @@ abstract class AbstractPlatform
return true; return true;
} }
/**
* Does this plaform support to add inline column comments as postfix.
*
* @return bool
*/
public function supportsInlineColumnComments()
{
return false;
}
/**
* Does this platform support the propriortary synatx "COMMENT ON asset"
*
* @return bool
*/
public function supportsCommentOnStatement()
{
return false;
}
public function getIdentityColumnNullInsertSQL() public function getIdentityColumnNullInsertSQL()
{ {
return ""; return "";
......
...@@ -259,6 +259,11 @@ class MySqlPlatform extends AbstractPlatform ...@@ -259,6 +259,11 @@ class MySqlPlatform extends AbstractPlatform
return true; return true;
} }
public function supportsInlineColumnComments()
{
return true;
}
public function getShowDatabasesSQL() public function getShowDatabasesSQL()
{ {
return 'SHOW DATABASES'; return 'SHOW DATABASES';
......
...@@ -499,10 +499,14 @@ LEFT JOIN all_cons_columns r_cols ...@@ -499,10 +499,14 @@ LEFT JOIN all_cons_columns r_cols
public function getAlterTableSQL(TableDiff $diff) public function getAlterTableSQL(TableDiff $diff)
{ {
$sql = array(); $sql = array();
$commentsSQL = array();
$fields = array(); $fields = array();
foreach ($diff->addedColumns AS $column) { foreach ($diff->addedColumns AS $column) {
$fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
if ($column->getComment()) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $column->getComment());
}
} }
if (count($fields)) { if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' ADD (' . implode(', ', $fields) . ')'; $sql[] = 'ALTER TABLE ' . $diff->name . ' ADD (' . implode(', ', $fields) . ')';
...@@ -512,6 +516,9 @@ LEFT JOIN all_cons_columns r_cols ...@@ -512,6 +516,9 @@ LEFT JOIN all_cons_columns r_cols
foreach ($diff->changedColumns AS $columnDiff) { foreach ($diff->changedColumns AS $columnDiff) {
$column = $columnDiff->column; $column = $columnDiff->column;
$fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray()); $fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray());
if ($columnDiff->hasChanged('comment') && $column->getComment()) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $column->getComment());
}
} }
if (count($fields)) { if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' MODIFY (' . implode(', ', $fields) . ')'; $sql[] = 'ALTER TABLE ' . $diff->name . ' MODIFY (' . implode(', ', $fields) . ')';
...@@ -533,9 +540,7 @@ LEFT JOIN all_cons_columns r_cols ...@@ -533,9 +540,7 @@ LEFT JOIN all_cons_columns r_cols
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName; $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
} }
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff)); return array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
return $sql;
} }
/** /**
...@@ -548,6 +553,11 @@ LEFT JOIN all_cons_columns r_cols ...@@ -548,6 +553,11 @@ LEFT JOIN all_cons_columns r_cols
return true; return true;
} }
public function supportsCommentOnStatement()
{
return true;
}
/** /**
* Get the platform name for this instance * Get the platform name for this instance
* *
......
...@@ -135,6 +135,11 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -135,6 +135,11 @@ class PostgreSqlPlatform extends AbstractPlatform
{ {
return true; return true;
} }
public function supportsCommentOnStatement()
{
return true;
}
/** /**
* Whether the platform prefers sequences for ID generation. * Whether the platform prefers sequences for ID generation.
...@@ -385,6 +390,9 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -385,6 +390,9 @@ class PostgreSqlPlatform extends AbstractPlatform
$sql[] = "ALTER TABLE " . $diff->name . " " . $query; $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
} }
} }
if ($columnDiff->hasChanged('comment')) {
$sql[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $column->getComment());
}
} }
foreach ($diff->renamedColumns as $oldColumnName => $column) { foreach ($diff->renamedColumns as $oldColumnName => $column) {
......
...@@ -357,6 +357,7 @@ class Column extends AbstractAsset ...@@ -357,6 +357,7 @@ class Column extends AbstractAsset
'unsigned' => $this->_unsigned, 'unsigned' => $this->_unsigned,
'autoincrement' => $this->_autoincrement, 'autoincrement' => $this->_autoincrement,
'columnDefinition' => $this->_columnDefinition, 'columnDefinition' => $this->_columnDefinition,
'comment' => $this->_comment,
), $this->_platformOptions); ), $this->_platformOptions);
} }
} }
\ No newline at end of file
...@@ -165,4 +165,43 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -165,4 +165,43 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED'); $field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED');
$this->assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSQL('foo', $field)); $this->assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSQL('foo', $field));
} }
/**
* @group DBAL-42
*/
public function testCreateTableColumnComments()
{
$table = new \Doctrine\DBAL\Schema\Table('test');
$table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
$table->setPrimaryKey(array('id'));
$this->assertEquals($this->getCreateTableColumnCommentsSQL(), $this->_platform->getCreateTableSQL($table));
}
/**
* @group DBAL-42
*/
public function testAlterTableColumnComments()
{
$tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
$tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('comment' => 'A comment'));
$tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
'bar', new \Doctrine\DBAL\Schema\Column(
'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('comment' => 'B comment')
),
array('comment')
);
$this->assertEquals($this->getAlterTableColumnCommentsSQL(), $this->_platform->getAlterTableSQL($tableDiff));
}
public function getCreateTableColumnCommentsSQL()
{
$this->markTestSkipped('Platform does not support Column comments.');
}
public function getAlterTableColumnCommentsSQL()
{
$this->markTestSkipped('Platform does not support Column comments.');
}
} }
...@@ -171,4 +171,5 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase ...@@ -171,4 +171,5 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase
$this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username DESC', $sql); $this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username DESC', $sql);
} }
} }
\ No newline at end of file
...@@ -166,4 +166,14 @@ class MySqlPlatformTest extends AbstractPlatformTestCase ...@@ -166,4 +166,14 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
$this->assertEquals("TIMESTAMP", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => true))); $this->assertEquals("TIMESTAMP", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => true)));
$this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array())); $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array()));
} }
public function getCreateTableColumnCommentsSQL()
{
return array("CREATE TABLE test (id INT NOT NULL COMMENT 'This is a comment', PRIMARY KEY(id)) ENGINE = InnoDB");
}
public function getAlterTableColumnCommentsSQL()
{
return array("ALTER TABLE mytable ADD quota INT NOT NULL COMMENT 'A comment', CHANGE bar baz VARCHAR(255) NOT NULL COMMENT 'B comment'");
}
} }
\ No newline at end of file
...@@ -186,4 +186,21 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -186,4 +186,21 @@ class OraclePlatformTest extends AbstractPlatformTestCase
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10); $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10);
$this->assertEquals('SELECT a.* FROM (SELECT * FROM user ORDER BY username DESC) a WHERE ROWNUM <= 10', $sql); $this->assertEquals('SELECT a.* FROM (SELECT * FROM user ORDER BY username DESC) a WHERE ROWNUM <= 10', $sql);
} }
public function getCreateTableColumnCommentsSQL()
{
return array(
"CREATE TABLE test (id NUMBER(10) NOT NULL, PRIMARY KEY(id))",
"COMMENT ON test.id IS 'This is a comment'",
);
}
public function getAlterTableColumnCommentsSQL()
{
return array(
"ALTER TABLE mytable ADD (quota NUMBER(10) NOT NULL)",
"ALTER TABLE mytable MODIFY (baz VARCHAR2(255) NOT NULL)",
"COMMENT ON mytable.baz IS 'B comment'",
);
}
} }
\ No newline at end of file
...@@ -199,4 +199,20 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase ...@@ -199,4 +199,20 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10); $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
$this->assertEquals('SELECT * FROM user LIMIT 10', $sql); $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
} }
public function getCreateTableColumnCommentsSQL()
{
return array(
"CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))",
"COMMENT ON test.id IS 'This is a comment'",
);
}
public function getAlterTableColumnCommentsSQL()
{
return array(
"ALTER TABLE mytable ADD quota INT NOT NULL",
"COMMENT ON mytable.baz IS 'B comment'",
);
}
} }
\ No newline at end of file
...@@ -46,6 +46,7 @@ class ColumnTest extends \PHPUnit_Framework_TestCase ...@@ -46,6 +46,7 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
'unsigned' => true, 'unsigned' => true,
'autoincrement' => false, 'autoincrement' => false,
'columnDefinition' => null, 'columnDefinition' => null,
'comment' => null,
'foo' => 'bar', 'foo' => 'bar',
); );
...@@ -93,10 +94,14 @@ class ColumnTest extends \PHPUnit_Framework_TestCase ...@@ -93,10 +94,14 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
*/ */
public function testColumnComment() public function testColumnComment()
{ {
$column = new Column("`bar`", Type::getType('string')); $column = new Column("bar", Type::getType('string'));
$this->assertNull($column->getComment()); $this->assertNull($column->getComment());
$column->setComment("foo"); $column->setComment("foo");
$this->assertEquals("foo", $column->getComment()); $this->assertEquals("foo", $column->getComment());
$columnArray = $column->toArray();
$this->assertArrayHasKey('comment', $columnArray);
$this->assertEquals('foo', $columnArray['comment']);
} }
} }
\ No newline at end of file
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