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 270
        if ($this->_platform->supportsForeignKeyConstraints()) {
            $foreignKeys = $this->listTableForeignKeys($tableName);
        }
        $indexes = $this->listTableIndexes($tableName);

271
        return new Table($tableName, $columns, $indexes, $foreignKeys, false, []);
272 273
    }

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

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

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

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

307 308
    /* drop*() Methods */

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

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

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

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

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

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

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

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

    /* create*() Methods */

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

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

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

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

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

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

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

487 488
    /* dropAndCreate*() Methods */

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

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

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

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

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

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

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

580 581
    /* alterTable() Methods */

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

        foreach ($queries as $ddlQuery) {
            $this->_execSql($ddlQuery);
596
        }
597 598
    }

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

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

            if (! $value) {
631
                continue;
632
            }
633 634

            $list[] = $value;
635
        }
Benjamin Morel's avatar
Benjamin Morel committed
636

637
        return $list;
638 639
    }

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

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

        return $namespacesList;
    }

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

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

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

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

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

698
        return $list;
699 700
    }

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

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

            if (! $value) {
723
                continue;
724
            }
725 726

            $list[] = $value;
727
        }
Benjamin Morel's avatar
Benjamin Morel committed
728

729
        return $list;
730 731
    }

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

Benjamin Morel's avatar
Benjamin Morel committed
742
    /**
743
     * @param mixed[][] $sequences
Benjamin Morel's avatar
Benjamin Morel committed
744
     *
745
     * @return Sequence[]
Benjamin Morel's avatar
Benjamin Morel committed
746
     */
747 748
    protected function _getPortableSequencesList($sequences)
    {
749
        $list = [];
750
        foreach ($sequences as $value) {
751 752 753
            $value = $this->_getPortableSequenceDefinition($value);

            if (! $value) {
754
                continue;
755
            }
756 757

            $list[] = $value;
758
        }
Benjamin Morel's avatar
Benjamin Morel committed
759

760
        return $list;
761 762
    }

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

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

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

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

                $defaultPrevented = $eventArgs->isDefaultPrevented();
800
                $column           = $eventArgs->getColumn();
801 802
            }

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

807 808
            if (! $column) {
                continue;
809
            }
810 811 812

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

815
        return $list;
816 817
    }

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

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

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

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

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

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

868 869
        $eventManager = $this->_platform->getEventManager();

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

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

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

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

887 888
            if (! $index) {
                continue;
889
            }
890 891

            $indexes[$indexKey] = $index;
892 893 894
        }

        return $indexes;
895 896
    }

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

            if (! $value) {
909
                continue;
910
            }
911 912

            $list[] = $value;
913
        }
Benjamin Morel's avatar
Benjamin Morel committed
914

915
        return $list;
916 917
    }

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

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

            if (! $value) {
940
                continue;
941
            }
942 943

            $list[] = $value;
944
        }
Benjamin Morel's avatar
Benjamin Morel committed
945

946
        return $list;
947 948
    }

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

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

            if (! $view) {
971
                continue;
972
            }
973 974 975

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

978
        return $list;
979 980
    }

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

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

            if (! $value) {
1003
                continue;
1004
            }
1005 1006

            $list[] = $value;
1007
        }
Benjamin Morel's avatar
Benjamin Morel committed
1008

1009 1010 1011
        return $list;
    }

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

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

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

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

1047
        $sequences = [];
1048

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

1053 1054
        $tables = $this->listTables();

1055
        return new Schema($tables, $sequences, $this->createSchemaConfig(), $namespaces);
1056 1057 1058
    }

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

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

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

1082
        return $schemaConfig;
1083
    }
1084

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

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

1117 1118 1119
        return $currentType;
    }

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