Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
bdbf2cfa
Commit
bdbf2cfa
authored
Dec 07, 2011
by
Jan Sorgalla
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add event dispatching in AbstractPlatform::getCreateTableSQL
parent
5ae7de71
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
284 additions
and
1 deletion
+284
-1
SchemaCreateTableColumnEventArgs.php
lib/Doctrine/DBAL/Event/SchemaCreateTableColumnEventArgs.php
+58
-0
SchemaCreateTableEventArgs.php
lib/Doctrine/DBAL/Event/SchemaCreateTableEventArgs.php
+70
-0
SchemaEventArgs.php
lib/Doctrine/DBAL/Event/SchemaEventArgs.php
+88
-0
Events.php
lib/Doctrine/DBAL/Events.php
+4
-0
AbstractPlatform.php
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
+36
-1
AbstractPlatformTestCase.php
...octrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
+28
-0
No files found.
lib/Doctrine/DBAL/Event/SchemaCreateTableColumnEventArgs.php
0 → 100644
View file @
bdbf2cfa
<?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
;
}
}
lib/Doctrine/DBAL/Event/SchemaCreateTableEventArgs.php
0 → 100644
View file @
bdbf2cfa
<?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
;
}
}
lib/Doctrine/DBAL/Event/SchemaEventArgs.php
0 → 100644
View file @
bdbf2cfa
<?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
;
}
}
lib/Doctrine/DBAL/Events.php
View file @
bdbf2cfa
...
...
@@ -34,5 +34,9 @@ final class Events
private
function
__construct
()
{}
const
postConnect
=
'postConnect'
;
const
preSchemaCreateTable
=
'preSchemaCreateTable'
;
const
onSchemaCreateTableColumn
=
'onSchemaCreateTableColumn'
;
const
postSchemaCreateTable
=
'postSchemaCreateTable'
;
}
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
View file @
bdbf2cfa
...
...
@@ -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
;
}
...
...
tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
View file @
bdbf2cfa
...
...
@@ -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'
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment