AbstractSchemaManager.php 28.9 KB
Newer Older
romanb's avatar
romanb committed
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
 * <http://www.doctrine-project.org>.
romanb's avatar
romanb committed
18 19
 */

20
namespace Doctrine\DBAL\Schema;
romanb's avatar
romanb committed
21

22 23
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs;
24
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
25 26 27
use Doctrine\DBAL\Types;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
28

romanb's avatar
romanb committed
29
/**
30
 * Base class for schema managers. Schema managers are used to inspect and/or
31
 * modify the database schema/structure.
romanb's avatar
romanb committed
32
 *
Benjamin Morel's avatar
Benjamin Morel committed
33 34 35 36 37 38
 * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
 * @author Roman Borschel <roman@code-factory.org>
 * @author Jonathan H. Wage <jonwage@gmail.com>
 * @author Benjamin Eberlei <kontakt@beberlei.de>
 * @since  2.0
romanb's avatar
romanb committed
39
 */
40
abstract class AbstractSchemaManager
romanb's avatar
romanb committed
41
{
42
    /**
Benjamin Morel's avatar
Benjamin Morel committed
43
     * Holds instance of the Doctrine connection for this schema manager.
44
     *
45
     * @var \Doctrine\DBAL\Connection
46
     */
romanb's avatar
romanb committed
47 48
    protected $_conn;

49
    /**
Benjamin Morel's avatar
Benjamin Morel committed
50
     * Holds instance of the database platform used for this schema manager.
51
     *
52
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
53 54 55 56
     */
    protected $_platform;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
57
     * Constructor. Accepts the Connection instance to manage the schema for.
58
     *
Benjamin Morel's avatar
Benjamin Morel committed
59 60
     * @param \Doctrine\DBAL\Connection                      $conn
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform|null $platform
61
     */
62
    public function __construct(\Doctrine\DBAL\Connection $conn, AbstractPlatform $platform = null)
63
    {
64 65
        $this->_conn     = $conn;
        $this->_platform = $platform ?: $this->_conn->getDatabasePlatform();
66 67
    }

68
    /**
Benjamin Morel's avatar
Benjamin Morel committed
69
     * Returns the associated platform.
70
     *
71
     * @return \Doctrine\DBAL\Platforms\AbstractPlatform
72 73 74 75 76 77
     */
    public function getDatabasePlatform()
    {
        return $this->_platform;
    }

78
    /**
Benjamin Morel's avatar
Benjamin Morel committed
79
     * Tries any method on the schema manager. Normally a method throws an
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
     * exception when your DBMS doesn't support it or if an error occurs.
     * This method allows you to try and method on your SchemaManager
     * instance and will return false if it does not work or is not supported.
     *
     * <code>
     * $result = $sm->tryMethod('dropView', 'view_name');
     * </code>
     *
     * @return mixed
     */
    public function tryMethod()
    {
        $args = func_get_args();
        $method = $args[0];
        unset($args[0]);
        $args = array_values($args);

        try {
            return call_user_func_array(array($this, $method), $args);
        } catch (\Exception $e) {
            return false;
        }
    }

romanb's avatar
romanb committed
104
    /**
Benjamin Morel's avatar
Benjamin Morel committed
105
     * Lists the available databases for this connection.
romanb's avatar
romanb committed
106
     *
Benjamin Morel's avatar
Benjamin Morel committed
107
     * @return array
romanb's avatar
romanb committed
108 109 110
     */
    public function listDatabases()
    {
111
        $sql = $this->_platform->getListDatabasesSQL();
112 113 114 115

        $databases = $this->_conn->fetchAll($sql);

        return $this->_getPortableDatabasesList($databases);
romanb's avatar
romanb committed
116 117 118
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
119 120 121
     * Lists the available sequences for this connection.
     *
     * @param string|null $database
romanb's avatar
romanb committed
122
     *
Benjamin Morel's avatar
Benjamin Morel committed
123
     * @return \Doctrine\DBAL\Schema\Sequence[]
romanb's avatar
romanb committed
124
     */
125
    public function listSequences($database = null)
romanb's avatar
romanb committed
126
    {
127 128 129
        if (is_null($database)) {
            $database = $this->_conn->getDatabase();
        }
130
        $sql = $this->_platform->getListSequencesSQL($database);
131 132 133

        $sequences = $this->_conn->fetchAll($sql);

134
        return $this->filterAssetNames($this->_getPortableSequencesList($sequences));
romanb's avatar
romanb committed
135 136 137
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
138
     * Lists the columns for a given table.
romanb's avatar
romanb committed
139
     *
140 141 142 143 144 145 146
     * In contrast to other libraries and to the old version of Doctrine,
     * this column definition does try to contain the 'primary' field for
     * the reason that it is not portable accross different RDBMS. Use
     * {@see listTableIndexes($tableName)} to retrieve the primary key
     * of a table. We're a RDBMS specifies more details these are held
     * in the platformDetails array.
     *
Benjamin Morel's avatar
Benjamin Morel committed
147 148 149 150
     * @param string      $table    The name of the table.
     * @param string|null $database
     *
     * @return \Doctrine\DBAL\Schema\Column[]
romanb's avatar
romanb committed
151
     */
152
    public function listTableColumns($table, $database = null)
romanb's avatar
romanb committed
153
    {
154
        if ( ! $database) {
155 156 157 158
            $database = $this->_conn->getDatabase();
        }

        $sql = $this->_platform->getListTableColumnsSQL($table, $database);
159 160 161

        $tableColumns = $this->_conn->fetchAll($sql);

162
        return $this->_getPortableTableColumnList($table, $database, $tableColumns);
romanb's avatar
romanb committed
163 164 165
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
166
     * Lists the indexes for a given table returning an array of Index instances.
romanb's avatar
romanb committed
167
     *
168 169
     * Keys of the portable indexes list are all lower-cased.
     *
Benjamin Morel's avatar
Benjamin Morel committed
170 171 172
     * @param string $table The name of the table.
     *
     * @return \Doctrine\DBAL\Schema\Index[]
romanb's avatar
romanb committed
173 174 175
     */
    public function listTableIndexes($table)
    {
176
        $sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());
177 178 179

        $tableIndexes = $this->_conn->fetchAll($sql);

180
        return $this->_getPortableTableIndexesList($tableIndexes, $table);
romanb's avatar
romanb committed
181 182
    }

183
    /**
Benjamin Morel's avatar
Benjamin Morel committed
184
     * Returns true if all the given tables exist.
185
     *
186
     * @param array $tableNames
Benjamin Morel's avatar
Benjamin Morel committed
187 188
     *
     * @return boolean
189 190 191 192
     */
    public function tablesExist($tableNames)
    {
        $tableNames = array_map('strtolower', (array)$tableNames);
Benjamin Morel's avatar
Benjamin Morel committed
193

194 195 196
        return count($tableNames) == count(\array_intersect($tableNames, array_map('strtolower', $this->listTableNames())));
    }

romanb's avatar
romanb committed
197
    /**
Benjamin Morel's avatar
Benjamin Morel committed
198
     * Returns a list of all tables in the current database.
romanb's avatar
romanb committed
199
     *
200
     * @return array
romanb's avatar
romanb committed
201
     */
202
    public function listTableNames()
romanb's avatar
romanb committed
203
    {
204
        $sql = $this->_platform->getListTablesSQL();
205 206

        $tables = $this->_conn->fetchAll($sql);
207
        $tableNames = $this->_getPortableTablesList($tables);
Benjamin Morel's avatar
Benjamin Morel committed
208

209 210
        return $this->filterAssetNames($tableNames);
    }
211

212
    /**
Benjamin Morel's avatar
Benjamin Morel committed
213
     * Filters asset names if they are configured to return only a subset of all
214 215 216
     * the found elements.
     *
     * @param array $assetNames
Benjamin Morel's avatar
Benjamin Morel committed
217
     *
218 219 220 221 222
     * @return array
     */
    protected function filterAssetNames($assetNames)
    {
        $filterExpr = $this->getFilterSchemaAssetsExpression();
223
        if ( ! $filterExpr) {
224 225
            return $assetNames;
        }
Benjamin Morel's avatar
Benjamin Morel committed
226

227 228 229
        return array_values (
            array_filter($assetNames, function ($assetName) use ($filterExpr) {
                $assetName = ($assetName instanceof AbstractAsset) ? $assetName->getName() : $assetName;
230
                return preg_match($filterExpr, $assetName);
231 232 233 234
            })
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
235 236 237
    /**
     * @return string|null
     */
238 239 240
    protected function getFilterSchemaAssetsExpression()
    {
        return $this->_conn->getConfiguration()->getFilterSchemaAssetsExpression();
241 242 243
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
244
     * Lists the tables for this connection.
245
     *
Benjamin Morel's avatar
Benjamin Morel committed
246
     * @return \Doctrine\DBAL\Schema\Table[]
247 248 249 250
     */
    public function listTables()
    {
        $tableNames = $this->listTableNames();
251

252
        $tables = array();
253
        foreach ($tableNames as $tableName) {
254
            $tables[] = $this->listTableDetails($tableName);
255 256 257
        }

        return $tables;
romanb's avatar
romanb committed
258 259
    }

260
    /**
Benjamin Morel's avatar
Benjamin Morel committed
261 262 263
     * @param string $tableName
     *
     * @return \Doctrine\DBAL\Schema\Table
264 265 266 267 268 269 270 271 272 273
     */
    public function listTableDetails($tableName)
    {
        $columns = $this->listTableColumns($tableName);
        $foreignKeys = array();
        if ($this->_platform->supportsForeignKeyConstraints()) {
            $foreignKeys = $this->listTableForeignKeys($tableName);
        }
        $indexes = $this->listTableIndexes($tableName);

274
        return new Table($tableName, $columns, $indexes, $foreignKeys, false, array());
275 276
    }

romanb's avatar
romanb committed
277
    /**
Benjamin Morel's avatar
Benjamin Morel committed
278
     * Lists the views this connection has.
romanb's avatar
romanb committed
279
     *
Benjamin Morel's avatar
Benjamin Morel committed
280
     * @return \Doctrine\DBAL\Schema\View[]
romanb's avatar
romanb committed
281
     */
282
    public function listViews()
romanb's avatar
romanb committed
283
    {
284
        $database = $this->_conn->getDatabase();
285
        $sql = $this->_platform->getListViewsSQL($database);
286 287 288
        $views = $this->_conn->fetchAll($sql);

        return $this->_getPortableViewsList($views);
romanb's avatar
romanb committed
289 290
    }

291
    /**
Benjamin Morel's avatar
Benjamin Morel committed
292 293 294 295
     * Lists the foreign keys for the given table.
     *
     * @param string      $table    The name of the table.
     * @param string|null $database
296
     *
Benjamin Morel's avatar
Benjamin Morel committed
297
     * @return \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
298 299 300 301 302 303
     */
    public function listTableForeignKeys($table, $database = null)
    {
        if (is_null($database)) {
            $database = $this->_conn->getDatabase();
        }
304
        $sql = $this->_platform->getListTableForeignKeysSQL($table, $database);
305 306 307 308 309
        $tableForeignKeys = $this->_conn->fetchAll($sql);

        return $this->_getPortableTableForeignKeysList($tableForeignKeys);
    }

310 311
    /* drop*() Methods */

romanb's avatar
romanb committed
312
    /**
313
     * Drops a database.
314
     *
315
     * NOTE: You can not drop the database this SchemaManager is currently connected to.
romanb's avatar
romanb committed
316
     *
Benjamin Morel's avatar
Benjamin Morel committed
317 318 319
     * @param string $database The name of the database to drop.
     *
     * @return void
romanb's avatar
romanb committed
320
     */
321
    public function dropDatabase($database)
romanb's avatar
romanb committed
322
    {
323
        $this->_execSql($this->_platform->getDropDatabaseSQL($database));
romanb's avatar
romanb committed
324 325 326
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
327 328
     * Drops the given table.
     *
jeroendedauw's avatar
jeroendedauw committed
329
     * @param string $tableName The name of the table to drop.
romanb's avatar
romanb committed
330
     *
Benjamin Morel's avatar
Benjamin Morel committed
331
     * @return void
romanb's avatar
romanb committed
332
     */
jeroendedauw's avatar
jeroendedauw committed
333
    public function dropTable($tableName)
romanb's avatar
romanb committed
334
    {
jeroendedauw's avatar
jeroendedauw committed
335
        $this->_execSql($this->_platform->getDropTableSQL($tableName));
romanb's avatar
romanb committed
336 337 338
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
339
     * Drops the index from the given table.
romanb's avatar
romanb committed
340
     *
Benjamin Morel's avatar
Benjamin Morel committed
341 342 343 344
     * @param \Doctrine\DBAL\Schema\Index|string $index The name of the index.
     * @param \Doctrine\DBAL\Schema\Table|string $table The name of the table.
     *
     * @return void
romanb's avatar
romanb committed
345
     */
346
    public function dropIndex($index, $table)
romanb's avatar
romanb committed
347
    {
Steve Müller's avatar
Steve Müller committed
348
        if ($index instanceof Index) {
349
            $index = $index->getQuotedName($this->_platform);
350 351
        }

352
        $this->_execSql($this->_platform->getDropIndexSQL($index, $table));
romanb's avatar
romanb committed
353 354 355
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
356 357 358 359
     * Drops the constraint from the given table.
     *
     * @param \Doctrine\DBAL\Schema\Constraint   $constraint
     * @param \Doctrine\DBAL\Schema\Table|string $table      The name of the table.
romanb's avatar
romanb committed
360
     *
Benjamin Morel's avatar
Benjamin Morel committed
361
     * @return void
romanb's avatar
romanb committed
362
     */
363
    public function dropConstraint(Constraint $constraint, $table)
romanb's avatar
romanb committed
364
    {
365
        $this->_execSql($this->_platform->getDropConstraintSQL($constraint, $table));
romanb's avatar
romanb committed
366 367 368
    }

    /**
romanb's avatar
romanb committed
369
     * Drops a foreign key from a table.
romanb's avatar
romanb committed
370
     *
Benjamin Morel's avatar
Benjamin Morel committed
371 372 373 374
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint|string $foreignKey The name of the foreign key.
     * @param \Doctrine\DBAL\Schema\Table|string                $table      The name of the table with the foreign key.
     *
     * @return void
romanb's avatar
romanb committed
375
     */
376
    public function dropForeignKey($foreignKey, $table)
romanb's avatar
romanb committed
377
    {
378
        $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table));
romanb's avatar
romanb committed
379 380 381
    }

    /**
romanb's avatar
romanb committed
382
     * Drops a sequence with a given name.
romanb's avatar
romanb committed
383
     *
romanb's avatar
romanb committed
384
     * @param string $name The name of the sequence to drop.
Benjamin Morel's avatar
Benjamin Morel committed
385 386
     *
     * @return void
romanb's avatar
romanb committed
387
     */
388
    public function dropSequence($name)
romanb's avatar
romanb committed
389
    {
390
        $this->_execSql($this->_platform->getDropSequenceSQL($name));
romanb's avatar
romanb committed
391 392
    }

393
    /**
Benjamin Morel's avatar
Benjamin Morel committed
394
     * Drops a view.
395
     *
Benjamin Morel's avatar
Benjamin Morel committed
396 397 398
     * @param string $name The name of the view.
     *
     * @return void
399 400 401
     */
    public function dropView($name)
    {
402
        $this->_execSql($this->_platform->getDropViewSQL($name));
403 404 405 406
    }

    /* create*() Methods */

romanb's avatar
romanb committed
407
    /**
romanb's avatar
romanb committed
408
     * Creates a new database.
romanb's avatar
romanb committed
409
     *
romanb's avatar
romanb committed
410
     * @param string $database The name of the database to create.
Benjamin Morel's avatar
Benjamin Morel committed
411 412
     *
     * @return void
romanb's avatar
romanb committed
413
     */
romanb's avatar
romanb committed
414
    public function createDatabase($database)
romanb's avatar
romanb committed
415
    {
416
        $this->_execSql($this->_platform->getCreateDatabaseSQL($database));
romanb's avatar
romanb committed
417 418 419
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
420
     * Creates a new table.
romanb's avatar
romanb committed
421
     *
Benjamin Morel's avatar
Benjamin Morel committed
422 423 424
     * @param \Doctrine\DBAL\Schema\Table $table
     *
     * @return void
romanb's avatar
romanb committed
425
     */
426
    public function createTable(Table $table)
romanb's avatar
romanb committed
427
    {
428
        $createFlags = AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS;
429
        $this->_execSql($this->_platform->getCreateTableSQL($table, $createFlags));
romanb's avatar
romanb committed
430 431 432
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
433 434 435
     * Creates a new sequence.
     *
     * @param \Doctrine\DBAL\Schema\Sequence $sequence
romanb's avatar
romanb committed
436
     *
Benjamin Morel's avatar
Benjamin Morel committed
437 438 439
     * @return void
     *
     * @throws \Doctrine\DBAL\ConnectionException If something fails at database level.
romanb's avatar
romanb committed
440
     */
441
    public function createSequence($sequence)
romanb's avatar
romanb committed
442
    {
443
        $this->_execSql($this->_platform->getCreateSequenceSQL($sequence));
romanb's avatar
romanb committed
444 445 446
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
447 448 449 450
     * Creates a constraint on a table.
     *
     * @param \Doctrine\DBAL\Schema\Constraint   $constraint
     * @param \Doctrine\DBAL\Schema\Table|string $table
romanb's avatar
romanb committed
451
     *
Benjamin Morel's avatar
Benjamin Morel committed
452
     * @return void
453 454
     */
    public function createConstraint(Constraint $constraint, $table)
romanb's avatar
romanb committed
455
    {
456
        $this->_execSql($this->_platform->getCreateConstraintSQL($constraint, $table));
romanb's avatar
romanb committed
457 458 459
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
460
     * Creates a new index on a table.
romanb's avatar
romanb committed
461
     *
Benjamin Morel's avatar
Benjamin Morel committed
462 463 464 465
     * @param \Doctrine\DBAL\Schema\Index        $index
     * @param \Doctrine\DBAL\Schema\Table|string $table The name of the table on which the index is to be created.
     *
     * @return void
romanb's avatar
romanb committed
466
     */
467
    public function createIndex(Index $index, $table)
romanb's avatar
romanb committed
468
    {
469
        $this->_execSql($this->_platform->getCreateIndexSQL($index, $table));
romanb's avatar
romanb committed
470 471 472
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
473 474 475 476
     * Creates a new foreign key.
     *
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey The ForeignKey instance.
     * @param \Doctrine\DBAL\Schema\Table|string         $table      The name of the table on which the foreign key is to be created.
romanb's avatar
romanb committed
477
     *
Benjamin Morel's avatar
Benjamin Morel committed
478
     * @return void
romanb's avatar
romanb committed
479
     */
480
    public function createForeignKey(ForeignKeyConstraint $foreignKey, $table)
romanb's avatar
romanb committed
481
    {
482
        $this->_execSql($this->_platform->getCreateForeignKeySQL($foreignKey, $table));
romanb's avatar
romanb committed
483 484
    }

485
    /**
Benjamin Morel's avatar
Benjamin Morel committed
486
     * Creates a new view.
487
     *
Benjamin Morel's avatar
Benjamin Morel committed
488 489 490
     * @param \Doctrine\DBAL\Schema\View $view
     *
     * @return void
491
     */
492
    public function createView(View $view)
493
    {
494
        $this->_execSql($this->_platform->getCreateViewSQL($view->getQuotedName($this->_platform), $view->getSql()));
495 496
    }

497 498
    /* dropAndCreate*() Methods */

499
    /**
Benjamin Morel's avatar
Benjamin Morel committed
500
     * Drops and creates a constraint.
501 502 503
     *
     * @see dropConstraint()
     * @see createConstraint()
Benjamin Morel's avatar
Benjamin Morel committed
504 505 506 507 508
     *
     * @param \Doctrine\DBAL\Schema\Constraint   $constraint
     * @param \Doctrine\DBAL\Schema\Table|string $table
     *
     * @return void
509
     */
510
    public function dropAndCreateConstraint(Constraint $constraint, $table)
511
    {
512 513
        $this->tryMethod('dropConstraint', $constraint, $table);
        $this->createConstraint($constraint, $table);
514 515 516
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
517 518 519 520
     * Drops and creates a new index on a table.
     *
     * @param \Doctrine\DBAL\Schema\Index        $index
     * @param \Doctrine\DBAL\Schema\Table|string $table The name of the table on which the index is to be created.
521
     *
Benjamin Morel's avatar
Benjamin Morel committed
522
     * @return void
523
     */
524
    public function dropAndCreateIndex(Index $index, $table)
525
    {
526
        $this->tryMethod('dropIndex', $index->getQuotedName($this->_platform), $table);
527
        $this->createIndex($index, $table);
528 529 530
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
531
     * Drops and creates a new foreign key.
532
     *
Benjamin Morel's avatar
Benjamin Morel committed
533 534 535 536
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey An associative array that defines properties of the foreign key to be created.
     * @param \Doctrine\DBAL\Schema\Table|string         $table      The name of the table on which the foreign key is to be created.
     *
     * @return void
537
     */
538
    public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table)
539
    {
540 541
        $this->tryMethod('dropForeignKey', $foreignKey, $table);
        $this->createForeignKey($foreignKey, $table);
542 543 544
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
545 546 547
     * Drops and create a new sequence.
     *
     * @param \Doctrine\DBAL\Schema\Sequence $sequence
548
     *
Benjamin Morel's avatar
Benjamin Morel committed
549 550 551
     * @return void
     *
     * @throws \Doctrine\DBAL\ConnectionException If something fails at database level.
552
     */
553
    public function dropAndCreateSequence(Sequence $sequence)
554
    {
Benjamin Eberlei's avatar
Benjamin Eberlei committed
555 556
        $this->tryMethod('dropSequence', $sequence->getQuotedName($this->_platform));
        $this->createSequence($sequence);
557 558 559
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
560 561 562
     * Drops and creates a new table.
     *
     * @param \Doctrine\DBAL\Schema\Table $table
563
     *
Benjamin Morel's avatar
Benjamin Morel committed
564
     * @return void
565
     */
566
    public function dropAndCreateTable(Table $table)
567
    {
568
        $this->tryMethod('dropTable', $table->getQuotedName($this->_platform));
569
        $this->createTable($table);
570 571 572
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
573
     * Drops and creates a new database.
574 575
     *
     * @param string $database The name of the database to create.
Benjamin Morel's avatar
Benjamin Morel committed
576 577
     *
     * @return void
578 579 580 581 582 583 584 585
     */
    public function dropAndCreateDatabase($database)
    {
        $this->tryMethod('dropDatabase', $database);
        $this->createDatabase($database);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
586 587 588
     * Drops and creates a new view.
     *
     * @param \Doctrine\DBAL\Schema\View $view
589
     *
Benjamin Morel's avatar
Benjamin Morel committed
590
     * @return void
591
     */
592
    public function dropAndCreateView(View $view)
593
    {
594
        $this->tryMethod('dropView', $view->getQuotedName($this->_platform));
595
        $this->createView($view);
596 597
    }

598 599
    /* alterTable() Methods */

romanb's avatar
romanb committed
600
    /**
Benjamin Morel's avatar
Benjamin Morel committed
601
     * Alters an existing tables schema.
romanb's avatar
romanb committed
602
     *
Benjamin Morel's avatar
Benjamin Morel committed
603 604 605
     * @param \Doctrine\DBAL\Schema\TableDiff $tableDiff
     *
     * @return void
606 607 608
     */
    public function alterTable(TableDiff $tableDiff)
    {
609
        $queries = $this->_platform->getAlterTableSQL($tableDiff);
610
        if (is_array($queries) && count($queries)) {
611
            foreach ($queries as $ddlQuery) {
612 613
                $this->_execSql($ddlQuery);
            }
614
        }
615 616
    }

617
    /**
Benjamin Morel's avatar
Benjamin Morel committed
618 619 620 621
     * Renames a given table to another name.
     *
     * @param string $name    The current name of the table.
     * @param string $newName The new name of the table.
622
     *
Benjamin Morel's avatar
Benjamin Morel committed
623
     * @return void
624 625 626
     */
    public function renameTable($name, $newName)
    {
627 628 629
        $tableDiff = new TableDiff($name);
        $tableDiff->newName = $newName;
        $this->alterTable($tableDiff);
630 631
    }

632 633 634 635 636
    /**
     * Methods for filtering return values of list*() methods to convert
     * the native DBMS data definition to a portable Doctrine definition
     */

Benjamin Morel's avatar
Benjamin Morel committed
637 638 639 640 641
    /**
     * @param array $databases
     *
     * @return array
     */
642 643
    protected function _getPortableDatabasesList($databases)
    {
644
        $list = array();
645
        foreach ($databases as $value) {
646 647 648
            if ($value = $this->_getPortableDatabaseDefinition($value)) {
                $list[] = $value;
            }
649
        }
Benjamin Morel's avatar
Benjamin Morel committed
650

651
        return $list;
652 653
    }

Benjamin Morel's avatar
Benjamin Morel committed
654 655 656 657 658
    /**
     * @param array $database
     *
     * @return mixed
     */
659 660 661 662 663
    protected function _getPortableDatabaseDefinition($database)
    {
        return $database;
    }

Benjamin Morel's avatar
Benjamin Morel committed
664 665 666 667 668
    /**
     * @param array $functions
     *
     * @return array
     */
669 670
    protected function _getPortableFunctionsList($functions)
    {
671
        $list = array();
672
        foreach ($functions as $value) {
673 674 675
            if ($value = $this->_getPortableFunctionDefinition($value)) {
                $list[] = $value;
            }
676
        }
Benjamin Morel's avatar
Benjamin Morel committed
677

678
        return $list;
679 680
    }

Benjamin Morel's avatar
Benjamin Morel committed
681 682 683 684 685
    /**
     * @param array $function
     *
     * @return mixed
     */
686 687 688 689 690
    protected function _getPortableFunctionDefinition($function)
    {
        return $function;
    }

Benjamin Morel's avatar
Benjamin Morel committed
691 692 693 694 695
    /**
     * @param array $triggers
     *
     * @return array
     */
696 697
    protected function _getPortableTriggersList($triggers)
    {
698
        $list = array();
699
        foreach ($triggers as $value) {
700 701 702
            if ($value = $this->_getPortableTriggerDefinition($value)) {
                $list[] = $value;
            }
703
        }
Benjamin Morel's avatar
Benjamin Morel committed
704

705
        return $list;
706 707
    }

Benjamin Morel's avatar
Benjamin Morel committed
708 709 710 711 712
    /**
     * @param array $trigger
     *
     * @return mixed
     */
713 714 715 716 717
    protected function _getPortableTriggerDefinition($trigger)
    {
        return $trigger;
    }

Benjamin Morel's avatar
Benjamin Morel committed
718 719 720 721 722
    /**
     * @param array $sequences
     *
     * @return array
     */
723 724
    protected function _getPortableSequencesList($sequences)
    {
725
        $list = array();
726
        foreach ($sequences as $value) {
727 728 729
            if ($value = $this->_getPortableSequenceDefinition($value)) {
                $list[] = $value;
            }
730
        }
Benjamin Morel's avatar
Benjamin Morel committed
731

732
        return $list;
733 734
    }

735 736
    /**
     * @param array $sequence
Benjamin Morel's avatar
Benjamin Morel committed
737 738 739 740
     *
     * @return \Doctrine\DBAL\Schema\Sequence
     *
     * @throws \Doctrine\DBAL\DBALException
741
     */
742 743
    protected function _getPortableSequenceDefinition($sequence)
    {
744
        throw DBALException::notSupported('Sequences');
745 746
    }

747 748 749 750 751
    /**
     * Independent of the database the keys of the column list result are lowercased.
     *
     * The name of the created column instance however is kept in its case.
     *
Benjamin Morel's avatar
Benjamin Morel committed
752 753 754 755
     * @param string $table        The name of the table.
     * @param string $database
     * @param array  $tableColumns
     *
756 757
     * @return array
     */
758
    protected function _getPortableTableColumnList($table, $database, $tableColumns)
759
    {
760 761
        $eventManager = $this->_platform->getEventManager();

762
        $list = array();
763
        foreach ($tableColumns as $tableColumn) {
764
            $column = null;
765 766 767
            $defaultPrevented = false;

            if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaColumnDefinition)) {
768
                $eventArgs = new SchemaColumnDefinitionEventArgs($tableColumn, $table, $database, $this->_conn);
769 770 771
                $eventManager->dispatchEvent(Events::onSchemaColumnDefinition, $eventArgs);

                $defaultPrevented = $eventArgs->isDefaultPrevented();
772
                $column = $eventArgs->getColumn();
773 774
            }

775
            if ( ! $defaultPrevented) {
776
                $column = $this->_getPortableTableColumnDefinition($tableColumn);
777 778
            }

779 780 781
            if ($column) {
                $name = strtolower($column->getQuotedName($this->_platform));
                $list[$name] = $column;
782
            }
783
        }
Benjamin Morel's avatar
Benjamin Morel committed
784

785
        return $list;
786 787
    }

788
    /**
Benjamin Morel's avatar
Benjamin Morel committed
789
     * Gets Table Column Definition.
790 791
     *
     * @param array $tableColumn
Benjamin Morel's avatar
Benjamin Morel committed
792 793
     *
     * @return \Doctrine\DBAL\Schema\Column
794 795
     */
    abstract protected function _getPortableTableColumnDefinition($tableColumn);
796

797
    /**
Benjamin Morel's avatar
Benjamin Morel committed
798 799 800 801
     * Aggregates and groups the index results according to the required data result.
     *
     * @param array       $tableIndexRows
     * @param string|null $tableName
802 803 804
     *
     * @return array
     */
805
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
806
    {
807
        $result = array();
Steve Müller's avatar
Steve Müller committed
808
        foreach ($tableIndexRows as $tableIndex) {
809
            $indexName = $keyName = $tableIndex['key_name'];
Steve Müller's avatar
Steve Müller committed
810
            if ($tableIndex['primary']) {
811 812
                $keyName = 'primary';
            }
813
            $keyName = strtolower($keyName);
814

Steve Müller's avatar
Steve Müller committed
815
            if (!isset($result[$keyName])) {
816 817 818 819 820
                $result[$keyName] = array(
                    'name' => $indexName,
                    'columns' => array($tableIndex['column_name']),
                    'unique' => $tableIndex['non_unique'] ? false : true,
                    'primary' => $tableIndex['primary'],
821
                    'flags' => isset($tableIndex['flags']) ? $tableIndex['flags'] : array(),
822 823 824
                );
            } else {
                $result[$keyName]['columns'][] = $tableIndex['column_name'];
825
            }
826
        }
827

828 829
        $eventManager = $this->_platform->getEventManager();

830
        $indexes = array();
Steve Müller's avatar
Steve Müller committed
831
        foreach ($result as $indexKey => $data) {
832 833 834 835 836 837 838 839 840 841 842
            $index = null;
            $defaultPrevented = false;

            if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
                $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
                $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);

                $defaultPrevented = $eventArgs->isDefaultPrevented();
                $index = $eventArgs->getIndex();
            }

843
            if ( ! $defaultPrevented) {
844
                $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary'], $data['flags']);
845 846 847 848 849
            }

            if ($index) {
                $indexes[$indexKey] = $index;
            }
850 851 852
        }

        return $indexes;
853 854
    }

Benjamin Morel's avatar
Benjamin Morel committed
855 856 857 858 859
    /**
     * @param array $tables
     *
     * @return array
     */
860 861
    protected function _getPortableTablesList($tables)
    {
862
        $list = array();
863
        foreach ($tables as $value) {
864 865 866
            if ($value = $this->_getPortableTableDefinition($value)) {
                $list[] = $value;
            }
867
        }
Benjamin Morel's avatar
Benjamin Morel committed
868

869
        return $list;
870 871
    }

Benjamin Morel's avatar
Benjamin Morel committed
872 873 874 875 876
    /**
     * @param array $table
     *
     * @return array
     */
877 878 879 880 881
    protected function _getPortableTableDefinition($table)
    {
        return $table;
    }

Benjamin Morel's avatar
Benjamin Morel committed
882 883 884 885 886
    /**
     * @param array $users
     *
     * @return array
     */
887 888
    protected function _getPortableUsersList($users)
    {
889
        $list = array();
890
        foreach ($users as $value) {
891 892 893
            if ($value = $this->_getPortableUserDefinition($value)) {
                $list[] = $value;
            }
894
        }
Benjamin Morel's avatar
Benjamin Morel committed
895

896
        return $list;
897 898
    }

Benjamin Morel's avatar
Benjamin Morel committed
899 900 901 902 903
    /**
     * @param array $user
     *
     * @return mixed
     */
904 905 906 907 908
    protected function _getPortableUserDefinition($user)
    {
        return $user;
    }

Benjamin Morel's avatar
Benjamin Morel committed
909 910 911 912
    /**
     * @param array $views
     * @return array
     */
913 914
    protected function _getPortableViewsList($views)
    {
915
        $list = array();
916
        foreach ($views as $value) {
917
            if ($view = $this->_getPortableViewDefinition($value)) {
918
                $viewName = strtolower($view->getQuotedName($this->_platform));
919
                $list[$viewName] = $view;
920
            }
921
        }
Benjamin Morel's avatar
Benjamin Morel committed
922

923
        return $list;
924 925
    }

Benjamin Morel's avatar
Benjamin Morel committed
926 927 928 929 930
    /**
     * @param array $view
     *
     * @return mixed
     */
931 932
    protected function _getPortableViewDefinition($view)
    {
933
        return false;
934 935
    }

Benjamin Morel's avatar
Benjamin Morel committed
936 937 938 939 940
    /**
     * @param array $tableForeignKeys
     *
     * @return array
     */
941 942 943
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
    {
        $list = array();
944
        foreach ($tableForeignKeys as $value) {
945 946 947 948
            if ($value = $this->_getPortableTableForeignKeyDefinition($value)) {
                $list[] = $value;
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
949

950 951 952
        return $list;
    }

Benjamin Morel's avatar
Benjamin Morel committed
953 954 955 956 957
    /**
     * @param array $tableForeignKey
     *
     * @return mixed
     */
958 959 960 961
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
    {
        return $tableForeignKey;
    }
962

Benjamin Morel's avatar
Benjamin Morel committed
963 964 965 966 967
    /**
     * @param array|string $sql
     *
     * @return void
     */
romanb's avatar
romanb committed
968
    protected function _execSql($sql)
969 970
    {
        foreach ((array) $sql as $query) {
971
            $this->_conn->executeUpdate($query);
romanb's avatar
romanb committed
972 973
        }
    }
974 975

    /**
Benjamin Morel's avatar
Benjamin Morel committed
976
     * Creates a schema instance for the current database.
977
     *
Benjamin Morel's avatar
Benjamin Morel committed
978
     * @return \Doctrine\DBAL\Schema\Schema
979 980 981 982
     */
    public function createSchema()
    {
        $sequences = array();
Steve Müller's avatar
Steve Müller committed
983
        if ($this->_platform->supportsSequences()) {
984 985 986 987
            $sequences = $this->listSequences();
        }
        $tables = $this->listTables();

988
        return new Schema($tables, $sequences, $this->createSchemaConfig());
989 990 991
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
992
     * Creates the configuration for this schema.
993
     *
Benjamin Morel's avatar
Benjamin Morel committed
994
     * @return \Doctrine\DBAL\Schema\SchemaConfig
995 996 997 998 999
     */
    public function createSchemaConfig()
    {
        $schemaConfig = new SchemaConfig();
        $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength());
1000 1001 1002 1003 1004

        $searchPaths = $this->getSchemaSearchPaths();
        if (isset($searchPaths[0])) {
            $schemaConfig->setName($searchPaths[0]);
        }
1005

1006 1007
        $params = $this->_conn->getParams();
        if (isset($params['defaultTableOptions'])) {
1008
            $schemaConfig->setDefaultTableOptions($params['defaultTableOptions']);
1009 1010
        }

1011
        return $schemaConfig;
1012
    }
1013

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
    /**
     * The search path for namespaces in the currently connected database.
     *
     * The first entry is usually the default namespace in the Schema. All
     * further namespaces contain tables/sequences which can also be addressed
     * with a short, not full-qualified name.
     *
     * For databases that don't support subschema/namespaces this method
     * returns the name of the currently connected database.
     *
     * @return array
     */
1026 1027 1028 1029 1030
    public function getSchemaSearchPaths()
    {
        return array($this->_conn->getDatabase());
    }

1031 1032 1033
    /**
     * Given a table comment this method tries to extract a typehint for Doctrine Type, or returns
     * the type given as default.
1034
     *
Benjamin Morel's avatar
Benjamin Morel committed
1035 1036 1037
     * @param string $comment
     * @param string $currentType
     *
1038 1039 1040 1041
     * @return string
     */
    public function extractDoctrineTypeFromComment($comment, $currentType)
    {
1042
        if (preg_match("(\(DC2Type:([a-zA-Z0-9_]+)\))", $comment, $match)) {
1043 1044
            $currentType = $match[1];
        }
Benjamin Morel's avatar
Benjamin Morel committed
1045

1046 1047 1048
        return $currentType;
    }

Benjamin Morel's avatar
Benjamin Morel committed
1049 1050 1051 1052 1053 1054
    /**
     * @param string $comment
     * @param string $type
     *
     * @return string
     */
1055 1056 1057 1058
    public function removeDoctrineTypeFromComment($comment, $type)
    {
        return str_replace('(DC2Type:'.$type.')', '', $comment);
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
1059
}