Schema.php 10.6 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 22
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Schema;

use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
23 24
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
use Doctrine\DBAL\Schema\Visitor\Visitor;
25 26 27 28

/**
 * Object representation of a database schema
 *
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 * Different vendors have very inconsistent naming with regard to the concept
 * of a "schema". Doctrine understands a schema as the entity that conceptually
 * wraps a set of database objects such as tables, sequences, indexes and
 * foreign keys that belong to each other into a namespace. A Doctrine Schema
 * has nothing to do with the "SCHEMA" defined as in PostgreSQL, it is more
 * related to the concept of "DATABASE" that exists in MySQL and PostgreSQL.
 *
 * Every asset in the doctrine schema has a name. A name consists of either a
 * namespace.local name pair or just a local unqualified name.
 *
 * The abstraction layer that covers a PostgreSQL schema is the namespace of an
 * database object (asset). A schema can have a name, which will be used as
 * default namespace for the unqualified database objects that are created in
 * the schema.
 *
 * In the case of MySQL where cross-database queries are allowed this leads to
 * databases being "misinterpreted" as namespaces. This is intentional, however
 * the CREATE/DROP SQL visitors will just filter this queries and do not
 * execute them. Only the queries for the currently connected database are
 * executed.
 *
50
 *
51 52 53 54 55 56 57 58 59 60
 * @link    www.doctrine-project.org
 * @since   2.0
 * @author  Benjamin Eberlei <kontakt@beberlei.de>
 */
class Schema extends AbstractAsset
{
    /**
     * @var array
     */
    protected $_tables = array();
61

62 63 64 65 66
    /**
     * @var array
     */
    protected $_sequences = array();

67
    /**
68
     * @var SchemaConfig
69
     */
70
    protected $_schemaConfig = false;
71

72 73 74
    /**
     * @param array $tables
     * @param array $sequences
75 76
     * @param array $views
     * @param array $triggers
77
     * @param SchemaConfig $schemaConfig
78
     */
79
    public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
80
    {
81 82 83 84
        if ($schemaConfig == null) {
            $schemaConfig = new SchemaConfig();
        }
        $this->_schemaConfig = $schemaConfig;
85
        $this->_setName($schemaConfig->getName() ?: 'public');
86

87
        foreach ($tables as $table) {
88 89
            $this->_addTable($table);
        }
90

91
        foreach ($sequences as $sequence) {
92 93
            $this->_addSequence($sequence);
        }
94 95 96 97 98 99 100
    }

    /**
     * @return bool
     */
    public function hasExplicitForeignKeyIndexes()
    {
101
        return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
102 103 104 105 106 107 108
    }

    /**
     * @param Table $table
     */
    protected function _addTable(Table $table)
    {
109
        $tableName = $table->getFullQualifiedName($this->getName());
110 111 112 113 114
        if(isset($this->_tables[$tableName])) {
            throw SchemaException::tableAlreadyExists($tableName);
        }

        $this->_tables[$tableName] = $table;
115
        $table->setSchemaConfig($this->_schemaConfig);
116 117 118 119 120 121 122
    }

    /**
     * @param Sequence $sequence
     */
    protected function _addSequence(Sequence $sequence)
    {
123
        $seqName = $sequence->getFullQualifiedName($this->getName());
124 125 126 127 128 129 130 131
        if (isset($this->_sequences[$seqName])) {
            throw SchemaException::sequenceAlreadyExists($seqName);
        }
        $this->_sequences[$seqName] = $sequence;
    }

    /**
     * Get all tables of this schema.
132
     *
133 134 135 136 137 138 139 140 141 142 143 144 145
     * @return array
     */
    public function getTables()
    {
        return $this->_tables;
    }

    /**
     * @param string $tableName
     * @return Table
     */
    public function getTable($tableName)
    {
146
        $tableName = $this->getFullQualifiedAssetName($tableName);
147 148 149 150 151 152 153
        if (!isset($this->_tables[$tableName])) {
            throw SchemaException::tableDoesNotExist($tableName);
        }

        return $this->_tables[$tableName];
    }

154 155 156 157 158
    /**
     * @return string
     */
    private function getFullQualifiedAssetName($name)
    {
159
        if ($this->isIdentifierQuoted($name)) {
160 161
            $name = $this->trimQuotes($name);
        }
162 163 164 165 166 167
        if (strpos($name, ".") === false) {
            $name = $this->getName() . "." . $name;
        }
        return strtolower($name);
    }

168 169
    /**
     * Does this schema have a table with the given name?
170
     *
171 172 173 174 175
     * @param  string $tableName
     * @return Schema
     */
    public function hasTable($tableName)
    {
176
        $tableName = $this->getFullQualifiedAssetName($tableName);
177 178 179
        return isset($this->_tables[$tableName]);
    }

180 181 182 183 184 185
    /**
     * Get all table names, prefixed with a schema name, even the default one
     * if present.
     *
     * @return array
     */
186
    public function getTableNames()
187
    {
188
        return array_keys($this->_tables);
189 190
    }

191 192
    public function hasSequence($sequenceName)
    {
193
        $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
194 195 196 197 198 199
        return isset($this->_sequences[$sequenceName]);
    }

    /**
     * @throws SchemaException
     * @param  string $sequenceName
200
     * @return \Doctrine\DBAL\Schema\Sequence
201 202 203
     */
    public function getSequence($sequenceName)
    {
204
        $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
205 206 207 208 209 210 211
        if(!$this->hasSequence($sequenceName)) {
            throw SchemaException::sequenceDoesNotExist($sequenceName);
        }
        return $this->_sequences[$sequenceName];
    }

    /**
212
     * @return \Doctrine\DBAL\Schema\Sequence[]
213 214 215 216 217 218 219 220
     */
    public function getSequences()
    {
        return $this->_sequences;
    }

    /**
     * Create a new table
221
     *
222 223 224 225 226 227 228
     * @param  string $tableName
     * @return Table
     */
    public function createTable($tableName)
    {
        $table = new Table($tableName);
        $this->_addTable($table);
229 230 231 232 233

        foreach ($this->_schemaConfig->getDefaultTableOptions() as $name => $value) {
            $table->addOption($name, $value);
        }

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        return $table;
    }

    /**
     * Rename a table
     *
     * @param string $oldTableName
     * @param string $newTableName
     * @return Schema
     */
    public function renameTable($oldTableName, $newTableName)
    {
        $table = $this->getTable($oldTableName);
        $table->_setName($newTableName);

        $this->dropTable($oldTableName);
        $this->_addTable($table);
        return $this;
    }

    /**
     * Drop a table from the schema.
     *
     * @param string $tableName
     * @return Schema
     */
    public function dropTable($tableName)
    {
262
        $tableName = $this->getFullQualifiedAssetName($tableName);
263 264 265 266 267 268 269
        $table = $this->getTable($tableName);
        unset($this->_tables[$tableName]);
        return $this;
    }

    /**
     * Create a new sequence
270
     *
271 272 273
     * @param  string $sequenceName
     * @param  int $allocationSize
     * @param  int $initialValue
274
     * @return Sequence
275 276 277 278 279
     */
    public function createSequence($sequenceName, $allocationSize=1, $initialValue=1)
    {
        $seq = new Sequence($sequenceName, $allocationSize, $initialValue);
        $this->_addSequence($seq);
280
        return $seq;
281 282 283 284 285 286 287 288
    }

    /**
     * @param string $sequenceName
     * @return Schema
     */
    public function dropSequence($sequenceName)
    {
289
        $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
290 291 292 293 294
        unset($this->_sequences[$sequenceName]);
        return $this;
    }

    /**
295 296
     * Return an array of necessary sql queries to create the schema on the given platform.
     *
297
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
298
     * @return array
299 300 301 302 303 304 305 306 307
     */
    public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
    {
        $sqlCollector = new CreateSchemaSqlCollector($platform);
        $this->visit($sqlCollector);

        return $sqlCollector->getQueries();
    }

308 309 310
    /**
     * Return an array of necessary sql queries to drop the schema on the given platform.
     *
311
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
312 313 314 315 316 317 318 319 320 321
     * @return array
     */
    public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
    {
        $dropSqlCollector = new DropSchemaSqlCollector($platform);
        $this->visit($dropSqlCollector);

        return $dropSqlCollector->getQueries();
    }

322
    /**
323
     * @param Schema $toSchema
324
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
325
     */
326
    public function getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
327
    {
328
        $comparator = new Comparator();
329 330
        $schemaDiff = $comparator->compare($this, $toSchema);
        return $schemaDiff->toSql($platform);
331 332
    }

333
    /**
334
     * @param Schema $fromSchema
335
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
336
     */
337
    public function getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
338
    {
339
        $comparator = new Comparator();
340 341
        $schemaDiff = $comparator->compare($fromSchema, $this);
        return $schemaDiff->toSql($platform);
342 343
    }

344 345 346 347 348 349
    /**
     * @param Visitor $visitor
     */
    public function visit(Visitor $visitor)
    {
        $visitor->acceptSchema($this);
350

351
        foreach ($this->_tables as $table) {
352 353
            $table->visit($visitor);
        }
354
        foreach ($this->_sequences as $sequence) {
355 356 357
            $sequence->visit($visitor);
        }
    }
358 359 360 361 362 363 364 365

    /**
     * Cloning a Schema triggers a deep clone of all related assets.
     *
     * @return void
     */
    public function __clone()
    {
366
        foreach ($this->_tables as $k => $table) {
367 368
            $this->_tables[$k] = clone $table;
        }
369
        foreach ($this->_sequences as $k => $sequence) {
370 371 372
            $this->_sequences[$k] = clone $sequence;
        }
    }
373
}