Commit a1b18d86 authored by jsor's avatar jsor

Add alter table events to missing platforms

parent 6abba72f
...@@ -22,6 +22,11 @@ namespace Doctrine\DBAL\Platforms; ...@@ -22,6 +22,11 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
class DB2Platform extends AbstractPlatform class DB2Platform extends AbstractPlatform
{ {
...@@ -373,17 +378,51 @@ class DB2Platform extends AbstractPlatform ...@@ -373,17 +378,51 @@ class DB2Platform extends AbstractPlatform
public function getAlterTableSQL(TableDiff $diff) public function getAlterTableSQL(TableDiff $diff)
{ {
$sql = array(); $sql = array();
$columnSql = array();
$queryParts = array(); $queryParts = array();
foreach ($diff->addedColumns AS $fieldName => $column) { foreach ($diff->addedColumns AS $fieldName => $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableAddColumn)) {
$eventArgs = new SchemaAlterTableAddColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableAddColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$queryParts[] = 'ADD COLUMN ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); $queryParts[] = 'ADD COLUMN ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
} }
foreach ($diff->removedColumns AS $column) { foreach ($diff->removedColumns AS $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableRemoveColumn)) {
$eventArgs = new SchemaAlterTableRemoveColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRemoveColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this); $queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this);
} }
foreach ($diff->changedColumns AS $columnDiff) { foreach ($diff->changedColumns AS $columnDiff) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableChangeColumn)) {
$eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
/* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */ /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
$column = $columnDiff->column; $column = $columnDiff->column;
$queryParts[] = 'ALTER ' . ($columnDiff->oldColumnName) . ' ' $queryParts[] = 'ALTER ' . ($columnDiff->oldColumnName) . ' '
...@@ -391,6 +430,17 @@ class DB2Platform extends AbstractPlatform ...@@ -391,6 +430,17 @@ class DB2Platform extends AbstractPlatform
} }
foreach ($diff->renamedColumns AS $oldColumnName => $column) { foreach ($diff->renamedColumns AS $oldColumnName => $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableRenameColumn)) {
$eventArgs = new SchemaAlterTableRenameColumnEventArgs($oldColumnName, $column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRenameColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$queryParts[] = 'RENAME ' . $oldColumnName . ' TO ' . $column->getQuotedName($this); $queryParts[] = 'RENAME ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
} }
...@@ -404,7 +454,7 @@ class DB2Platform extends AbstractPlatform ...@@ -404,7 +454,7 @@ class DB2Platform extends AbstractPlatform
$sql[] = 'RENAME TABLE TO ' . $diff->newName; $sql[] = 'RENAME TABLE TO ' . $diff->newName;
} }
return $sql; return array_merge($sql, $columnSql);
} }
public function getDefaultValueDeclarationSQL($field) public function getDefaultValueDeclarationSQL($field)
......
...@@ -23,7 +23,12 @@ namespace Doctrine\DBAL\Platforms; ...@@ -23,7 +23,12 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Index, use Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Table; Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Events,
Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
/** /**
* The MsSqlPlatform provides the behavior, features and SQL dialect of the * The MsSqlPlatform provides the behavior, features and SQL dialect of the
...@@ -278,20 +283,54 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -278,20 +283,54 @@ class MsSqlPlatform extends AbstractPlatform
{ {
$queryParts = array(); $queryParts = array();
$sql = array(); $sql = array();
$columnSql = array();
if ($diff->newName !== false) { if ($diff->newName !== false) {
$queryParts[] = 'RENAME TO ' . $diff->newName; $queryParts[] = 'RENAME TO ' . $diff->newName;
} }
foreach ($diff->addedColumns AS $fieldName => $column) { foreach ($diff->addedColumns AS $fieldName => $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableAddColumn)) {
$eventArgs = new SchemaAlterTableAddColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableAddColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
} }
foreach ($diff->removedColumns AS $column) { foreach ($diff->removedColumns AS $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableRemoveColumn)) {
$eventArgs = new SchemaAlterTableRemoveColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRemoveColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this); $queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this);
} }
foreach ($diff->changedColumns AS $columnDiff) { foreach ($diff->changedColumns AS $columnDiff) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableChangeColumn)) {
$eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
/* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */ /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
$column = $columnDiff->column; $column = $columnDiff->column;
$queryParts[] = 'ALTER COLUMN ' . $queryParts[] = 'ALTER COLUMN ' .
...@@ -299,6 +338,17 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -299,6 +338,17 @@ class MsSqlPlatform extends AbstractPlatform
} }
foreach ($diff->renamedColumns AS $oldColumnName => $column) { foreach ($diff->renamedColumns AS $oldColumnName => $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableRenameColumn)) {
$eventArgs = new SchemaAlterTableRenameColumnEventArgs($oldColumnName, $column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRenameColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$sql[] = "sp_RENAME '". $diff->name. ".". $oldColumnName . "' , '".$column->getQuotedName($this)."', 'COLUMN'"; $sql[] = "sp_RENAME '". $diff->name. ".". $oldColumnName . "' , '".$column->getQuotedName($this)."', 'COLUMN'";
$queryParts[] = 'ALTER COLUMN ' . $queryParts[] = 'ALTER COLUMN ' .
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
...@@ -308,7 +358,7 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -308,7 +358,7 @@ class MsSqlPlatform extends AbstractPlatform
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
} }
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff)); $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $columnSql);
return $sql; return $sql;
} }
......
...@@ -22,7 +22,12 @@ namespace Doctrine\DBAL\Platforms; ...@@ -22,7 +22,12 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException, use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Schema\TableDiff, Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Index, Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Table; Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Events,
Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
/** /**
* The MySqlPlatform provides the behavior, features and SQL dialect of the * The MySqlPlatform provides the behavior, features and SQL dialect of the
...@@ -454,22 +459,56 @@ class MySqlPlatform extends AbstractPlatform ...@@ -454,22 +459,56 @@ class MySqlPlatform extends AbstractPlatform
*/ */
public function getAlterTableSQL(TableDiff $diff) public function getAlterTableSQL(TableDiff $diff)
{ {
$columnSql = array();
$queryParts = array(); $queryParts = array();
if ($diff->newName !== false) { if ($diff->newName !== false) {
$queryParts[] = 'RENAME TO ' . $diff->newName; $queryParts[] = 'RENAME TO ' . $diff->newName;
} }
foreach ($diff->addedColumns AS $fieldName => $column) { foreach ($diff->addedColumns AS $fieldName => $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableAddColumn)) {
$eventArgs = new SchemaAlterTableAddColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableAddColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$columnArray = $column->toArray(); $columnArray = $column->toArray();
$columnArray['comment'] = $this->getColumnComment($column); $columnArray['comment'] = $this->getColumnComment($column);
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
} }
foreach ($diff->removedColumns AS $column) { foreach ($diff->removedColumns AS $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableRemoveColumn)) {
$eventArgs = new SchemaAlterTableRemoveColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRemoveColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$queryParts[] = 'DROP ' . $column->getQuotedName($this); $queryParts[] = 'DROP ' . $column->getQuotedName($this);
} }
foreach ($diff->changedColumns AS $columnDiff) { foreach ($diff->changedColumns AS $columnDiff) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableChangeColumn)) {
$eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
/* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */ /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
$column = $columnDiff->column; $column = $columnDiff->column;
$columnArray = $column->toArray(); $columnArray = $column->toArray();
...@@ -479,6 +518,17 @@ class MySqlPlatform extends AbstractPlatform ...@@ -479,6 +518,17 @@ class MySqlPlatform extends AbstractPlatform
} }
foreach ($diff->renamedColumns AS $oldColumnName => $column) { foreach ($diff->renamedColumns AS $oldColumnName => $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableRenameColumn)) {
$eventArgs = new SchemaAlterTableRenameColumnEventArgs($oldColumnName, $column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRenameColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$columnArray = $column->toArray(); $columnArray = $column->toArray();
$columnArray['comment'] = $this->getColumnComment($column); $columnArray['comment'] = $this->getColumnComment($column);
$queryParts[] = 'CHANGE ' . $oldColumnName . ' ' $queryParts[] = 'CHANGE ' . $oldColumnName . ' '
...@@ -492,7 +542,8 @@ class MySqlPlatform extends AbstractPlatform ...@@ -492,7 +542,8 @@ class MySqlPlatform extends AbstractPlatform
$sql = array_merge( $sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff), $this->getPreAlterTableIndexForeignKeySQL($diff),
$sql, $sql,
$this->getPostAlterTableIndexForeignKeySQL($diff) $this->getPostAlterTableIndexForeignKeySQL($diff),
$columnSql
); );
return $sql; return $sql;
} }
......
...@@ -19,7 +19,12 @@ ...@@ -19,7 +19,12 @@
namespace Doctrine\DBAL\Platforms; namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Events,
Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
/** /**
* OraclePlatform. * OraclePlatform.
...@@ -540,9 +545,21 @@ LEFT JOIN all_cons_columns r_cols ...@@ -540,9 +545,21 @@ LEFT JOIN all_cons_columns r_cols
{ {
$sql = array(); $sql = array();
$commentsSQL = array(); $commentsSQL = array();
$columnSql = array();
$fields = array(); $fields = array();
foreach ($diff->addedColumns AS $column) { foreach ($diff->addedColumns AS $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableAddColumn)) {
$eventArgs = new SchemaAlterTableAddColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableAddColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
if ($comment = $this->getColumnComment($column)) { if ($comment = $this->getColumnComment($column)) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment); $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
...@@ -554,6 +571,17 @@ LEFT JOIN all_cons_columns r_cols ...@@ -554,6 +571,17 @@ LEFT JOIN all_cons_columns r_cols
$fields = array(); $fields = array();
foreach ($diff->changedColumns AS $columnDiff) { foreach ($diff->changedColumns AS $columnDiff) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableChangeColumn)) {
$eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$column = $columnDiff->column; $column = $columnDiff->column;
$fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray()); $fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray());
if ($columnDiff->hasChanged('comment') && $comment = $this->getColumnComment($column)) { if ($columnDiff->hasChanged('comment') && $comment = $this->getColumnComment($column)) {
...@@ -565,11 +593,33 @@ LEFT JOIN all_cons_columns r_cols ...@@ -565,11 +593,33 @@ LEFT JOIN all_cons_columns r_cols
} }
foreach ($diff->renamedColumns AS $oldColumnName => $column) { foreach ($diff->renamedColumns AS $oldColumnName => $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableRenameColumn)) {
$eventArgs = new SchemaAlterTableRenameColumnEventArgs($oldColumnName, $column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRenameColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getQuotedName($this); $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getQuotedName($this);
} }
$fields = array(); $fields = array();
foreach ($diff->removedColumns AS $column) { foreach ($diff->removedColumns AS $column) {
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaAlterTableRemoveColumn)) {
$eventArgs = new SchemaAlterTableRemoveColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRemoveColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$fields[] = $column->getQuotedName($this); $fields[] = $column->getQuotedName($this);
} }
if (count($fields)) { if (count($fields)) {
...@@ -580,7 +630,7 @@ LEFT JOIN all_cons_columns r_cols ...@@ -580,7 +630,7 @@ LEFT JOIN all_cons_columns r_cols
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName; $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
} }
return array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL); return array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL, $columnSql);
} }
/** /**
......
...@@ -126,4 +126,9 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -126,4 +126,9 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
{ {
$this->markTestSkipped('SQlite does not support ALTER Table.'); $this->markTestSkipped('SQlite does not support ALTER Table.');
} }
public function testGetAlterTableSqlDispatchEvent()
{
$this->markTestSkipped('SQlite does not support ALTER Table.');
}
} }
\ No newline at end of file
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