Skip to content

Commit

Permalink
Merge pull request #627 from PVince81/pgsql-altertable-keyword
Browse files Browse the repository at this point in the history
Fix escaping of column name for specific alter table case
  • Loading branch information
deeky666 committed Jul 22, 2014
2 parents 0b51b81 + b4f2b77 commit a43e8da
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public function getAlterTableSQL(TableDiff $diff)
}

if ($columnDiff->hasChanged('length')) {
$query = 'ALTER ' . $column->getName() . ' TYPE ' . $column->getType()->getSqlDeclaration($column->toArray(), $this);
$query = 'ALTER ' . $oldColumnName . ' TYPE ' . $column->getType()->getSqlDeclaration($column->toArray(), $this);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,20 @@ protected function getQuotedAlterTableRenameColumnSQL()
"CHANGE quoted3 `baz` INT NOT NULL COMMENT 'Quoted 3'"
);
}

/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableChangeColumnLengthSQL()
{
return array(
"ALTER TABLE mytable " .
"CHANGE unquoted1 unquoted1 VARCHAR(255) NOT NULL COMMENT 'Unquoted 1', " .
"CHANGE unquoted2 unquoted2 VARCHAR(255) NOT NULL COMMENT 'Unquoted 2', " .
"CHANGE unquoted3 unquoted3 VARCHAR(255) NOT NULL COMMENT 'Unquoted 3', " .
"CHANGE `create` `create` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 1', " .
"CHANGE `table` `table` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 2', " .
"CHANGE `select` `select` VARCHAR(255) NOT NULL COMMENT 'Reserved keyword 3'"
);
}
}
42 changes: 42 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,48 @@ public function testQuotesAlterTableRenameColumn()
*/
abstract protected function getQuotedAlterTableRenameColumnSQL();

/**
* @group DBAL-835
*/
public function testQuotesAlterTableChangeColumnLength()
{
$fromTable = new Table('mytable');

$fromTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 10));
$fromTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 10));
$fromTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 10));

$fromTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 10));
$fromTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 10));
$fromTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 10));

$toTable = new Table('mytable');

$toTable->addColumn('unquoted1', 'string', array('comment' => 'Unquoted 1', 'length' => 255));
$toTable->addColumn('unquoted2', 'string', array('comment' => 'Unquoted 2', 'length' => 255));
$toTable->addColumn('unquoted3', 'string', array('comment' => 'Unquoted 3', 'length' => 255));

$toTable->addColumn('create', 'string', array('comment' => 'Reserved keyword 1', 'length' => 255));
$toTable->addColumn('table', 'string', array('comment' => 'Reserved keyword 2', 'length' => 255));
$toTable->addColumn('select', 'string', array('comment' => 'Reserved keyword 3', 'length' => 255));

$comparator = new Comparator();

$this->assertEquals(
$this->getQuotedAlterTableChangeColumnLengthSQL(),
$this->_platform->getAlterTableSQL($comparator->diffTable($fromTable, $toTable))
);
}

/**
* Returns SQL statements for {@link testQuotesAlterTableChangeColumnLength}.
*
* @return array
*
* @group DBAL-835
*/
abstract protected function getQuotedAlterTableChangeColumnLengthSQL();

/**
* @group DBAL-807
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,21 @@ protected function getQuotedAlterTableRenameColumnSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableChangeColumnLengthSQL()
{
return array(
'ALTER TABLE mytable ALTER unquoted1 TYPE VARCHAR(255)',
'ALTER TABLE mytable ALTER unquoted2 TYPE VARCHAR(255)',
'ALTER TABLE mytable ALTER unquoted3 TYPE VARCHAR(255)',
'ALTER TABLE mytable ALTER "create" TYPE VARCHAR(255)',
'ALTER TABLE mytable ALTER "table" TYPE VARCHAR(255)',
'ALTER TABLE mytable ALTER "select" TYPE VARCHAR(255)',
);
}

/**
* @group DBAL-807
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,14 @@ protected function getQuotedAlterTableRenameColumnSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableChangeColumnLengthSQL()
{
$this->markTestIncomplete('Not implemented yet');
}

/**
* @group DBAL-807
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/DB2PlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ protected function getQuotedAlterTableRenameColumnSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableChangeColumnLengthSQL()
{
$this->markTestIncomplete('Not implemented yet');
}

/**
* @group DBAL-807
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,14 @@ protected function getQuotedAlterTableRenameColumnSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableChangeColumnLengthSQL()
{
$this->markTestIncomplete('Not implemented yet');
}

/**
* @group DBAL-807
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,14 @@ protected function getQuotedAlterTableRenameColumnSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableChangeColumnLengthSQL()
{
$this->markTestIncomplete('Not implemented yet');
}

/**
* @group DBAL-807
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,20 @@ protected function getQuotedAlterTableRenameColumnSQL()
'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}
*/
protected function getQuotedAlterTableChangeColumnLengthSQL()
{
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)',
'INSERT INTO mytable (unquoted1, unquoted2, unquoted3, "create", "table", "select") SELECT unquoted1, unquoted2, unquoted3, "create", "table", "select" FROM __temp__mytable',
'DROP TABLE __temp__mytable',
);
}

/**
Expand Down

0 comments on commit a43e8da

Please sign in to comment.