DropSchemaSqlCollector.php 3.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?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
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Schema\Visitor;

Benjamin Morel's avatar
Benjamin Morel committed
22 23 24 25 26
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\SchemaException;
27 28

/**
Benjamin Morel's avatar
Benjamin Morel committed
29
 * Gathers SQL statements that allow to completely drop the current schema.
30
 *
Benjamin Morel's avatar
Benjamin Morel committed
31 32 33
 * @link   www.doctrine-project.org
 * @since  2.0
 * @author Benjamin Eberlei <kontakt@beberlei.de>
34
 */
35
class DropSchemaSqlCollector extends AbstractVisitor
36 37
{
    /**
38
     * @var \SplObjectStorage
39
     */
40 41
    private $constraints;

42
    /**
43
     * @var \SplObjectStorage
44
     */
45
    private $sequences;
46 47

    /**
48
     * @var \SplObjectStorage
49
     */
50
    private $tables;
51 52

    /**
53
     * @var AbstractPlatform
54
     */
55
    private $platform;
56 57

    /**
58
     * @param AbstractPlatform $platform
59 60 61
     */
    public function __construct(AbstractPlatform $platform)
    {
62
        $this->platform = $platform;
63
        $this->clearQueries();
64 65 66
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
67
     * {@inheritdoc}
68 69 70
     */
    public function acceptTable(Table $table)
    {
71
        $this->tables->attach($table);
72 73 74
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
75
     * {@inheritdoc}
76 77 78
     */
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
79 80 81 82
        if (strlen($fkConstraint->getName()) == 0) {
            throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
        }

83
        $this->constraints->attach($fkConstraint, $localTable);
84 85 86
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
87
     * {@inheritdoc}
88 89 90
     */
    public function acceptSequence(Sequence $sequence)
    {
91
        $this->sequences->attach($sequence);
92 93 94
    }

    /**
95
     * @return void
96 97 98
     */
    public function clearQueries()
    {
99 100 101
        $this->constraints = new \SplObjectStorage();
        $this->sequences = new \SplObjectStorage();
        $this->tables = new \SplObjectStorage();
102 103 104 105 106 107 108
    }

    /**
     * @return array
     */
    public function getQueries()
    {
109
        $sql = array();
110

111
        foreach ($this->constraints as $fkConstraint) {
112
            $localTable = $this->constraints[$fkConstraint];
113
            $sql[] = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
114 115
        }

116
        foreach ($this->sequences as $sequence) {
117
            $sql[] = $this->platform->getDropSequenceSQL($sequence);
118
        }
119

120
        foreach ($this->tables as $table) {
121
            $sql[] = $this->platform->getDropTableSQL($table);
122 123 124
        }

        return $sql;
125 126
    }
}