Commit acdd2145 authored by Steve Müller's avatar Steve Müller

quote old column name in rename column SQL

parent 171a8762
......@@ -19,6 +19,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\TableDiff;
......@@ -511,7 +512,10 @@ class DB2Platform extends AbstractPlatform
continue;
}
$queryParts[] = 'RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
$oldColumnName = new Identifier($oldColumnName);
$queryParts[] = 'RENAME COLUMN ' . $oldColumnName->getQuotedName($this) .
' TO ' . $column->getQuotedName($this);
}
$tableSql = array();
......
......@@ -19,6 +19,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
......@@ -573,9 +574,11 @@ class DrizzlePlatform extends AbstractPlatform
continue;
}
$oldColumnName = new Identifier($oldColumnName);
$columnArray = $column->toArray();
$columnArray['comment'] = $this->getColumnComment($column);
$queryParts[] = 'CHANGE ' . $oldColumnName . ' '
$queryParts[] = 'CHANGE ' . $oldColumnName->getQuotedName($this) . ' '
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
}
......
......@@ -19,6 +19,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
......@@ -612,9 +613,10 @@ class MySqlPlatform extends AbstractPlatform
continue;
}
$oldColumnName = new Identifier($oldColumnName);
$columnArray = $column->toArray();
$columnArray['comment'] = $this->getColumnComment($column);
$queryParts[] = 'CHANGE ' . $oldColumnName . ' '
$queryParts[] = 'CHANGE ' . $oldColumnName->getQuotedName($this) . ' '
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
}
......
......@@ -20,6 +20,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
......@@ -710,7 +711,10 @@ LEFT JOIN user_cons_columns r_cols
continue;
}
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getQuotedName($this);
$oldColumnName = new Identifier($oldColumnName);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) .
' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) .' TO ' . $column->getQuotedName($this);
}
$fields = array();
......
......@@ -533,7 +533,10 @@ class PostgreSqlPlatform extends AbstractPlatform
continue;
}
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
$oldColumnName = new Identifier($oldColumnName);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) .
' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this);
}
$tableSql = array();
......
......@@ -251,7 +251,9 @@ class SQLAnywherePlatform extends AbstractPlatform
*/
protected function getAlterTableRenameColumnClause($oldColumnName, Column $column)
{
return 'RENAME ' . $oldColumnName .' TO ' . $column->getQuotedName($this);
$oldColumnName = new Identifier($oldColumnName);
return 'RENAME ' . $oldColumnName->getQuotedName($this) .' TO ' . $column->getQuotedName($this);
}
/**
......
......@@ -22,6 +22,7 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
......@@ -538,7 +539,10 @@ class SQLServerPlatform extends AbstractPlatform
continue;
}
$sql[] = "sp_RENAME '". $diff->name. ".". $oldColumnName . "', '".$column->getQuotedName($this)."', 'COLUMN'";
$oldColumnName = new Identifier($oldColumnName);
$sql[] = "sp_RENAME '" . $diff->name . "." . $oldColumnName->getQuotedName($this) .
"', '" . $column->getQuotedName($this) . "', 'COLUMN'";
// Recreate default constraint with new column name if necessary (for future reference).
if ($column->getDefault() !== null) {
......
......@@ -537,4 +537,23 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
$this->assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)));
}
/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableRenameColumnSQL()
{
return array(
"ALTER TABLE mytable " .
"CHANGE unquoted1 unquoted INT NOT NULL COMMENT 'Unquoted 1', " .
"CHANGE unquoted2 `where` INT NOT NULL COMMENT 'Unquoted 2', " .
"CHANGE unquoted3 `foo` INT NOT NULL COMMENT 'Unquoted 3', " .
"CHANGE `create` reserved_keyword INT NOT NULL COMMENT 'Reserved keyword 1', " .
"CHANGE `table` `from` INT NOT NULL COMMENT 'Reserved keyword 2', " .
"CHANGE `select` `bar` INT NOT NULL COMMENT 'Reserved keyword 3', " .
"CHANGE quoted1 quoted INT NOT NULL COMMENT 'Quoted 1', " .
"CHANGE quoted2 `and` INT NOT NULL COMMENT 'Quoted 2', " .
"CHANGE quoted3 `baz` INT NOT NULL COMMENT 'Quoted 3'"
);
}
}
......@@ -5,6 +5,7 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
......@@ -665,4 +666,54 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
'CREATE INDEX "bar" ON "table" (id)',
);
}
/**
* @group DBAL-835
*/
public function testQuotesAlterTableRenameColumn()
{
$fromTable = new Table('mytable');
$fromTable->addColumn('unquoted1', 'integer', array('comment' => 'Unquoted 1'));
$fromTable->addColumn('unquoted2', 'integer', array('comment' => 'Unquoted 2'));
$fromTable->addColumn('unquoted3', 'integer', array('comment' => 'Unquoted 3'));
$fromTable->addColumn('create', 'integer', array('comment' => 'Reserved keyword 1'));
$fromTable->addColumn('table', 'integer', array('comment' => 'Reserved keyword 2'));
$fromTable->addColumn('select', 'integer', array('comment' => 'Reserved keyword 3'));
$fromTable->addColumn('`quoted1`', 'integer', array('comment' => 'Quoted 1'));
$fromTable->addColumn('`quoted2`', 'integer', array('comment' => 'Quoted 2'));
$fromTable->addColumn('`quoted3`', 'integer', array('comment' => 'Quoted 3'));
$toTable = new Table('mytable');
$toTable->addColumn('unquoted', 'integer', array('comment' => 'Unquoted 1')); // unquoted -> unquoted
$toTable->addColumn('where', 'integer', array('comment' => 'Unquoted 2')); // unquoted -> reserved keyword
$toTable->addColumn('`foo`', 'integer', array('comment' => 'Unquoted 3')); // unquoted -> quoted
$toTable->addColumn('reserved_keyword', 'integer', array('comment' => 'Reserved keyword 1')); // reserved keyword -> unquoted
$toTable->addColumn('from', 'integer', array('comment' => 'Reserved keyword 2')); // reserved keyword -> reserved keyword
$toTable->addColumn('`bar`', 'integer', array('comment' => 'Reserved keyword 3')); // reserved keyword -> quoted
$toTable->addColumn('quoted', 'integer', array('comment' => 'Quoted 1')); // quoted -> unquoted
$toTable->addColumn('and', 'integer', array('comment' => 'Quoted 2')); // quoted -> reserved keyword
$toTable->addColumn('`baz`', 'integer', array('comment' => 'Quoted 3')); // quoted -> quoted
$comparator = new Comparator();
$this->assertEquals(
$this->getQuotedAlterTableRenameColumnSQL(),
$this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
);
}
/**
* Returns SQL statements for {@link testQuotesAlterTableRenameColumn}.
*
* @return array
*
* @group DBAL-835
*/
abstract protected function getQuotedAlterTableRenameColumnSQL();
}
......@@ -520,4 +520,22 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
'ALTER INDEX "foo" RENAME TO "bar"',
);
}
/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableRenameColumnSQL()
{
return array(
'ALTER TABLE mytable RENAME COLUMN unquoted1 TO unquoted',
'ALTER TABLE mytable RENAME COLUMN unquoted2 TO "where"',
'ALTER TABLE mytable RENAME COLUMN unquoted3 TO "foo"',
'ALTER TABLE mytable RENAME COLUMN "create" TO reserved_keyword',
'ALTER TABLE mytable RENAME COLUMN "table" TO "from"',
'ALTER TABLE mytable RENAME COLUMN "select" TO "bar"',
'ALTER TABLE mytable RENAME COLUMN quoted1 TO quoted',
'ALTER TABLE mytable RENAME COLUMN quoted2 TO "and"',
'ALTER TABLE mytable RENAME COLUMN quoted3 TO "baz"',
);
}
}
......@@ -190,7 +190,7 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user ORDER BY username ASC', 10);
$this->assertEquals('SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY username ASC) AS doctrine_rownum FROM user) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 1 AND 10', $sql);
}
public function testModifyLimitQueryWithLowercaseOrderBy()
{
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user order by username', 10);
......@@ -863,4 +863,22 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
)
);
}
/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableRenameColumnSQL()
{
return array(
"sp_RENAME 'mytable.unquoted1', 'unquoted', 'COLUMN'",
"sp_RENAME 'mytable.unquoted2', '[where]', 'COLUMN'",
"sp_RENAME 'mytable.unquoted3', '[foo]', 'COLUMN'",
"sp_RENAME 'mytable.[create]', 'reserved_keyword', 'COLUMN'",
"sp_RENAME 'mytable.[table]', '[from]', 'COLUMN'",
"sp_RENAME 'mytable.[select]', '[bar]', 'COLUMN'",
"sp_RENAME 'mytable.quoted1', 'quoted', 'COLUMN'",
"sp_RENAME 'mytable.quoted2', '[and]', 'COLUMN'",
"sp_RENAME 'mytable.quoted3', '[baz]', 'COLUMN'",
);
}
}
......@@ -405,4 +405,23 @@ class DB2PlatformTest extends AbstractPlatformTestCase
'RENAME INDEX "foo" TO "bar"',
);
}
/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableRenameColumnSQL()
{
return array(
'ALTER TABLE mytable ' .
'RENAME COLUMN unquoted1 TO unquoted ' .
'RENAME COLUMN unquoted2 TO "where" ' .
'RENAME COLUMN unquoted3 TO "foo" ' .
'RENAME COLUMN "create" TO reserved_keyword ' .
'RENAME COLUMN "table" TO "from" ' .
'RENAME COLUMN "select" TO "bar" ' .
'RENAME COLUMN quoted1 TO quoted ' .
'RENAME COLUMN quoted2 TO "and" ' .
'RENAME COLUMN quoted3 TO "baz"'
);
}
}
......@@ -437,4 +437,22 @@ class OraclePlatformTest extends AbstractPlatformTestCase
'ALTER INDEX "foo" RENAME TO "bar"',
);
}
/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableRenameColumnSQL()
{
return array(
'ALTER TABLE mytable RENAME COLUMN unquoted1 TO unquoted',
'ALTER TABLE mytable RENAME COLUMN unquoted2 TO "where"',
'ALTER TABLE mytable RENAME COLUMN unquoted3 TO "foo"',
'ALTER TABLE mytable RENAME COLUMN "create" TO reserved_keyword',
'ALTER TABLE mytable RENAME COLUMN "table" TO "from"',
'ALTER TABLE mytable RENAME COLUMN "select" TO "bar"',
'ALTER TABLE mytable RENAME COLUMN quoted1 TO quoted',
'ALTER TABLE mytable RENAME COLUMN quoted2 TO "and"',
'ALTER TABLE mytable RENAME COLUMN quoted3 TO "baz"',
);
}
}
......@@ -809,4 +809,22 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
'ALTER INDEX "foo" ON "table" RENAME TO "bar"',
);
}
/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableRenameColumnSQL()
{
return array(
'ALTER TABLE mytable RENAME unquoted1 TO unquoted',
'ALTER TABLE mytable RENAME unquoted2 TO "where"',
'ALTER TABLE mytable RENAME unquoted3 TO "foo"',
'ALTER TABLE mytable RENAME "create" TO reserved_keyword',
'ALTER TABLE mytable RENAME "table" TO "from"',
'ALTER TABLE mytable RENAME "select" TO "bar"',
'ALTER TABLE mytable RENAME quoted1 TO quoted',
'ALTER TABLE mytable RENAME quoted2 TO "and"',
'ALTER TABLE mytable RENAME quoted3 TO "baz"',
);
}
}
......@@ -482,4 +482,18 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'CREATE INDEX "bar" ON table (id)',
);
}
/**
* {@inheritdoc}
*/
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)',
'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',
);
}
}
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