CreateSchemaSqlCollector.php 3.36 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
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Sequence;
26

27
class CreateSchemaSqlCollector extends AbstractVisitor
28
{
29 30 31
    /**
     * @var array
     */
32
    private $createNamespaceQueries = [];
33

34 35 36
    /**
     * @var array
     */
37
    private $createTableQueries = [];
38 39 40 41

    /**
     * @var array
     */
42
    private $createSequenceQueries = [];
43 44 45 46

    /**
     * @var array
     */
47
    private $createFkConstraintQueries = [];
48 49 50 51 52

    /**
     *
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
     */
53
    private $platform = null;
54 55 56 57 58 59

    /**
     * @param AbstractPlatform $platform
     */
    public function __construct(AbstractPlatform $platform)
    {
60
        $this->platform = $platform;
61 62 63
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
64
     * {@inheritdoc}
65
     */
66
    public function acceptNamespace($namespaceName)
67
    {
68
        if ($this->platform->supportsSchemas()) {
69
            $this->createNamespaceQueries[] = $this->platform->getCreateSchemaSQL($namespaceName);
70 71
        }
    }
72

73 74 75 76 77 78
    /**
     * {@inheritdoc}
     */
    public function acceptTable(Table $table)
    {
        $this->createTableQueries = array_merge($this->createTableQueries, (array) $this->platform->getCreateTableSQL($table));
79 80 81
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
82
     * {@inheritdoc}
83 84 85
     */
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
86
        if ($this->platform->supportsForeignKeyConstraints()) {
87
            $this->createFkConstraintQueries[] = $this->platform->getCreateForeignKeySQL($fkConstraint, $localTable);
88 89 90 91
        }
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
92
     * {@inheritdoc}
93 94 95
     */
    public function acceptSequence(Sequence $sequence)
    {
96
        $this->createSequenceQueries[] = $this->platform->getCreateSequenceSQL($sequence);
97 98 99
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
100
     * @return void
101 102 103
     */
    public function resetQueries()
    {
104 105 106 107
        $this->createNamespaceQueries = [];
        $this->createTableQueries = [];
        $this->createSequenceQueries = [];
        $this->createFkConstraintQueries = [];
108 109 110
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
111
     * Gets all queries collected so far.
112 113 114 115 116
     *
     * @return array
     */
    public function getQueries()
    {
117 118 119 120 121 122
        return array_merge(
            $this->createNamespaceQueries,
            $this->createTableQueries,
            $this->createSequenceQueries,
            $this->createFkConstraintQueries
        );
123
    }
124
}