AbstractSchemaManager.php 29 KB
Newer Older
romanb's avatar
romanb committed
1 2
<?php

3
namespace Doctrine\DBAL\Schema;
romanb's avatar
romanb committed
4

5 6 7
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs;
9
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
10
use Doctrine\DBAL\Events;
11
use Doctrine\DBAL\Platforms\AbstractPlatform;
12
use Throwable;
13
use function array_filter;
14
use function array_intersect;
15 16
use function array_map;
use function array_values;
Sergei Morozov's avatar
Sergei Morozov committed
17
use function assert;
18 19 20 21
use function call_user_func_array;
use function count;
use function func_get_args;
use function is_array;
Sergei Morozov's avatar
Sergei Morozov committed
22
use function is_callable;
23 24 25
use function preg_match;
use function str_replace;
use function strtolower;
26

romanb's avatar
romanb committed
27
/**
28
 * Base class for schema managers. Schema managers are used to inspect and/or
29
 * modify the database schema/structure.
romanb's avatar
romanb committed
30
 */
31
abstract class AbstractSchemaManager
romanb's avatar
romanb committed
32
{
33
    /**
Benjamin Morel's avatar
Benjamin Morel committed
34
     * Holds instance of the Doctrine connection for this schema manager.
35
     *
36
     * @var Connection
37
     */
romanb's avatar
romanb committed
38 39
    protected $_conn;

40
    /**
Benjamin Morel's avatar
Benjamin Morel committed
41
     * Holds instance of the database platform used for this schema manager.
42
     *
43
     * @var AbstractPlatform
44 45 46 47
     */
    protected $_platform;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
48
     * Constructor. Accepts the Connection instance to manage the schema for.
49
     */
50
    public function __construct(Connection $conn, ?AbstractPlatform $platform = null)
51
    {
52 53
        $this->_conn     = $conn;
        $this->_platform = $platform ?: $this->_conn->getDatabasePlatform();
54 55
    }

56
    /**
Benjamin Morel's avatar
Benjamin Morel committed
57
     * Returns the associated platform.
58
     *
59
     * @return AbstractPlatform
60 61 62 63 64 65
     */
    public function getDatabasePlatform()
    {
        return $this->_platform;
    }

66
    /**
Benjamin Morel's avatar
Benjamin Morel committed
67
     * Tries any method on the schema manager. Normally a method throws an
68 69 70 71 72 73 74 75 76 77 78 79
     * 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()
    {
80
        $args   = func_get_args();
81 82 83 84
        $method = $args[0];
        unset($args[0]);
        $args = array_values($args);

Sergei Morozov's avatar
Sergei Morozov committed
85 86 87
        $callback = [$this, $method];
        assert(is_callable($callback));

88
        try {
Sergei Morozov's avatar
Sergei Morozov committed
89
            return call_user_func_array($callback, $args);
90
        } catch (Throwable $e) {
91 92 93 94
            return false;
        }
    }

romanb's avatar
romanb committed
95
    /**
Benjamin Morel's avatar
Benjamin Morel committed
96
     * Lists the available databases for this connection.
romanb's avatar
romanb committed
97
     *
98
     * @return string[]
romanb's avatar
romanb committed
99 100 101
     */
    public function listDatabases()
    {
102
        $sql = $this->_platform->getListDatabasesSQL();
103 104 105 106

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

        return $this->_getPortableDatabasesList($databases);
romanb's avatar
romanb committed
107 108
    }

109 110 111
    /**
     * Returns a list of all namespaces in the current database.
     *
112
     * @return string[]
113 114 115 116 117 118 119 120 121 122
     */
    public function listNamespaceNames()
    {
        $sql = $this->_platform->getListNamespacesSQL();

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

        return $this->getPortableNamespacesList($namespaces);
    }

romanb's avatar
romanb committed
123
    /**
Benjamin Morel's avatar
Benjamin Morel committed
124 125 126
     * Lists the available sequences for this connection.
     *
     * @param string|null $database
romanb's avatar
romanb committed
127
     *
128
     * @return Sequence[]
romanb's avatar
romanb committed
129
     */
130
    public function listSequences($database = null)
romanb's avatar
romanb committed
131
    {
132
        if ($database === null) {
133 134
            $database = $this->_conn->getDatabase();
        }
Grégoire Paris's avatar
Grégoire Paris committed
135

136
        $sql = $this->_platform->getListSequencesSQL($database);
137 138 139

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

140
        return $this->filterAssetNames($this->_getPortableSequencesList($sequences));
romanb's avatar
romanb committed
141 142 143
    }

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

        $sql = $this->_platform->getListTableColumnsSQL($table, $database);
165 166 167

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

168
        return $this->_getPortableTableColumnList($table, $database, $tableColumns);
romanb's avatar
romanb committed
169 170 171
    }

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

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

186
        return $this->_getPortableTableIndexesList($tableIndexes, $table);
romanb's avatar
romanb committed
187 188
    }

189
    /**
Benjamin Morel's avatar
Benjamin Morel committed
190
     * Returns true if all the given tables exist.
191
     *
192 193
     * The usage of a string $tableNames is deprecated. Pass a one-element array instead.
     *
Sergei Morozov's avatar
Sergei Morozov committed
194
     * @param string|string[] $tableNames
Benjamin Morel's avatar
Benjamin Morel committed
195
     *
196
     * @return bool
197 198 199
     */
    public function tablesExist($tableNames)
    {
200
        $tableNames = array_map('strtolower', (array) $tableNames);
Benjamin Morel's avatar
Benjamin Morel committed
201

202
        return count($tableNames) === count(array_intersect($tableNames, array_map('strtolower', $this->listTableNames())));
203 204
    }

romanb's avatar
romanb committed
205
    /**
Benjamin Morel's avatar
Benjamin Morel committed
206
     * Returns a list of all tables in the current database.
romanb's avatar
romanb committed
207
     *
208
     * @return string[]
romanb's avatar
romanb committed
209
     */
210
    public function listTableNames()
romanb's avatar
romanb committed
211
    {
212
        $sql = $this->_platform->getListTablesSQL();
213

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

217 218
        return $this->filterAssetNames($tableNames);
    }
219

220
    /**
Benjamin Morel's avatar
Benjamin Morel committed
221
     * Filters asset names if they are configured to return only a subset of all
222 223
     * the found elements.
     *
224
     * @param mixed[] $assetNames
Benjamin Morel's avatar
Benjamin Morel committed
225
     *
226
     * @return mixed[]
227 228 229
     */
    protected function filterAssetNames($assetNames)
    {
230 231
        $filter = $this->_conn->getConfiguration()->getSchemaAssetsFilter();
        if (! $filter) {
232 233
            return $assetNames;
        }
Benjamin Morel's avatar
Benjamin Morel committed
234

235
        return array_values(array_filter($assetNames, $filter));
236 237
    }

Benjamin Morel's avatar
Benjamin Morel committed
238
    /**
239 240
     * @deprecated Use Configuration::getSchemaAssetsFilter() instead
     *
Benjamin Morel's avatar
Benjamin Morel committed
241 242
     * @return string|null
     */
243 244 245
    protected function getFilterSchemaAssetsExpression()
    {
        return $this->_conn->getConfiguration()->getFilterSchemaAssetsExpression();
246 247 248
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
249
     * Lists the tables for this connection.
250
     *
251
     * @return Table[]
252 253 254 255
     */
    public function listTables()
    {
        $tableNames = $this->listTableNames();
256

257
        $tables = [];
258
        foreach ($tableNames as $tableName) {
259
            $tables[] = $this->listTableDetails($tableName);
260 261 262
        }

        return $tables;
romanb's avatar
romanb committed
263 264
    }

265
    /**
Benjamin Morel's avatar
Benjamin Morel committed
266 267
     * @param string $tableName
     *
268
     * @return Table
269 270 271
     */
    public function listTableDetails($tableName)
    {
272
        $columns     = $this->listTableColumns($tableName);
273
        $foreignKeys = [];
274 275 276
        if ($this->_platform->supportsForeignKeyConstraints()) {
            $foreignKeys = $this->listTableForeignKeys($tableName);
        }
Grégoire Paris's avatar
Grégoire Paris committed
277

278 279
        $indexes = $this->listTableIndexes($tableName);

Sergei Morozov's avatar
Sergei Morozov committed
280
        return new Table($tableName, $columns, $indexes, $foreignKeys);
281 282
    }

romanb's avatar
romanb committed
283
    /**
Benjamin Morel's avatar
Benjamin Morel committed
284
     * Lists the views this connection has.
romanb's avatar
romanb committed
285
     *
286
     * @return View[]
romanb's avatar
romanb committed
287
     */
288
    public function listViews()
romanb's avatar
romanb committed
289
    {
290
        $database = $this->_conn->getDatabase();
291 292
        $sql      = $this->_platform->getListViewsSQL($database);
        $views    = $this->_conn->fetchAll($sql);
293 294

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

297
    /**
Benjamin Morel's avatar
Benjamin Morel committed
298 299 300 301
     * Lists the foreign keys for the given table.
     *
     * @param string      $table    The name of the table.
     * @param string|null $database
302
     *
303
     * @return ForeignKeyConstraint[]
304 305 306
     */
    public function listTableForeignKeys($table, $database = null)
    {
307
        if ($database === null) {
308 309
            $database = $this->_conn->getDatabase();
        }
Grégoire Paris's avatar
Grégoire Paris committed
310

311
        $sql              = $this->_platform->getListTableForeignKeysSQL($table, $database);
312 313 314 315 316
        $tableForeignKeys = $this->_conn->fetchAll($sql);

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

317 318
    /* drop*() Methods */

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
334 335
     * Drops the given table.
     *
jeroendedauw's avatar
jeroendedauw committed
336
     * @param string $tableName The name of the table to drop.
romanb's avatar
romanb committed
337
     *
Benjamin Morel's avatar
Benjamin Morel committed
338
     * @return void
romanb's avatar
romanb committed
339
     */
jeroendedauw's avatar
jeroendedauw committed
340
    public function dropTable($tableName)
romanb's avatar
romanb committed
341
    {
jeroendedauw's avatar
jeroendedauw committed
342
        $this->_execSql($this->_platform->getDropTableSQL($tableName));
romanb's avatar
romanb committed
343 344 345
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
346
     * Drops the index from the given table.
romanb's avatar
romanb committed
347
     *
348 349
     * @param Index|string $index The name of the index.
     * @param Table|string $table The name of the table.
Benjamin Morel's avatar
Benjamin Morel committed
350 351
     *
     * @return void
romanb's avatar
romanb committed
352
     */
353
    public function dropIndex($index, $table)
romanb's avatar
romanb committed
354
    {
Steve Müller's avatar
Steve Müller committed
355
        if ($index instanceof Index) {
356
            $index = $index->getQuotedName($this->_platform);
357 358
        }

359
        $this->_execSql($this->_platform->getDropIndexSQL($index, $table));
romanb's avatar
romanb committed
360 361 362
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
363 364
     * Drops the constraint from the given table.
     *
365
     * @param Table|string $table The name of the table.
romanb's avatar
romanb committed
366
     *
Benjamin Morel's avatar
Benjamin Morel committed
367
     * @return void
romanb's avatar
romanb committed
368
     */
369
    public function dropConstraint(Constraint $constraint, $table)
romanb's avatar
romanb committed
370
    {
371
        $this->_execSql($this->_platform->getDropConstraintSQL($constraint, $table));
romanb's avatar
romanb committed
372 373 374
    }

    /**
romanb's avatar
romanb committed
375
     * Drops a foreign key from a table.
romanb's avatar
romanb committed
376
     *
377 378
     * @param ForeignKeyConstraint|string $foreignKey The name of the foreign key.
     * @param Table|string                $table      The name of the table with the foreign key.
Benjamin Morel's avatar
Benjamin Morel committed
379 380
     *
     * @return void
romanb's avatar
romanb committed
381
     */
382
    public function dropForeignKey($foreignKey, $table)
romanb's avatar
romanb committed
383
    {
384
        $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table));
romanb's avatar
romanb committed
385 386 387
    }

    /**
romanb's avatar
romanb committed
388
     * Drops a sequence with a given name.
romanb's avatar
romanb committed
389
     *
romanb's avatar
romanb committed
390
     * @param string $name The name of the sequence to drop.
Benjamin Morel's avatar
Benjamin Morel committed
391 392
     *
     * @return void
romanb's avatar
romanb committed
393
     */
394
    public function dropSequence($name)
romanb's avatar
romanb committed
395
    {
396
        $this->_execSql($this->_platform->getDropSequenceSQL($name));
romanb's avatar
romanb committed
397 398
    }

399
    /**
Benjamin Morel's avatar
Benjamin Morel committed
400
     * Drops a view.
401
     *
Benjamin Morel's avatar
Benjamin Morel committed
402 403 404
     * @param string $name The name of the view.
     *
     * @return void
405 406 407
     */
    public function dropView($name)
    {
408
        $this->_execSql($this->_platform->getDropViewSQL($name));
409 410 411 412
    }

    /* create*() Methods */

romanb's avatar
romanb committed
413
    /**
romanb's avatar
romanb committed
414
     * Creates a new database.
romanb's avatar
romanb committed
415
     *
romanb's avatar
romanb committed
416
     * @param string $database The name of the database to create.
Benjamin Morel's avatar
Benjamin Morel committed
417 418
     *
     * @return void
romanb's avatar
romanb committed
419
     */
romanb's avatar
romanb committed
420
    public function createDatabase($database)
romanb's avatar
romanb committed
421
    {
422
        $this->_execSql($this->_platform->getCreateDatabaseSQL($database));
romanb's avatar
romanb committed
423 424 425
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
426
     * Creates a new table.
romanb's avatar
romanb committed
427
     *
Benjamin Morel's avatar
Benjamin Morel committed
428
     * @return void
romanb's avatar
romanb committed
429
     */
430
    public function createTable(Table $table)
romanb's avatar
romanb committed
431
    {
432
        $createFlags = AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS;
433
        $this->_execSql($this->_platform->getCreateTableSQL($table, $createFlags));
romanb's avatar
romanb committed
434 435 436
    }

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

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

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

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

487
    /**
Benjamin Morel's avatar
Benjamin Morel committed
488
     * Creates a new view.
489
     *
Benjamin Morel's avatar
Benjamin Morel committed
490
     * @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
     * @param Table|string $table
Benjamin Morel's avatar
Benjamin Morel committed
506 507
     *
     * @return void
508
     */
509
    public function dropAndCreateConstraint(Constraint $constraint, $table)
510
    {
511 512
        $this->tryMethod('dropConstraint', $constraint, $table);
        $this->createConstraint($constraint, $table);
513 514 515
    }

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

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
556 557 558
     * Drops and creates a new table.
     *
     * @return void
559
     */
560
    public function dropAndCreateTable(Table $table)
561
    {
562
        $this->tryMethod('dropTable', $table->getQuotedName($this->_platform));
563
        $this->createTable($table);
564 565 566
    }

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
580 581 582
     * Drops and creates a new view.
     *
     * @return void
583
     */
584
    public function dropAndCreateView(View $view)
585
    {
586
        $this->tryMethod('dropView', $view->getQuotedName($this->_platform));
587
        $this->createView($view);
588 589
    }

590 591
    /* alterTable() Methods */

romanb's avatar
romanb committed
592
    /**
Benjamin Morel's avatar
Benjamin Morel committed
593
     * Alters an existing tables schema.
romanb's avatar
romanb committed
594
     *
Benjamin Morel's avatar
Benjamin Morel committed
595
     * @return void
596 597 598
     */
    public function alterTable(TableDiff $tableDiff)
    {
599
        $queries = $this->_platform->getAlterTableSQL($tableDiff);
600 601 602 603 604 605
        if (! is_array($queries) || ! count($queries)) {
            return;
        }

        foreach ($queries as $ddlQuery) {
            $this->_execSql($ddlQuery);
606
        }
607 608
    }

609
    /**
Benjamin Morel's avatar
Benjamin Morel committed
610 611 612 613
     * 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.
614
     *
Benjamin Morel's avatar
Benjamin Morel committed
615
     * @return void
616 617 618
     */
    public function renameTable($name, $newName)
    {
619
        $tableDiff          = new TableDiff($name);
620 621
        $tableDiff->newName = $newName;
        $this->alterTable($tableDiff);
622 623
    }

624 625 626 627 628
    /**
     * 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
629
    /**
630
     * @param mixed[] $databases
Benjamin Morel's avatar
Benjamin Morel committed
631
     *
632
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
633
     */
634 635
    protected function _getPortableDatabasesList($databases)
    {
636
        $list = [];
637
        foreach ($databases as $value) {
638 639 640
            $value = $this->_getPortableDatabaseDefinition($value);

            if (! $value) {
641
                continue;
642
            }
643 644

            $list[] = $value;
645
        }
Benjamin Morel's avatar
Benjamin Morel committed
646

647
        return $list;
648 649
    }

650 651 652
    /**
     * Converts a list of namespace names from the native DBMS data definition to a portable Doctrine definition.
     *
653
     * @param mixed[][] $namespaces The list of namespace names in the native DBMS data definition.
654
     *
655
     * @return string[]
656 657 658
     */
    protected function getPortableNamespacesList(array $namespaces)
    {
659
        $namespacesList = [];
660 661 662 663 664 665 666 667

        foreach ($namespaces as $namespace) {
            $namespacesList[] = $this->getPortableNamespaceDefinition($namespace);
        }

        return $namespacesList;
    }

Benjamin Morel's avatar
Benjamin Morel committed
668
    /**
669
     * @param mixed $database
Benjamin Morel's avatar
Benjamin Morel committed
670 671 672
     *
     * @return mixed
     */
673 674 675 676 677
    protected function _getPortableDatabaseDefinition($database)
    {
        return $database;
    }

678 679 680
    /**
     * Converts a namespace definition from the native DBMS data definition to a portable Doctrine definition.
     *
681
     * @param mixed[] $namespace The native DBMS namespace definition.
682 683 684 685 686 687 688 689
     *
     * @return mixed
     */
    protected function getPortableNamespaceDefinition(array $namespace)
    {
        return $namespace;
    }

Benjamin Morel's avatar
Benjamin Morel committed
690
    /**
691 692
     * @deprecated
     *
693
     * @param mixed[][] $functions
Benjamin Morel's avatar
Benjamin Morel committed
694
     *
695
     * @return mixed[][]
Benjamin Morel's avatar
Benjamin Morel committed
696
     */
697 698
    protected function _getPortableFunctionsList($functions)
    {
699
        $list = [];
700
        foreach ($functions as $value) {
701 702 703
            $value = $this->_getPortableFunctionDefinition($value);

            if (! $value) {
704
                continue;
705
            }
706 707

            $list[] = $value;
708
        }
Benjamin Morel's avatar
Benjamin Morel committed
709

710
        return $list;
711 712
    }

Benjamin Morel's avatar
Benjamin Morel committed
713
    /**
714 715
     * @deprecated
     *
716
     * @param mixed[] $function
Benjamin Morel's avatar
Benjamin Morel committed
717 718 719
     *
     * @return mixed
     */
720 721 722 723 724
    protected function _getPortableFunctionDefinition($function)
    {
        return $function;
    }

Benjamin Morel's avatar
Benjamin Morel committed
725
    /**
726
     * @param mixed[][] $triggers
Benjamin Morel's avatar
Benjamin Morel committed
727
     *
728
     * @return mixed[][]
Benjamin Morel's avatar
Benjamin Morel committed
729
     */
730 731
    protected function _getPortableTriggersList($triggers)
    {
732
        $list = [];
733
        foreach ($triggers as $value) {
734 735 736
            $value = $this->_getPortableTriggerDefinition($value);

            if (! $value) {
737
                continue;
738
            }
739 740

            $list[] = $value;
741
        }
Benjamin Morel's avatar
Benjamin Morel committed
742

743
        return $list;
744 745
    }

Benjamin Morel's avatar
Benjamin Morel committed
746
    /**
747
     * @param mixed[] $trigger
Benjamin Morel's avatar
Benjamin Morel committed
748 749 750
     *
     * @return mixed
     */
751 752 753 754 755
    protected function _getPortableTriggerDefinition($trigger)
    {
        return $trigger;
    }

Benjamin Morel's avatar
Benjamin Morel committed
756
    /**
757
     * @param mixed[][] $sequences
Benjamin Morel's avatar
Benjamin Morel committed
758
     *
759
     * @return Sequence[]
Benjamin Morel's avatar
Benjamin Morel committed
760
     */
761 762
    protected function _getPortableSequencesList($sequences)
    {
763
        $list = [];
764

Sergei Morozov's avatar
Sergei Morozov committed
765 766
        foreach ($sequences as $value) {
            $list[] = $this->_getPortableSequenceDefinition($value);
767
        }
Benjamin Morel's avatar
Benjamin Morel committed
768

769
        return $list;
770 771
    }

772
    /**
773
     * @param mixed[] $sequence
Benjamin Morel's avatar
Benjamin Morel committed
774
     *
775
     * @return Sequence
Benjamin Morel's avatar
Benjamin Morel committed
776
     *
777
     * @throws DBALException
778
     */
779 780
    protected function _getPortableSequenceDefinition($sequence)
    {
781
        throw DBALException::notSupported('Sequences');
782 783
    }

784 785 786 787 788
    /**
     * 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.
     *
789 790 791
     * @param string    $table        The name of the table.
     * @param string    $database
     * @param mixed[][] $tableColumns
Benjamin Morel's avatar
Benjamin Morel committed
792
     *
793
     * @return Column[]
794
     */
795
    protected function _getPortableTableColumnList($table, $database, $tableColumns)
796
    {
797 798
        $eventManager = $this->_platform->getEventManager();

799
        $list = [];
800
        foreach ($tableColumns as $tableColumn) {
801
            $column           = null;
802 803
            $defaultPrevented = false;

804
            if ($eventManager !== null && $eventManager->hasListeners(Events::onSchemaColumnDefinition)) {
805
                $eventArgs = new SchemaColumnDefinitionEventArgs($tableColumn, $table, $database, $this->_conn);
806 807 808
                $eventManager->dispatchEvent(Events::onSchemaColumnDefinition, $eventArgs);

                $defaultPrevented = $eventArgs->isDefaultPrevented();
809
                $column           = $eventArgs->getColumn();
810 811
            }

812
            if (! $defaultPrevented) {
813
                $column = $this->_getPortableTableColumnDefinition($tableColumn);
814 815
            }

816 817
            if (! $column) {
                continue;
818
            }
819 820 821

            $name        = strtolower($column->getQuotedName($this->_platform));
            $list[$name] = $column;
822
        }
Benjamin Morel's avatar
Benjamin Morel committed
823

824
        return $list;
825 826
    }

827
    /**
Benjamin Morel's avatar
Benjamin Morel committed
828
     * Gets Table Column Definition.
829
     *
830
     * @param mixed[] $tableColumn
Benjamin Morel's avatar
Benjamin Morel committed
831
     *
832
     * @return Column
833 834
     */
    abstract protected function _getPortableTableColumnDefinition($tableColumn);
835

836
    /**
Benjamin Morel's avatar
Benjamin Morel committed
837 838
     * Aggregates and groups the index results according to the required data result.
     *
839
     * @param mixed[][]   $tableIndexRows
Benjamin Morel's avatar
Benjamin Morel committed
840
     * @param string|null $tableName
841
     *
842
     * @return Index[]
843
     */
844
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
845
    {
846
        $result = [];
Steve Müller's avatar
Steve Müller committed
847
        foreach ($tableIndexRows as $tableIndex) {
848
            $indexName = $keyName = $tableIndex['key_name'];
Steve Müller's avatar
Steve Müller committed
849
            if ($tableIndex['primary']) {
850 851
                $keyName = 'primary';
            }
Grégoire Paris's avatar
Grégoire Paris committed
852

853
            $keyName = strtolower($keyName);
854

855
            if (! isset($result[$keyName])) {
856 857 858 859 860 861 862 863
                $options = [
                    'lengths' => [],
                ];

                if (isset($tableIndex['where'])) {
                    $options['where'] = $tableIndex['where'];
                }

864
                $result[$keyName] = [
865
                    'name' => $indexName,
866
                    'columns' => [],
867
                    'unique' => ! $tableIndex['non_unique'],
868
                    'primary' => $tableIndex['primary'],
869
                    'flags' => $tableIndex['flags'] ?? [],
870
                    'options' => $options,
871
                ];
872
            }
873 874 875

            $result[$keyName]['columns'][]            = $tableIndex['column_name'];
            $result[$keyName]['options']['lengths'][] = $tableIndex['length'] ?? null;
876
        }
877

878 879
        $eventManager = $this->_platform->getEventManager();

880
        $indexes = [];
Steve Müller's avatar
Steve Müller committed
881
        foreach ($result as $indexKey => $data) {
882
            $index            = null;
883 884
            $defaultPrevented = false;

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

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

893
            if (! $defaultPrevented) {
894
                $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary'], $data['flags'], $data['options']);
895 896
            }

897 898
            if (! $index) {
                continue;
899
            }
900 901

            $indexes[$indexKey] = $index;
902 903 904
        }

        return $indexes;
905 906
    }

Benjamin Morel's avatar
Benjamin Morel committed
907
    /**
908
     * @param mixed[][] $tables
Benjamin Morel's avatar
Benjamin Morel committed
909
     *
910
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
911
     */
912 913
    protected function _getPortableTablesList($tables)
    {
914
        $list = [];
915
        foreach ($tables as $value) {
916 917 918
            $value = $this->_getPortableTableDefinition($value);

            if (! $value) {
919
                continue;
920
            }
921 922

            $list[] = $value;
923
        }
Benjamin Morel's avatar
Benjamin Morel committed
924

925
        return $list;
926 927
    }

Benjamin Morel's avatar
Benjamin Morel committed
928
    /**
929
     * @param mixed $table
Benjamin Morel's avatar
Benjamin Morel committed
930
     *
931
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
932
     */
933 934 935 936 937
    protected function _getPortableTableDefinition($table)
    {
        return $table;
    }

Benjamin Morel's avatar
Benjamin Morel committed
938
    /**
939
     * @param mixed[][] $users
Benjamin Morel's avatar
Benjamin Morel committed
940
     *
941
     * @return string[][]
Benjamin Morel's avatar
Benjamin Morel committed
942
     */
943 944
    protected function _getPortableUsersList($users)
    {
945
        $list = [];
946
        foreach ($users as $value) {
947 948 949
            $value = $this->_getPortableUserDefinition($value);

            if (! $value) {
950
                continue;
951
            }
952 953

            $list[] = $value;
954
        }
Benjamin Morel's avatar
Benjamin Morel committed
955

956
        return $list;
957 958
    }

Benjamin Morel's avatar
Benjamin Morel committed
959
    /**
960
     * @param string[] $user
Benjamin Morel's avatar
Benjamin Morel committed
961
     *
962
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
963
     */
964 965 966 967 968
    protected function _getPortableUserDefinition($user)
    {
        return $user;
    }

Benjamin Morel's avatar
Benjamin Morel committed
969
    /**
970
     * @param mixed[][] $views
971
     *
972
     * @return View[]
Benjamin Morel's avatar
Benjamin Morel committed
973
     */
974 975
    protected function _getPortableViewsList($views)
    {
976
        $list = [];
977
        foreach ($views as $value) {
978 979 980
            $view = $this->_getPortableViewDefinition($value);

            if (! $view) {
981
                continue;
982
            }
983 984 985

            $viewName        = strtolower($view->getQuotedName($this->_platform));
            $list[$viewName] = $view;
986
        }
Benjamin Morel's avatar
Benjamin Morel committed
987

988
        return $list;
989 990
    }

Benjamin Morel's avatar
Benjamin Morel committed
991
    /**
992
     * @param mixed[] $view
Benjamin Morel's avatar
Benjamin Morel committed
993
     *
994
     * @return View|false
Benjamin Morel's avatar
Benjamin Morel committed
995
     */
996 997
    protected function _getPortableViewDefinition($view)
    {
998
        return false;
999 1000
    }

Benjamin Morel's avatar
Benjamin Morel committed
1001
    /**
1002
     * @param mixed[][] $tableForeignKeys
Benjamin Morel's avatar
Benjamin Morel committed
1003
     *
1004
     * @return ForeignKeyConstraint[]
Benjamin Morel's avatar
Benjamin Morel committed
1005
     */
1006 1007
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
    {
1008
        $list = [];
1009

Sergei Morozov's avatar
Sergei Morozov committed
1010 1011
        foreach ($tableForeignKeys as $value) {
            $list[] = $this->_getPortableTableForeignKeyDefinition($value);
1012
        }
Benjamin Morel's avatar
Benjamin Morel committed
1013

1014 1015 1016
        return $list;
    }

Benjamin Morel's avatar
Benjamin Morel committed
1017
    /**
1018
     * @param mixed $tableForeignKey
Benjamin Morel's avatar
Benjamin Morel committed
1019
     *
1020
     * @return ForeignKeyConstraint
Benjamin Morel's avatar
Benjamin Morel committed
1021
     */
1022 1023 1024 1025
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
    {
        return $tableForeignKey;
    }
1026

Benjamin Morel's avatar
Benjamin Morel committed
1027
    /**
1028
     * @param string[]|string $sql
Benjamin Morel's avatar
Benjamin Morel committed
1029 1030 1031
     *
     * @return void
     */
romanb's avatar
romanb committed
1032
    protected function _execSql($sql)
1033 1034
    {
        foreach ((array) $sql as $query) {
1035
            $this->_conn->executeUpdate($query);
romanb's avatar
romanb committed
1036 1037
        }
    }
1038 1039

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1040
     * Creates a schema instance for the current database.
1041
     *
1042
     * @return Schema
1043 1044 1045
     */
    public function createSchema()
    {
1046
        $namespaces = [];
1047 1048 1049 1050 1051

        if ($this->_platform->supportsSchemas()) {
            $namespaces = $this->listNamespaceNames();
        }

1052
        $sequences = [];
1053

Steve Müller's avatar
Steve Müller committed
1054
        if ($this->_platform->supportsSequences()) {
1055 1056
            $sequences = $this->listSequences();
        }
1057

1058 1059
        $tables = $this->listTables();

1060
        return new Schema($tables, $sequences, $this->createSchemaConfig(), $namespaces);
1061 1062 1063
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1064
     * Creates the configuration for this schema.
1065
     *
1066
     * @return SchemaConfig
1067 1068 1069 1070 1071
     */
    public function createSchemaConfig()
    {
        $schemaConfig = new SchemaConfig();
        $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength());
1072 1073 1074 1075 1076

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

1078
        $params = $this->_conn->getParams();
1079 1080
        if (! isset($params['defaultTableOptions'])) {
            $params['defaultTableOptions'] = [];
1081
        }
Grégoire Paris's avatar
Grégoire Paris committed
1082

1083 1084 1085
        if (! isset($params['defaultTableOptions']['charset']) && isset($params['charset'])) {
            $params['defaultTableOptions']['charset'] = $params['charset'];
        }
Grégoire Paris's avatar
Grégoire Paris committed
1086

1087
        $schemaConfig->setDefaultTableOptions($params['defaultTableOptions']);
1088

1089
        return $schemaConfig;
1090
    }
1091

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    /**
     * 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.
     *
1102
     * @return string[]
1103
     */
1104 1105
    public function getSchemaSearchPaths()
    {
1106
        return [$this->_conn->getDatabase()];
1107 1108
    }

1109 1110 1111
    /**
     * Given a table comment this method tries to extract a typehint for Doctrine Type, or returns
     * the type given as default.
1112
     *
Sergei Morozov's avatar
Sergei Morozov committed
1113 1114
     * @param string|null $comment
     * @param string      $currentType
Benjamin Morel's avatar
Benjamin Morel committed
1115
     *
1116 1117 1118 1119
     * @return string
     */
    public function extractDoctrineTypeFromComment($comment, $currentType)
    {
Sergei Morozov's avatar
Sergei Morozov committed
1120 1121
        if ($comment !== null && preg_match('(\(DC2Type:(((?!\)).)+)\))', $comment, $match)) {
            return $match[1];
1122
        }
Benjamin Morel's avatar
Benjamin Morel committed
1123

1124 1125 1126
        return $currentType;
    }

Benjamin Morel's avatar
Benjamin Morel committed
1127
    /**
Sergei Morozov's avatar
Sergei Morozov committed
1128 1129
     * @param string|null $comment
     * @param string|null $type
Benjamin Morel's avatar
Benjamin Morel committed
1130
     *
Sergei Morozov's avatar
Sergei Morozov committed
1131
     * @return string|null
Benjamin Morel's avatar
Benjamin Morel committed
1132
     */
1133 1134
    public function removeDoctrineTypeFromComment($comment, $type)
    {
Sergei Morozov's avatar
Sergei Morozov committed
1135 1136 1137 1138
        if ($comment === null) {
            return null;
        }

1139
        return str_replace('(DC2Type:' . $type . ')', '', $comment);
1140
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
1141
}