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;
11

Grégoire Paris's avatar
Grégoire Paris committed
12
use function assert;
13
use function strlen;
14 15

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return $sql;
104 105
    }
}