Commit 09025274 authored by beberlei's avatar beberlei

[2.0] - DDC-169 - Added DropSchemaSql Visitor - Refactored Visitor package a...

[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
parent 22cfa37f
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use \Doctrine\DBAL\Types\Type; use \Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\Visitor\Visitor;
/** /**
* Object representation of a database column * Object representation of a database column
......
<?php <?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; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Schema\Visitor\Visitor;
class ForeignKeyConstraint extends AbstractAsset implements Constraint class ForeignKeyConstraint extends AbstractAsset implements Constraint
{ {
/** /**
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Schema\Visitor\Visitor;
class Index extends AbstractAsset class Index extends AbstractAsset
{ {
/** /**
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector; use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
use Doctrine\DBAL\Schema\Visitor\Visitor;
/** /**
* Object representation of a database schema * Object representation of a database schema
...@@ -216,7 +218,10 @@ class Schema extends AbstractAsset ...@@ -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 * @param AbstractPlatform $platform
* @return array
*/ */
public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform) public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{ {
...@@ -226,6 +231,30 @@ class Schema extends AbstractAsset ...@@ -226,6 +231,30 @@ class Schema extends AbstractAsset
return $sqlCollector->getQueries(); 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 * @param Visitor $visitor
*/ */
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Schema\Visitor\Visitor;
/** /**
* Sequence Structure * Sequence Structure
* *
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\Visitor\Visitor;
/** /**
* Object Representation of a table * Object Representation of a table
...@@ -440,16 +441,16 @@ class Table extends AbstractAsset ...@@ -440,16 +441,16 @@ class Table extends AbstractAsset
{ {
$visitor->acceptTable($this); $visitor->acceptTable($this);
foreach($this->getColumns() AS $column) { foreach ($this->getColumns() AS $column) {
$visitor->acceptColunn($this, $column); $visitor->acceptColunn($this, $column);
} }
foreach($this->getIndexes() AS $index) { foreach ($this->getIndexes() AS $index) {
$visitor->acceptIndex($this, $index); $visitor->acceptIndex($this, $index);
} }
foreach($this->getConstraints() AS $constraint) { foreach ($this->getConstraints() AS $constraint) {
if($constraint instanceof ForeignKeyConstraint) { if ($constraint instanceof ForeignKeyConstraint) {
$visitor->acceptForeignKey($this, $constraint); $visitor->acceptForeignKey($this, $constraint);
} else { } else {
$visitor->acceptCheckConstraint($this, $constraint); $visitor->acceptCheckConstraint($this, $constraint);
......
...@@ -21,8 +21,7 @@ ...@@ -21,8 +21,7 @@
namespace Doctrine\DBAL\Schema\Visitor; namespace Doctrine\DBAL\Schema\Visitor;
use Doctrine\DBAL\Schema\Visitor, use Doctrine\DBAL\Platforms\AbstractPlatform,
Doctrine\DBAL\Platforms\AbstractPlatform,
Doctrine\DBAL\Schema\Table, Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Schema\Schema, Doctrine\DBAL\Schema\Schema,
Doctrine\DBAL\Schema\Column, Doctrine\DBAL\Schema\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\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 @@ ...@@ -19,7 +19,16 @@
* <http://www.doctrine-project.org>. * <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. * Schema Visitor used for Validation or Generation purposes.
......
...@@ -39,5 +39,8 @@ class ColumnTest extends \PHPUnit_Framework_TestCase ...@@ -39,5 +39,8 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
$this->assertEquals("baz", $column->getDefault()); $this->assertEquals("baz", $column->getDefault());
$this->assertEquals(array('foo' => 'bar'), $column->getPlatformOptions()); $this->assertEquals(array('foo' => 'bar'), $column->getPlatformOptions());
$this->assertTrue($column->hasPlatformOption('foo'));
$this->assertEquals('bar', $column->getPlatformOption('foo'));
$this->assertFalse($column->hasPlatformOption('bar'));
} }
} }
\ No newline at end of file
...@@ -8,7 +8,7 @@ use Doctrine\DBAL\Schema\Schema; ...@@ -8,7 +8,7 @@ use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
class CreateSchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase class SchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase
{ {
public function testCreateSchema() public function testCreateSchema()
{ {
...@@ -18,14 +18,49 @@ class CreateSchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase ...@@ -18,14 +18,49 @@ class CreateSchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase
); );
$platformMock->expects($this->exactly(2)) $platformMock->expects($this->exactly(2))
->method('getCreateTableSql') ->method('getCreateTableSql')
->will($this->returnValue(array("foo" => "bar"))); ->will($this->returnValue(array("foo")));
$platformMock->expects($this->exactly(1)) $platformMock->expects($this->exactly(1))
->method('getCreateSequenceSql') ->method('getCreateSequenceSql')
->will($this->returnValue(array("bar" => "baz"))); ->will($this->returnValue(array("bar")));
$platformMock->expects($this->exactly(1)) $platformMock->expects($this->exactly(1))
->method('getCreateForeignKeySql') ->method('getCreateForeignKeySql')
->will($this->returnValue(array("baz" => "foo"))); ->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(); $schema = new Schema();
$tableA = $schema->createTable("foo"); $tableA = $schema->createTable("foo");
$tableA->createColumn("id", 'integer'); $tableA->createColumn("id", 'integer');
...@@ -41,8 +76,6 @@ class CreateSchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase ...@@ -41,8 +76,6 @@ class CreateSchemaSqlCollectorTest extends \PHPUnit_Framework_TestCase
$tableA->addForeignKeyConstraint($tableB, array("bar"), array("id")); $tableA->addForeignKeyConstraint($tableB, array("bar"), array("id"));
$sql = $schema->toSql($platformMock); return $schema;
$this->assertEquals(array("foo" => "bar", "bar" => "baz", "baz" => "foo"), $sql);
} }
} }
\ No newline at end of file
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