Commit 5bc8a179 authored by Jan Sorgalla's avatar Jan Sorgalla

Add missing general alter table event

parent a6bef6a3
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Event;
use Doctrine\Common\EventArgs,
Doctrine\DBAL\Platforms\AbstractPlatform,
Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Column;
/**
* Event Arguments used when SQL queries for creating tables are generated inside Doctrine\DBAL\Platform\*Platform.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.2
* @author Jan Sorgalla <jsorgalla@googlemail.com>
*/
class SchemaAlterTableEventArgs extends SchemaEventArgs
{
/**
* @var TableDiff
*/
private $_tableDiff = null;
/**
* @var AbstractPlatform
*/
private $_platform = null;
/**
* @var array
*/
private $_sql = array();
/**
* @param TableDiff $tableDiff
* @param AbstractPlatform $platform
*/
public function __construct(TableDiff $tableDiff, AbstractPlatform $platform)
{
$this->_tableDiff = $tableDiff;
$this->_platform = $platform;
}
/**
* @return Doctrine\DBAL\Schema\TableDiff
*/
public function getTableDiff()
{
return $this->_tableDiff;
}
/**
* @return Doctrine\DBAL\Platforms\AbstractPlatform
*/
public function getPlatform()
{
return $this->_platform;
}
/**
* @param string|array $sql
* @return SchemaEventArgs
*/
public function addSql($sql)
{
if (is_array($sql)) {
$this->_sql = array_merge($this->_sql, $sql);
} else {
$this->_sql[] = $sql;
}
return $this;
}
/**
* @return array
*/
public function getSql()
{
return $this->_sql;
}
}
...@@ -38,9 +38,10 @@ final class Events ...@@ -38,9 +38,10 @@ final class Events
const onSchemaCreateTable = 'onSchemaCreateTable'; const onSchemaCreateTable = 'onSchemaCreateTable';
const onSchemaCreateTableColumn = 'onSchemaCreateTableColumn'; const onSchemaCreateTableColumn = 'onSchemaCreateTableColumn';
const onSchemaDropTable = 'onSchemaDropTable'; const onSchemaDropTable = 'onSchemaDropTable';
const onSchemaAlterTableAddColumn = 'onSchemaAlterTableAddColumn'; const onSchemaAlterTable = 'onSchemaAlterTable';
const onSchemaAlterTableRemoveColumn = 'onSchemaAlterTableRemoveColumn'; const onSchemaAlterTableAddColumn = 'onSchemaAlterTableAddColumn';
const onSchemaAlterTableChangeColumn = 'onSchemaAlterTableChangeColumn'; const onSchemaAlterTableRemoveColumn = 'onSchemaAlterTableRemoveColumn';
const onSchemaAlterTableRenameColumn = 'onSchemaAlterTableRenameColumn'; const onSchemaAlterTableChangeColumn = 'onSchemaAlterTableChangeColumn';
const onSchemaAlterTableRenameColumn = 'onSchemaAlterTableRenameColumn';
const onSchemaColumnDefinition = 'onSchemaColumnDefinition'; const onSchemaColumnDefinition = 'onSchemaColumnDefinition';
} }
...@@ -34,6 +34,7 @@ use Doctrine\DBAL\DBALException, ...@@ -34,6 +34,7 @@ use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Event\SchemaCreateTableEventArgs, Doctrine\DBAL\Event\SchemaCreateTableEventArgs,
Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs, Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs,
Doctrine\DBAL\Event\SchemaDropTableEventArgs, Doctrine\DBAL\Event\SchemaDropTableEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs, Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs, Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs,
Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs, Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs,
...@@ -1378,6 +1379,27 @@ abstract class AbstractPlatform ...@@ -1378,6 +1379,27 @@ abstract class AbstractPlatform
return $eventArgs->isDefaultPrevented(); return $eventArgs->isDefaultPrevented();
} }
/**
* @param TableDiff $diff
* @param array $columnSql
*/
protected function onSchemaAlterTable(TableDiff $diff, &$sql)
{
if (null === $this->_eventManager) {
return false;
}
if (!$this->_eventManager->hasListeners(Events::onSchemaAlterTable)) {
return false;
}
$eventArgs = new SchemaAlterTableEventArgs($diff, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaAlterTable, $eventArgs);
$sql = array_merge($sql, $eventArgs->getSql());
return $eventArgs->isDefaultPrevented();
}
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff) protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{ {
......
...@@ -411,17 +411,21 @@ class DB2Platform extends AbstractPlatform ...@@ -411,17 +411,21 @@ class DB2Platform extends AbstractPlatform
$queryParts[] = 'RENAME ' . $oldColumnName . ' TO ' . $column->getQuotedName($this); $queryParts[] = 'RENAME ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
} }
if (count($queryParts) > 0) { $tableSql = array();
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(" ", $queryParts);
} if (!$this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(" ", $queryParts);
}
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff)); $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
if ($diff->newName !== false) { if ($diff->newName !== false) {
$sql[] = 'RENAME TABLE TO ' . $diff->newName; $sql[] = 'RENAME TABLE TO ' . $diff->newName;
}
} }
return array_merge($sql, $columnSql); return array_merge($sql, $tableSql, $columnSql);
} }
public function getDefaultValueDeclarationSQL($field) public function getDefaultValueDeclarationSQL($field)
......
...@@ -320,14 +320,20 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -320,14 +320,20 @@ class MsSqlPlatform extends AbstractPlatform
$queryParts[] = 'ALTER COLUMN ' . $queryParts[] = 'ALTER COLUMN ' .
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
} }
$tableSql = array();
if ($this->onSchemaAlterTable($diff, $tableSql)) {
return array_merge($tableSql, $columnSql);
}
foreach ($queryParts as $query) { foreach ($queryParts as $query) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
} }
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $columnSql); $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
return $sql; return array_merge($sql, $tableSql, $columnSql);
} }
/** /**
......
...@@ -503,16 +503,20 @@ class MySqlPlatform extends AbstractPlatform ...@@ -503,16 +503,20 @@ class MySqlPlatform extends AbstractPlatform
} }
$sql = array(); $sql = array();
if (count($queryParts) > 0) { $tableSql = array();
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
if (!$this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
}
$sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
$sql,
$this->getPostAlterTableIndexForeignKeySQL($diff)
);
} }
$sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff), return array_merge($sql, $tableSql, $columnSql);
$sql,
$this->getPostAlterTableIndexForeignKeySQL($diff),
$columnSql
);
return $sql;
} }
/** /**
......
...@@ -593,11 +593,17 @@ LEFT JOIN all_cons_columns r_cols ...@@ -593,11 +593,17 @@ LEFT JOIN all_cons_columns r_cols
$sql[] = 'ALTER TABLE ' . $diff->name . ' DROP (' . implode(', ', $fields).')'; $sql[] = 'ALTER TABLE ' . $diff->name . ' DROP (' . implode(', ', $fields).')';
} }
if ($diff->newName !== false) { $tableSql = array();
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
if (!$this->onSchemaAlterTable($diff, $tableSql)) {
if ($diff->newName !== false) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
}
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
} }
return array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL, $columnSql); return array_merge($sql, $tableSql, $columnSql);
} }
/** /**
......
...@@ -454,11 +454,17 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -454,11 +454,17 @@ class PostgreSqlPlatform extends AbstractPlatform
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this); $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
} }
if ($diff->newName !== false) { $tableSql = array();
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
if (!$this->onSchemaAlterTable($diff, $tableSql)) {
if ($diff->newName !== false) {
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
}
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
} }
return array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL, $columnSql); return array_merge($sql, $tableSql, $columnSql);
} }
/** /**
......
...@@ -219,6 +219,7 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -219,6 +219,7 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
public function testGetAlterTableSqlDispatchEvent() public function testGetAlterTableSqlDispatchEvent()
{ {
$events = array( $events = array(
'onSchemaAlterTable',
'onSchemaAlterTableAddColumn', 'onSchemaAlterTableAddColumn',
'onSchemaAlterTableRemoveColumn', 'onSchemaAlterTableRemoveColumn',
'onSchemaAlterTableChangeColumn', 'onSchemaAlterTableChangeColumn',
...@@ -226,6 +227,9 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -226,6 +227,9 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
); );
$listenerMock = $this->getMock('GetAlterTableSqlDispatchEvenListener', $events); $listenerMock = $this->getMock('GetAlterTableSqlDispatchEvenListener', $events);
$listenerMock
->expects($this->once())
->method('onSchemaAlterTable');
$listenerMock $listenerMock
->expects($this->once()) ->expects($this->once())
->method('onSchemaAlterTableAddColumn'); ->method('onSchemaAlterTableAddColumn');
...@@ -241,6 +245,7 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -241,6 +245,7 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$eventManager = new EventManager(); $eventManager = new EventManager();
$events = array( $events = array(
Events::onSchemaAlterTable,
Events::onSchemaAlterTableAddColumn, Events::onSchemaAlterTableAddColumn,
Events::onSchemaAlterTableRemoveColumn, Events::onSchemaAlterTableRemoveColumn,
Events::onSchemaAlterTableChangeColumn, Events::onSchemaAlterTableChangeColumn,
......
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