Commit 24c1d54e authored by Jan Sorgalla's avatar Jan Sorgalla

Refactor duplicate event code in platforms

parent 8a207e03
......@@ -27,12 +27,17 @@ use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Schema\ForeignKeyConstraint,
Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Column,
Doctrine\DBAL\Schema\ColumnDiff,
Doctrine\DBAL\Types\Type,
Doctrine\DBAL\Events,
Doctrine\Common\EventManager,
Doctrine\DBAL\Event\SchemaCreateTableEventArgs,
Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs,
Doctrine\DBAL\Event\SchemaDropTableEventArgs;
Doctrine\DBAL\Event\SchemaDropTableEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
/**
* Base class for all DatabasePlatforms. The DatabasePlatforms are the central
......@@ -1285,6 +1290,99 @@ abstract class AbstractPlatform
throw DBALException::notSupported(__METHOD__);
}
/**
* @param Column $column
* @param TableDiff $diff
* @param array $columnSql
*/
protected function onSchemaAlterTableAddColumn(Column $column, TableDiff $diff, &$columnSql)
{
if (null === $this->_eventManager) {
return false;
}
if (!$this->_eventManager->hasListeners(Events::onSchemaAlterTableAddColumn)) {
return false;
}
$eventArgs = new SchemaAlterTableAddColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableAddColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
return $eventArgs->isDefaultPrevented();
}
/**
* @param Column $column
* @param TableDiff $diff
* @param array $columnSql
*/
protected function onSchemaAlterTableRemoveColumn(Column $column, TableDiff $diff, &$columnSql)
{
if (null === $this->_eventManager) {
return false;
}
if (!$this->_eventManager->hasListeners(Events::onSchemaAlterTableRemoveColumn)) {
return false;
}
$eventArgs = new SchemaAlterTableRemoveColumnEventArgs($column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRemoveColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
return $eventArgs->isDefaultPrevented();
}
/**
* @param ColumnDiff $columnDiff
* @param TableDiff $diff
* @param array $columnSql
*/
protected function onSchemaAlterTableChangeColumn(ColumnDiff $columnDiff, TableDiff $diff, &$columnSql)
{
if (null === $this->_eventManager) {
return false;
}
if (!$this->_eventManager->hasListeners(Events::onSchemaAlterTableChangeColumn)) {
return false;
}
$eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
return $eventArgs->isDefaultPrevented();
}
/**
* @param string $oldColumnName
* @param Column $column
* @param TableDiff $diff
* @param array $columnSql
*/
protected function onSchemaAlterTableRenameColumn($oldColumnName, Column $column, TableDiff $diff, &$columnSql)
{
if (null === $this->_eventManager) {
return false;
}
if (!$this->_eventManager->hasListeners(Events::onSchemaAlterTableRenameColumn)) {
return false;
}
$eventArgs = new SchemaAlterTableRenameColumnEventArgs($oldColumnName, $column, $diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRenameColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
return $eventArgs->isDefaultPrevented();
}
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{
$tableName = $diff->name;
......
......@@ -22,11 +22,6 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Index;
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
{
......@@ -382,45 +377,24 @@ class DB2Platform extends AbstractPlatform
$queryParts = array();
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;
}
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
continue;
}
$queryParts[] = 'ADD COLUMN ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
}
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;
}
if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
continue;
}
$queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this);
}
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;
}
if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
continue;
}
/* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
......@@ -430,15 +404,8 @@ class DB2Platform extends AbstractPlatform
}
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;
}
if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
continue;
}
$queryParts[] = 'RENAME ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
......
......@@ -23,12 +23,7 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Events,
Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
Doctrine\DBAL\Schema\Table;
/**
* The MsSqlPlatform provides the behavior, features and SQL dialect of the
......@@ -290,45 +285,24 @@ class MsSqlPlatform extends AbstractPlatform
}
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;
}
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
continue;
}
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
}
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;
}
if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
continue;
}
$queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this);
}
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;
}
if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
continue;
}
/* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
......@@ -338,15 +312,8 @@ class MsSqlPlatform extends AbstractPlatform
}
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;
}
if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
continue;
}
$sql[] = "sp_RENAME '". $diff->name. ".". $oldColumnName . "' , '".$column->getQuotedName($this)."', 'COLUMN'";
......
......@@ -22,12 +22,7 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Events,
Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
Doctrine\DBAL\Schema\Table;
/**
* The MySqlPlatform provides the behavior, features and SQL dialect of the
......@@ -466,15 +461,8 @@ class MySqlPlatform extends AbstractPlatform
}
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;
}
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
continue;
}
$columnArray = $column->toArray();
......@@ -483,30 +471,16 @@ class MySqlPlatform extends AbstractPlatform
}
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;
}
if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
continue;
}
$queryParts[] = 'DROP ' . $column->getQuotedName($this);
}
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;
}
if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
continue;
}
/* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
......@@ -518,15 +492,8 @@ class MySqlPlatform extends AbstractPlatform
}
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;
}
if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
continue;
}
$columnArray = $column->toArray();
......
......@@ -19,12 +19,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Events,
Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
use Doctrine\DBAL\Schema\TableDiff;
/**
* OraclePlatform.
......@@ -549,15 +544,8 @@ LEFT JOIN all_cons_columns r_cols
$fields = array();
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;
}
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
continue;
}
$fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
......@@ -571,15 +559,8 @@ LEFT JOIN all_cons_columns r_cols
$fields = array();
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;
}
if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
continue;
}
$column = $columnDiff->column;
......@@ -593,15 +574,8 @@ LEFT JOIN all_cons_columns r_cols
}
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;
}
if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
continue;
}
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getQuotedName($this);
......@@ -609,15 +583,8 @@ LEFT JOIN all_cons_columns r_cols
$fields = array();
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;
}
if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
continue;
}
$fields[] = $column->getQuotedName($this);
......
......@@ -20,12 +20,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Events,
Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
Doctrine\DBAL\Schema\Table;
/**
* PostgreSqlPlatform.
......@@ -388,15 +383,8 @@ class PostgreSqlPlatform extends AbstractPlatform
$columnSql = array();
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;
}
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
continue;
}
$query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
......@@ -407,15 +395,8 @@ class PostgreSqlPlatform extends AbstractPlatform
}
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;
}
if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
continue;
}
$query = 'DROP ' . $column->getQuotedName($this);
......@@ -423,15 +404,8 @@ class PostgreSqlPlatform extends AbstractPlatform
}
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;
}
if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
continue;
}
$oldColumnName = $columnDiff->oldColumnName;
......@@ -473,15 +447,8 @@ class PostgreSqlPlatform extends AbstractPlatform
}
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;
}
if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
continue;
}
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
......
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