Commit 07e84011 authored by jsor's avatar jsor

Change SchemaCreateTable event handling

parent ff5f1fdc
......@@ -35,13 +35,28 @@ use Doctrine\Common\EventArgs,
* @version $Revision$
* @author Jan Sorgalla <jsorgalla@googlemail.com>
*/
class SchemaCreateTableColumnEventArgs extends SchemaCreateTableEventArgs
class SchemaCreateTableColumnEventArgs extends SchemaEventArgs
{
/**
* @var Column
*/
private $_column = null;
/**
* @var Table
*/
private $_table = null;
/**
* @var AbstractPlatform
*/
private $_platform = null;
/**
* @var array
*/
private $_sql = array();
/**
* @param Column $column
* @param Table $table
......@@ -49,8 +64,9 @@ class SchemaCreateTableColumnEventArgs extends SchemaCreateTableEventArgs
*/
public function __construct(Column $column, Table $table, AbstractPlatform $platform)
{
parent::__construct($table, $platform);
$this->_column = $column;
$this->_column = $column;
$this->_table = $table;
$this->_platform = $platform;
}
/**
......@@ -60,4 +76,43 @@ class SchemaCreateTableColumnEventArgs extends SchemaCreateTableEventArgs
{
return $this->_column;
}
/**
* @return Doctrine\DBAL\Schema\Table
*/
public function getTable()
{
return $this->_table;
}
/**
* @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;
}
}
......@@ -40,7 +40,17 @@ class SchemaCreateTableEventArgs extends SchemaEventArgs
* @var Table
*/
private $_table = null;
/**
* @var array
*/
private $_columns = null;
/**
* @var array
*/
private $_options = null;
/**
* @var AbstractPlatform
*/
......@@ -53,11 +63,15 @@ class SchemaCreateTableEventArgs extends SchemaEventArgs
/**
* @param Table $table
* @param array $columns
* @param array $options
* @param AbstractPlatform $platform
*/
public function __construct(Table $table, AbstractPlatform $platform)
public function __construct(Table $table, array $columns, array $options, AbstractPlatform $platform)
{
$this->_table = $table;
$this->_table = $table;
$this->_columns = $columns;
$this->_options = $options;
$this->_platform = $platform;
}
......@@ -69,6 +83,22 @@ class SchemaCreateTableEventArgs extends SchemaEventArgs
return $this->_table;
}
/**
* @return array
*/
public function getColumns()
{
return $this->_columns;
}
/**
* @return array
*/
public function getOptions()
{
return $this->_options;
}
/**
* @return Doctrine\DBAL\Platforms\AbstractPlatform
*/
......
......@@ -35,10 +35,8 @@ final class Events
const postConnect = 'postConnect';
const preSchemaCreateTable = 'preSchemaCreateTable';
const onSchemaCreateTable = 'onSchemaCreateTable';
const onSchemaCreateTableColumn = 'onSchemaCreateTableColumn';
const postSchemaCreateTable = 'postSchemaCreateTable';
const onSchemaDropTable = 'onSchemaDropTable';
const onSchemaColumnDefinition = 'onSchemaColumnDefinition';
}
......@@ -971,15 +971,6 @@ 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());
}
......@@ -1050,25 +1041,29 @@ abstract class AbstractPlatform
}
}
$sql = $this->_getCreateTableSQL($tableName, $columns, $options);
if ($this->supportsCommentOnStatement()) {
foreach ($table->getColumns() AS $column) {
if ($column->getComment()) {
$sql[] = $this->getCommentOnColumnSQL($tableName, $column->getName(), $this->getColumnComment($column));
}
}
}
$tableSql = array();
$defaultPrevented = false;
$sql = array_merge($sql, $columnSql);
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
$eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::postSchemaCreateTable)) {
$eventArgs = new SchemaCreateTableEventArgs($table, $this);
$this->_eventManager->dispatchEvent(Events::postSchemaCreateTable, $eventArgs);
$defaultPrevented = $eventArgs->isDefaultPrevented();
$tableSql = $eventArgs->getSql();
}
$sql = array_merge($sql, $eventArgs->getSql());
if (!$defaultPrevented) {
$tableSql = $this->_getCreateTableSQL($tableName, $columns, $options);
if ($this->supportsCommentOnStatement()) {
foreach ($table->getColumns() AS $column) {
if ($column->getComment()) {
$tableSql[] = $this->getCommentOnColumnSQL($tableName, $column->getName(), $this->getColumnComment($column));
}
}
}
}
return $sql;
return array_merge($tableSql, $columnSql);
}
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
......
......@@ -181,19 +181,16 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
public function testGetCreateTableSqlDispatchEvent()
{
$listenerMock = $this->getMock('GetCreateTableSqlDispatchEvenListener', array('preSchemaCreateTable', 'onSchemaCreateTableColumn', 'postSchemaCreateTable'));
$listenerMock = $this->getMock('GetCreateTableSqlDispatchEvenListener', array('onSchemaCreateTable', 'onSchemaCreateTableColumn'));
$listenerMock
->expects($this->once())
->method('preSchemaCreateTable');
->method('onSchemaCreateTable');
$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);
$eventManager->addEventListener(array(Events::onSchemaCreateTable, Events::onSchemaCreateTableColumn), $listenerMock);
$this->_platform->setEventManager($eventManager);
......
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