Commit 4b8b6434 authored by Steve Müller's avatar Steve Müller

fix generating COMMENT ON COLUMN statements for quoted identifiers

parent fbd49846
......@@ -1594,9 +1594,12 @@ abstract class AbstractPlatform
*/
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
$tableName = new Identifier($tableName);
$columnName = new Identifier($columnName);
$comment = $this->quoteStringLiteral($comment);
return "COMMENT ON COLUMN " . $tableName . "." . $columnName . " IS " . $comment;
return "COMMENT ON COLUMN " . $tableName->getQuotedName($this) . "." . $columnName->getQuotedName($this) .
" IS " . $comment;
}
/**
......
......@@ -697,7 +697,11 @@ LEFT JOIN user_cons_columns r_cols
$fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
if ($comment = $this->getColumnComment($column)) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->getName($this)->getQuotedName($this),
$column->getQuotedName($this),
$comment
);
}
}
......@@ -741,8 +745,8 @@ LEFT JOIN user_cons_columns r_cols
if ($columnHasChangedComment) {
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->name,
$column->getName(),
$diff->getName($this)->getQuotedName($this),
$column->getQuotedName($this),
$this->getColumnComment($column)
);
}
......
......@@ -458,7 +458,11 @@ class PostgreSqlPlatform extends AbstractPlatform
$comment = $this->getColumnComment($column);
if (null !== $comment && '' !== $comment) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->getName($this)->getQuotedName($this),
$column->getQuotedName($this),
$comment
);
}
}
......@@ -523,8 +527,8 @@ class PostgreSqlPlatform extends AbstractPlatform
if ($columnDiff->hasChanged('comment')) {
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->name,
$column->getName(),
$diff->getName($this)->getQuotedName($this),
$column->getQuotedName($this),
$this->getColumnComment($column)
);
}
......@@ -624,9 +628,12 @@ class PostgreSqlPlatform extends AbstractPlatform
*/
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
$tableName = new Identifier($tableName);
$columnName = new Identifier($columnName);
$comment = $comment === null ? 'NULL' : $this->quoteStringLiteral($comment);
return "COMMENT ON COLUMN $tableName.$columnName IS $comment";
return "COMMENT ON COLUMN " . $tableName->getQuotedName($this) . "." . $columnName->getQuotedName($this) .
" IS $comment";
}
/**
......
......@@ -144,7 +144,11 @@ class SQLAnywherePlatform extends AbstractPlatform
$comment = $this->getColumnComment($column);
if (null !== $comment && '' !== $comment) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getQuotedName($this), $comment);
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->getName($this)->getQuotedName($this),
$column->getQuotedName($this),
$comment
);
}
}
......@@ -173,7 +177,7 @@ class SQLAnywherePlatform extends AbstractPlatform
$column = $columnDiff->column;
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->name,
$diff->getName($this)->getQuotedName($this),
$column->getQuotedName($this),
$this->getColumnComment($column)
);
......@@ -363,9 +367,12 @@ class SQLAnywherePlatform extends AbstractPlatform
*/
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
$tableName = new Identifier($tableName);
$columnName = new Identifier($columnName);
$comment = $comment === null ? 'NULL' : $this->quoteStringLiteral($comment);
return "COMMENT ON COLUMN $tableName.$columnName IS $comment";
return "COMMENT ON COLUMN " . $tableName->getQuotedName($this) . '.' . $columnName->getQuotedName($this) .
" IS $comment";
}
/**
......
......@@ -629,4 +629,16 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
'ALTER TABLE `table` ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
/**
* {@inheritdoc}
*/
protected function getCommentOnColumnSQL()
{
return array(
"COMMENT ON COLUMN foo.bar IS 'comment'",
"COMMENT ON COLUMN `Foo`.`BAR` IS 'comment'",
"COMMENT ON COLUMN `select`.`from` IS 'comment'",
);
}
}
......@@ -916,6 +916,28 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
);
}
/**
* @return array
*
* @see testGetCommentOnColumnSQL
*/
abstract protected function getCommentOnColumnSQL();
/**
* @group DBAL-1004
*/
public function testGetCommentOnColumnSQL()
{
$this->assertSame(
$this->getCommentOnColumnSQL(),
array(
$this->_platform->getCommentOnColumnSQL('foo', 'bar', 'comment'), // regular identifiers
$this->_platform->getCommentOnColumnSQL('`Foo`', '`BAR`', 'comment'), // explicitly quoted identifiers
$this->_platform->getCommentOnColumnSQL('select', 'from', 'comment'), // reserved keyword identifiers
)
);
}
protected function getQuotedStringLiteralWithoutQuoteCharacter()
{
return "'No quote'";
......
......@@ -2,9 +2,11 @@
namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCase
{
......@@ -702,4 +704,37 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
'INITIALLY IMMEDIATE',
);
}
/**
* {@inheritdoc}
*/
protected function getCommentOnColumnSQL()
{
return array(
'COMMENT ON COLUMN foo.bar IS \'comment\'',
'COMMENT ON COLUMN "Foo"."BAR" IS \'comment\'',
'COMMENT ON COLUMN "select"."from" IS \'comment\'',
);
}
/**
* @group DBAL-1004
*/
public function testAltersTableColumnCommentWithExplicitlyQuotedIdentifiers()
{
$table1 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'))));
$table2 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'), array('comment' => 'baz'))));
$comparator = new Comparator();
$tableDiff = $comparator->diffTable($table1, $table2);
$this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
$this->assertSame(
array(
'COMMENT ON COLUMN "foo"."bar" IS \'baz\'',
),
$this->_platform->getAlterTableSQL($tableDiff)
);
}
}
......@@ -1159,4 +1159,16 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
'ALTER TABLE [table] ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
/**
* {@inheritdoc}
*/
protected function getCommentOnColumnSQL()
{
return array(
"COMMENT ON COLUMN foo.bar IS 'comment'",
"COMMENT ON COLUMN [Foo].[BAR] IS 'comment'",
"COMMENT ON COLUMN [select].[from] IS 'comment'",
);
}
}
......@@ -498,4 +498,16 @@ class DB2PlatformTest extends AbstractPlatformTestCase
'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
/**
* {@inheritdoc}
*/
protected function getCommentOnColumnSQL()
{
return array(
'COMMENT ON COLUMN foo.bar IS \'comment\'',
'COMMENT ON COLUMN "Foo"."BAR" IS \'comment\'',
'COMMENT ON COLUMN "select"."from" IS \'comment\'',
);
}
}
......@@ -3,8 +3,10 @@
namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
require_once __DIR__ . '/../../TestInit.php';
......@@ -567,4 +569,37 @@ class OraclePlatformTest extends AbstractPlatformTestCase
'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
/**
* {@inheritdoc}
*/
protected function getCommentOnColumnSQL()
{
return array(
'COMMENT ON COLUMN foo.bar IS \'comment\'',
'COMMENT ON COLUMN "Foo"."BAR" IS \'comment\'',
'COMMENT ON COLUMN "select"."from" IS \'comment\'',
);
}
/**
* @group DBAL-1004
*/
public function testAltersTableColumnCommentWithExplicitlyQuotedIdentifiers()
{
$table1 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'))));
$table2 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'), array('comment' => 'baz'))));
$comparator = new Comparator();
$tableDiff = $comparator->diffTable($table1, $table2);
$this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
$this->assertSame(
array(
'COMMENT ON COLUMN "foo"."bar" IS \'baz\'',
),
$this->_platform->getAlterTableSQL($tableDiff)
);
}
}
......@@ -8,6 +8,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
......@@ -889,4 +890,37 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
/**
* {@inheritdoc}
*/
protected function getCommentOnColumnSQL()
{
return array(
'COMMENT ON COLUMN foo.bar IS \'comment\'',
'COMMENT ON COLUMN "Foo"."BAR" IS \'comment\'',
'COMMENT ON COLUMN "select"."from" IS \'comment\'',
);
}
/**
* @group DBAL-1004
*/
public function testAltersTableColumnCommentWithExplicitlyQuotedIdentifiers()
{
$table1 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'))));
$table2 = new Table('"foo"', array(new Column('"bar"', Type::getType('integer'), array('comment' => 'baz'))));
$comparator = new Comparator();
$tableDiff = $comparator->diffTable($table1, $table2);
$this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
$this->assertSame(
array(
'COMMENT ON COLUMN "foo"."bar" IS \'baz\'',
),
$this->_platform->getAlterTableSQL($tableDiff)
);
}
}
......@@ -596,4 +596,16 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'CREATE INDEX IDX_8C736521FDC58D6C ON "table" (fk2)',
);
}
/**
* {@inheritdoc}
*/
protected function getCommentOnColumnSQL()
{
return array(
'COMMENT ON COLUMN foo.bar IS \'comment\'',
'COMMENT ON COLUMN "Foo"."BAR" IS \'comment\'',
'COMMENT ON COLUMN "select"."from" IS \'comment\'',
);
}
}
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