Commit b175e1c2 authored by Jan Sorgalla's avatar Jan Sorgalla

Add new onSchemaIndexDefinition event

parent 445e39e4
<?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\DBAL\Connection,
Doctrine\DBAL\Schema\Index;
/**
* Event Arguments used when the portable index definition is generated inside Doctrine\DBAL\Schema\AbstractSchemaManager.
*
* @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 SchemaIndexDefinitionEventArgs extends SchemaEventArgs
{
/**
* @var \Doctrine\DBAL\Schema\Index
*/
private $_index = null;
/**
* Raw index data as fetched from the database
*
* @var array
*/
private $_tableIndex = null;
/**
* @var string
*/
private $_table = null;
/**
* @var \Doctrine\DBAL\Connection
*/
private $_connection = null;
/**
* @param array $tableIndex
* @param string $table
* @param \Doctrine\DBAL\Connection $conn
*/
public function __construct(array $tableIndex, $table, Connection $connection)
{
$this->_tableIndex = $tableIndex;
$this->_table = $table;
$this->_connection = $connection;
}
/**
* Allows to clear the index which means the index will be excluded from
* tables index list.
*
* @param null|\Doctrine\DBAL\Schema\Index $index
* @return SchemaIndexDefinitionEventArgs
*/
public function setIndex(Index $index = null)
{
$this->_index = $index;
return $this;
}
/**
* @return \Doctrine\DBAL\Schema\Index
*/
public function getIndex()
{
return $this->_index;
}
/**
* @return array
*/
public function getTableIndex()
{
return $this->_tableIndex;
}
/**
* @return string
*/
public function getTable()
{
return $this->_table;
}
/**
* @return \Doctrine\DBAL\Connection
*/
public function getConnection()
{
return $this->_connection;
}
/**
* @return \Doctrine\DBAL\Platforms\AbstractPlatform
*/
public function getDatabasePlatform()
{
return $this->_connection->getDatabasePlatform();
}
}
......@@ -44,4 +44,5 @@ final class Events
const onSchemaAlterTableChangeColumn = 'onSchemaAlterTableChangeColumn';
const onSchemaAlterTableRenameColumn = 'onSchemaAlterTableRenameColumn';
const onSchemaColumnDefinition = 'onSchemaColumnDefinition';
const onSchemaIndexDefinition = 'onSchemaIndexDefinition';
}
......@@ -21,6 +21,7 @@ namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs;
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
use Doctrine\DBAL\Types;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
......@@ -691,9 +692,28 @@ abstract class AbstractSchemaManager
}
}
$eventManager = $this->_platform->getEventManager();
$indexes = array();
foreach($result AS $indexKey => $data) {
$indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
$index = null;
$defaultPrevented = false;
if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
$eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
$eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
$defaultPrevented = $eventArgs->isDefaultPrevented();
$index = $eventArgs->getIndex();
}
if (!$defaultPrevented) {
$index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
}
if ($index) {
$indexes[$indexKey] = $index;
}
}
return $indexes;
......
......@@ -21,6 +21,8 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
/**
* IBM Db2 Schema Manager
*
......@@ -120,6 +122,8 @@ class DB2SchemaManager extends AbstractSchemaManager
protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
{
$eventManager = $this->_platform->getEventManager();
$tableIndexRows = array();
$indexes = array();
foreach($tableIndexes AS $indexKey => $data) {
......@@ -134,7 +138,31 @@ class DB2SchemaManager extends AbstractSchemaManager
$keyName = $indexName;
}
$indexes[$keyName] = new Index($indexName, explode("+", ltrim($data['colnames'], '+')), $unique, $primary);
$data = array(
'name' => $indexName,
'columns' => explode("+", ltrim($data['colnames'], '+')),
'unique' => $unique,
'primary' => $primary
);
$index = null;
$defaultPrevented = false;
if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
$eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
$eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
$defaultPrevented = $eventArgs->isDefaultPrevented();
$index = $eventArgs->getIndex();
}
if (!$defaultPrevented) {
$index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
}
if ($index) {
$indexes[$indexKey] = $index;
}
}
return $indexes;
......
......@@ -19,6 +19,8 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
/**
* xxx
*
......@@ -119,9 +121,28 @@ class MsSqlSchemaManager extends AbstractSchemaManager
);
}
$eventManager = $this->_platform->getEventManager();
$indexes = array();
foreach ($result AS $indexKey => $data) {
$indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
$index = null;
$defaultPrevented = false;
if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
$eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
$eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
$defaultPrevented = $eventArgs->isDefaultPrevented();
$index = $eventArgs->getIndex();
}
if (!$defaultPrevented) {
$index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
}
if ($index) {
$indexes[$indexKey] = $index;
}
}
return $indexes;
......
......@@ -218,7 +218,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
}
}
}
return parent::_getPortableTableIndexesList($buffer);
return parent::_getPortableTableIndexesList($buffer, $tableName);
}
protected function _getPortableDatabaseDefinition($database)
......
......@@ -196,6 +196,31 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->_sm->getDatabasePlatform()->setEventManager($oldEventManager);
}
public function testListTableIndexesDispatchEvent()
{
$table = $this->getTestTable('list_table_indexes_test');
$table->addUniqueIndex(array('test'), 'test_index_name');
$table->addIndex(array('id', 'test'), 'test_composite_idx');
$this->_sm->dropAndCreateTable($table);
$listenerMock = $this->getMock('ListTableIndexesDispatchEventListener', array('onSchemaIndexDefinition'));
$listenerMock
->expects($this->exactly(3))
->method('onSchemaIndexDefinition');
$oldEventManager = $this->_sm->getDatabasePlatform()->getEventManager();
$eventManager = new EventManager();
$eventManager->addEventListener(array(Events::onSchemaIndexDefinition), $listenerMock);
$this->_sm->getDatabasePlatform()->setEventManager($eventManager);
$this->_sm->listTableIndexes('list_table_indexes_test');
$this->_sm->getDatabasePlatform()->setEventManager($oldEventManager);
}
public function testDiffListTableColumns()
{
if ($this->_sm->getDatabasePlatform()->getName() == 'oracle') {
......@@ -217,7 +242,7 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$table->addUniqueIndex(array('test'), 'test_index_name');
$table->addIndex(array('id', 'test'), 'test_composite_idx');
$this->_sm->createTable($table);
$this->_sm->dropAndCreateTable($table);
$tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
......
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