AbstractSchemaManager.php 28.8 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 17 18 19 20 21 22 23
use function array_map;
use function array_values;
use function call_user_func_array;
use function count;
use function func_get_args;
use function is_array;
use function preg_match;
use function str_replace;
use function strtolower;
24

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

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

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

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

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

        try {
84
            return call_user_func_array([$this, $method], $args);
85
        } catch (Throwable $e) {
86 87 88 89
            return false;
        }
    }

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

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

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

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

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

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

romanb's avatar
romanb committed
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
     *
123
     * @return Sequence[]
romanb's avatar
romanb committed
124
     */
125
    public function listSequences($database = null)
romanb's avatar
romanb committed
126
    {
127
        if ($database === null) {
128 129
            $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
     * 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
142
     * the reason that it is not portable across different RDBMS. Use
143 144 145 146
     * {@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
     * @param string      $table    The name of the table.
     * @param string|null $database
     *
150
     * @return 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
     * @param string $table The name of the table.
     *
172
     * @return 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 string[] $tableNames
Benjamin Morel's avatar
Benjamin Morel committed
187
     *
188
     * @return bool
189 190 191
     */
    public function tablesExist($tableNames)
    {
192
        $tableNames = array_map('strtolower', (array) $tableNames);
Benjamin Morel's avatar
Benjamin Morel committed
193

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

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 string[]
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
     * the found elements.
     *
216
     * @param mixed[] $assetNames
Benjamin Morel's avatar
Benjamin Morel committed
217
     *
218
     * @return mixed[]
219 220 221
     */
    protected function filterAssetNames($assetNames)
    {
222 223
        $filter = $this->_conn->getConfiguration()->getSchemaAssetsFilter();
        if (! $filter) {
224 225
            return $assetNames;
        }
Benjamin Morel's avatar
Benjamin Morel committed
226

227
        return array_values(array_filter($assetNames, $filter));
228 229
    }

Benjamin Morel's avatar
Benjamin Morel committed
230
    /**
231 232
     * @deprecated Use Configuration::getSchemaAssetsFilter() instead
     *
Benjamin Morel's avatar
Benjamin Morel committed
233 234
     * @return string|null
     */
235 236 237
    protected function getFilterSchemaAssetsExpression()
    {
        return $this->_conn->getConfiguration()->getFilterSchemaAssetsExpression();
238 239 240
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
241
     * Lists the tables for this connection.
242
     *
243
     * @return Table[]
244 245 246 247
     */
    public function listTables()
    {
        $tableNames = $this->listTableNames();
248

249
        $tables = [];
250
        foreach ($tableNames as $tableName) {
251
            $tables[] = $this->listTableDetails($tableName);
252 253 254
        }

        return $tables;
romanb's avatar
romanb committed
255 256
    }

257
    /**
Benjamin Morel's avatar
Benjamin Morel committed
258 259
     * @param string $tableName
     *
260
     * @return Table
261 262 263
     */
    public function listTableDetails($tableName)
    {
264
        $columns     = $this->listTableColumns($tableName);
265
        $foreignKeys = [];
266

267 268 269
        if ($this->_platform->supportsForeignKeyConstraints()) {
            $foreignKeys = $this->listTableForeignKeys($tableName);
        }
270

271 272
        $indexes = $this->listTableIndexes($tableName);

273
        return new Table($tableName, $columns, $indexes, [], $foreignKeys, []);
274 275
    }

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

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

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

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

309 310
    /* drop*() Methods */

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

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

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

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

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

    /**
romanb's avatar
romanb committed
367
     * Drops a foreign key from a table.
romanb's avatar
romanb committed
368
     *
369 370
     * @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
371 372
     *
     * @return void
romanb's avatar
romanb committed
373
     */
374
    public function dropForeignKey($foreignKey, $table)
romanb's avatar
romanb committed
375
    {
376
        $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table));
romanb's avatar
romanb committed
377 378 379
    }

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

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

    /* create*() Methods */

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

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
443 444
     * Creates a constraint on a table.
     *
445
     * @param Table|string $table
romanb's avatar
romanb committed
446
     *
Benjamin Morel's avatar
Benjamin Morel committed
447
     * @return void
448 449
     */
    public function createConstraint(Constraint $constraint, $table)
romanb's avatar
romanb committed
450
    {
451
        $this->_execSql($this->_platform->getCreateConstraintSQL($constraint, $table));
romanb's avatar
romanb committed
452 453 454
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
455
     * Creates a new index on a table.
romanb's avatar
romanb committed
456
     *
457
     * @param Table|string $table The name of the table on which the index is to be created.
Benjamin Morel's avatar
Benjamin Morel committed
458 459
     *
     * @return void
romanb's avatar
romanb committed
460
     */
461
    public function createIndex(Index $index, $table)
romanb's avatar
romanb committed
462
    {
463
        $this->_execSql($this->_platform->getCreateIndexSQL($index, $table));
romanb's avatar
romanb committed
464 465 466
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
467 468
     * Creates a new foreign key.
     *
469 470
     * @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
471
     *
Benjamin Morel's avatar
Benjamin Morel committed
472
     * @return void
romanb's avatar
romanb committed
473
     */
474
    public function createForeignKey(ForeignKeyConstraint $foreignKey, $table)
romanb's avatar
romanb committed
475
    {
476
        $this->_execSql($this->_platform->getCreateForeignKeySQL($foreignKey, $table));
romanb's avatar
romanb committed
477 478
    }

479
    /**
Benjamin Morel's avatar
Benjamin Morel committed
480
     * Creates a new view.
481
     *
Benjamin Morel's avatar
Benjamin Morel committed
482
     * @return void
483
     */
484
    public function createView(View $view)
485
    {
486
        $this->_execSql($this->_platform->getCreateViewSQL($view->getQuotedName($this->_platform), $view->getSql()));
487 488
    }

489 490
    /* dropAndCreate*() Methods */

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

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

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
548 549 550
     * Drops and creates a new table.
     *
     * @return void
551
     */
552
    public function dropAndCreateTable(Table $table)
553
    {
554
        $this->tryMethod('dropTable', $table->getQuotedName($this->_platform));
555
        $this->createTable($table);
556 557 558
    }

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
572 573 574
     * Drops and creates a new view.
     *
     * @return void
575
     */
576
    public function dropAndCreateView(View $view)
577
    {
578
        $this->tryMethod('dropView', $view->getQuotedName($this->_platform));
579
        $this->createView($view);
580 581
    }

582 583
    /* alterTable() Methods */

romanb's avatar
romanb committed
584
    /**
Benjamin Morel's avatar
Benjamin Morel committed
585
     * Alters an existing tables schema.
romanb's avatar
romanb committed
586
     *
Benjamin Morel's avatar
Benjamin Morel committed
587
     * @return void
588 589 590
     */
    public function alterTable(TableDiff $tableDiff)
    {
591
        $queries = $this->_platform->getAlterTableSQL($tableDiff);
592

593 594 595 596 597 598
        if (! is_array($queries) || ! count($queries)) {
            return;
        }

        foreach ($queries as $ddlQuery) {
            $this->_execSql($ddlQuery);
599
        }
600 601
    }

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

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

            if (! $value) {
634
                continue;
635
            }
636 637

            $list[] = $value;
638
        }
Benjamin Morel's avatar
Benjamin Morel committed
639

640
        return $list;
641 642
    }

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

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

        return $namespacesList;
    }

Benjamin Morel's avatar
Benjamin Morel committed
661
    /**
662
     * @param mixed $database
Benjamin Morel's avatar
Benjamin Morel committed
663 664 665
     *
     * @return mixed
     */
666 667 668 669 670
    protected function _getPortableDatabaseDefinition($database)
    {
        return $database;
    }

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

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

            if (! $value) {
695
                continue;
696
            }
697 698

            $list[] = $value;
699
        }
Benjamin Morel's avatar
Benjamin Morel committed
700

701
        return $list;
702 703
    }

Benjamin Morel's avatar
Benjamin Morel committed
704
    /**
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
        foreach ($sequences as $value) {
754 755 756
            $value = $this->_getPortableSequenceDefinition($value);

            if (! $value) {
757
                continue;
758
            }
759 760

            $list[] = $value;
761
        }
Benjamin Morel's avatar
Benjamin Morel committed
762

763
        return $list;
764 765
    }

766
    /**
767
     * @param mixed[] $sequence
Benjamin Morel's avatar
Benjamin Morel committed
768
     *
769
     * @return Sequence
Benjamin Morel's avatar
Benjamin Morel committed
770
     *
771
     * @throws DBALException
772
     */
773 774
    protected function _getPortableSequenceDefinition($sequence)
    {
775
        throw DBALException::notSupported('Sequences');
776 777
    }

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

793
        $list = [];
794
        foreach ($tableColumns as $tableColumn) {
795
            $column           = null;
796 797
            $defaultPrevented = false;

798
            if ($eventManager !== null && $eventManager->hasListeners(Events::onSchemaColumnDefinition)) {
799
                $eventArgs = new SchemaColumnDefinitionEventArgs($tableColumn, $table, $database, $this->_conn);
800 801 802
                $eventManager->dispatchEvent(Events::onSchemaColumnDefinition, $eventArgs);

                $defaultPrevented = $eventArgs->isDefaultPrevented();
803
                $column           = $eventArgs->getColumn();
804 805
            }

806
            if (! $defaultPrevented) {
807
                $column = $this->_getPortableTableColumnDefinition($tableColumn);
808 809
            }

810 811
            if (! $column) {
                continue;
812
            }
813 814 815

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

818
        return $list;
819 820
    }

821
    /**
Benjamin Morel's avatar
Benjamin Morel committed
822
     * Gets Table Column Definition.
823
     *
824
     * @param mixed[] $tableColumn
Benjamin Morel's avatar
Benjamin Morel committed
825
     *
826
     * @return Column
827 828
     */
    abstract protected function _getPortableTableColumnDefinition($tableColumn);
829

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

848
            if (! isset($result[$keyName])) {
849 850 851 852 853 854 855 856
                $options = [
                    'lengths' => [],
                ];

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

857
                $result[$keyName] = [
858
                    'name' => $indexName,
859
                    'columns' => [],
860 861
                    'unique' => $tableIndex['non_unique'] ? false : true,
                    'primary' => $tableIndex['primary'],
862
                    'flags' => $tableIndex['flags'] ?? [],
863
                    'options' => $options,
864
                ];
865
            }
866 867 868

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

871 872
        $eventManager = $this->_platform->getEventManager();

873
        $indexes = [];
Steve Müller's avatar
Steve Müller committed
874
        foreach ($result as $indexKey => $data) {
875
            $index            = null;
876 877
            $defaultPrevented = false;

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

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

886
            if (! $defaultPrevented) {
887
                $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary'], $data['flags'], $data['options']);
888 889
            }

890 891
            if (! $index) {
                continue;
892
            }
893 894

            $indexes[$indexKey] = $index;
895 896 897
        }

        return $indexes;
898 899
    }

Benjamin Morel's avatar
Benjamin Morel committed
900
    /**
901
     * @param mixed[][] $tables
Benjamin Morel's avatar
Benjamin Morel committed
902
     *
903
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
904
     */
905 906
    protected function _getPortableTablesList($tables)
    {
907
        $list = [];
908
        foreach ($tables as $value) {
909 910 911
            $value = $this->_getPortableTableDefinition($value);

            if (! $value) {
912
                continue;
913
            }
914 915

            $list[] = $value;
916
        }
Benjamin Morel's avatar
Benjamin Morel committed
917

918
        return $list;
919 920
    }

Benjamin Morel's avatar
Benjamin Morel committed
921
    /**
922
     * @param mixed $table
Benjamin Morel's avatar
Benjamin Morel committed
923
     *
924
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
925
     */
926 927 928 929 930
    protected function _getPortableTableDefinition($table)
    {
        return $table;
    }

Benjamin Morel's avatar
Benjamin Morel committed
931
    /**
932
     * @param mixed[][] $users
Benjamin Morel's avatar
Benjamin Morel committed
933
     *
934
     * @return string[][]
Benjamin Morel's avatar
Benjamin Morel committed
935
     */
936 937
    protected function _getPortableUsersList($users)
    {
938
        $list = [];
939
        foreach ($users as $value) {
940 941 942
            $value = $this->_getPortableUserDefinition($value);

            if (! $value) {
943
                continue;
944
            }
945 946

            $list[] = $value;
947
        }
Benjamin Morel's avatar
Benjamin Morel committed
948

949
        return $list;
950 951
    }

Benjamin Morel's avatar
Benjamin Morel committed
952
    /**
953
     * @param mixed[] $user
Benjamin Morel's avatar
Benjamin Morel committed
954
     *
955
     * @return mixed[]
Benjamin Morel's avatar
Benjamin Morel committed
956
     */
957 958 959 960 961
    protected function _getPortableUserDefinition($user)
    {
        return $user;
    }

Benjamin Morel's avatar
Benjamin Morel committed
962
    /**
963
     * @param mixed[][] $views
964
     *
965
     * @return View[]
Benjamin Morel's avatar
Benjamin Morel committed
966
     */
967 968
    protected function _getPortableViewsList($views)
    {
969
        $list = [];
970
        foreach ($views as $value) {
971 972 973
            $view = $this->_getPortableViewDefinition($value);

            if (! $view) {
974
                continue;
975
            }
976 977 978

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

981
        return $list;
982 983
    }

Benjamin Morel's avatar
Benjamin Morel committed
984
    /**
985
     * @param mixed[] $view
Benjamin Morel's avatar
Benjamin Morel committed
986
     *
987
     * @return View|false
Benjamin Morel's avatar
Benjamin Morel committed
988
     */
989 990
    protected function _getPortableViewDefinition($view)
    {
991
        return false;
992 993
    }

Benjamin Morel's avatar
Benjamin Morel committed
994
    /**
995
     * @param mixed[][] $tableForeignKeys
Benjamin Morel's avatar
Benjamin Morel committed
996
     *
997
     * @return ForeignKeyConstraint[]
Benjamin Morel's avatar
Benjamin Morel committed
998
     */
999 1000
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
    {
1001
        $list = [];
1002
        foreach ($tableForeignKeys as $value) {
1003 1004 1005
            $value = $this->_getPortableTableForeignKeyDefinition($value);

            if (! $value) {
1006
                continue;
1007
            }
1008 1009

            $list[] = $value;
1010
        }
Benjamin Morel's avatar
Benjamin Morel committed
1011

1012 1013 1014
        return $list;
    }

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

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

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

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

1050
        $sequences = [];
1051

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

1056 1057
        $tables = $this->listTables();

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

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

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

1076
        $params = $this->_conn->getParams();
1077 1078
        if (! isset($params['defaultTableOptions'])) {
            $params['defaultTableOptions'] = [];
1079
        }
1080 1081 1082 1083
        if (! isset($params['defaultTableOptions']['charset']) && isset($params['charset'])) {
            $params['defaultTableOptions']['charset'] = $params['charset'];
        }
        $schemaConfig->setDefaultTableOptions($params['defaultTableOptions']);
1084

1085
        return $schemaConfig;
1086
    }
1087

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

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

1120 1121 1122
        return $currentType;
    }

Benjamin Morel's avatar
Benjamin Morel committed
1123 1124 1125 1126 1127 1128
    /**
     * @param string $comment
     * @param string $type
     *
     * @return string
     */
1129 1130
    public function removeDoctrineTypeFromComment($comment, $type)
    {
1131
        return str_replace('(DC2Type:' . $type . ')', '', $comment);
1132
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
1133
}