DropSchemaSqlCollector.php 2.52 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema\Visitor;

Benjamin Morel's avatar
Benjamin Morel committed
5 6 7
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\SchemaException;
8 9 10
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use SplObjectStorage;
Grégoire Paris's avatar
Grégoire Paris committed
11
use function assert;
12
use function strlen;
13 14

/**
Benjamin Morel's avatar
Benjamin Morel committed
15
 * Gathers SQL statements that allow to completely drop the current schema.
16
 */
17
class DropSchemaSqlCollector extends AbstractVisitor
18
{
19
    /** @var SplObjectStorage */
20 21
    private $constraints;

22
    /** @var SplObjectStorage */
23
    private $sequences;
24

25
    /** @var SplObjectStorage */
26
    private $tables;
27

28
    /** @var AbstractPlatform */
29
    private $platform;
30 31 32

    public function __construct(AbstractPlatform $platform)
    {
33
        $this->platform = $platform;
34
        $this->clearQueries();
35 36 37
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
38
     * {@inheritdoc}
39 40 41
     */
    public function acceptTable(Table $table)
    {
42
        $this->tables->attach($table);
43 44 45
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
46
     * {@inheritdoc}
47 48 49
     */
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
50 51 52 53
        if (! $this->platform->supportsCreateDropForeignKeyConstraints()) {
            return;
        }

54
        if (strlen($fkConstraint->getName()) === 0) {
55 56 57
            throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
        }

58
        $this->constraints->attach($fkConstraint, $localTable);
59 60 61
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
62
     * {@inheritdoc}
63 64 65
     */
    public function acceptSequence(Sequence $sequence)
    {
66
        $this->sequences->attach($sequence);
67 68 69
    }

    /**
70
     * @return void
71 72 73
     */
    public function clearQueries()
    {
74 75 76
        $this->constraints = new SplObjectStorage();
        $this->sequences   = new SplObjectStorage();
        $this->tables      = new SplObjectStorage();
77 78 79
    }

    /**
80
     * @return string[]
81 82 83
     */
    public function getQueries()
    {
84
        $sql = [];
85

86
        foreach ($this->constraints as $fkConstraint) {
Grégoire Paris's avatar
Grégoire Paris committed
87
            assert($fkConstraint instanceof ForeignKeyConstraint);
88
            $localTable = $this->constraints[$fkConstraint];
89
            $sql[]      = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable);
90 91
        }

92
        foreach ($this->sequences as $sequence) {
Grégoire Paris's avatar
Grégoire Paris committed
93
            assert($sequence instanceof Sequence);
94
            $sql[] = $this->platform->getDropSequenceSQL($sequence);
95
        }
96

97
        foreach ($this->tables as $table) {
Grégoire Paris's avatar
Grégoire Paris committed
98
            assert($table instanceof Table);
99
            $sql[] = $this->platform->getDropTableSQL($table);
100 101 102
        }

        return $sql;
103 104
    }
}