Commit 07e84011 authored by jsor's avatar jsor

Change SchemaCreateTable event handling

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