Commit 1d5afda9 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #453 from doctrine/DBAL-555

[DBAL-555] Fix using Identifier assets and quoting
parents 8f94c5dc 02fa06ce
......@@ -1717,7 +1717,7 @@ abstract class AbstractPlatform
*/
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{
$tableName = $diff->name;
$tableName = $diff->getName()->getQuotedName($this);
$sql = array();
if ($this->supportsForeignKeyConstraints()) {
......@@ -1746,7 +1746,9 @@ abstract class AbstractPlatform
*/
protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
{
$tableName = false !== $diff->newName ? $diff->newName : $diff->name;
$tableName = (false !== $diff->newName)
? $diff->getNewName()->getQuotedName($this)
: $diff->getName()->getQuotedName($this);
$sql = array();
if ($this->supportsForeignKeyConstraints()) {
......
......@@ -493,12 +493,12 @@ class DB2Platform extends AbstractPlatform
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(" ", $queryParts);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . implode(" ", $queryParts);
}
// Some table alteration operations require a table reorganization.
if ( ! empty($diff->removedColumns) || ! empty($diff->changedColumns)) {
$sql[] = "CALL SYSPROC.ADMIN_CMD ('REORG TABLE " . $diff->name . "')";
$sql[] = "CALL SYSPROC.ADMIN_CMD ('REORG TABLE " . $diff->getName()->getQuotedName($this) . "')";
}
$sql = array_merge(
......@@ -508,7 +508,7 @@ class DB2Platform extends AbstractPlatform
);
if ($diff->newName !== false) {
$sql[] = 'RENAME TABLE ' . $diff->name . ' TO ' . $diff->newName;
$sql[] = 'RENAME TABLE ' . $diff->getName()->getQuotedName($this) . ' TO ' . $diff->getNewName()->getQuotedName($this);
}
}
......@@ -521,7 +521,7 @@ class DB2Platform extends AbstractPlatform
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{
$sql = array();
$table = $diff->name;
$table = $diff->getName()->getQuotedName($this);
foreach ($diff->removedIndexes as $remKey => $remIndex) {
foreach ($diff->addedIndexes as $addKey => $addIndex) {
......
......@@ -396,7 +396,7 @@ class DrizzlePlatform extends AbstractPlatform
$queryParts = array();
if ($diff->newName !== false) {
$queryParts[] = 'RENAME TO ' . $diff->newName;
$queryParts[] = 'RENAME TO ' . $diff->getNewName()->getQuotedName($this);
}
foreach ($diff->addedColumns as $column) {
......@@ -446,7 +446,7 @@ class DrizzlePlatform extends AbstractPlatform
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . implode(", ", $queryParts);
}
$sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
......
......@@ -527,7 +527,7 @@ class MySqlPlatform extends AbstractPlatform
$columnSql = array();
$queryParts = array();
if ($diff->newName !== false) {
$queryParts[] = 'RENAME TO ' . $diff->newName;
$queryParts[] = 'RENAME TO ' . $diff->getNewName()->getQuotedName($this);
}
foreach ($diff->addedColumns as $column) {
......@@ -583,7 +583,7 @@ class MySqlPlatform extends AbstractPlatform
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . implode(", ", $queryParts);
}
$sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
......@@ -601,7 +601,7 @@ class MySqlPlatform extends AbstractPlatform
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{
$sql = array();
$table = $diff->name;
$table = $diff->getName()->getQuotedName($this);
foreach ($diff->removedIndexes as $remKey => $remIndex) {
// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
......
......@@ -615,7 +615,7 @@ LEFT JOIN user_cons_columns r_cols
}
if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' ADD (' . implode(', ', $fields) . ')';
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ADD (' . implode(', ', $fields) . ')';
}
$fields = array();
......@@ -650,7 +650,7 @@ LEFT JOIN user_cons_columns r_cols
}
if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' MODIFY (' . implode(', ', $fields) . ')';
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' MODIFY (' . implode(', ', $fields) . ')';
}
foreach ($diff->renamedColumns as $oldColumnName => $column) {
......@@ -658,7 +658,7 @@ LEFT JOIN user_cons_columns r_cols
continue;
}
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getQuotedName($this);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getQuotedName($this);
}
$fields = array();
......@@ -671,14 +671,14 @@ LEFT JOIN user_cons_columns r_cols
}
if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' DROP (' . implode(', ', $fields).')';
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' DROP (' . implode(', ', $fields).')';
}
$tableSql = array();
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if ($diff->newName !== false) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' RENAME TO ' . $diff->getNewName()->getQuotedName($this);
}
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
......
......@@ -421,7 +421,7 @@ class PostgreSqlPlatform extends AbstractPlatform
}
$query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query;
if ($comment = $this->getColumnComment($column)) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
}
......@@ -433,7 +433,7 @@ class PostgreSqlPlatform extends AbstractPlatform
}
$query = 'DROP ' . $column->getQuotedName($this);
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query;
}
foreach ($diff->changedColumns as $columnDiff) {
......@@ -450,17 +450,17 @@ class PostgreSqlPlatform extends AbstractPlatform
// here was a server version check before, but DBAL API does not support this anymore.
$query = 'ALTER ' . $oldColumnName . ' TYPE ' . $type->getSqlDeclaration($column->toArray(), $this);
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query;
}
if ($columnDiff->hasChanged('default')) {
$query = 'ALTER ' . $oldColumnName . ' SET ' . $this->getDefaultValueDeclarationSQL($column->toArray());
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query;
}
if ($columnDiff->hasChanged('notnull')) {
$query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL';
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query;
}
if ($columnDiff->hasChanged('autoincrement')) {
......@@ -469,13 +469,13 @@ class PostgreSqlPlatform extends AbstractPlatform
$seqName = $diff->name . '_' . $oldColumnName . '_seq';
$sql[] = "CREATE SEQUENCE " . $seqName;
$sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->name . "))";
$sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->getName()->getQuotedName($this) . "))";
$query = "ALTER " . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')";
$sql[] = "ALTER TABLE " . $diff->name . " " . $query;
$sql[] = "ALTER TABLE " . $diff->getName()->getQuotedName($this) . " " . $query;
} else {
// Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have
$query = "ALTER " . $oldColumnName . " " . "DROP DEFAULT";
$sql[] = "ALTER TABLE " . $diff->name . " " . $query;
$sql[] = "ALTER TABLE " . $diff->getName()->getQuotedName($this) . " " . $query;
}
}
......@@ -489,7 +489,7 @@ class PostgreSqlPlatform extends AbstractPlatform
if ($columnDiff->hasChanged('length')) {
$query = 'ALTER ' . $column->getName() . ' TYPE ' . $column->getType()->getSqlDeclaration($column->toArray(), $this);
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query;
}
}
......@@ -498,14 +498,14 @@ class PostgreSqlPlatform extends AbstractPlatform
continue;
}
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
}
$tableSql = array();
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if ($diff->newName !== false) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' RENAME TO ' . $diff->getNewName()->getQuotedName($this);
}
$sql = array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $sql, $this->getPostAlterTableIndexForeignKeySQL($diff), $commentsSQL);
......
......@@ -28,6 +28,7 @@ use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Constraint;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
......@@ -189,18 +190,18 @@ class SQLAnywherePlatform extends AbstractPlatform
continue;
}
$sql[] = $this->getAlterTableClause($diff->name) . ' ' .
$sql[] = $this->getAlterTableClause($diff->getName()) . ' ' .
$this->getAlterTableRenameColumnClause($oldColumnName, $column);
}
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if ( ! empty($alterClauses)) {
$sql[] = $this->getAlterTableClause($diff->name) . ' ' . implode(", ", $alterClauses);
$sql[] = $this->getAlterTableClause($diff->getName()) . ' ' . implode(", ", $alterClauses);
}
if ($diff->newName !== false) {
$sql[] = $this->getAlterTableClause($diff->name) . ' ' .
$this->getAlterTableRenameTableClause($diff->newName);
$sql[] = $this->getAlterTableClause($diff->getName()) . ' ' .
$this->getAlterTableRenameTableClause($diff->getNewName());
}
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
......@@ -224,13 +225,13 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* Returns the SQL clause for altering a table.
*
* @param string $tableName The quoted name of the table to alter.
* @param Identifier $tableName The quoted name of the table to alter.
*
* @return string
*/
protected function getAlterTableClause($tableName)
protected function getAlterTableClause(Identifier $tableName)
{
return 'ALTER TABLE ' . $tableName;
return 'ALTER TABLE ' . $tableName->getQuotedName($this);
}
/**
......@@ -261,13 +262,13 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* Returns the SQL clause for renaming a table in a table alteration.
*
* @param string $newTableName The quoted name of the table to rename to.
* @param Identifier $newTableName The quoted name of the table to rename to.
*
* @return string
*/
protected function getAlterTableRenameTableClause($newTableName)
protected function getAlterTableRenameTableClause(Identifier $newTableName)
{
return 'RENAME ' . $newTableName;
return 'RENAME ' . $newTableName->getQuotedName($this);
}
/**
......
......@@ -570,13 +570,13 @@ class SQLServerPlatform extends AbstractPlatform
}
foreach ($queryParts as $query) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query;
}
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSql);
if ($diff->newName !== false) {
$sql[] = "sp_RENAME '" . $diff->name . "', '" . $diff->newName . "'";
$sql[] = "sp_RENAME '" . $diff->getName()->getQuotedName($this) . "', '" . $diff->getNewName()->getQuotedName($this) . "'";
/**
* Rename table's default constraints names
......@@ -592,7 +592,7 @@ class SQLServerPlatform extends AbstractPlatform
"'" . $this->generateIdentifierName($diff->newName) . "') + ''', ''OBJECT'';' " .
"FROM sys.default_constraints dc " .
"JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id " .
"WHERE tbl.name = '" . $diff->newName . "';" .
"WHERE tbl.name = '" . $diff->getNewName()->getQuotedName($this) . "';" .
"EXEC sp_executesql @sql";
}
......
......@@ -24,6 +24,7 @@ use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Constraint;
/**
......@@ -804,7 +805,7 @@ class SqlitePlatform extends AbstractPlatform
$sql[] = $this->getDropTableSQL($dataTable);
if ($diff->newName && $diff->newName != $diff->name) {
$renamedTable = new Table($diff->newName);
$renamedTable = new Identifier($diff->newName);
$sql[] = 'ALTER TABLE '.$newTable->getQuotedName($this).' RENAME TO '.$renamedTable->getQuotedName($this);
}
......@@ -859,7 +860,7 @@ class SqlitePlatform extends AbstractPlatform
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if ($diff->newName !== false) {
$newTable = new Table($diff->newName);
$newTable = new Identifier($diff->newName);
$sql[] = 'ALTER TABLE '.$table->getQuotedName($this).' RENAME TO '.$newTable->getQuotedName($this);
}
}
......
......@@ -138,4 +138,20 @@ class TableDiff
$this->removedIndexes = $removedIndexes;
$this->fromTable = $fromTable;
}
/**
* @return \Doctrine\DBAL\Schema\Identifier
*/
public function getName()
{
return new Identifier($this->name);
}
/**
* @return \Doctrine\DBAL\Schema\Identifier
*/
public function getNewName()
{
return new Identifier($this->newName);
}
}
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