Failed Conditions
Push — master ( 379085...b45ed5 )
by Marco
54s queued 28s
created

_getPortableUserDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\ConnectionException;
7
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
use function array_map;
16
use function array_values;
17
use function call_user_func_array;
18
use function count;
19
use function func_get_args;
20
use function is_array;
21
use function preg_match;
22
use function str_replace;
23
use function strtolower;
24
25
/**
26
 * Base class for schema managers. Schema managers are used to inspect and/or
27
 * modify the database schema/structure.
28
 */
29
abstract class AbstractSchemaManager
30
{
31
    /**
32
     * Holds instance of the Doctrine connection for this schema manager.
33
     *
34
     * @var Connection
35
     */
36
    protected $_conn;
37
38
    /**
39
     * Holds instance of the database platform used for this schema manager.
40
     *
41
     * @var AbstractPlatform
42
     */
43
    protected $_platform;
44
45
    /**
46
     * Constructor. Accepts the Connection instance to manage the schema for.
47
     */
48 1350
    public function __construct(Connection $conn, ?AbstractPlatform $platform = null)
49
    {
50 1350
        $this->_conn     = $conn;
51 1350
        $this->_platform = $platform ?: $this->_conn->getDatabasePlatform();
52 1350
    }
53
54
    /**
55
     * Returns the associated platform.
56
     *
57
     * @return AbstractPlatform
58
     */
59 375
    public function getDatabasePlatform()
60
    {
61 375
        return $this->_platform;
62
    }
63
64
    /**
65
     * Tries any method on the schema manager. Normally a method throws an
66
     * exception when your DBMS doesn't support it or if an error occurs.
67
     * This method allows you to try and method on your SchemaManager
68
     * instance and will return false if it does not work or is not supported.
69
     *
70
     * <code>
71
     * $result = $sm->tryMethod('dropView', 'view_name');
72
     * </code>
73
     *
74
     * @return mixed
75
     */
76 1852
    public function tryMethod()
77
    {
78 1852
        $args   = func_get_args();
79 1852
        $method = $args[0];
80 1852
        unset($args[0]);
81 1852
        $args = array_values($args);
82
83
        try {
84 1852
            return call_user_func_array([$this, $method], $args);
85 1379
        } catch (Throwable $e) {
86 1379
            return false;
87
        }
88
    }
89
90
    /**
91
     * Lists the available databases for this connection.
92
     *
93
     * @return string[]
94
     */
95 35
    public function listDatabases()
96
    {
97 35
        $sql = $this->_platform->getListDatabasesSQL();
98
99 34
        $databases = $this->_conn->fetchAll($sql);
100
101 34
        return $this->_getPortableDatabasesList($databases);
102
    }
103
104
    /**
105
     * Returns a list of all namespaces in the current database.
106
     *
107
     * @return string[]
108
     */
109 16
    public function listNamespaceNames()
110
    {
111 16
        $sql = $this->_platform->getListNamespacesSQL();
112
113 16
        $namespaces = $this->_conn->fetchAll($sql);
114
115 16
        return $this->getPortableNamespacesList($namespaces);
116
    }
117
118
    /**
119
     * Lists the available sequences for this connection.
120
     *
121
     * @param string|null $database
122
     *
123
     * @return Sequence[]
124
     */
125 53
    public function listSequences($database = null)
126
    {
127 53
        if ($database === null) {
128 53
            $database = $this->_conn->getDatabase();
129
        }
130 53
        $sql = $this->_platform->getListSequencesSQL($database);
131
132 53
        $sequences = $this->_conn->fetchAll($sql);
133
134 53
        return $this->filterAssetNames($this->_getPortableSequencesList($sequences));
135
    }
136
137
    /**
138
     * Lists the columns for a given table.
139
     *
140
     * In contrast to other libraries and to the old version of Doctrine,
141
     * this column definition does try to contain the 'primary' field for
142
     * the reason that it is not portable across different RDBMS. Use
143
     * {@see listTableIndexes($tableName)} to retrieve the primary key
144
     * of a table. We're a RDBMS specifies more details these are held
145
     * in the platformDetails array.
146
     *
147
     * @param string      $table    The name of the table.
148
     * @param string|null $database
149
     *
150
     * @return Column[]
151
     */
152 908
    public function listTableColumns($table, $database = null)
153
    {
154 908
        if (! $database) {
155 907
            $database = $this->_conn->getDatabase();
156
        }
157
158 908
        $sql = $this->_platform->getListTableColumnsSQL($table, $database);
159
160 908
        $tableColumns = $this->_conn->fetchAll($sql);
161
162 908
        return $this->_getPortableTableColumnList($table, $database, $tableColumns);
163
    }
164
165
    /**
166
     * Lists the indexes for a given table returning an array of Index instances.
167
     *
168
     * Keys of the portable indexes list are all lower-cased.
169
     *
170
     * @param string $table The name of the table.
171
     *
172
     * @return Index[]
173
     */
174 684
    public function listTableIndexes($table)
175
    {
176 684
        $sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());
177
178 684
        $tableIndexes = $this->_conn->fetchAll($sql);
179
180 684
        return $this->_getPortableTableIndexesList($tableIndexes, $table);
181
    }
182
183
    /**
184
     * Returns true if all the given tables exist.
185
     *
186
     * @param string[] $tableNames
187
     *
188
     * @return bool
189
     */
190 1419
    public function tablesExist($tableNames)
191
    {
192 1419
        $tableNames = array_map('strtolower', (array) $tableNames);
193
194 1419
        return count($tableNames) === count(array_intersect($tableNames, array_map('strtolower', $this->listTableNames())));
195
    }
196
197
    /**
198
     * Returns a list of all tables in the current database.
199
     *
200
     * @return string[]
201
     */
202 1423
    public function listTableNames()
203
    {
204 1423
        $sql = $this->_platform->getListTablesSQL();
205
206 1423
        $tables     = $this->_conn->fetchAll($sql);
207 1423
        $tableNames = $this->_getPortableTablesList($tables);
208
209 1423
        return $this->filterAssetNames($tableNames);
210
    }
211
212
    /**
213
     * Filters asset names if they are configured to return only a subset of all
214
     * the found elements.
215
     *
216
     * @param mixed[] $assetNames
217
     *
218
     * @return mixed[]
219
     */
220 1485
    protected function filterAssetNames($assetNames)
221
    {
222 1485
        $filterExpr = $this->getFilterSchemaAssetsExpression();
223 1485
        if (! $filterExpr) {
224 1485
            return $assetNames;
225
        }
226
227 6
        return array_values(
228
            array_filter($assetNames, static function ($assetName) use ($filterExpr) {
229 6
                $assetName = $assetName instanceof AbstractAsset ? $assetName->getName() : $assetName;
230
231 6
                return preg_match($filterExpr, $assetName);
232 6
            })
233
        );
234
    }
235
236
    /**
237
     * @return string|null
238
     */
239 1485
    protected function getFilterSchemaAssetsExpression()
240
    {
241 1485
        return $this->_conn->getConfiguration()->getFilterSchemaAssetsExpression();
242
    }
243
244
    /**
245
     * Lists the tables for this connection.
246
     *
247
     * @return Table[]
248
     */
249 102
    public function listTables()
250
    {
251 102
        $tableNames = $this->listTableNames();
252
253 102
        $tables = [];
254 102
        foreach ($tableNames as $tableName) {
255 83
            $tables[] = $this->listTableDetails($tableName);
256
        }
257
258 102
        return $tables;
259
    }
260
261
    /**
262
     * @param string $tableName
263
     *
264
     * @return Table
265
     */
266 637
    public function listTableDetails($tableName)
267
    {
268 637
        $columns     = $this->listTableColumns($tableName);
269 637
        $foreignKeys = [];
270 637
        if ($this->_platform->supportsForeignKeyConstraints()) {
271 572
            $foreignKeys = $this->listTableForeignKeys($tableName);
272
        }
273 637
        $indexes = $this->listTableIndexes($tableName);
274
275 637
        return new Table($tableName, $columns, $indexes, $foreignKeys, false, []);
276
    }
277
278
    /**
279
     * Lists the views this connection has.
280
     *
281
     * @return View[]
282
     */
283 19
    public function listViews()
284
    {
285 19
        $database = $this->_conn->getDatabase();
286 19
        $sql      = $this->_platform->getListViewsSQL($database);
287 19
        $views    = $this->_conn->fetchAll($sql);
288
289 19
        return $this->_getPortableViewsList($views);
290
    }
291
292
    /**
293
     * Lists the foreign keys for the given table.
294
     *
295
     * @param string      $table    The name of the table.
296
     * @param string|null $database
297
     *
298
     * @return ForeignKeyConstraint[]
299
     */
300 635
    public function listTableForeignKeys($table, $database = null)
301
    {
302 635
        if ($database === null) {
303 635
            $database = $this->_conn->getDatabase();
304
        }
305 635
        $sql              = $this->_platform->getListTableForeignKeysSQL($table, $database);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Platforms\...stTableForeignKeysSQL() has too many arguments starting with $database. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

305
        /** @scrutinizer ignore-call */ 
306
        $sql              = $this->_platform->getListTableForeignKeysSQL($table, $database);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
306 635
        $tableForeignKeys = $this->_conn->fetchAll($sql);
307
308 635
        return $this->_getPortableTableForeignKeysList($tableForeignKeys);
309
    }
310
311
    /* drop*() Methods */
312
313
    /**
314
     * Drops a database.
315
     *
316
     * NOTE: You can not drop the database this SchemaManager is currently connected to.
317
     *
318
     * @param string $database The name of the database to drop.
319
     *
320
     * @return void
321
     */
322 34
    public function dropDatabase($database)
323
    {
324 34
        $this->_execSql($this->_platform->getDropDatabaseSQL($database));
325 23
    }
326
327
    /**
328
     * Drops the given table.
329
     *
330
     * @param string $tableName The name of the table to drop.
331
     *
332
     * @return void
333
     */
334 2106
    public function dropTable($tableName)
335
    {
336 2106
        $this->_execSql($this->_platform->getDropTableSQL($tableName));
337 1035
    }
338
339
    /**
340
     * Drops the index from the given table.
341
     *
342
     * @param Index|string $index The name of the index.
343
     * @param Table|string $table The name of the table.
344
     *
345
     * @return void
346
     */
347 19
    public function dropIndex($index, $table)
348
    {
349 19
        if ($index instanceof Index) {
350
            $index = $index->getQuotedName($this->_platform);
351
        }
352
353 19
        $this->_execSql($this->_platform->getDropIndexSQL($index, $table));
354 19
    }
355
356
    /**
357
     * Drops the constraint from the given table.
358
     *
359
     * @param Table|string $table The name of the table.
360
     *
361
     * @return void
362
     */
363
    public function dropConstraint(Constraint $constraint, $table)
364
    {
365
        $this->_execSql($this->_platform->getDropConstraintSQL($constraint, $table));
366
    }
367
368
    /**
369
     * Drops a foreign key from a table.
370
     *
371
     * @param ForeignKeyConstraint|string $foreignKey The name of the foreign key.
372
     * @param Table|string                $table      The name of the table with the foreign key.
373
     *
374
     * @return void
375
     */
376
    public function dropForeignKey($foreignKey, $table)
377
    {
378
        $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table));
379
    }
380
381
    /**
382
     * Drops a sequence with a given name.
383
     *
384
     * @param string $name The name of the sequence to drop.
385
     *
386
     * @return void
387
     */
388 18
    public function dropSequence($name)
389
    {
390 18
        $this->_execSql($this->_platform->getDropSequenceSQL($name));
391
    }
392
393
    /**
394
     * Drops a view.
395
     *
396
     * @param string $name The name of the view.
397
     *
398
     * @return void
399
     */
400 25
    public function dropView($name)
401
    {
402 25
        $this->_execSql($this->_platform->getDropViewSQL($name));
403
    }
404
405
    /* create*() Methods */
406
407
    /**
408
     * Creates a new database.
409
     *
410
     * @param string $database The name of the database to create.
411
     *
412
     * @return void
413
     */
414 32
    public function createDatabase($database)
415
    {
416 32
        $this->_execSql($this->_platform->getCreateDatabaseSQL($database));
417 32
    }
418
419
    /**
420
     * Creates a new table.
421
     *
422
     * @return void
423
     */
424 2734
    public function createTable(Table $table)
425
    {
426 2734
        $createFlags = AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS;
427 2734
        $this->_execSql($this->_platform->getCreateTableSQL($table, $createFlags));
428 2041
    }
429
430
    /**
431
     * Creates a new sequence.
432
     *
433
     * @param Sequence $sequence
434
     *
435
     * @return void
436
     *
437
     * @throws ConnectionException If something fails at database level.
438
     */
439 44
    public function createSequence($sequence)
440
    {
441 44
        $this->_execSql($this->_platform->getCreateSequenceSQL($sequence));
442 38
    }
443
444
    /**
445
     * Creates a constraint on a table.
446
     *
447
     * @param Table|string $table
448
     *
449
     * @return void
450
     */
451 1
    public function createConstraint(Constraint $constraint, $table)
452
    {
453 1
        $this->_execSql($this->_platform->getCreateConstraintSQL($constraint, $table));
454 1
    }
455
456
    /**
457
     * Creates a new index on a table.
458
     *
459
     * @param Table|string $table The name of the table on which the index is to be created.
460
     *
461
     * @return void
462
     */
463 19
    public function createIndex(Index $index, $table)
464
    {
465 19
        $this->_execSql($this->_platform->getCreateIndexSQL($index, $table));
466 19
    }
467
468
    /**
469
     * Creates a new foreign key.
470
     *
471
     * @param ForeignKeyConstraint $foreignKey The ForeignKey instance.
472
     * @param Table|string         $table      The name of the table on which the foreign key is to be created.
473
     *
474
     * @return void
475
     */
476 36
    public function createForeignKey(ForeignKeyConstraint $foreignKey, $table)
477
    {
478 36
        $this->_execSql($this->_platform->getCreateForeignKeySQL($foreignKey, $table));
479 36
    }
480
481
    /**
482
     * Creates a new view.
483
     *
484
     * @return void
485
     */
486 25
    public function createView(View $view)
487
    {
488 25
        $this->_execSql($this->_platform->getCreateViewSQL($view->getQuotedName($this->_platform), $view->getSql()));
489 25
    }
490
491
    /* dropAndCreate*() Methods */
492
493
    /**
494
     * Drops and creates a constraint.
495
     *
496
     * @see dropConstraint()
497
     * @see createConstraint()
498
     *
499
     * @param Table|string $table
500
     *
501
     * @return void
502
     */
503
    public function dropAndCreateConstraint(Constraint $constraint, $table)
504
    {
505
        $this->tryMethod('dropConstraint', $constraint, $table);
506
        $this->createConstraint($constraint, $table);
507
    }
508
509
    /**
510
     * Drops and creates a new index on a table.
511
     *
512
     * @param Table|string $table The name of the table on which the index is to be created.
513
     *
514
     * @return void
515
     */
516 19
    public function dropAndCreateIndex(Index $index, $table)
517
    {
518 19
        $this->tryMethod('dropIndex', $index->getQuotedName($this->_platform), $table);
519 19
        $this->createIndex($index, $table);
520 19
    }
521
522
    /**
523
     * Drops and creates a new foreign key.
524
     *
525
     * @param ForeignKeyConstraint $foreignKey An associative array that defines properties of the foreign key to be created.
526
     * @param Table|string         $table      The name of the table on which the foreign key is to be created.
527
     *
528
     * @return void
529
     */
530
    public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table)
531
    {
532
        $this->tryMethod('dropForeignKey', $foreignKey, $table);
533
        $this->createForeignKey($foreignKey, $table);
534
    }
535
536
    /**
537
     * Drops and create a new sequence.
538
     *
539
     * @return void
540
     *
541
     * @throws ConnectionException If something fails at database level.
542
     */
543 18
    public function dropAndCreateSequence(Sequence $sequence)
544
    {
545 18
        $this->tryMethod('dropSequence', $sequence->getQuotedName($this->_platform));
546 18
        $this->createSequence($sequence);
547 18
    }
548
549
    /**
550
     * Drops and creates a new table.
551
     *
552
     * @return void
553
     */
554 1257
    public function dropAndCreateTable(Table $table)
555
    {
556 1257
        $this->tryMethod('dropTable', $table->getQuotedName($this->_platform));
557 1257
        $this->createTable($table);
558 1257
    }
559
560
    /**
561
     * Drops and creates a new database.
562
     *
563
     * @param string $database The name of the database to create.
564
     *
565
     * @return void
566
     */
567 35
    public function dropAndCreateDatabase($database)
568
    {
569 35
        $this->tryMethod('dropDatabase', $database);
570 35
        $this->createDatabase($database);
571 35
    }
572
573
    /**
574
     * Drops and creates a new view.
575
     *
576
     * @return void
577
     */
578 25
    public function dropAndCreateView(View $view)
579
    {
580 25
        $this->tryMethod('dropView', $view->getQuotedName($this->_platform));
581 25
        $this->createView($view);
582 25
    }
583
584
    /* alterTable() Methods */
585
586
    /**
587
     * Alters an existing tables schema.
588
     *
589
     * @return void
590
     */
591 346
    public function alterTable(TableDiff $tableDiff)
592
    {
593 346
        $queries = $this->_platform->getAlterTableSQL($tableDiff);
594 346
        if (! is_array($queries) || ! count($queries)) {
595 6
            return;
596
        }
597
598 340
        foreach ($queries as $ddlQuery) {
599 340
            $this->_execSql($ddlQuery);
600
        }
601 340
    }
602
603
    /**
604
     * Renames a given table to another name.
605
     *
606
     * @param string $name    The current name of the table.
607
     * @param string $newName The new name of the table.
608
     *
609
     * @return void
610
     */
611 1
    public function renameTable($name, $newName)
612
    {
613 1
        $tableDiff          = new TableDiff($name);
614 1
        $tableDiff->newName = $newName;
615 1
        $this->alterTable($tableDiff);
616 1
    }
617
618
    /**
619
     * Methods for filtering return values of list*() methods to convert
620
     * the native DBMS data definition to a portable Doctrine definition
621
     */
622
623
    /**
624
     * @param mixed[] $databases
625
     *
626
     * @return string[]
627
     */
628 34
    protected function _getPortableDatabasesList($databases)
629
    {
630 34
        $list = [];
631 34
        foreach ($databases as $value) {
632 34
            $value = $this->_getPortableDatabaseDefinition($value);
633
634 34
            if (! $value) {
635
                continue;
636
            }
637
638 34
            $list[] = $value;
639
        }
640
641 34
        return $list;
642
    }
643
644
    /**
645
     * Converts a list of namespace names from the native DBMS data definition to a portable Doctrine definition.
646
     *
647
     * @param mixed[][] $namespaces The list of namespace names in the native DBMS data definition.
648
     *
649
     * @return string[]
650
     */
651 16
    protected function getPortableNamespacesList(array $namespaces)
652
    {
653 16
        $namespacesList = [];
654
655 16
        foreach ($namespaces as $namespace) {
656 16
            $namespacesList[] = $this->getPortableNamespaceDefinition($namespace);
657
        }
658
659 16
        return $namespacesList;
660
    }
661
662
    /**
663
     * @param mixed $database
664
     *
665
     * @return mixed
666
     */
667
    protected function _getPortableDatabaseDefinition($database)
668
    {
669
        return $database;
670
    }
671
672
    /**
673
     * Converts a namespace definition from the native DBMS data definition to a portable Doctrine definition.
674
     *
675
     * @param mixed[] $namespace The native DBMS namespace definition.
676
     *
677
     * @return mixed
678
     */
679
    protected function getPortableNamespaceDefinition(array $namespace)
680
    {
681
        return $namespace;
682
    }
683
684
    /**
685
     * @param mixed[][] $functions
686
     *
687
     * @return mixed[][]
688
     */
689
    protected function _getPortableFunctionsList($functions)
690
    {
691
        $list = [];
692
        foreach ($functions as $value) {
693
            $value = $this->_getPortableFunctionDefinition($value);
694
695
            if (! $value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type array<mixed,mixed> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
696
                continue;
697
            }
698
699
            $list[] = $value;
700
        }
701
702
        return $list;
703
    }
704
705
    /**
706
     * @param mixed[] $function
707
     *
708
     * @return mixed
709
     */
710
    protected function _getPortableFunctionDefinition($function)
711
    {
712
        return $function;
713
    }
714
715
    /**
716
     * @param mixed[][] $triggers
717
     *
718
     * @return mixed[][]
719
     */
720
    protected function _getPortableTriggersList($triggers)
721
    {
722
        $list = [];
723
        foreach ($triggers as $value) {
724
            $value = $this->_getPortableTriggerDefinition($value);
725
726
            if (! $value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type array<mixed,mixed> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
727
                continue;
728
            }
729
730
            $list[] = $value;
731
        }
732
733
        return $list;
734
    }
735
736
    /**
737
     * @param mixed[] $trigger
738
     *
739
     * @return mixed
740
     */
741
    protected function _getPortableTriggerDefinition($trigger)
742
    {
743
        return $trigger;
744
    }
745
746
    /**
747
     * @param mixed[][] $sequences
748
     *
749
     * @return Sequence[]
750
     */
751 17
    protected function _getPortableSequencesList($sequences)
752
    {
753 17
        $list = [];
754 17
        foreach ($sequences as $value) {
755 17
            $value = $this->_getPortableSequenceDefinition($value);
756
757 17
            if (! $value) {
758
                continue;
759
            }
760
761 17
            $list[] = $value;
762
        }
763
764 17
        return $list;
765
    }
766
767
    /**
768
     * @param mixed[] $sequence
769
     *
770
     * @return Sequence
771
     *
772
     * @throws DBALException
773
     */
774
    protected function _getPortableSequenceDefinition($sequence)
775
    {
776
        throw DBALException::notSupported('Sequences');
777
    }
778
779
    /**
780
     * Independent of the database the keys of the column list result are lowercased.
781
     *
782
     * The name of the created column instance however is kept in its case.
783
     *
784
     * @param string    $table        The name of the table.
785
     * @param string    $database
786
     * @param mixed[][] $tableColumns
787
     *
788
     * @return Column[]
789
     */
790 908
    protected function _getPortableTableColumnList($table, $database, $tableColumns)
791
    {
792 908
        $eventManager = $this->_platform->getEventManager();
793
794 908
        $list = [];
795 908
        foreach ($tableColumns as $tableColumn) {
796 908
            $column           = null;
797 908
            $defaultPrevented = false;
798
799 908
            if ($eventManager !== null && $eventManager->hasListeners(Events::onSchemaColumnDefinition)) {
800 19
                $eventArgs = new SchemaColumnDefinitionEventArgs($tableColumn, $table, $database, $this->_conn);
801 19
                $eventManager->dispatchEvent(Events::onSchemaColumnDefinition, $eventArgs);
802
803 19
                $defaultPrevented = $eventArgs->isDefaultPrevented();
804 19
                $column           = $eventArgs->getColumn();
805
            }
806
807 908
            if (! $defaultPrevented) {
808 908
                $column = $this->_getPortableTableColumnDefinition($tableColumn);
809
            }
810
811 908
            if (! $column) {
812
                continue;
813
            }
814
815 908
            $name        = strtolower($column->getQuotedName($this->_platform));
816 908
            $list[$name] = $column;
817
        }
818
819 908
        return $list;
820
    }
821
822
    /**
823
     * Gets Table Column Definition.
824
     *
825
     * @param mixed[] $tableColumn
826
     *
827
     * @return Column
828
     */
829
    abstract protected function _getPortableTableColumnDefinition($tableColumn);
830
831
    /**
832
     * Aggregates and groups the index results according to the required data result.
833
     *
834
     * @param mixed[][]   $tableIndexRows
835
     * @param string|null $tableName
836
     *
837
     * @return Index[]
838
     */
839 740
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
840
    {
841 740
        $result = [];
842 740
        foreach ($tableIndexRows as $tableIndex) {
843 430
            $indexName = $keyName = $tableIndex['key_name'];
844 430
            if ($tableIndex['primary']) {
845 372
                $keyName = 'primary';
846
            }
847 430
            $keyName = strtolower($keyName);
848
849 430
            if (! isset($result[$keyName])) {
850 430
                $result[$keyName] = [
851 430
                    'name' => $indexName,
852 430
                    'columns' => [$tableIndex['column_name']],
853 430
                    'unique' => $tableIndex['non_unique'] ? false : true,
854 430
                    'primary' => $tableIndex['primary'],
855 430
                    'flags' => $tableIndex['flags'] ?? [],
856 430
                    'options' => isset($tableIndex['where']) ? ['where' => $tableIndex['where']] : [],
857
                ];
858
            } else {
859 430
                $result[$keyName]['columns'][] = $tableIndex['column_name'];
860
            }
861
        }
862
863 740
        $eventManager = $this->_platform->getEventManager();
864
865 740
        $indexes = [];
866 740
        foreach ($result as $indexKey => $data) {
867 430
            $index            = null;
868 430
            $defaultPrevented = false;
869
870 430
            if ($eventManager !== null && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
871 19
                $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
872 19
                $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
873
874 19
                $defaultPrevented = $eventArgs->isDefaultPrevented();
875 19
                $index            = $eventArgs->getIndex();
876
            }
877
878 430
            if (! $defaultPrevented) {
879 430
                $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary'], $data['flags'], $data['options']);
880
            }
881
882 430
            if (! $index) {
883
                continue;
884
            }
885
886 430
            $indexes[$indexKey] = $index;
887
        }
888
889 740
        return $indexes;
890
    }
891
892
    /**
893
     * @param mixed[][] $tables
894
     *
895
     * @return string[]
896
     */
897 1423
    protected function _getPortableTablesList($tables)
898
    {
899 1423
        $list = [];
900 1423
        foreach ($tables as $value) {
901 1404
            $value = $this->_getPortableTableDefinition($value);
902
903 1404
            if (! $value) {
904
                continue;
905
            }
906
907 1404
            $list[] = $value;
908
        }
909
910 1423
        return $list;
911
    }
912
913
    /**
914
     * @param mixed $table
915
     *
916
     * @return string
917
     */
918
    protected function _getPortableTableDefinition($table)
919
    {
920
        return $table;
921
    }
922
923
    /**
924
     * @param mixed[][] $users
925
     *
926
     * @return string[][]
927
     */
928
    protected function _getPortableUsersList($users)
929
    {
930
        $list = [];
931
        foreach ($users as $value) {
932
            $value = $this->_getPortableUserDefinition($value);
933
934
            if (! $value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type array<mixed,mixed> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
935
                continue;
936
            }
937
938
            $list[] = $value;
939
        }
940
941
        return $list;
942
    }
943
944
    /**
945
     * @param mixed[] $user
946
     *
947
     * @return mixed[]
948
     */
949
    protected function _getPortableUserDefinition($user)
950
    {
951
        return $user;
952
    }
953
954
    /**
955
     * @param mixed[][] $views
956
     *
957
     * @return View[]
958
     */
959 19
    protected function _getPortableViewsList($views)
960
    {
961 19
        $list = [];
962 19
        foreach ($views as $value) {
963 19
            $view = $this->_getPortableViewDefinition($value);
964
965 19
            if (! $view) {
966
                continue;
967
            }
968
969 19
            $viewName        = strtolower($view->getQuotedName($this->_platform));
970 19
            $list[$viewName] = $view;
971
        }
972
973 19
        return $list;
974
    }
975
976
    /**
977
     * @param mixed[] $view
978
     *
979
     * @return View|false
980
     */
981
    protected function _getPortableViewDefinition($view)
982
    {
983
        return false;
984
    }
985
986
    /**
987
     * @param mixed[][] $tableForeignKeys
988
     *
989
     * @return ForeignKeyConstraint[]
990
     */
991 331
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
992
    {
993 331
        $list = [];
994 331
        foreach ($tableForeignKeys as $value) {
995 107
            $value = $this->_getPortableTableForeignKeyDefinition($value);
996
997 107
            if (! $value) {
998
                continue;
999
            }
1000
1001 107
            $list[] = $value;
1002
        }
1003
1004 331
        return $list;
1005
    }
1006
1007
    /**
1008
     * @param mixed $tableForeignKey
1009
     *
1010
     * @return ForeignKeyConstraint
1011
     */
1012
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
1013
    {
1014
        return $tableForeignKey;
1015
    }
1016
1017
    /**
1018
     * @param string[]|string $sql
1019
     *
1020
     * @return void
1021
     */
1022 3004
    protected function _execSql($sql)
1023
    {
1024 3004
        foreach ((array) $sql as $query) {
1025 3004
            $this->_conn->executeUpdate($query);
1026
        }
1027 2136
    }
1028
1029
    /**
1030
     * Creates a schema instance for the current database.
1031
     *
1032
     * @return Schema
1033
     */
1034 76
    public function createSchema()
1035
    {
1036 76
        $namespaces = [];
1037
1038 76
        if ($this->_platform->supportsSchemas()) {
1039 8
            $namespaces = $this->listNamespaceNames();
1040
        }
1041
1042 76
        $sequences = [];
1043
1044 76
        if ($this->_platform->supportsSequences()) {
1045 9
            $sequences = $this->listSequences();
1046
        }
1047
1048 76
        $tables = $this->listTables();
1049
1050 76
        return new Schema($tables, $sequences, $this->createSchemaConfig(), $namespaces);
1051
    }
1052
1053
    /**
1054
     * Creates the configuration for this schema.
1055
     *
1056
     * @return SchemaConfig
1057
     */
1058 334
    public function createSchemaConfig()
1059
    {
1060 334
        $schemaConfig = new SchemaConfig();
1061 334
        $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength());
1062
1063 334
        $searchPaths = $this->getSchemaSearchPaths();
1064 334
        if (isset($searchPaths[0])) {
1065 248
            $schemaConfig->setName($searchPaths[0]);
1066
        }
1067
1068 334
        $params = $this->_conn->getParams();
1069 334
        if (! isset($params['defaultTableOptions'])) {
1070 334
            $params['defaultTableOptions'] = [];
1071
        }
1072 334
        if (! isset($params['defaultTableOptions']['charset']) && isset($params['charset'])) {
1073 19
            $params['defaultTableOptions']['charset'] = $params['charset'];
1074
        }
1075 334
        $schemaConfig->setDefaultTableOptions($params['defaultTableOptions']);
1076
1077 334
        return $schemaConfig;
1078
    }
1079
1080
    /**
1081
     * The search path for namespaces in the currently connected database.
1082
     *
1083
     * The first entry is usually the default namespace in the Schema. All
1084
     * further namespaces contain tables/sequences which can also be addressed
1085
     * with a short, not full-qualified name.
1086
     *
1087
     * For databases that don't support subschema/namespaces this method
1088
     * returns the name of the currently connected database.
1089
     *
1090
     * @return string[]
1091
     */
1092 244
    public function getSchemaSearchPaths()
1093
    {
1094 244
        return [$this->_conn->getDatabase()];
1095
    }
1096
1097
    /**
1098
     * Given a table comment this method tries to extract a typehint for Doctrine Type, or returns
1099
     * the type given as default.
1100
     *
1101
     * @param string $comment
1102
     * @param string $currentType
1103
     *
1104
     * @return string
1105
     */
1106 969
    public function extractDoctrineTypeFromComment($comment, $currentType)
1107
    {
1108 969
        if (preg_match('(\(DC2Type:(((?!\)).)+)\))', $comment, $match)) {
1109 214
            $currentType = $match[1];
1110
        }
1111
1112 969
        return $currentType;
1113
    }
1114
1115
    /**
1116
     * @param string $comment
1117
     * @param string $type
1118
     *
1119
     * @return string
1120
     */
1121 843
    public function removeDoctrineTypeFromComment($comment, $type)
1122
    {
1123 843
        return str_replace('(DC2Type:' . $type . ')', '', $comment);
1124
    }
1125
}
1126