CreateSchemaSqlCollector.php 4.07 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 32 33
    /**
     * @var array
     */
    private $createNamespaceQueries = array();

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

    /**
     * @var array
     */
42
    private $createSequenceQueries = array();
43 44 45 46

    /**
     * @var array
     */
47
    private $createFkConstraintQueries = array();
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 69 70 71 72 73 74
        if ($this->platform->supportsSchemas()) {
            $this->createNamespaceQueries = array_merge(
                $this->createNamespaceQueries,
                (array) $this->platform->getCreateSchemaSQL($namespaceName)
            );
        }
    }
75

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
85
     * {@inheritdoc}
86 87 88
     */
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
89
        if ($this->platform->supportsForeignKeyConstraints()) {
90 91
            $this->createFkConstraintQueries = array_merge(
                $this->createFkConstraintQueries,
92
                (array) $this->platform->getCreateForeignKeySQL(
93
                    $fkConstraint, $localTable
94
                )
95 96 97 98 99
            );
        }
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
100
     * {@inheritdoc}
101 102 103
     */
    public function acceptSequence(Sequence $sequence)
    {
104 105
        $this->createSequenceQueries = array_merge(
            $this->createSequenceQueries,
106
            (array) $this->platform->getCreateSequenceSQL($sequence)
107 108 109 110
        );
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
111
     * @return void
112 113 114
     */
    public function resetQueries()
    {
115
        $this->createNamespaceQueries = array();
116 117 118
        $this->createTableQueries = array();
        $this->createSequenceQueries = array();
        $this->createFkConstraintQueries = array();
119 120 121
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
122
     * Gets all queries collected so far.
123 124 125 126 127
     *
     * @return array
     */
    public function getQueries()
    {
128
        $sql = array();
129

130 131
        foreach ($this->createNamespaceQueries as $schemaSql) {
            $sql = array_merge($sql, (array) $schemaSql);
Benjamin Eberlei's avatar
Benjamin Eberlei committed
132
        }
133 134

        foreach ($this->createTableQueries as $schemaSql) {
135
            $sql = array_merge($sql, (array) $schemaSql);
Benjamin Eberlei's avatar
Benjamin Eberlei committed
136
        }
137 138

        foreach ($this->createSequenceQueries as $schemaSql) {
139
            $sql = array_merge($sql, (array) $schemaSql);
Benjamin Eberlei's avatar
Benjamin Eberlei committed
140
        }
141 142

        foreach ($this->createFkConstraintQueries as $schemaSql) {
143
            $sql = array_merge($sql, (array) $schemaSql);
144
        }
145

146
        return $sql;
147
    }
148
}