[2.0] - DDC-169 - Added DropSchemaSql Visitor - Refactored Visitor package a bit, however its still not very appealing to have that much use statements cluttered in the code
This commit is contained in:
parent
22cfa37f43
commit
0902527476
@ -22,6 +22,7 @@
|
||||
namespace Doctrine\DBAL\Schema;
|
||||
|
||||
use \Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\DBAL\Schema\Visitor\Visitor;
|
||||
|
||||
/**
|
||||
* Object representation of a database column
|
||||
|
@ -1,7 +1,28 @@
|
||||
<?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\Schema;
|
||||
|
||||
use Doctrine\DBAL\Schema\Visitor\Visitor;
|
||||
|
||||
class ForeignKeyConstraint extends AbstractAsset implements Constraint
|
||||
{
|
||||
/**
|
||||
|
@ -21,6 +21,8 @@
|
||||
|
||||
namespace Doctrine\DBAL\Schema;
|
||||
|
||||
use Doctrine\DBAL\Schema\Visitor\Visitor;
|
||||
|
||||
class Index extends AbstractAsset
|
||||
{
|
||||
/**
|
||||
|
@ -22,6 +22,8 @@
|
||||
namespace Doctrine\DBAL\Schema;
|
||||
|
||||
use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
|
||||
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
|
||||
use Doctrine\DBAL\Schema\Visitor\Visitor;
|
||||
|
||||
/**
|
||||
* Object representation of a database schema
|
||||
@ -216,7 +218,10 @@ class Schema extends AbstractAsset
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of necessary sql queries to create the schema on the given platform.
|
||||
*
|
||||
* @param AbstractPlatform $platform
|
||||
* @return array
|
||||
*/
|
||||
public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
|
||||
{
|
||||
@ -226,6 +231,30 @@ class Schema extends AbstractAsset
|
||||
return $sqlCollector->getQueries();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of necessary sql queries to drop the schema on the given platform.
|
||||
*
|
||||
* @param AbstractPlatform $platform
|
||||
* @return array
|
||||
*/
|
||||
public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
|
||||
{
|
||||
$dropSqlCollector = new DropSchemaSqlCollector($platform);
|
||||
$this->visit($dropSqlCollector);
|
||||
|
||||
return $dropSqlCollector->getQueries();
|
||||
}
|
||||
|
||||
public function migrateTo(Schema $schema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function migrateFrom(Schema $schema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Visitor $visitor
|
||||
*/
|
||||
|
@ -21,6 +21,8 @@
|
||||
|
||||
namespace Doctrine\DBAL\Schema;
|
||||
|
||||
use Doctrine\DBAL\Schema\Visitor\Visitor;
|
||||
|
||||
/**
|
||||
* Sequence Structure
|
||||
*
|
||||
|
@ -22,6 +22,7 @@
|
||||
namespace Doctrine\DBAL\Schema;
|
||||
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\DBAL\Schema\Visitor\Visitor;
|
||||
|
||||
/**
|
||||
* Object Representation of a table
|
||||
@ -440,16 +441,16 @@ class Table extends AbstractAsset
|
||||
{
|
||||
$visitor->acceptTable($this);
|
||||
|
||||
foreach($this->getColumns() AS $column) {
|
||||
foreach ($this->getColumns() AS $column) {
|
||||
$visitor->acceptColunn($this, $column);
|
||||
}
|
||||
|
||||
foreach($this->getIndexes() AS $index) {
|
||||
foreach ($this->getIndexes() AS $index) {
|
||||
$visitor->acceptIndex($this, $index);
|
||||
}
|
||||
|
||||
foreach($this->getConstraints() AS $constraint) {
|
||||
if($constraint instanceof ForeignKeyConstraint) {
|
||||
foreach ($this->getConstraints() AS $constraint) {
|
||||
if ($constraint instanceof ForeignKeyConstraint) {
|
||||
$visitor->acceptForeignKey($this, $constraint);
|
||||
} else {
|
||||
$visitor->acceptCheckConstraint($this, $constraint);
|
||||
|
@ -21,8 +21,7 @@
|
||||
|
||||
namespace Doctrine\DBAL\Schema\Visitor;
|
||||
|
||||
use Doctrine\DBAL\Schema\Visitor,
|
||||
Doctrine\DBAL\Platforms\AbstractPlatform,
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform,
|
||||
Doctrine\DBAL\Schema\Table,
|
||||
Doctrine\DBAL\Schema\Schema,
|
||||
Doctrine\DBAL\Schema\Column,
|
||||
|
158
lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php
Normal file
158
lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php
Normal file
@ -0,0 +1,158 @@
|
||||
<?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\Schema\Visitor;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform,
|
||||
Doctrine\DBAL\Schema\Table,
|
||||
Doctrine\DBAL\Schema\Schema,
|
||||
Doctrine\DBAL\Schema\Column,
|
||||
Doctrine\DBAL\Schema\ForeignKeyConstraint,
|
||||
Doctrine\DBAL\Schema\Constraint,
|
||||
Doctrine\DBAL\Schema\Sequence,
|
||||
Doctrine\DBAL\Schema\Index;
|
||||
|
||||
/**
|
||||
* Gather SQL statements that allow to completly drop the current schema.
|
||||
*
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @link www.doctrine-project.org
|
||||
* @since 2.0
|
||||
* @version $Revision$
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
*/
|
||||
class DropSchemaSqlCollector implements Visitor
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_constraints = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_sequences = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_tables = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
|
||||
*/
|
||||
private $_platform = null;
|
||||
|
||||
/**
|
||||
* @param AbstractPlatform $platform
|
||||
*/
|
||||
public function __construct(AbstractPlatform $platform)
|
||||
{
|
||||
$this->_platform = $platform;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function acceptSchema(Schema $schema)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Table $table
|
||||
*/
|
||||
public function acceptTable(Table $table)
|
||||
{
|
||||
$this->_tables = array_merge($this->_tables, $this->_platform->getDropTableSql($table->getName()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Column $column
|
||||
*/
|
||||
public function acceptColunn(Table $table, Column $column)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Table $localTable
|
||||
* @param ForeignKeyConstraint $fkConstraint
|
||||
*/
|
||||
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
|
||||
{
|
||||
$this->_constraints = array_merge($this->_constraints,
|
||||
$this->_platform->getDropForeignKeySql(
|
||||
$localTable->getName(), $fkConstraint->getName()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Table $table
|
||||
* @param Constraint $constraint
|
||||
*/
|
||||
public function acceptCheckConstraint(Table $table, Constraint $constraint)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Table $table
|
||||
* @param Index $index
|
||||
*/
|
||||
public function acceptIndex(Table $table, Index $index)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Sequence $sequence
|
||||
*/
|
||||
public function acceptSequence(Sequence $sequence)
|
||||
{
|
||||
$this->_sequences = array_merge(
|
||||
$this->_sequences,
|
||||
$this->_platform->getDropSequenceSql($sequence->getName())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function clearQueries()
|
||||
{
|
||||
$this->_constraints = $this->_sequences = $this->_tables = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getQueries()
|
||||
{
|
||||
return array_merge(
|
||||
$this->_constraints,
|
||||
$this->_sequences,
|
||||
$this->_tables
|
||||
);
|
||||
}
|
||||
}
|
@ -19,7 +19,16 @@
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\DBAL\Schema;
|
||||
namespace Doctrine\DBAL\Schema\Visitor;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform,
|
||||
Doctrine\DBAL\Schema\Table,
|
||||
Doctrine\DBAL\Schema\Schema,
|
||||
Doctrine\DBAL\Schema\Column,
|
||||
Doctrine\DBAL\Schema\ForeignKeyConstraint,
|
||||
Doctrine\DBAL\Schema\Constraint,
|
||||
Doctrine\DBAL\Schema\Sequence,
|
||||
Doctrine\DBAL\Schema\Index;
|
||||
|
||||
/**
|
||||
* Schema Visitor used for Validation or Generation purposes.
|
@ -39,5 +39,8 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals("baz", $column->getDefault());
|
||||
|
||||
$this->assertEquals(array('foo' => 'bar'), $column->getPlatformOptions());
|
||||
$this->assertTrue($column->hasPlatformOption('foo'));
|
||||
$this->assertEquals('bar', $column->getPlatformOption('foo'));
|
||||
$this->assertFalse($column->hasPlatformOption('bar'));
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\DBAL\Schema\Visitor;
|
||||
|
||||
require_once __DIR__ . '/../../../TestInit.php';
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\Table;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
|
||||
class CreateSchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCreateSchema()
|
||||
{
|
||||
$platformMock = $this->getMock(
|
||||
'Doctrine\DBAL\Platforms\MySqlPlatform',
|
||||
array('getCreateTableSql', 'getCreateSequenceSql', 'getCreateForeignKeySql')
|
||||
);
|
||||
$platformMock->expects($this->exactly(2))
|
||||
->method('getCreateTableSql')
|
||||
->will($this->returnValue(array("foo" => "bar")));
|
||||
$platformMock->expects($this->exactly(1))
|
||||
->method('getCreateSequenceSql')
|
||||
->will($this->returnValue(array("bar" => "baz")));
|
||||
$platformMock->expects($this->exactly(1))
|
||||
->method('getCreateForeignKeySql')
|
||||
->will($this->returnValue(array("baz" => "foo")));
|
||||
|
||||
$schema = new Schema();
|
||||
$tableA = $schema->createTable("foo");
|
||||
$tableA->createColumn("id", 'integer');
|
||||
$tableA->createColumn("bar", 'string', array('length' => 255));
|
||||
$tableA->setPrimaryKey(array("id"));
|
||||
$tableA->setIdGeneratorType(Table::ID_SEQUENCE);
|
||||
|
||||
$schema->createSequence("foo_seq");
|
||||
|
||||
$tableB = $schema->createTable("bar");
|
||||
$tableB->createColumn("id", 'integer');
|
||||
$tableB->setPrimaryKey(array("id"));
|
||||
|
||||
$tableA->addForeignKeyConstraint($tableB, array("bar"), array("id"));
|
||||
|
||||
$sql = $schema->toSql($platformMock);
|
||||
|
||||
$this->assertEquals(array("foo" => "bar", "bar" => "baz", "baz" => "foo"), $sql);
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\DBAL\Schema\Visitor;
|
||||
|
||||
require_once __DIR__ . '/../../../TestInit.php';
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\Table;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
|
||||
class SchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCreateSchema()
|
||||
{
|
||||
$platformMock = $this->getMock(
|
||||
'Doctrine\DBAL\Platforms\MySqlPlatform',
|
||||
array('getCreateTableSql', 'getCreateSequenceSql', 'getCreateForeignKeySql')
|
||||
);
|
||||
$platformMock->expects($this->exactly(2))
|
||||
->method('getCreateTableSql')
|
||||
->will($this->returnValue(array("foo")));
|
||||
$platformMock->expects($this->exactly(1))
|
||||
->method('getCreateSequenceSql')
|
||||
->will($this->returnValue(array("bar")));
|
||||
$platformMock->expects($this->exactly(1))
|
||||
->method('getCreateForeignKeySql')
|
||||
->will($this->returnValue(array("baz")));
|
||||
|
||||
$schema = $this->createFixtureSchema();
|
||||
|
||||
$sql = $schema->toSql($platformMock);
|
||||
|
||||
$this->assertEquals(array("foo", "foo", "bar", "baz"), $sql);
|
||||
}
|
||||
|
||||
public function testDropSchema()
|
||||
{
|
||||
$platformMock = $this->getMock(
|
||||
'Doctrine\DBAL\Platforms\MySqlPlatform',
|
||||
array('getDropTableSql', 'getDropSequenceSql', 'getDropForeignKeySql')
|
||||
);
|
||||
$platformMock->expects($this->exactly(2))
|
||||
->method('getDropTableSql')
|
||||
->will($this->returnValue(array("tbl")));
|
||||
$platformMock->expects($this->exactly(1))
|
||||
->method('getDropSequenceSql')
|
||||
->will($this->returnValue(array("seq")));
|
||||
$platformMock->expects($this->exactly(1))
|
||||
->method('getDropForeignKeySql')
|
||||
->will($this->returnValue(array("fk")));
|
||||
|
||||
$schema = $this->createFixtureSchema();
|
||||
|
||||
$sql = $schema->toDropSql($platformMock);
|
||||
|
||||
$this->assertEquals(array("fk", "seq", "tbl", "tbl"), $sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Schema
|
||||
*/
|
||||
public function createFixtureSchema()
|
||||
{
|
||||
$schema = new Schema();
|
||||
$tableA = $schema->createTable("foo");
|
||||
$tableA->createColumn("id", 'integer');
|
||||
$tableA->createColumn("bar", 'string', array('length' => 255));
|
||||
$tableA->setPrimaryKey(array("id"));
|
||||
$tableA->setIdGeneratorType(Table::ID_SEQUENCE);
|
||||
|
||||
$schema->createSequence("foo_seq");
|
||||
|
||||
$tableB = $schema->createTable("bar");
|
||||
$tableB->createColumn("id", 'integer');
|
||||
$tableB->setPrimaryKey(array("id"));
|
||||
|
||||
$tableA->addForeignKeyConstraint($tableB, array("bar"), array("id"));
|
||||
|
||||
return $schema;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user