AbstractSchemaManager.php 28.7 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
use function call_user_func_array;
use function count;
use function func_get_args;
Sergei Morozov's avatar
Sergei Morozov committed
21
use function is_callable;
22 23 24
use function preg_match;
use function str_replace;
use function strtolower;
25

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
142
     * Lists the columns for a given table.
romanb's avatar
romanb committed
143
     *
144 145
     * 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
146
     * the reason that it is not portable across different RDBMS. Use
147
     * {@see listTableIndexes($tableName)} to retrieve the primary key
148
     * of a table. Where a RDBMS specifies more details, these are held
149 150
     * in the platformDetails array.
     *
Benjamin Morel's avatar
Benjamin Morel committed
151 152 153
     * @param string      $table    The name of the table.
     * @param string|null $database
     *
154
     * @return Column[]
romanb's avatar
romanb committed
155
     */
156
    public function listTableColumns($table, $database = null)
romanb's avatar
romanb committed
157
    {
158
        if ($database === null) {
159 160 161 162
            $database = $this->_conn->getDatabase();
        }

        $sql = $this->_platform->getListTableColumnsSQL($table, $database);
163 164 165

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

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

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

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

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

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

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

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

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

215 216
        return $this->filterAssetNames($tableNames);
    }
217

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

233
        return array_values(array_filter($assetNames, $filter));
234 235
    }

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

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

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

        return $tables;
romanb's avatar
romanb committed
261 262
    }

263
    /**
Benjamin Morel's avatar
Benjamin Morel committed
264 265
     * @param string $tableName
     *
266
     * @return Table
267 268 269
     */
    public function listTableDetails($tableName)
    {
270
        $columns     = $this->listTableColumns($tableName);
271
        $foreignKeys = [];
272

273 274 275
        if ($this->_platform->supportsForeignKeyConstraints()) {
            $foreignKeys = $this->listTableForeignKeys($tableName);
        }
276

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

279
        return new Table($tableName, $columns, $indexes, [], $foreignKeys);
280 281
    }

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

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

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

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

315 316
    /* drop*() Methods */

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
344
     * Drops the index from the given table.
romanb's avatar
romanb committed
345
     *
346 347
     * @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
348 349
     *
     * @return void
romanb's avatar
romanb committed
350
     */
351
    public function dropIndex($index, $table)
romanb's avatar
romanb committed
352
    {
Steve Müller's avatar
Steve Müller committed
353
        if ($index instanceof Index) {
354
            $index = $index->getQuotedName($this->_platform);
355 356
        }

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

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

    /**
romanb's avatar
romanb committed
373
     * Drops a foreign key from a table.
romanb's avatar
romanb committed
374
     *
375 376
     * @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
377 378
     *
     * @return void
romanb's avatar
romanb committed
379
     */
380
    public function dropForeignKey($foreignKey, $table)
romanb's avatar
romanb committed
381
    {
382
        $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table));
romanb's avatar
romanb committed
383 384 385
    }

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

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

    /* create*() Methods */

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

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

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
461
     * Creates a new index on a table.
romanb's avatar
romanb committed
462
     *
463
     * @param Table|string $table The name of the table on which the index is to be created.
Benjamin Morel's avatar
Benjamin Morel committed
464 465
     *
     * @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
     * Creates a new foreign key.
     *
475 476
     * @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
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
     * @return void
489
     */
490
    public function createView(View $view)
491
    {
492
        $this->_execSql($this->_platform->getCreateViewSQL($view->getQuotedName($this->_platform), $view->getSql()));
493 494
    }

495 496
    /* dropAndCreate*() Methods */

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
527
     * Drops and creates a new foreign key.
528
     *
529 530
     * @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
531 532
     *
     * @return void
533
     */
534
    public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table)
535
    {
536 537
        $this->tryMethod('dropForeignKey', $foreignKey, $table);
        $this->createForeignKey($foreignKey, $table);
538 539 540
    }

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

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

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

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

588 589
    /* alterTable() Methods */

romanb's avatar
romanb committed
590
    /**
Benjamin Morel's avatar
Benjamin Morel committed
591
     * Alters an existing tables schema.
romanb's avatar
romanb committed
592
     *
Benjamin Morel's avatar
Benjamin Morel committed
593
     * @return void
594 595 596
     */
    public function alterTable(TableDiff $tableDiff)
    {
597
        $queries = $this->_platform->getAlterTableSQL($tableDiff);
598 599 600

        foreach ($queries as $ddlQuery) {
            $this->_execSql($ddlQuery);
601
        }
602 603
    }

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

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

636
        return $list;
637 638
    }

639 640 641
    /**
     * Converts a list of namespace names from the native DBMS data definition to a portable Doctrine definition.
     *
642
     * @param mixed[][] $namespaces The list of namespace names in the native DBMS data definition.
643
     *
644
     * @return string[]
645 646 647
     */
    protected function getPortableNamespacesList(array $namespaces)
    {
648
        $namespacesList = [];
649 650 651 652 653 654 655 656

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

        return $namespacesList;
    }

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

667 668 669
    /**
     * Converts a namespace definition from the native DBMS data definition to a portable Doctrine definition.
     *
670
     * @param mixed[] $namespace The native DBMS namespace definition.
671 672 673 674 675 676 677 678
     *
     * @return mixed
     */
    protected function getPortableNamespaceDefinition(array $namespace)
    {
        return $namespace;
    }

Benjamin Morel's avatar
Benjamin Morel committed
679
    /**
680 681
     * @deprecated
     *
682
     * @param mixed[][] $functions
Benjamin Morel's avatar
Benjamin Morel committed
683
     *
684
     * @return mixed[][]
Benjamin Morel's avatar
Benjamin Morel committed
685
     */
686 687
    protected function _getPortableFunctionsList($functions)
    {
688
        $list = [];
689
        foreach ($functions as $value) {
690 691 692
            $value = $this->_getPortableFunctionDefinition($value);

            if (! $value) {
693
                continue;
694
            }
695 696

            $list[] = $value;
697
        }
Benjamin Morel's avatar
Benjamin Morel committed
698

699
        return $list;
700 701
    }

Benjamin Morel's avatar
Benjamin Morel committed
702
    /**
703 704
     * @deprecated
     *
705
     * @param mixed[] $function
Benjamin Morel's avatar
Benjamin Morel committed
706 707 708
     *
     * @return mixed
     */
709 710 711 712 713
    protected function _getPortableFunctionDefinition($function)
    {
        return $function;
    }

Benjamin Morel's avatar
Benjamin Morel committed
714
    /**
715
     * @param mixed[][] $triggers
Benjamin Morel's avatar
Benjamin Morel committed
716
     *
717
     * @return mixed[][]
Benjamin Morel's avatar
Benjamin Morel committed
718
     */
719 720
    protected function _getPortableTriggersList($triggers)
    {
721
        $list = [];
722
        foreach ($triggers as $value) {
723 724 725
            $value = $this->_getPortableTriggerDefinition($value);

            if (! $value) {
726
                continue;
727
            }
728 729

            $list[] = $value;
730
        }
Benjamin Morel's avatar
Benjamin Morel committed
731

732
        return $list;
733 734
    }

Benjamin Morel's avatar
Benjamin Morel committed
735
    /**
736
     * @param mixed[] $trigger
Benjamin Morel's avatar
Benjamin Morel committed
737 738 739
     *
     * @return mixed
     */
740 741 742 743 744
    protected function _getPortableTriggerDefinition($trigger)
    {
        return $trigger;
    }

Benjamin Morel's avatar
Benjamin Morel committed
745
    /**
746
     * @param mixed[][] $sequences
Benjamin Morel's avatar
Benjamin Morel committed
747
     *
748
     * @return Sequence[]
Benjamin Morel's avatar
Benjamin Morel committed
749
     */
750 751
    protected function _getPortableSequencesList($sequences)
    {
752
        $list = [];
753

Sergei Morozov's avatar
Sergei Morozov committed
754 755
        foreach ($sequences as $value) {
            $list[] = $this->_getPortableSequenceDefinition($value);
756
        }
Benjamin Morel's avatar
Benjamin Morel committed
757

758
        return $list;
759 760
    }

761
    /**
762
     * @param mixed[] $sequence
Benjamin Morel's avatar
Benjamin Morel committed
763
     *
764
     * @return Sequence
Benjamin Morel's avatar
Benjamin Morel committed
765
     *
766
     * @throws DBALException
767
     */
768 769
    protected function _getPortableSequenceDefinition($sequence)
    {
770
        throw DBALException::notSupported('Sequences');
771 772
    }

773 774 775 776 777
    /**
     * 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.
     *
778 779 780
     * @param string    $table        The name of the table.
     * @param string    $database
     * @param mixed[][] $tableColumns
Benjamin Morel's avatar
Benjamin Morel committed
781
     *
782
     * @return Column[]
783
     */
784
    protected function _getPortableTableColumnList($table, $database, $tableColumns)
785
    {
786 787
        $eventManager = $this->_platform->getEventManager();

788
        $list = [];
789
        foreach ($tableColumns as $tableColumn) {
790
            $column           = null;
791 792
            $defaultPrevented = false;

793
            if ($eventManager !== null && $eventManager->hasListeners(Events::onSchemaColumnDefinition)) {
794
                $eventArgs = new SchemaColumnDefinitionEventArgs($tableColumn, $table, $database, $this->_conn);
795 796 797
                $eventManager->dispatchEvent(Events::onSchemaColumnDefinition, $eventArgs);

                $defaultPrevented = $eventArgs->isDefaultPrevented();
798
                $column           = $eventArgs->getColumn();
799 800
            }

801
            if (! $defaultPrevented) {
802
                $column = $this->_getPortableTableColumnDefinition($tableColumn);
803 804
            }

805
            if ($column === null) {
806
                continue;
807
            }
808 809 810

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

813
        return $list;
814 815
    }

816
    /**
Benjamin Morel's avatar
Benjamin Morel committed
817
     * Gets Table Column Definition.
818
     *
819
     * @param mixed[] $tableColumn
Benjamin Morel's avatar
Benjamin Morel committed
820
     *
821
     * @return Column
822 823
     */
    abstract protected function _getPortableTableColumnDefinition($tableColumn);
824

825
    /**
Benjamin Morel's avatar
Benjamin Morel committed
826 827
     * Aggregates and groups the index results according to the required data result.
     *
828
     * @param mixed[][]   $tableIndexRows
Benjamin Morel's avatar
Benjamin Morel committed
829
     * @param string|null $tableName
830
     *
831
     * @return Index[]
832
     */
833
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
834
    {
835
        $result = [];
Steve Müller's avatar
Steve Müller committed
836
        foreach ($tableIndexRows as $tableIndex) {
837
            $indexName = $keyName = $tableIndex['key_name'];
Steve Müller's avatar
Steve Müller committed
838
            if ($tableIndex['primary']) {
839 840
                $keyName = 'primary';
            }
841
            $keyName = strtolower($keyName);
842

843
            if (! isset($result[$keyName])) {
844 845 846 847 848 849 850 851
                $options = [
                    'lengths' => [],
                ];

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

852
                $result[$keyName] = [
853
                    'name' => $indexName,
854
                    'columns' => [],
855
                    'unique' => ! $tableIndex['non_unique'],
856
                    'primary' => $tableIndex['primary'],
857
                    'flags' => $tableIndex['flags'] ?? [],
858
                    'options' => $options,
859
                ];
860
            }
861 862 863

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

866 867
        $eventManager = $this->_platform->getEventManager();

868
        $indexes = [];
Steve Müller's avatar
Steve Müller committed
869
        foreach ($result as $indexKey => $data) {
870
            $index            = null;
871 872
            $defaultPrevented = false;

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

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

881
            if (! $defaultPrevented) {
882
                $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary'], $data['flags'], $data['options']);
883 884
            }

885
            if ($index === null) {
886
                continue;
887
            }
888 889

            $indexes[$indexKey] = $index;
890 891 892
        }

        return $indexes;
893 894
    }

Benjamin Morel's avatar
Benjamin Morel committed
895
    /**
896
     * @param mixed[][] $tables
Benjamin Morel's avatar
Benjamin Morel committed
897
     *
898
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
899
     */
900 901
    protected function _getPortableTablesList($tables)
    {
902
        $list = [];
903
        foreach ($tables as $value) {
904
            $list[] = $this->_getPortableTableDefinition($value);
905
        }
Benjamin Morel's avatar
Benjamin Morel committed
906

907
        return $list;
908 909
    }

Benjamin Morel's avatar
Benjamin Morel committed
910
    /**
911
     * @param mixed $table
Benjamin Morel's avatar
Benjamin Morel committed
912
     *
913
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
914
     */
915 916 917 918 919
    protected function _getPortableTableDefinition($table)
    {
        return $table;
    }

Benjamin Morel's avatar
Benjamin Morel committed
920
    /**
921
     * @param mixed[][] $users
Benjamin Morel's avatar
Benjamin Morel committed
922
     *
923
     * @return string[][]
Benjamin Morel's avatar
Benjamin Morel committed
924
     */
925 926
    protected function _getPortableUsersList($users)
    {
927
        $list = [];
928
        foreach ($users as $value) {
929
            $list[] = $this->_getPortableUserDefinition($value);
930
        }
Benjamin Morel's avatar
Benjamin Morel committed
931

932
        return $list;
933 934
    }

Benjamin Morel's avatar
Benjamin Morel committed
935
    /**
936
     * @param string[] $user
Benjamin Morel's avatar
Benjamin Morel committed
937
     *
938
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
939
     */
940 941 942 943 944
    protected function _getPortableUserDefinition($user)
    {
        return $user;
    }

Benjamin Morel's avatar
Benjamin Morel committed
945
    /**
946
     * @param mixed[][] $views
947
     *
948
     * @return View[]
Benjamin Morel's avatar
Benjamin Morel committed
949
     */
950 951
    protected function _getPortableViewsList($views)
    {
952
        $list = [];
953
        foreach ($views as $value) {
954 955
            $view = $this->_getPortableViewDefinition($value);

956
            if ($view === false) {
957
                continue;
958
            }
959 960 961

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

964
        return $list;
965 966
    }

Benjamin Morel's avatar
Benjamin Morel committed
967
    /**
968
     * @param mixed[] $view
Benjamin Morel's avatar
Benjamin Morel committed
969
     *
970
     * @return View|false
Benjamin Morel's avatar
Benjamin Morel committed
971
     */
972 973
    protected function _getPortableViewDefinition($view)
    {
974
        return false;
975 976
    }

Benjamin Morel's avatar
Benjamin Morel committed
977
    /**
978
     * @param mixed[][] $tableForeignKeys
Benjamin Morel's avatar
Benjamin Morel committed
979
     *
980
     * @return ForeignKeyConstraint[]
Benjamin Morel's avatar
Benjamin Morel committed
981
     */
982 983
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
    {
984
        $list = [];
985

Sergei Morozov's avatar
Sergei Morozov committed
986 987
        foreach ($tableForeignKeys as $value) {
            $list[] = $this->_getPortableTableForeignKeyDefinition($value);
988
        }
Benjamin Morel's avatar
Benjamin Morel committed
989

990 991 992
        return $list;
    }

Benjamin Morel's avatar
Benjamin Morel committed
993
    /**
994
     * @param mixed $tableForeignKey
Benjamin Morel's avatar
Benjamin Morel committed
995
     *
996
     * @return ForeignKeyConstraint
Benjamin Morel's avatar
Benjamin Morel committed
997
     */
998 999 1000 1001
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
    {
        return $tableForeignKey;
    }
1002

Benjamin Morel's avatar
Benjamin Morel committed
1003
    /**
1004
     * @param string[]|string $sql
Benjamin Morel's avatar
Benjamin Morel committed
1005 1006 1007
     *
     * @return void
     */
romanb's avatar
romanb committed
1008
    protected function _execSql($sql)
1009 1010
    {
        foreach ((array) $sql as $query) {
1011
            $this->_conn->executeUpdate($query);
romanb's avatar
romanb committed
1012 1013
        }
    }
1014 1015

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1016
     * Creates a schema instance for the current database.
1017
     *
1018
     * @return Schema
1019 1020 1021
     */
    public function createSchema()
    {
1022
        $namespaces = [];
1023 1024 1025 1026 1027

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

1028
        $sequences = [];
1029

Steve Müller's avatar
Steve Müller committed
1030
        if ($this->_platform->supportsSequences()) {
1031 1032
            $sequences = $this->listSequences();
        }
1033

1034 1035
        $tables = $this->listTables();

1036
        return new Schema($tables, $sequences, $this->createSchemaConfig(), $namespaces);
1037 1038 1039
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1040
     * Creates the configuration for this schema.
1041
     *
1042
     * @return SchemaConfig
1043 1044 1045 1046 1047
     */
    public function createSchemaConfig()
    {
        $schemaConfig = new SchemaConfig();
        $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength());
1048 1049 1050 1051 1052

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

1054
        $params = $this->_conn->getParams();
1055 1056
        if (! isset($params['defaultTableOptions'])) {
            $params['defaultTableOptions'] = [];
1057
        }
1058 1059 1060 1061
        if (! isset($params['defaultTableOptions']['charset']) && isset($params['charset'])) {
            $params['defaultTableOptions']['charset'] = $params['charset'];
        }
        $schemaConfig->setDefaultTableOptions($params['defaultTableOptions']);
1062

1063
        return $schemaConfig;
1064
    }
1065

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    /**
     * 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.
     *
1076
     * @return string[]
1077
     */
1078 1079
    public function getSchemaSearchPaths()
    {
1080
        return [$this->_conn->getDatabase()];
1081 1082
    }

1083 1084 1085
    /**
     * Given a table comment this method tries to extract a typehint for Doctrine Type, or returns
     * the type given as default.
1086
     *
Sergei Morozov's avatar
Sergei Morozov committed
1087 1088
     * @param string|null $comment
     * @param string      $currentType
Benjamin Morel's avatar
Benjamin Morel committed
1089
     *
1090 1091 1092 1093
     * @return string
     */
    public function extractDoctrineTypeFromComment($comment, $currentType)
    {
1094
        if ($comment !== null && preg_match('(\(DC2Type:(((?!\)).)+)\))', $comment, $match) === 1) {
Sergei Morozov's avatar
Sergei Morozov committed
1095
            return $match[1];
1096
        }
Benjamin Morel's avatar
Benjamin Morel committed
1097

1098 1099 1100
        return $currentType;
    }

Benjamin Morel's avatar
Benjamin Morel committed
1101
    /**
Sergei Morozov's avatar
Sergei Morozov committed
1102 1103
     * @param string|null $comment
     * @param string|null $type
Benjamin Morel's avatar
Benjamin Morel committed
1104
     *
Sergei Morozov's avatar
Sergei Morozov committed
1105
     * @return string|null
Benjamin Morel's avatar
Benjamin Morel committed
1106
     */
1107 1108
    public function removeDoctrineTypeFromComment($comment, $type)
    {
Sergei Morozov's avatar
Sergei Morozov committed
1109 1110 1111 1112
        if ($comment === null) {
            return null;
        }

1113
        return str_replace('(DC2Type:' . $type . ')', '', $comment);
1114
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
1115
}