Commit 5eea9913 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Started refactoring Schema and SchemaDiff Visitors.

parent 65806f59
......@@ -24,12 +24,10 @@ use \Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Schema Diff
*
*
* @link www.doctrine-project.org
* @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License
* @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class SchemaDiff
......
<?php
/*
* 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 MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Schema\Visitor;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Constraint;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Index;
/**
* Abstract Visitor with empty methods for easy extension.
*/
class AbstractVisitor implements Visitor
{
/**
* @param Schema $schema
*/
public function acceptSchema(Schema $schema)
{
}
/**
* @param Table $table
*/
public function acceptTable(Table $table)
{
}
/**
* @param Column $column
*/
public function acceptColumn(Table $table, Column $column)
{
}
/**
* @param Table $localTable
* @param ForeignKeyConstraint $fkConstraint
*/
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
{
}
/**
* @param Table $table
* @param Index $index
*/
public function acceptIndex(Table $table, Index $index)
{
}
/**
* @param Sequence $sequence
*/
public function acceptSequence(Sequence $sequence)
{
}
}
<?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
......@@ -24,49 +22,39 @@ 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;
Doctrine\DBAL\Schema\Sequence;
class CreateSchemaSqlCollector implements Visitor
class CreateSchemaSqlCollector extends AbstractVisitor
{
/**
* @var array
*/
private $_createTableQueries = array();
private $createTableQueries = array();
/**
* @var array
*/
private $_createSequenceQueries = array();
private $createSequenceQueries = array();
/**
* @var array
*/
private $_createFkConstraintQueries = array();
private $createFkConstraintQueries = array();
/**
*
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
*/
private $_platform = null;
private $platform = null;
/**
* @param AbstractPlatform $platform
*/
public function __construct(AbstractPlatform $platform)
{
$this->_platform = $platform;
}
/**
* @param Schema $schema
*/
public function acceptSchema(Schema $schema)
{
$this->platform = $platform;
}
/**
......@@ -78,17 +66,12 @@ class CreateSchemaSqlCollector implements Visitor
{
$namespace = $this->getNamespace($table);
$this->_createTableQueries[$namespace] = array_merge(
$this->_createTableQueries[$namespace],
$this->_platform->getCreateTableSQL($table)
$this->createTableQueries[$namespace] = array_merge(
$this->createTableQueries[$namespace],
$this->platform->getCreateTableSQL($table)
);
}
public function acceptColumn(Table $table, Column $column)
{
}
/**
* @param Table $localTable
* @param ForeignKeyConstraint $fkConstraint
......@@ -97,25 +80,16 @@ class CreateSchemaSqlCollector implements Visitor
{
$namespace = $this->getNamespace($localTable);
if ($this->_platform->supportsForeignKeyConstraints()) {
$this->_createFkConstraintQueries[$namespace] = array_merge(
$this->_createFkConstraintQueries[$namespace],
(array) $this->_platform->getCreateForeignKeySQL(
if ($this->platform->supportsForeignKeyConstraints()) {
$this->createFkConstraintQueries[$namespace] = array_merge(
$this->createFkConstraintQueries[$namespace],
(array) $this->platform->getCreateForeignKeySQL(
$fkConstraint, $localTable
)
);
}
}
/**
* @param Table $table
* @param Index $index
*/
public function acceptIndex(Table $table, Index $index)
{
}
/**
* @param Sequence $sequence
*/
......@@ -123,19 +97,19 @@ class CreateSchemaSqlCollector implements Visitor
{
$namespace = $this->getNamespace($sequence);
$this->_createSequenceQueries[$namespace] = array_merge(
$this->_createSequenceQueries[$namespace],
(array)$this->_platform->getCreateSequenceSQL($sequence)
$this->createSequenceQueries[$namespace] = array_merge(
$this->createSequenceQueries[$namespace],
(array)$this->platform->getCreateSequenceSQL($sequence)
);
}
private function getNamespace($asset)
{
$namespace = $asset->getNamespaceName() ?: 'default';
if ( !isset($this->_createTableQueries[$namespace])) {
$this->_createTableQueries[$namespace] = array();
$this->_createSequenceQueries[$namespace] = array();
$this->_createFkConstraintQueries[$namespace] = array();
if ( !isset($this->createTableQueries[$namespace])) {
$this->createTableQueries[$namespace] = array();
$this->createSequenceQueries[$namespace] = array();
$this->createFkConstraintQueries[$namespace] = array();
}
return $namespace;
......@@ -146,9 +120,9 @@ class CreateSchemaSqlCollector implements Visitor
*/
public function resetQueries()
{
$this->_createTableQueries = array();
$this->_createSequenceQueries = array();
$this->_createFkConstraintQueries = array();
$this->createTableQueries = array();
$this->createSequenceQueries = array();
$this->createFkConstraintQueries = array();
}
/**
......@@ -159,20 +133,25 @@ class CreateSchemaSqlCollector implements Visitor
public function getQueries()
{
$sql = array();
foreach (array_keys($this->_createTableQueries) as $namespace) {
if ($this->_platform->supportsSchemas()) {
foreach (array_keys($this->createTableQueries) as $namespace) {
if ($this->platform->supportsSchemas()) {
// TODO: Create Schema here
}
}
foreach ($this->_createTableQueries as $schemaSql) {
foreach ($this->createTableQueries as $schemaSql) {
$sql = array_merge($sql, $schemaSql);
}
foreach ($this->_createSequenceQueries as $schemaSql) {
foreach ($this->createSequenceQueries as $schemaSql) {
$sql = array_merge($sql, $schemaSql);
}
foreach ($this->_createFkConstraintQueries as $schemaSql) {
foreach ($this->createFkConstraintQueries as $schemaSql) {
$sql = array_merge($sql, $schemaSql);
}
return $sql;
}
}
<?php
/*
* 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 MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
<?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
......@@ -24,7 +22,6 @@ 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,
......@@ -34,12 +31,11 @@ use Doctrine\DBAL\Platforms\AbstractPlatform,
/**
* Gather SQL statements that allow to completely drop the current schema.
*
*
* @link www.doctrine-project.org
* @since 2.0
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class DropSchemaSqlCollector implements Visitor
class DropSchemaSqlCollector extends AbstractVisitor
{
/**
* @var \SplObjectStorage
......@@ -71,14 +67,6 @@ class DropSchemaSqlCollector implements Visitor
$this->clearQueries();
}
/**
* @param Schema $schema
*/
public function acceptSchema(Schema $schema)
{
}
/**
* @param Table $table
*/
......@@ -87,14 +75,6 @@ class DropSchemaSqlCollector implements Visitor
$this->tables->attach($table);
}
/**
* @param Column $column
*/
public function acceptColumn(Table $table, Column $column)
{
}
/**
* @param Table $localTable
* @param ForeignKeyConstraint $fkConstraint
......@@ -109,15 +89,6 @@ class DropSchemaSqlCollector implements Visitor
$this->constraints[$fkConstraint] = $localTable;
}
/**
* @param Table $table
* @param Index $index
*/
public function acceptIndex(Table $table, Index $index)
{
}
/**
* @param Sequence $sequence
*/
......@@ -142,6 +113,7 @@ class DropSchemaSqlCollector implements Visitor
public function getQueries()
{
$sql = array();
foreach ($this->constraints as $fkConstraint) {
$localTable = $this->constraints[$fkConstraint];
$sql[] = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
......
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
......@@ -23,21 +22,19 @@ 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;
Doctrine\DBAL\Schema\Constraint;
class Graphviz implements \Doctrine\DBAL\Schema\Visitor\Visitor
/**
* Create a Graphviz output of a Schema.
*/
class Graphviz extends AbstractVisitor
{
/**
* @var string
*/
private $output = '';
public function acceptColumn(Table $table, Column $column)
{
}
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
{
$this->output .= $this->createNodeRelation(
......@@ -51,11 +48,6 @@ class Graphviz implements \Doctrine\DBAL\Schema\Visitor\Visitor
);
}
public function acceptIndex(Table $table, Index $index)
{
}
public function acceptSchema(Schema $schema)
{
$this->output = 'digraph "' . sha1( mt_rand() ) . '" {' . "\n";
......@@ -66,11 +58,6 @@ class Graphviz implements \Doctrine\DBAL\Schema\Visitor\Visitor
$this->output .= 'sep = .2;' . "\n";
}
public function acceptSequence(Sequence $sequence)
{
}
public function acceptTable(Table $table)
{
$this->output .= $this->createNode(
......@@ -133,6 +120,16 @@ class Graphviz implements \Doctrine\DBAL\Schema\Visitor\Visitor
return $relation;
}
/**
* Get Graphviz Output
*
* @return string
*/
public function getOutput()
{
return $this->output . "}";
}
/**
* Write dot language output to a file. This should usually be a *.dot file.
*
......@@ -146,6 +143,6 @@ class Graphviz implements \Doctrine\DBAL\Schema\Visitor\Visitor
*/
public function write($filename)
{
file_put_contents($filename, $this->output . "}");
file_put_contents($filename, $this->getOutput());
}
}
......@@ -22,11 +22,9 @@ 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;
Doctrine\DBAL\Schema\Sequence;
/**
* Remove assets from a schema that are not in the default namespace.
......@@ -42,7 +40,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform,
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @since 2.2
*/
class RemoveNamespacedAssets implements Visitor
class RemoveNamespacedAssets extends AbstractVisitor
{
/**
* @var Schema
......@@ -76,13 +74,6 @@ class RemoveNamespacedAssets implements Visitor
}
}
/**
* @param Column $column
*/
public function acceptColumn(Table $table, Column $column)
{
}
/**
* @param Table $localTable
* @param ForeignKeyConstraint $fkConstraint
......@@ -102,12 +93,4 @@ class RemoveNamespacedAssets implements Visitor
$localTable->removeForeignKey($fkConstraint->getName());
}
}
/**
* @param Table $table
* @param Index $index
*/
public function acceptIndex(Table $table, Index $index)
{
}
}
<?php
/*
* 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 MIT license. 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\TableDiff,
Doctrine\DBAL\Schema\Schema,
Doctrine\DBAL\Schema\ForeignKeyConstraint,
Doctrine\DBAL\Schema\Constraint,
Doctrine\DBAL\Schema\Sequence,
Doctrine\DBAL\Schema\SchemaException,
Doctrine\DBAL\Schema\Index;
/**
* Visit a SchemaDiff.
*
* @link www.doctrine-project.org
* @since 2.4
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
interface SchemaDiffVisitor
{
/**
* Visit an orphaned foreign key whose table was deleted.
*
* @param ForeignKeyConstraint $foreignKey
*/
function visitOrphanedForeignKey(ForeignKeyConstraint $foreignKey);
/**
* Visit a sequence that has changed.
*
* @param Sequence $sequence
*/
function visitChangedSequence(Sequence $sequence);
/**
* Visit a sequence that has been removed.
*
* @param Sequence $sequence
*/
function visitRemovedSequence(Sequence $sequence);
function visitNewSequence(Sequence $sequence);
function visitNewTable(Table $table);
function visitNewTableForeignKey(Table $table, ForeignKeyConstraint $foreignKey);
function visitRemovedTable(Table $table);
function visitChangedTable(TableDiff $tableDiff);
}
......@@ -21,14 +21,14 @@
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;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Constraint;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Index;
/**
* Schema Visitor used for Validation or Generation purposes.
......
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