CreateSchemaSqlCollector.php 2.35 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\Sequence;
8
use Doctrine\DBAL\Schema\Table;
9
use function array_merge;
10

11
class CreateSchemaSqlCollector extends AbstractVisitor
12
{
13
    /** @var string[] */
14
    private $createNamespaceQueries = [];
15

16
    /** @var string[] */
17
    private $createTableQueries = [];
18

19
    /** @var string[] */
20
    private $createSequenceQueries = [];
21

22
    /** @var string[] */
23
    private $createFkConstraintQueries = [];
24

25
    /** @var AbstractPlatform */
26
    private $platform = null;
27 28 29

    public function __construct(AbstractPlatform $platform)
    {
30
        $this->platform = $platform;
31 32 33
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
34
     * {@inheritdoc}
35
     */
36
    public function acceptNamespace($namespaceName)
37
    {
38 39
        if (! $this->platform->supportsSchemas()) {
            return;
40
        }
41 42

        $this->createNamespaceQueries[] = $this->platform->getCreateSchemaSQL($namespaceName);
43
    }
44

45 46 47 48 49 50
    /**
     * {@inheritdoc}
     */
    public function acceptTable(Table $table)
    {
        $this->createTableQueries = array_merge($this->createTableQueries, (array) $this->platform->getCreateTableSQL($table));
51 52 53
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
54
     * {@inheritdoc}
55 56 57
     */
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
58 59
        if (! $this->platform->supportsForeignKeyConstraints()) {
            return;
60
        }
61 62

        $this->createFkConstraintQueries[] = $this->platform->getCreateForeignKeySQL($fkConstraint, $localTable);
63 64 65
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
66
     * {@inheritdoc}
67 68 69
     */
    public function acceptSequence(Sequence $sequence)
    {
70
        $this->createSequenceQueries[] = $this->platform->getCreateSequenceSQL($sequence);
71 72 73
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
74
     * @return void
75 76 77
     */
    public function resetQueries()
    {
78 79 80
        $this->createNamespaceQueries    = [];
        $this->createTableQueries        = [];
        $this->createSequenceQueries     = [];
81
        $this->createFkConstraintQueries = [];
82 83 84
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
85
     * Gets all queries collected so far.
86
     *
87
     * @return string[]
88 89 90
     */
    public function getQueries()
    {
91 92 93 94 95 96
        return array_merge(
            $this->createNamespaceQueries,
            $this->createTableQueries,
            $this->createSequenceQueries,
            $this->createFkConstraintQueries
        );
97
    }
98
}