Commit bdbf2cfa authored by Jan Sorgalla's avatar Jan Sorgalla

Add event dispatching in AbstractPlatform::getCreateTableSQL

parent 5ae7de71
<?php
/*
* $Id$
*
* 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\Table,
Doctrine\DBAL\Schema\Column;
/**
* Event Arguments used when SQL queries for creating table columns are generated inside Doctrine\DBAL\Platform\AbstractPlatform.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.2
* @version $Revision$
* @author Jan Sorgalla <jsorgalla@googlemail.com>
*/
class SchemaCreateTableColumnEventArgs extends SchemaCreateTableEventArgs
{
/**
* @var Column
*/
private $_column = null;
public function __construct(Column $column, Table $table, AbstractPlatform $platform)
{
parent::__construct($table, $platform);
$this->_column = $column;
}
/**
* @return Doctrine\DBAL\Schema\Column
*/
public function getColumn()
{
return $this->_column;
}
}
<?php
/*
* $Id$
*
* 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\Table;
/**
* Event Arguments used when SQL queries for creating tables are generated inside Doctrine\DBAL\Platform\AbstractPlatform.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.2
* @version $Revision$
* @author Jan Sorgalla <jsorgalla@googlemail.com>
*/
class SchemaCreateTableEventArgs extends SchemaEventArgs
{
/**
* @var Table
*/
private $_table = null;
/**
* @var AbstractPlatform
*/
private $_platform = null;
public function __construct(Table $table, AbstractPlatform $platform)
{
$this->_table = $table;
$this->_platform = $platform;
}
/**
* @return Doctrine\DBAL\Schema\Table
*/
public function getTable()
{
return $this->_table;
}
/**
* @return Doctrine\DBAL\Platforms\AbstractPlatform
*/
public function getPlatform()
{
return $this->_platform;
}
}
<?php
/*
* $Id$
*
* 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;
/**
* Event Arguments used when SQL queries for creating tables are generated inside Doctrine\DBAL\Platform\AbstractPlatform.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.2
* @version $Revision$
* @author Jan Sorgalla <jsorgalla@googlemail.com>
*/
class SchemaEventArgs extends EventArgs
{
/**
* @var boolean
*/
private $_preventDefault = false;
/**
* @var array
*/
private $_sql = array();
public function __construct(Table $table)
{
$this->_table = $table;
}
/**
* @return SchemaEventArgs
*/
public function preventDefault()
{
$this->_preventDefault = true;
return $this;
}
/**
* @return boolean
*/
public function isDefaultPrevented()
{
return $this->_preventDefault;
}
/**
* @param string $sql
* @return SchemaEventArgs
*/
public function addSql($sql)
{
$this->_sql[] = $sql;
return $this;
}
/**
* @return array
*/
public function getSql()
{
return $this->_sql;
}
}
......@@ -34,5 +34,9 @@ final class Events
private function __construct() {}
const postConnect = 'postConnect';
const preSchemaCreateTable = 'preSchemaCreateTable';
const onSchemaCreateTableColumn = 'onSchemaCreateTableColumn';
const postSchemaCreateTable = 'postSchemaCreateTable';
}
......@@ -28,7 +28,10 @@ use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Column,
Doctrine\DBAL\Types\Type,
Doctrine\Common\EventManager;
Doctrine\DBAL\Events,
Doctrine\Common\EventManager,
Doctrine\DBAL\Event\SchemaCreateTableEventArgs,
Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
/**
* Base class for all DatabasePlatforms. The DatabasePlatforms are the central
......@@ -958,6 +961,15 @@ abstract class AbstractPlatform
throw new \InvalidArgumentException("Second argument of AbstractPlatform::getCreateTableSQL() has to be integer.");
}
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::preSchemaCreateTable)) {
$eventArgs = new SchemaCreateTableEventArgs($table, $this);
$this->_eventManager->dispatchEvent(Events::preSchemaCreateTable, $eventArgs);
if ($eventArgs->isDefaultPrevented()) {
return $eventArgs->getSql();
}
}
if (count($table->getColumns()) == 0) {
throw DBALException::noColumnsSpecifiedForTable($table->getName());
}
......@@ -979,9 +991,22 @@ abstract class AbstractPlatform
}
}
$columnSql = array();
$columns = array();
foreach ($table->getColumns() AS $column) {
/* @var \Doctrine\DBAL\Schema\Column $column */
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)) {
$eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);
$columnSql = array_merge($columnSql, $eventArgs->getSql());
if ($eventArgs->isDefaultPrevented()) {
continue;
}
}
$columnData = array();
$columnData['name'] = $column->getQuotedName($this);
$columnData['type'] = $column->getType();
......@@ -1023,6 +1048,16 @@ abstract class AbstractPlatform
}
}
}
$sql = array_merge($sql, $columnSql);
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::postSchemaCreateTable)) {
$eventArgs = new SchemaCreateTableEventArgs($table, $this);
$this->_eventManager->dispatchEvent(Events::postSchemaCreateTable, $eventArgs);
$sql = array_merge($sql, $eventArgs->getSql());
}
return $sql;
}
......
......@@ -2,6 +2,9 @@
namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Events;
abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
{
/**
......@@ -84,6 +87,31 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
abstract public function getGenerateTableWithMultiColumnUniqueIndexSql();
public function testGetTableSqlDispatchEvent()
{
$listenerMock = $this->getMock('GetTableSqlDispatchEventListener', array('preSchemaCreateTable', 'onSchemaCreateTableColumn', 'postSchemaCreateTable'));
$listenerMock
->expects($this->once())
->method('preSchemaCreateTable');
$listenerMock
->expects($this->exactly(2))
->method('onSchemaCreateTableColumn');
$listenerMock
->expects($this->once())
->method('postSchemaCreateTable');
$eventManager = new EventManager();
$eventManager->addEventListener(array(Events::preSchemaCreateTable, Events::onSchemaCreateTableColumn, Events::postSchemaCreateTable), $listenerMock);
$this->_platform->setEventManager($eventManager);
$table = new \Doctrine\DBAL\Schema\Table('test');
$table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
$table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
$this->_platform->getCreateTableSQL($table);
}
public function testGeneratesIndexCreationSql()
{
$indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));
......
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