Complex classes like AbstractSchemaManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractSchemaManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | abstract class AbstractSchemaManager |
||
36 | { |
||
37 | /** |
||
38 | * Holds instance of the Doctrine connection for this schema manager. |
||
39 | * |
||
40 | * @var Connection |
||
41 | */ |
||
42 | protected $_conn; |
||
43 | |||
44 | /** |
||
45 | * Holds instance of the database platform used for this schema manager. |
||
46 | * |
||
47 | * @var AbstractPlatform |
||
48 | */ |
||
49 | protected $_platform; |
||
50 | |||
51 | /** |
||
52 | * Constructor. Accepts the Connection instance to manage the schema for. |
||
53 | */ |
||
54 | 1652 | public function __construct(Connection $conn, ?AbstractPlatform $platform = null) |
|
55 | { |
||
56 | 1652 | $this->_conn = $conn; |
|
57 | 1652 | $this->_platform = $platform ?: $this->_conn->getDatabasePlatform(); |
|
58 | 1652 | } |
|
59 | |||
60 | /** |
||
61 | * Returns the associated platform. |
||
62 | */ |
||
63 | 487 | public function getDatabasePlatform() : AbstractPlatform |
|
64 | { |
||
65 | 487 | return $this->_platform; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Tries any method on the schema manager. Normally a method throws an |
||
70 | * exception when your DBMS doesn't support it or if an error occurs. |
||
71 | * This method allows you to try and method on your SchemaManager |
||
72 | * instance and will return false if it does not work or is not supported. |
||
73 | * |
||
74 | * <code> |
||
75 | * $result = $sm->tryMethod('dropView', 'view_name'); |
||
76 | * </code> |
||
77 | * |
||
78 | * @return mixed |
||
79 | */ |
||
80 | 4114 | public function tryMethod() |
|
81 | { |
||
82 | 4114 | $args = func_get_args(); |
|
83 | 4114 | $method = $args[0]; |
|
84 | 4114 | unset($args[0]); |
|
85 | 4114 | $args = array_values($args); |
|
86 | |||
87 | 4114 | $callback = [$this, $method]; |
|
88 | 4114 | assert(is_callable($callback)); |
|
89 | |||
90 | try { |
||
91 | 4114 | return call_user_func_array($callback, $args); |
|
92 | 2285 | } catch (Throwable $e) { |
|
93 | 2285 | return false; |
|
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Lists the available databases for this connection. |
||
99 | * |
||
100 | * @return array<int, string> |
||
|
|||
101 | */ |
||
102 | 53 | public function listDatabases() : array |
|
103 | { |
||
104 | 53 | $sql = $this->_platform->getListDatabasesSQL(); |
|
105 | |||
106 | 52 | $databases = $this->_conn->fetchAll($sql); |
|
107 | |||
108 | 52 | return $this->_getPortableDatabasesList($databases); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Returns a list of all namespaces in the current database. |
||
113 | * |
||
114 | * @return array<int, string> |
||
115 | */ |
||
116 | 14 | public function listNamespaceNames() : array |
|
117 | { |
||
118 | 14 | $sql = $this->_platform->getListNamespacesSQL(); |
|
119 | |||
120 | 14 | $namespaces = $this->_conn->fetchAll($sql); |
|
121 | |||
122 | 14 | return $this->getPortableNamespacesList($namespaces); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Lists the available sequences for this connection. |
||
127 | * |
||
128 | * @return array<int, Sequence> |
||
129 | */ |
||
130 | 51 | public function listSequences(?string $database = null) : array |
|
131 | { |
||
132 | 51 | $database = $this->ensureDatabase( |
|
133 | 51 | $database ?? $this->_conn->getDatabase(), |
|
134 | 51 | __METHOD__ |
|
135 | ); |
||
136 | |||
137 | 51 | $sql = $this->_platform->getListSequencesSQL($database); |
|
138 | |||
139 | 51 | $sequences = $this->_conn->fetchAll($sql); |
|
140 | |||
141 | 51 | return $this->filterAssetNames($this->_getPortableSequencesList($sequences)); |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Lists the columns for a given table. |
||
146 | * |
||
147 | * In contrast to other libraries and to the old version of Doctrine, |
||
148 | * this column definition does try to contain the 'primary' field for |
||
149 | * the reason that it is not portable across different RDBMS. Use |
||
150 | * {@see listTableIndexes($tableName)} to retrieve the primary key |
||
151 | * of a table. Where a RDBMS specifies more details, these are held |
||
152 | * in the platformDetails array. |
||
153 | * |
||
154 | * @return array<string, Column> |
||
155 | */ |
||
156 | 1847 | public function listTableColumns(string $table, ?string $database = null) : array |
|
157 | { |
||
158 | 1847 | $database = $this->ensureDatabase( |
|
159 | 1847 | $database ?? $this->_conn->getDatabase(), |
|
160 | 1847 | __METHOD__ |
|
161 | ); |
||
162 | |||
163 | 1830 | $sql = $this->_platform->getListTableColumnsSQL($table, $database); |
|
164 | |||
165 | 1830 | $tableColumns = $this->_conn->fetchAll($sql); |
|
166 | |||
167 | 1830 | return $this->_getPortableTableColumnList($table, $database, $tableColumns); |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Lists the indexes for a given table returning an array of Index instances. |
||
172 | * |
||
173 | * Keys of the portable indexes list are all lower-cased. |
||
174 | * |
||
175 | * @return array<string, Index> |
||
176 | */ |
||
177 | 1510 | public function listTableIndexes(string $table) : array |
|
178 | { |
||
179 | 1510 | $sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase()); |
|
180 | |||
181 | 1510 | $tableIndexes = $this->_conn->fetchAll($sql); |
|
182 | |||
183 | 1510 | return $this->_getPortableTableIndexesList($tableIndexes, $table); |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Returns true if all the given tables exist. |
||
188 | * |
||
189 | * @param array<int, string> $tableNames |
||
190 | */ |
||
191 | 220 | public function tablesExist(array $tableNames) : bool |
|
192 | { |
||
193 | 220 | $tableNames = array_map('strtolower', $tableNames); |
|
194 | |||
195 | 220 | return count($tableNames) === count(array_intersect($tableNames, array_map('strtolower', $this->listTableNames()))); |
|
196 | } |
||
197 | |||
198 | 220 | public function tableExists(string $tableName) : bool |
|
202 | |||
203 | /** |
||
204 | * Returns a list of all tables in the current database. |
||
205 | * |
||
206 | * @return array<int, string> |
||
207 | */ |
||
208 | 397 | public function listTableNames() : array |
|
217 | |||
218 | /** |
||
219 | * Filters asset names if they are configured to return only a subset of all |
||
220 | * the found elements. |
||
221 | * |
||
222 | * @param array<int, mixed> $assetNames |
||
223 | * |
||
224 | * @return array<int, mixed> |
||
225 | */ |
||
226 | 439 | protected function filterAssetNames(array $assetNames) : array |
|
235 | |||
236 | /** |
||
237 | * Lists the tables for this connection. |
||
238 | * |
||
239 | * @return array<int, Table> |
||
240 | */ |
||
241 | 159 | public function listTables() : array |
|
242 | { |
||
243 | 159 | $tableNames = $this->listTableNames(); |
|
244 | |||
245 | 159 | $tables = []; |
|
246 | 159 | foreach ($tableNames as $tableName) { |
|
247 | 132 | $tables[] = $this->listTableDetails($tableName); |
|
248 | } |
||
249 | |||
250 | 159 | return $tables; |
|
251 | } |
||
252 | |||
253 | 1432 | public function listTableDetails(string $tableName) : Table |
|
254 | { |
||
255 | 1432 | $columns = $this->listTableColumns($tableName); |
|
256 | 1432 | $foreignKeys = []; |
|
257 | |||
258 | 1432 | if ($this->_platform->supportsForeignKeyConstraints()) { |
|
259 | 1333 | $foreignKeys = $this->listTableForeignKeys($tableName); |
|
260 | } |
||
261 | |||
262 | 1432 | $indexes = $this->listTableIndexes($tableName); |
|
263 | |||
264 | 1432 | return new Table($tableName, $columns, $indexes, [], $foreignKeys, []); |
|
265 | } |
||
266 | |||
267 | /** |
||
268 | * Lists the views this connection has. |
||
269 | * |
||
270 | * @return array<string, View> |
||
271 | */ |
||
272 | 27 | public function listViews() : array |
|
273 | { |
||
274 | 27 | $database = $this->ensureDatabase( |
|
275 | 27 | $this->_conn->getDatabase(), |
|
276 | 27 | __METHOD__ |
|
277 | ); |
||
278 | |||
279 | 27 | $sql = $this->_platform->getListViewsSQL($database); |
|
280 | 27 | $views = $this->_conn->fetchAll($sql); |
|
281 | |||
282 | 27 | return $this->_getPortableViewsList($views); |
|
283 | } |
||
284 | |||
285 | /** |
||
286 | * Lists the foreign keys for the given table. |
||
287 | * |
||
288 | * @return array<int|string, ForeignKeyConstraint> |
||
289 | */ |
||
290 | 1419 | public function listTableForeignKeys(string $table, ?string $database = null) : array |
|
291 | { |
||
292 | 1419 | if ($database === null) { |
|
293 | 1419 | $database = $this->_conn->getDatabase(); |
|
294 | } |
||
295 | 1419 | $sql = $this->_platform->getListTableForeignKeysSQL($table, $database); |
|
296 | 1419 | $tableForeignKeys = $this->_conn->fetchAll($sql); |
|
297 | |||
298 | 1419 | return $this->_getPortableTableForeignKeysList($tableForeignKeys); |
|
299 | } |
||
300 | |||
301 | /* drop*() Methods */ |
||
302 | |||
303 | /** |
||
304 | * Drops a database. |
||
305 | * |
||
306 | * NOTE: You can not drop the database this SchemaManager is currently connected to. |
||
307 | */ |
||
308 | 69 | public function dropDatabase(string $database) : void |
|
312 | |||
313 | /** |
||
314 | * Drops the given table. |
||
315 | */ |
||
316 | 4185 | public function dropTable(string $tableName) : void |
|
317 | { |
||
318 | 4185 | $this->_execSql($this->_platform->getDropTableSQL($tableName)); |
|
319 | 2357 | } |
|
320 | |||
321 | /** |
||
322 | * Drops the index from the given table. |
||
323 | * |
||
324 | * @param Index|string $index The name of the index. |
||
325 | * @param Table|string $table The name of the table. |
||
326 | */ |
||
327 | 27 | public function dropIndex($index, $table) : void |
|
328 | { |
||
329 | 27 | if ($index instanceof Index) { |
|
330 | $index = $index->getQuotedName($this->_platform); |
||
331 | } |
||
332 | |||
333 | 27 | $this->_execSql($this->_platform->getDropIndexSQL($index, $table)); |
|
334 | 27 | } |
|
335 | |||
336 | /** |
||
337 | * Drops the constraint from the given table. |
||
338 | * |
||
339 | * @param Table|string $table The name of the table. |
||
340 | */ |
||
341 | public function dropConstraint(Constraint $constraint, $table) : void |
||
342 | { |
||
343 | $this->_execSql($this->_platform->getDropConstraintSQL($constraint, $table)); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Drops a foreign key from a table. |
||
348 | * |
||
349 | * @param ForeignKeyConstraint|string $foreignKey The name of the foreign key. |
||
350 | * @param Table|string $table The name of the table with the foreign key. |
||
351 | */ |
||
352 | public function dropForeignKey($foreignKey, $table) : void |
||
353 | { |
||
354 | $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table)); |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Drops a sequence with a given name. |
||
359 | */ |
||
360 | 18 | public function dropSequence(string $name) : void |
|
361 | { |
||
362 | 18 | $this->_execSql($this->_platform->getDropSequenceSQL($name)); |
|
363 | } |
||
364 | |||
365 | /** |
||
366 | * Drops a view. |
||
367 | */ |
||
368 | 32 | public function dropView(string $name) : void |
|
369 | { |
||
370 | 32 | $this->_execSql($this->_platform->getDropViewSQL($name)); |
|
371 | } |
||
372 | |||
373 | /* create*() Methods */ |
||
374 | |||
375 | /** |
||
376 | * Creates a new database. |
||
377 | */ |
||
378 | 65 | public function createDatabase(string $database) : void |
|
379 | { |
||
380 | 65 | $this->_execSql($this->_platform->getCreateDatabaseSQL($database)); |
|
381 | 65 | } |
|
382 | |||
383 | /** |
||
384 | * Creates a new table. |
||
385 | */ |
||
386 | 4157 | public function createTable(Table $table) : void |
|
387 | { |
||
388 | 4157 | $createFlags = AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS; |
|
389 | 4157 | $this->_execSql($this->_platform->getCreateTableSQL($table, $createFlags)); |
|
390 | 4021 | } |
|
391 | |||
392 | /** |
||
393 | * Creates a new sequence. |
||
394 | * |
||
395 | * @throws ConnectionException If something fails at database level. |
||
396 | */ |
||
397 | 42 | public function createSequence(Sequence $sequence) : void |
|
398 | { |
||
399 | 42 | $this->_execSql($this->_platform->getCreateSequenceSQL($sequence)); |
|
400 | 37 | } |
|
401 | |||
402 | /** |
||
403 | * Creates a constraint on a table. |
||
404 | * |
||
405 | * @param Table|string $table |
||
406 | */ |
||
407 | 2 | public function createConstraint(Constraint $constraint, $table) : void |
|
408 | { |
||
409 | 2 | $this->_execSql($this->_platform->getCreateConstraintSQL($constraint, $table)); |
|
410 | 2 | } |
|
411 | |||
412 | /** |
||
413 | * Creates a new index on a table. |
||
414 | * |
||
415 | * @param Table|string $table The name of the table on which the index is to be created. |
||
416 | */ |
||
417 | 27 | public function createIndex(Index $index, $table) : void |
|
421 | |||
422 | /** |
||
423 | * Creates a new foreign key. |
||
424 | * |
||
425 | * @param ForeignKeyConstraint $foreignKey The ForeignKey instance. |
||
426 | * @param Table|string $table The name of the table on which the foreign key is to be created. |
||
427 | */ |
||
428 | 52 | public function createForeignKey(ForeignKeyConstraint $foreignKey, $table) : void |
|
432 | |||
433 | /** |
||
434 | * Creates a new view. |
||
435 | */ |
||
436 | 32 | public function createView(View $view) : void |
|
437 | { |
||
438 | 32 | $this->_execSql($this->_platform->getCreateViewSQL($view->getQuotedName($this->_platform), $view->getSql())); |
|
439 | 32 | } |
|
440 | |||
441 | /* dropAndCreate*() Methods */ |
||
442 | |||
443 | /** |
||
444 | * Drops and creates a constraint. |
||
445 | * |
||
446 | * @see dropConstraint() |
||
447 | * @see createConstraint() |
||
448 | * |
||
449 | * @param Table|string $table |
||
450 | */ |
||
451 | public function dropAndCreateConstraint(Constraint $constraint, $table) : void |
||
452 | { |
||
453 | $this->tryMethod('dropConstraint', $constraint, $table); |
||
454 | $this->createConstraint($constraint, $table); |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Drops and creates a new index on a table. |
||
459 | * |
||
460 | * @param Table|string $table The name of the table on which the index is to be created. |
||
461 | */ |
||
462 | 27 | public function dropAndCreateIndex(Index $index, $table) : void |
|
463 | { |
||
464 | 27 | $this->tryMethod('dropIndex', $index->getQuotedName($this->_platform), $table); |
|
465 | 27 | $this->createIndex($index, $table); |
|
466 | 27 | } |
|
467 | |||
468 | /** |
||
469 | * Drops and creates a new foreign key. |
||
470 | * |
||
471 | * @param ForeignKeyConstraint $foreignKey An associative array that defines properties of the foreign key to be created. |
||
472 | * @param Table|string $table The name of the table on which the foreign key is to be created. |
||
473 | */ |
||
474 | public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table) : void |
||
475 | { |
||
476 | $this->tryMethod('dropForeignKey', $foreignKey, $table); |
||
477 | $this->createForeignKey($foreignKey, $table); |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Drops and create a new sequence. |
||
482 | * |
||
483 | * @throws ConnectionException If something fails at database level. |
||
484 | */ |
||
485 | 18 | public function dropAndCreateSequence(Sequence $sequence) : void |
|
486 | { |
||
487 | 18 | $this->tryMethod('dropSequence', $sequence->getQuotedName($this->_platform)); |
|
488 | 18 | $this->createSequence($sequence); |
|
489 | 18 | } |
|
490 | |||
491 | /** |
||
492 | * Drops and creates a new table. |
||
493 | */ |
||
494 | 3319 | public function dropAndCreateTable(Table $table) : void |
|
495 | { |
||
496 | 3319 | $this->tryMethod('dropTable', $table->getQuotedName($this->_platform)); |
|
497 | 3319 | $this->createTable($table); |
|
498 | 3319 | } |
|
499 | |||
500 | /** |
||
501 | * Drops and creates a new database. |
||
502 | */ |
||
503 | 70 | public function dropAndCreateDatabase(string $database) : void |
|
504 | { |
||
505 | 70 | $this->tryMethod('dropDatabase', $database); |
|
506 | 70 | $this->createDatabase($database); |
|
507 | 70 | } |
|
508 | |||
509 | /** |
||
510 | * Drops and creates a new view. |
||
511 | */ |
||
512 | 32 | public function dropAndCreateView(View $view) : void |
|
513 | { |
||
514 | 32 | $this->tryMethod('dropView', $view->getQuotedName($this->_platform)); |
|
515 | 32 | $this->createView($view); |
|
516 | 32 | } |
|
517 | |||
518 | /* alterTable() Methods */ |
||
519 | |||
520 | /** |
||
521 | * Alters an existing tables schema. |
||
522 | */ |
||
523 | 515 | public function alterTable(TableDiff $tableDiff) : void |
|
524 | { |
||
525 | 515 | $queries = $this->_platform->getAlterTableSQL($tableDiff); |
|
526 | |||
527 | 515 | if (! is_array($queries) || ! count($queries)) { |
|
528 | 13 | return; |
|
529 | } |
||
530 | |||
531 | 502 | foreach ($queries as $ddlQuery) { |
|
532 | 502 | $this->_execSql($ddlQuery); |
|
533 | } |
||
534 | 502 | } |
|
535 | |||
536 | /** |
||
537 | * Renames a given table to another name. |
||
538 | */ |
||
539 | 2 | public function renameTable(string $name, string $newName) : void |
|
540 | { |
||
541 | 2 | $tableDiff = new TableDiff($name); |
|
542 | 2 | $tableDiff->newName = $newName; |
|
543 | 2 | $this->alterTable($tableDiff); |
|
544 | 2 | } |
|
545 | |||
546 | /** |
||
547 | * Methods for filtering return values of list*() methods to convert |
||
548 | * the native DBMS data definition to a portable Doctrine definition |
||
549 | */ |
||
550 | |||
551 | /** |
||
552 | * @param array<int, mixed> $databases |
||
553 | * |
||
554 | * @return array<int, string> |
||
555 | */ |
||
556 | 52 | protected function _getPortableDatabasesList(array $databases) : array |
|
557 | { |
||
558 | 52 | $list = []; |
|
559 | 52 | foreach ($databases as $value) { |
|
560 | 52 | $value = $this->_getPortableDatabaseDefinition($value); |
|
561 | |||
562 | 52 | if (! $value) { |
|
563 | continue; |
||
564 | } |
||
565 | |||
566 | 52 | $list[] = $value; |
|
567 | } |
||
568 | |||
569 | 52 | return $list; |
|
570 | } |
||
571 | |||
572 | /** |
||
573 | * Converts a list of namespace names from the native DBMS data definition to a portable Doctrine definition. |
||
574 | * |
||
575 | * @param array<int, array<int, mixed>> $namespaces The list of namespace names in the native DBMS data definition. |
||
576 | * |
||
577 | * @return array<int, string> |
||
578 | */ |
||
579 | 14 | protected function getPortableNamespacesList(array $namespaces) : array |
|
580 | { |
||
581 | 14 | $namespacesList = []; |
|
582 | |||
583 | 14 | foreach ($namespaces as $namespace) { |
|
584 | 14 | $namespacesList[] = $this->getPortableNamespaceDefinition($namespace); |
|
585 | } |
||
586 | |||
587 | 14 | return $namespacesList; |
|
588 | } |
||
589 | |||
590 | /** |
||
591 | * @param array<string, string> $database |
||
592 | */ |
||
593 | protected function _getPortableDatabaseDefinition(array $database) : string |
||
594 | { |
||
595 | return array_shift($database); |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * Converts a namespace definition from the native DBMS data definition to a portable Doctrine definition. |
||
600 | * |
||
601 | * @param array<string|int, mixed> $namespace The native DBMS namespace definition. |
||
602 | */ |
||
603 | protected function getPortableNamespaceDefinition(array $namespace) : string |
||
604 | { |
||
605 | return array_shift($namespace); |
||
606 | } |
||
607 | |||
608 | /** |
||
609 | * @param array<int, array<string, mixed>> $sequences |
||
610 | * |
||
611 | * @return array<int, Sequence> |
||
612 | */ |
||
613 | 21 | protected function _getPortableSequencesList(array $sequences) : array |
|
623 | |||
624 | /** |
||
625 | * @param array<string, mixed> $sequence |
||
626 | * |
||
627 | * @throws DBALException |
||
628 | */ |
||
629 | protected function _getPortableSequenceDefinition(array $sequence) : Sequence |
||
630 | { |
||
631 | throw NotSupported::new('Sequences'); |
||
632 | } |
||
633 | |||
634 | /** |
||
635 | * Independent of the database the keys of the column list result are lowercased. |
||
636 | * |
||
637 | * The name of the created column instance however is kept in its case. |
||
638 | * |
||
639 | * @param array<int, array<string, mixed>> $tableColumns |
||
640 | * |
||
641 | * @return array<string, Column> |
||
642 | */ |
||
643 | 1830 | protected function _getPortableTableColumnList(string $table, string $database, array $tableColumns) : array |
|
644 | { |
||
645 | 1830 | $eventManager = $this->_platform->getEventManager(); |
|
646 | |||
647 | 1830 | $list = []; |
|
648 | 1830 | foreach ($tableColumns as $tableColumn) { |
|
649 | 1813 | $column = null; |
|
650 | 1813 | $defaultPrevented = false; |
|
651 | |||
652 | 1813 | if ($eventManager !== null && $eventManager->hasListeners(Events::onSchemaColumnDefinition)) { |
|
653 | 27 | $eventArgs = new SchemaColumnDefinitionEventArgs($tableColumn, $table, $database, $this->_conn); |
|
654 | 27 | $eventManager->dispatchEvent(Events::onSchemaColumnDefinition, $eventArgs); |
|
655 | |||
656 | 27 | $defaultPrevented = $eventArgs->isDefaultPrevented(); |
|
657 | 27 | $column = $eventArgs->getColumn(); |
|
658 | } |
||
659 | |||
660 | 1813 | if (! $defaultPrevented) { |
|
661 | 1813 | $column = $this->_getPortableTableColumnDefinition($tableColumn); |
|
662 | } |
||
663 | |||
664 | 1813 | if (! $column) { |
|
665 | continue; |
||
666 | } |
||
667 | |||
668 | 1813 | $name = strtolower($column->getQuotedName($this->_platform)); |
|
669 | 1813 | $list[$name] = $column; |
|
670 | } |
||
671 | |||
672 | 1830 | return $list; |
|
673 | } |
||
674 | |||
675 | /** |
||
676 | * Gets Table Column Definition. |
||
677 | * |
||
678 | * @param array<string, mixed> $tableColumn |
||
679 | */ |
||
680 | abstract protected function _getPortableTableColumnDefinition(array $tableColumn) : Column; |
||
681 | |||
682 | /** |
||
683 | * Aggregates and groups the index results according to the required data result. |
||
684 | * |
||
685 | * @param array<int, array<string, mixed>> $tableIndexRows |
||
686 | * |
||
687 | * @return array<string, Index> |
||
688 | */ |
||
689 | 1602 | protected function _getPortableTableIndexesList(array $tableIndexRows, string $tableName) : array |
|
690 | { |
||
691 | 1602 | $result = []; |
|
692 | 1602 | foreach ($tableIndexRows as $tableIndex) { |
|
693 | 653 | $indexName = $keyName = $tableIndex['key_name']; |
|
694 | 653 | if ($tableIndex['primary']) { |
|
695 | 528 | $keyName = 'primary'; |
|
696 | } |
||
697 | 653 | $keyName = strtolower($keyName); |
|
698 | |||
699 | 653 | if (! isset($result[$keyName])) { |
|
700 | $options = [ |
||
701 | 653 | 'lengths' => [], |
|
702 | ]; |
||
703 | |||
704 | 653 | if (isset($tableIndex['where'])) { |
|
705 | 15 | $options['where'] = $tableIndex['where']; |
|
706 | } |
||
707 | |||
708 | 653 | $result[$keyName] = [ |
|
709 | 653 | 'name' => $indexName, |
|
710 | 'columns' => [], |
||
711 | 653 | 'unique' => ! $tableIndex['non_unique'], |
|
712 | 653 | 'primary' => $tableIndex['primary'], |
|
713 | 653 | 'flags' => $tableIndex['flags'] ?? [], |
|
714 | 653 | 'options' => $options, |
|
715 | ]; |
||
716 | } |
||
717 | |||
718 | 653 | $result[$keyName]['columns'][] = $tableIndex['column_name']; |
|
719 | 653 | $result[$keyName]['options']['lengths'][] = $tableIndex['length'] ?? null; |
|
720 | } |
||
721 | |||
722 | 1602 | $eventManager = $this->_platform->getEventManager(); |
|
723 | |||
724 | 1602 | $indexes = []; |
|
725 | 1602 | foreach ($result as $indexKey => $data) { |
|
726 | 653 | $index = null; |
|
727 | 653 | $defaultPrevented = false; |
|
728 | |||
729 | 653 | if ($eventManager !== null && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) { |
|
730 | 27 | $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn); |
|
731 | 27 | $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs); |
|
732 | |||
733 | 27 | $defaultPrevented = $eventArgs->isDefaultPrevented(); |
|
734 | 27 | $index = $eventArgs->getIndex(); |
|
735 | } |
||
736 | |||
737 | 653 | if (! $defaultPrevented) { |
|
738 | 653 | $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary'], $data['flags'], $data['options']); |
|
739 | } |
||
740 | |||
741 | 653 | if (! $index) { |
|
742 | continue; |
||
743 | } |
||
744 | |||
745 | 653 | $indexes[$indexKey] = $index; |
|
746 | } |
||
747 | |||
748 | 1602 | return $indexes; |
|
749 | } |
||
750 | |||
751 | /** |
||
752 | * @param array<int, array<string, mixed>> $tables |
||
753 | * |
||
754 | * @return array<int, string> |
||
755 | */ |
||
756 | 397 | protected function _getPortableTablesList(array $tables) : array |
|
757 | { |
||
758 | 397 | $list = []; |
|
759 | 397 | foreach ($tables as $value) { |
|
760 | 370 | $value = $this->_getPortableTableDefinition($value); |
|
761 | |||
762 | 370 | if (! $value) { |
|
763 | continue; |
||
764 | } |
||
765 | |||
766 | 370 | $list[] = $value; |
|
767 | } |
||
768 | |||
769 | 397 | return $list; |
|
770 | } |
||
771 | |||
772 | /** |
||
773 | * @param array<string, string> $table |
||
774 | */ |
||
775 | protected function _getPortableTableDefinition(array $table) : string |
||
779 | |||
780 | /** |
||
781 | * @param array<int, array<string, mixed>> $users |
||
782 | * |
||
783 | * @return array<int, array<string, mixed>> |
||
784 | */ |
||
785 | protected function _getPortableUsersList(array $users) : array |
||
786 | { |
||
787 | $list = []; |
||
788 | foreach ($users as $value) { |
||
789 | $value = $this->_getPortableUserDefinition($value); |
||
790 | |||
791 | if (! $value) { |
||
792 | continue; |
||
793 | } |
||
794 | |||
795 | $list[] = $value; |
||
796 | } |
||
797 | |||
798 | return $list; |
||
799 | } |
||
800 | |||
801 | /** |
||
802 | * @param array<string, mixed> $user |
||
803 | * |
||
804 | * @return array<string, mixed> |
||
805 | */ |
||
806 | protected function _getPortableUserDefinition(array $user) : array |
||
807 | { |
||
808 | return $user; |
||
809 | } |
||
810 | |||
811 | /** |
||
812 | * @param array<int, array<string, mixed>> $views |
||
813 | * |
||
814 | * @return array<string, View> |
||
815 | */ |
||
816 | 27 | protected function _getPortableViewsList(array $views) : array |
|
817 | { |
||
818 | 27 | $list = []; |
|
819 | 27 | foreach ($views as $value) { |
|
820 | 27 | $view = $this->_getPortableViewDefinition($value); |
|
821 | 27 | $name = strtolower($view->getQuotedName($this->_platform)); |
|
822 | 27 | $list[$name] = $view; |
|
823 | } |
||
824 | |||
825 | 27 | return $list; |
|
826 | } |
||
827 | |||
828 | /** |
||
829 | * @param array<string, mixed> $view |
||
830 | */ |
||
831 | protected function _getPortableViewDefinition(array $view) : View |
||
832 | { |
||
833 | throw NotSupported::new('Views'); |
||
834 | } |
||
835 | |||
836 | /** |
||
837 | * @param array<int|string, array<string, mixed>> $tableForeignKeys |
||
838 | * |
||
839 | * @return array<int, ForeignKeyConstraint> |
||
840 | */ |
||
841 | 381 | protected function _getPortableTableForeignKeysList(array $tableForeignKeys) : array |
|
842 | { |
||
843 | 381 | $list = []; |
|
844 | |||
845 | 381 | foreach ($tableForeignKeys as $value) { |
|
846 | 86 | $list[] = $this->_getPortableTableForeignKeyDefinition($value); |
|
847 | } |
||
848 | |||
849 | 381 | return $list; |
|
850 | } |
||
851 | |||
852 | /** |
||
853 | * @param array<string, mixed> $tableForeignKey |
||
854 | */ |
||
855 | protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey) : ForeignKeyConstraint |
||
856 | { |
||
857 | throw NotSupported::new('ForeignKey'); |
||
858 | } |
||
859 | |||
860 | /** |
||
861 | * @param array<int, string>|string $sql |
||
862 | */ |
||
863 | 4574 | protected function _execSql($sql) : void |
|
869 | |||
870 | /** |
||
871 | * Creates a schema instance for the current database. |
||
872 | */ |
||
873 | 125 | public function createSchema() : Schema |
|
874 | { |
||
875 | 125 | $namespaces = []; |
|
876 | |||
877 | 125 | if ($this->_platform->supportsSchemas()) { |
|
878 | 7 | $namespaces = $this->listNamespaceNames(); |
|
879 | } |
||
880 | |||
881 | 125 | $sequences = []; |
|
882 | |||
883 | 125 | if ($this->_platform->supportsSequences()) { |
|
884 | 9 | $sequences = $this->listSequences(); |
|
885 | } |
||
886 | |||
887 | 125 | $tables = $this->listTables(); |
|
888 | |||
889 | 125 | return new Schema($tables, $sequences, $this->createSchemaConfig(), $namespaces); |
|
890 | } |
||
891 | |||
892 | /** |
||
893 | * Creates the configuration for this schema. |
||
894 | */ |
||
895 | 485 | public function createSchemaConfig() : SchemaConfig |
|
896 | { |
||
897 | 485 | $schemaConfig = new SchemaConfig(); |
|
898 | 485 | $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength()); |
|
899 | |||
900 | 485 | $searchPaths = $this->getSchemaSearchPaths(); |
|
901 | 485 | if (isset($searchPaths[0])) { |
|
902 | 458 | $schemaConfig->setName($searchPaths[0]); |
|
903 | } |
||
904 | |||
905 | 485 | $params = $this->_conn->getParams(); |
|
906 | 485 | if (! isset($params['defaultTableOptions'])) { |
|
907 | 485 | $params['defaultTableOptions'] = []; |
|
908 | } |
||
909 | 485 | if (! isset($params['defaultTableOptions']['charset']) && isset($params['charset'])) { |
|
910 | 27 | $params['defaultTableOptions']['charset'] = $params['charset']; |
|
911 | } |
||
912 | 485 | $schemaConfig->setDefaultTableOptions($params['defaultTableOptions']); |
|
913 | |||
914 | 485 | return $schemaConfig; |
|
915 | } |
||
916 | |||
917 | /** |
||
918 | * The search path for namespaces in the currently connected database. |
||
919 | * |
||
920 | * The first entry is usually the default namespace in the Schema. All |
||
921 | * further namespaces contain tables/sequences which can also be addressed |
||
922 | * with a short, not full-qualified name. |
||
923 | * |
||
924 | * For databases that don't support subschema/namespaces this method |
||
925 | * returns the name of the currently connected database. |
||
926 | * |
||
927 | * @return array<int, string> |
||
928 | */ |
||
929 | 410 | public function getSchemaSearchPaths() : array |
|
939 | |||
940 | /** |
||
941 | * Given a table comment this method tries to extract a type hint for Doctrine Type. If the type hint is found, |
||
942 | * it's removed from the comment. |
||
943 | * |
||
944 | * @return string|null The extracted Doctrine type or NULL of the type hint was not found. |
||
945 | */ |
||
946 | 2002 | final protected function extractDoctrineTypeFromComment(?string &$comment) : ?string |
|
956 | |||
957 | /** |
||
958 | * @throws DatabaseRequired |
||
959 | */ |
||
960 | 1916 | private function ensureDatabase(?string $database, string $methodName) : string |
|
968 | } |
||
969 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.