DropSchemaSqlCollector.php 3.37 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
use function strlen;
28 29

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return $sql;
126 127
    }
}