Commit f6639e15 authored by Jérôme Brissonnet's avatar Jérôme Brissonnet Committed by Steve Müller

#657 - Fixed single quote in comments

parent 3eebba76
......@@ -1589,7 +1589,9 @@ abstract class AbstractPlatform
*/
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
return "COMMENT ON COLUMN " . $tableName . "." . $columnName . " IS '" . $comment . "'";
$comment = $this->quoteStringLiteral($comment);
return "COMMENT ON COLUMN " . $tableName . "." . $columnName . " IS " . $comment;
}
/**
......@@ -2201,7 +2203,7 @@ abstract class AbstractPlatform
}
if ($this->supportsInlineColumnComments() && isset($field['comment']) && $field['comment']) {
$columnDef .= " COMMENT '" . $field['comment'] . "'";
$columnDef .= " COMMENT " . $this->quoteStringLiteral($field['comment']);
}
return $name . ' ' . $columnDef;
......@@ -3484,4 +3486,28 @@ abstract class AbstractPlatform
{
throw DBALException::notSupported(__METHOD__);
}
/**
* Quotes a literal string
*
* @param string $str The literal string to be quoted.
*
* @return string The quoted literal string.
*/
public function quoteStringLiteral($str)
{
$c = $this->getStringLiteralQuoteCharacter();
return $c . str_replace($c, $c . $c, $str) . $c;
}
/**
* Gets the character used for string literal quoting.
*
* @return string
*/
public function getStringLiteralQuoteCharacter()
{
return "'";
}
}
......@@ -275,7 +275,7 @@ class DrizzlePlatform extends AbstractPlatform
if (isset($options['comment'])) {
$comment = trim($options['comment'], " '");
$tableOptions[] = sprintf("COMMENT = '%s' ", str_replace("'", "''", $comment));
$tableOptions[] = sprintf("COMMENT = %s ", $this->quoteStringLiteral($comment));
}
// Row format
......
......@@ -494,7 +494,7 @@ class MySqlPlatform extends AbstractPlatform
if (isset($options['comment'])) {
$comment = trim($options['comment'], " '");
$tableOptions[] = sprintf("COMMENT = '%s' ", str_replace("'", "''", $comment));
$tableOptions[] = sprintf("COMMENT = %s ", $this->quoteStringLiteral($comment));
}
// Row format
......
......@@ -615,7 +615,7 @@ class PostgreSqlPlatform extends AbstractPlatform
*/
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
$comment = $comment === null ? 'NULL' : "'$comment'";
$comment = $comment === null ? 'NULL' : $this->quoteStringLiteral($comment);
return "COMMENT ON COLUMN $tableName.$columnName IS $comment";
}
......
......@@ -357,7 +357,7 @@ class SQLAnywherePlatform extends AbstractPlatform
*/
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
$comment = $comment === null ? 'NULL' : "'$comment'";
$comment = $comment === null ? 'NULL' : $this->quoteStringLiteral($comment);
return "COMMENT ON COLUMN $tableName.$columnName IS $comment";
}
......
......@@ -869,4 +869,22 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->_sm->listTableForeignKeys($defaultSchemaName . '.' . $primaryTableName)
);
}
public function testCommentStringsAreQuoted()
{
if ( ! $this->_conn->getDatabasePlatform()->supportsInlineColumnComments() &&
! $this->_conn->getDatabasePlatform()->supportsCommentOnStatement() &&
$this->_conn->getDatabasePlatform()->getName() != 'mssql') {
$this->markTestSkipped('Database does not support column comments.');
}
$table = new Table('my_table');
$table->addColumn('id', 'integer', array('comment' => "It's a comment with a quote"));
$table->setPrimaryKey(array('id'));
$this->_sm->createTable($table);
$columns = $this->_sm->listTableColumns("my_table");
$this->assertEquals("It's a comment with a quote", $columns['id']->getComment());
}
}
......@@ -860,4 +860,25 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
'CREATE INDEX "bar" ON "schema"."table" (id)',
);
}
public function testGetCommentOnColumnSQL()
{
$this->assertEquals(
"COMMENT ON COLUMN mytable.id IS 'This is a comment'",
$this->_platform->getCommentOnColumnSQL('mytable', 'id', 'This is a comment')
);
$this->assertEquals(
"COMMENT ON COLUMN mytable.id IS 'It''s a quote !'",
$this->_platform->getCommentOnColumnSQL('mytable', 'id', "It's a quote !")
);
}
public function testQuoteStringLiteral()
{
$c = $this->_platform->getStringLiteralQuoteCharacter();
$this->assertEquals($c . 'No quote' . $c, $this->_platform->quoteStringLiteral('No quote'));
$this->assertEquals($c . 'It' . $c . $c . 's a quote' . $c, $this->_platform->quoteStringLiteral('It' . $c . 's a quote'));
$this->assertEquals($c . $c . $c . $c, $this->_platform->quoteStringLiteral($c));
}
}
......@@ -656,4 +656,12 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
'ALTER INDEX "schema"."foo" RENAME TO "bar"',
);
}
public function testGetNullCommentOnColumnSQL()
{
$this->assertEquals(
"COMMENT ON COLUMN mytable.id IS NULL",
$this->_platform->getCommentOnColumnSQL('mytable', 'id', null)
);
}
}
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