diff --git a/lib/Doctrine/DBAL/Schema/Column.php b/lib/Doctrine/DBAL/Schema/Column.php index 9a5e0bb67..a1d187186 100644 --- a/lib/Doctrine/DBAL/Schema/Column.php +++ b/lib/Doctrine/DBAL/Schema/Column.php @@ -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 diff --git a/lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php b/lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php index e0745b820..fa1cc5384 100644 --- a/lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php +++ b/lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php @@ -1,7 +1,28 @@ . + */ namespace Doctrine\DBAL\Schema; +use Doctrine\DBAL\Schema\Visitor\Visitor; + class ForeignKeyConstraint extends AbstractAsset implements Constraint { /** diff --git a/lib/Doctrine/DBAL/Schema/Index.php b/lib/Doctrine/DBAL/Schema/Index.php index 7ffbbf13b..022896e96 100644 --- a/lib/Doctrine/DBAL/Schema/Index.php +++ b/lib/Doctrine/DBAL/Schema/Index.php @@ -21,6 +21,8 @@ namespace Doctrine\DBAL\Schema; +use Doctrine\DBAL\Schema\Visitor\Visitor; + class Index extends AbstractAsset { /** diff --git a/lib/Doctrine/DBAL/Schema/Schema.php b/lib/Doctrine/DBAL/Schema/Schema.php index 4a990462f..6e772b93e 100644 --- a/lib/Doctrine/DBAL/Schema/Schema.php +++ b/lib/Doctrine/DBAL/Schema/Schema.php @@ -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 */ diff --git a/lib/Doctrine/DBAL/Schema/Sequence.php b/lib/Doctrine/DBAL/Schema/Sequence.php index 494a175f1..782ed8c63 100644 --- a/lib/Doctrine/DBAL/Schema/Sequence.php +++ b/lib/Doctrine/DBAL/Schema/Sequence.php @@ -21,6 +21,8 @@ namespace Doctrine\DBAL\Schema; +use Doctrine\DBAL\Schema\Visitor\Visitor; + /** * Sequence Structure * diff --git a/lib/Doctrine/DBAL/Schema/Table.php b/lib/Doctrine/DBAL/Schema/Table.php index d0582ed4c..c4d8d8eba 100644 --- a/lib/Doctrine/DBAL/Schema/Table.php +++ b/lib/Doctrine/DBAL/Schema/Table.php @@ -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); diff --git a/lib/Doctrine/DBAL/Schema/Visitor/CreateSchemaSqlCollector.php b/lib/Doctrine/DBAL/Schema/Visitor/CreateSchemaSqlCollector.php index 4f7e2341c..cf9e4884d 100644 --- a/lib/Doctrine/DBAL/Schema/Visitor/CreateSchemaSqlCollector.php +++ b/lib/Doctrine/DBAL/Schema/Visitor/CreateSchemaSqlCollector.php @@ -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, diff --git a/lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php b/lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php new file mode 100644 index 000000000..2b1bbb4dd --- /dev/null +++ b/lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php @@ -0,0 +1,158 @@ +. + */ + +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 + */ +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 + ); + } +} diff --git a/lib/Doctrine/DBAL/Schema/Visitor.php b/lib/Doctrine/DBAL/Schema/Visitor/Visitor.php similarity index 86% rename from lib/Doctrine/DBAL/Schema/Visitor.php rename to lib/Doctrine/DBAL/Schema/Visitor/Visitor.php index fce413b6c..668b5fa5b 100644 --- a/lib/Doctrine/DBAL/Schema/Visitor.php +++ b/lib/Doctrine/DBAL/Schema/Visitor/Visitor.php @@ -19,7 +19,16 @@ * . */ -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. diff --git a/tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php b/tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php index 2616f3c89..fe748dc43 100644 --- a/tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php +++ b/tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php @@ -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')); } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Schema/Visitor/CreateSchemaSqlCollectorTest.php b/tests/Doctrine/Tests/DBAL/Schema/Visitor/CreateSchemaSqlCollectorTest.php deleted file mode 100644 index f555b4375..000000000 --- a/tests/Doctrine/Tests/DBAL/Schema/Visitor/CreateSchemaSqlCollectorTest.php +++ /dev/null @@ -1,48 +0,0 @@ -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); - } -} \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Schema/Visitor/SchemaSqlCollectorTest.php b/tests/Doctrine/Tests/DBAL/Schema/Visitor/SchemaSqlCollectorTest.php new file mode 100644 index 000000000..ac828fbb7 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Schema/Visitor/SchemaSqlCollectorTest.php @@ -0,0 +1,81 @@ +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; + } +} \ No newline at end of file