CreateSchemaSqlCollector.php 4.64 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 $createTableQueries = array();
33 34 35 36

    /**
     * @var array
     */
37
    private $createSequenceQueries = array();
38 39 40 41

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

    /**
     *
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
     */
48
    private $platform = null;
49 50 51 52 53 54

    /**
     * @param AbstractPlatform $platform
     */
    public function __construct(AbstractPlatform $platform)
    {
55
        $this->platform = $platform;
56 57 58
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
59
     * {@inheritdoc}
60 61 62
     */
    public function acceptTable(Table $table)
    {
63 64
        $namespace = $this->getNamespace($table);

65 66 67
        $this->createTableQueries[$namespace] = array_merge(
            $this->createTableQueries[$namespace],
            $this->platform->getCreateTableSQL($table)
68 69 70 71
        );
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
72
     * {@inheritdoc}
73 74 75
     */
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
76 77
        $namespace = $this->getNamespace($localTable);

78 79 80 81
        if ($this->platform->supportsForeignKeyConstraints()) {
            $this->createFkConstraintQueries[$namespace] = array_merge(
                $this->createFkConstraintQueries[$namespace],
                (array) $this->platform->getCreateForeignKeySQL(
82
                    $fkConstraint, $localTable
83
                )
84 85 86 87 88
            );
        }
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
89
     * {@inheritdoc}
90 91 92
     */
    public function acceptSequence(Sequence $sequence)
    {
93 94
        $namespace = $this->getNamespace($sequence);

95 96 97
        $this->createSequenceQueries[$namespace] = array_merge(
            $this->createSequenceQueries[$namespace],
            (array)$this->platform->getCreateSequenceSQL($sequence)
98 99 100
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
101 102 103 104 105
    /**
     * @param \Doctrine\DBAL\Schema\AbstractAsset $asset
     *
     * @return string
     */
106 107
    private function getNamespace($asset)
    {
108 109
        $namespace = $asset->getNamespaceName();

110 111
        if ( !isset($namespace)) {
            $namespace = $this->platform->supportsSchemas() ? $this->platform->getDefaultSchemaName() : 'default';
112
        }
113

114 115 116 117
        if ( !isset($this->createTableQueries[$namespace])) {
            $this->createTableQueries[$namespace] = array();
            $this->createSequenceQueries[$namespace] = array();
            $this->createFkConstraintQueries[$namespace] = array();
118 119 120 121 122
        }

        return $namespace;
    }

123
    /**
Benjamin Morel's avatar
Benjamin Morel committed
124
     * @return void
125 126 127
     */
    public function resetQueries()
    {
128 129 130
        $this->createTableQueries = array();
        $this->createSequenceQueries = array();
        $this->createFkConstraintQueries = array();
131 132 133
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
134
     * Gets all queries collected so far.
135 136 137 138 139
     *
     * @return array
     */
    public function getQueries()
    {
140
        $sql = array();
141 142

        foreach (array_keys($this->createTableQueries) as $namespace) {
143 144
            if ($this->platform->supportsSchemas() && $this->platform->schemaNeedsCreation($namespace)) {
                $query = $this->platform->getCreateSchemaSQL($namespace);
145
                $sql[] = $query;
146
            }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
147
        }
148 149

        foreach ($this->createTableQueries as $schemaSql) {
Benjamin Eberlei's avatar
Benjamin Eberlei committed
150 151
            $sql = array_merge($sql, $schemaSql);
        }
152 153

        foreach ($this->createSequenceQueries as $schemaSql) {
Benjamin Eberlei's avatar
Benjamin Eberlei committed
154 155
            $sql = array_merge($sql, $schemaSql);
        }
156 157

        foreach ($this->createFkConstraintQueries as $schemaSql) {
Benjamin Eberlei's avatar
Benjamin Eberlei committed
158
            $sql = array_merge($sql, $schemaSql);
159
        }
160

161
        return $sql;
162
    }
163
}