Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MySqlPlatform 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 MySqlPlatform, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class MySqlPlatform extends AbstractPlatform |
||
42 | { |
||
43 | const LENGTH_LIMIT_TINYTEXT = 255; |
||
44 | const LENGTH_LIMIT_TEXT = 65535; |
||
45 | const LENGTH_LIMIT_MEDIUMTEXT = 16777215; |
||
46 | |||
47 | const LENGTH_LIMIT_TINYBLOB = 255; |
||
48 | const LENGTH_LIMIT_BLOB = 65535; |
||
49 | const LENGTH_LIMIT_MEDIUMBLOB = 16777215; |
||
50 | |||
51 | /** |
||
52 | * Adds MySQL-specific LIMIT clause to the query |
||
53 | * 18446744073709551615 is 2^64-1 maximum of unsigned BIGINT the biggest limit possible |
||
54 | * |
||
55 | * @param string $query |
||
56 | * @param integer $limit |
||
57 | * @param integer $offset |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 9 | protected function doModifyLimitQuery($query, $limit, $offset) |
|
74 | |||
75 | /** |
||
76 | * {@inheritDoc} |
||
77 | */ |
||
78 | 82 | public function getIdentifierQuoteCharacter() |
|
82 | |||
83 | /** |
||
84 | * {@inheritDoc} |
||
85 | */ |
||
86 | 3 | public function getRegexpExpression() |
|
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | public function getGuidExpression() |
||
98 | |||
99 | /** |
||
100 | * {@inheritDoc} |
||
101 | */ |
||
102 | View Code Duplication | public function getLocateExpression($str, $substr, $startPos = false) |
|
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | 3 | public function getConcatExpression() |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) |
||
130 | |||
131 | /** |
||
132 | * {@inheritDoc} |
||
133 | */ |
||
134 | public function getDateDiffExpression($date1, $date2) |
||
138 | |||
139 | /** |
||
140 | * {@inheritDoc} |
||
141 | */ |
||
142 | 3 | public function getListDatabasesSQL() |
|
146 | |||
147 | /** |
||
148 | * {@inheritDoc} |
||
149 | */ |
||
150 | public function getListTableConstraintsSQL($table) |
||
154 | |||
155 | /** |
||
156 | * {@inheritDoc} |
||
157 | * |
||
158 | * Two approaches to listing the table indexes. The information_schema is |
||
159 | * preferred, because it doesn't cause problems with SQL keywords such as "order" or "table". |
||
160 | */ |
||
161 | 6 | View Code Duplication | public function getListTableIndexesSQL($table, $currentDatabase = null) |
176 | |||
177 | /** |
||
178 | * {@inheritDoc} |
||
179 | */ |
||
180 | 3 | public function getListViewsSQL($database) |
|
186 | |||
187 | /** |
||
188 | * {@inheritDoc} |
||
189 | */ |
||
190 | 9 | public function getListTableForeignKeysSQL($table, $database = null) |
|
212 | |||
213 | /** |
||
214 | * {@inheritDoc} |
||
215 | */ |
||
216 | public function getCreateViewSQL($name, $sql) |
||
220 | |||
221 | /** |
||
222 | * {@inheritDoc} |
||
223 | */ |
||
224 | public function getDropViewSQL($name) |
||
228 | |||
229 | /** |
||
230 | * {@inheritDoc} |
||
231 | */ |
||
232 | 45 | protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) |
|
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | 3 | protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed) |
|
245 | |||
246 | /** |
||
247 | * Gets the SQL snippet used to declare a CLOB column type. |
||
248 | * TINYTEXT : 2 ^ 8 - 1 = 255 |
||
249 | * TEXT : 2 ^ 16 - 1 = 65535 |
||
250 | * MEDIUMTEXT : 2 ^ 24 - 1 = 16777215 |
||
251 | * LONGTEXT : 2 ^ 32 - 1 = 4294967295 |
||
252 | * |
||
253 | * @param array $field |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 17 | View Code Duplication | public function getClobTypeDeclarationSQL(array $field) |
277 | |||
278 | /** |
||
279 | * {@inheritDoc} |
||
280 | */ |
||
281 | 3 | View Code Duplication | public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) |
289 | |||
290 | /** |
||
291 | * {@inheritDoc} |
||
292 | */ |
||
293 | public function getDateTypeDeclarationSQL(array $fieldDeclaration) |
||
297 | |||
298 | /** |
||
299 | * {@inheritDoc} |
||
300 | */ |
||
301 | public function getTimeTypeDeclarationSQL(array $fieldDeclaration) |
||
305 | |||
306 | /** |
||
307 | * {@inheritDoc} |
||
308 | */ |
||
309 | 3 | public function getBooleanTypeDeclarationSQL(array $field) |
|
313 | |||
314 | /** |
||
315 | * Obtain DBMS specific SQL code portion needed to set the COLLATION |
||
316 | * of a field declaration to be used in statements like CREATE TABLE. |
||
317 | * |
||
318 | * @deprecated Deprecated since version 2.5, Use {@link self::getColumnCollationDeclarationSQL()} instead. |
||
319 | * |
||
320 | * @param string $collation name of the collation |
||
321 | * |
||
322 | * @return string DBMS specific SQL code portion needed to set the COLLATION |
||
323 | * of a field declaration. |
||
324 | */ |
||
325 | public function getCollationFieldDeclaration($collation) |
||
329 | |||
330 | /** |
||
331 | * {@inheritDoc} |
||
332 | * |
||
333 | * MySql prefers "autoincrement" identity columns since sequences can only |
||
334 | * be emulated with a table. |
||
335 | */ |
||
336 | 3 | public function prefersIdentityColumns() |
|
340 | |||
341 | /** |
||
342 | * {@inheritDoc} |
||
343 | * |
||
344 | * MySql supports this through AUTO_INCREMENT columns. |
||
345 | */ |
||
346 | 3 | public function supportsIdentityColumns() |
|
350 | |||
351 | /** |
||
352 | * {@inheritDoc} |
||
353 | */ |
||
354 | 99 | public function supportsInlineColumnComments() |
|
358 | |||
359 | /** |
||
360 | * {@inheritDoc} |
||
361 | */ |
||
362 | public function supportsColumnCollation() |
||
366 | |||
367 | /** |
||
368 | * {@inheritDoc} |
||
369 | */ |
||
370 | public function getListTablesSQL() |
||
374 | |||
375 | /** |
||
376 | * {@inheritDoc} |
||
377 | */ |
||
378 | 6 | View Code Duplication | public function getListTableColumnsSQL($table, $database = null) |
393 | |||
394 | /** |
||
395 | * {@inheritDoc} |
||
396 | */ |
||
397 | 3 | public function getCreateDatabaseSQL($name) |
|
401 | |||
402 | /** |
||
403 | * {@inheritDoc} |
||
404 | */ |
||
405 | 3 | public function getDropDatabaseSQL($name) |
|
409 | |||
410 | /** |
||
411 | * {@inheritDoc} |
||
412 | */ |
||
413 | 45 | protected function _getCreateTableSQL($tableName, array $columns, array $options = []) |
|
462 | |||
463 | /** |
||
464 | * Tells whether a field type supports declaration of a default value. |
||
465 | * |
||
466 | * MySQL (as of 5.7.19) does not support default values for Blob and Text |
||
467 | * columns while MariaDB 10.2.1 does. |
||
468 | */ |
||
469 | 68 | protected function isDefaultValueSupportedForType(Type $field) : bool |
|
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | 102 | public function getDefaultValueDeclarationSQL($field) |
|
486 | |||
487 | /** |
||
488 | * Build SQL for table options |
||
489 | * |
||
490 | * @param array $options |
||
491 | * |
||
492 | * @return string |
||
493 | */ |
||
494 | 45 | private function buildTableOptions(array $options) |
|
542 | |||
543 | /** |
||
544 | * Build SQL for partition options. |
||
545 | * |
||
546 | * @param array $options |
||
547 | * |
||
548 | * @return string |
||
549 | */ |
||
550 | 45 | private function buildPartitionOptions(array $options) |
|
556 | |||
557 | /** |
||
558 | * {@inheritDoc} |
||
559 | */ |
||
560 | 79 | public function getAlterTableSQL(TableDiff $diff) |
|
561 | { |
||
562 | 79 | $columnSql = []; |
|
563 | 79 | $queryParts = []; |
|
564 | 79 | if ($diff->newName !== false) { |
|
565 | 6 | $queryParts[] = 'RENAME TO ' . $diff->getNewName()->getQuotedName($this); |
|
566 | } |
||
567 | |||
568 | 79 | View Code Duplication | foreach ($diff->addedColumns as $column) { |
569 | 18 | if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { |
|
570 | continue; |
||
571 | } |
||
572 | |||
573 | 18 | $columnArray = $column->toArray(); |
|
574 | 18 | $columnArray['comment'] = $this->getColumnComment($column); |
|
575 | 18 | $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
|
576 | } |
||
577 | |||
578 | 79 | View Code Duplication | foreach ($diff->removedColumns as $column) { |
579 | 9 | if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) { |
|
580 | continue; |
||
581 | } |
||
582 | |||
583 | 9 | $queryParts[] = 'DROP ' . $column->getQuotedName($this); |
|
584 | } |
||
585 | |||
586 | 79 | View Code Duplication | foreach ($diff->changedColumns as $columnDiff) { |
587 | 23 | if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) { |
|
588 | continue; |
||
589 | } |
||
590 | |||
591 | /* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */ |
||
592 | 23 | $column = $columnDiff->column; |
|
593 | 23 | $columnArray = $column->toArray(); |
|
594 | |||
595 | // Don't propagate default value changes for unsupported column types. |
||
596 | 23 | if ($columnDiff->hasChanged('default') && |
|
597 | 23 | count($columnDiff->changedProperties) === 1 && |
|
598 | 23 | ! $this->isDefaultValueSupportedForType($columnArray['type']) |
|
599 | ) { |
||
600 | 2 | continue; |
|
601 | } |
||
602 | |||
603 | 21 | $columnArray['comment'] = $this->getColumnComment($column); |
|
604 | 21 | $queryParts[] = 'CHANGE ' . ($columnDiff->getOldColumnName()->getQuotedName($this)) . ' ' |
|
605 | 21 | . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
|
606 | } |
||
607 | |||
608 | 79 | View Code Duplication | foreach ($diff->renamedColumns as $oldColumnName => $column) { |
609 | 12 | if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) { |
|
610 | continue; |
||
611 | } |
||
612 | |||
613 | 12 | $oldColumnName = new Identifier($oldColumnName); |
|
614 | 12 | $columnArray = $column->toArray(); |
|
615 | 12 | $columnArray['comment'] = $this->getColumnComment($column); |
|
616 | 12 | $queryParts[] = 'CHANGE ' . $oldColumnName->getQuotedName($this) . ' ' |
|
617 | 12 | . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
|
618 | } |
||
619 | |||
620 | 79 | if (isset($diff->addedIndexes['primary'])) { |
|
621 | 10 | $keyColumns = array_unique(array_values($diff->addedIndexes['primary']->getColumns())); |
|
622 | 10 | $queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')'; |
|
623 | 10 | unset($diff->addedIndexes['primary']); |
|
624 | } |
||
625 | |||
626 | 79 | $sql = []; |
|
627 | 79 | $tableSql = []; |
|
628 | |||
629 | 79 | View Code Duplication | if ( ! $this->onSchemaAlterTable($diff, $tableSql)) { |
630 | 79 | if (count($queryParts) > 0) { |
|
631 | 40 | $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(", ", $queryParts); |
|
632 | } |
||
633 | 79 | $sql = array_merge( |
|
634 | 79 | $this->getPreAlterTableIndexForeignKeySQL($diff), |
|
635 | 79 | $sql, |
|
636 | 79 | $this->getPostAlterTableIndexForeignKeySQL($diff) |
|
637 | ); |
||
638 | } |
||
639 | |||
640 | 79 | return array_merge($sql, $tableSql, $columnSql); |
|
641 | } |
||
642 | |||
643 | /** |
||
644 | * {@inheritDoc} |
||
645 | */ |
||
646 | 79 | protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff) |
|
647 | { |
||
648 | 79 | $sql = []; |
|
649 | 79 | $table = $diff->getName($this)->getQuotedName($this); |
|
650 | |||
651 | 79 | foreach ($diff->changedIndexes as $changedIndex) { |
|
652 | 16 | $sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $changedIndex)); |
|
653 | } |
||
654 | |||
655 | 79 | foreach ($diff->removedIndexes as $remKey => $remIndex) { |
|
656 | 9 | $sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $remIndex)); |
|
657 | |||
658 | 9 | foreach ($diff->addedIndexes as $addKey => $addIndex) { |
|
659 | 3 | if ($remIndex->getColumns() == $addIndex->getColumns()) { |
|
660 | |||
661 | 3 | $indexClause = 'INDEX ' . $addIndex->getName(); |
|
662 | |||
663 | 3 | if ($addIndex->isPrimary()) { |
|
664 | $indexClause = 'PRIMARY KEY'; |
||
665 | 3 | } elseif ($addIndex->isUnique()) { |
|
666 | 3 | $indexClause = 'UNIQUE INDEX ' . $addIndex->getName(); |
|
667 | } |
||
668 | |||
669 | 3 | $query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', '; |
|
670 | 3 | $query .= 'ADD ' . $indexClause; |
|
671 | 3 | $query .= ' (' . $this->getIndexFieldDeclarationListSQL($addIndex->getQuotedColumns($this)) . ')'; |
|
672 | |||
673 | 3 | $sql[] = $query; |
|
674 | |||
675 | 3 | unset($diff->removedIndexes[$remKey]); |
|
676 | 3 | unset($diff->addedIndexes[$addKey]); |
|
677 | |||
678 | 9 | break; |
|
679 | } |
||
680 | } |
||
681 | } |
||
682 | |||
683 | 79 | $engine = 'INNODB'; |
|
684 | |||
685 | 79 | if ($diff->fromTable instanceof Table && $diff->fromTable->hasOption('engine')) { |
|
686 | 3 | $engine = strtoupper(trim($diff->fromTable->getOption('engine'))); |
|
687 | } |
||
688 | |||
689 | // Suppress foreign key constraint propagation on non-supporting engines. |
||
690 | 79 | if ('INNODB' !== $engine) { |
|
691 | 3 | $diff->addedForeignKeys = []; |
|
692 | 3 | $diff->changedForeignKeys = []; |
|
693 | 3 | $diff->removedForeignKeys = []; |
|
694 | } |
||
695 | |||
696 | 79 | $sql = array_merge( |
|
697 | 79 | $sql, |
|
698 | 79 | $this->getPreAlterTableAlterIndexForeignKeySQL($diff), |
|
699 | 79 | parent::getPreAlterTableIndexForeignKeySQL($diff), |
|
700 | 79 | $this->getPreAlterTableRenameIndexForeignKeySQL($diff) |
|
701 | ); |
||
702 | |||
703 | 79 | return $sql; |
|
704 | } |
||
705 | |||
706 | /** |
||
707 | * @param TableDiff $diff |
||
708 | * @param Index $index |
||
709 | * |
||
710 | * @return string[] |
||
711 | */ |
||
712 | 25 | private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index) |
|
713 | { |
||
714 | 25 | $sql = []; |
|
715 | |||
716 | 25 | if (! $index->isPrimary() || ! $diff->fromTable instanceof Table) { |
|
717 | 9 | return $sql; |
|
718 | } |
||
719 | |||
720 | 16 | $tableName = $diff->getName($this)->getQuotedName($this); |
|
721 | |||
722 | // Dropping primary keys requires to unset autoincrement attribute on the particular column first. |
||
723 | 16 | View Code Duplication | foreach ($index->getColumns() as $columnName) { |
724 | 16 | if (! $diff->fromTable->hasColumn($columnName)) { |
|
725 | 3 | continue; |
|
726 | } |
||
727 | |||
728 | 16 | $column = $diff->fromTable->getColumn($columnName); |
|
729 | |||
730 | 16 | if ($column->getAutoincrement() === true) { |
|
731 | 9 | $column->setAutoincrement(false); |
|
732 | |||
733 | 9 | $sql[] = 'ALTER TABLE ' . $tableName . ' MODIFY ' . |
|
734 | 9 | $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
|
735 | |||
736 | // original autoincrement information might be needed later on by other parts of the table alteration |
||
737 | 16 | $column->setAutoincrement(true); |
|
738 | } |
||
739 | } |
||
740 | |||
741 | 16 | return $sql; |
|
742 | } |
||
743 | |||
744 | /** |
||
745 | * @param TableDiff $diff The table diff to gather the SQL for. |
||
746 | * |
||
747 | * @return array |
||
748 | */ |
||
749 | 79 | private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff) |
|
750 | { |
||
751 | 79 | $sql = []; |
|
752 | 79 | $table = $diff->getName($this)->getQuotedName($this); |
|
753 | |||
754 | 79 | foreach ($diff->changedIndexes as $changedIndex) { |
|
755 | // Changed primary key |
||
756 | 16 | if ($changedIndex->isPrimary() && $diff->fromTable instanceof Table) { |
|
757 | 13 | View Code Duplication | foreach ($diff->fromTable->getPrimaryKeyColumns() as $columnName) { |
758 | 13 | $column = $diff->fromTable->getColumn($columnName); |
|
759 | |||
760 | // Check if an autoincrement column was dropped from the primary key. |
||
761 | 13 | if ($column->getAutoincrement() && ! in_array($columnName, $changedIndex->getColumns())) { |
|
762 | // The autoincrement attribute needs to be removed from the dropped column |
||
763 | // before we can drop and recreate the primary key. |
||
764 | 3 | $column->setAutoincrement(false); |
|
765 | |||
766 | 3 | $sql[] = 'ALTER TABLE ' . $table . ' MODIFY ' . |
|
767 | 3 | $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
|
768 | |||
769 | // Restore the autoincrement attribute as it might be needed later on |
||
770 | // by other parts of the table alteration. |
||
771 | 16 | $column->setAutoincrement(true); |
|
772 | } |
||
773 | } |
||
774 | } |
||
775 | } |
||
776 | |||
777 | 79 | return $sql; |
|
778 | } |
||
779 | |||
780 | /** |
||
781 | * @param TableDiff $diff The table diff to gather the SQL for. |
||
782 | * |
||
783 | * @return array |
||
784 | */ |
||
785 | 53 | protected function getPreAlterTableRenameIndexForeignKeySQL(TableDiff $diff) |
|
786 | { |
||
787 | 53 | $sql = []; |
|
788 | 53 | $tableName = $diff->getName($this)->getQuotedName($this); |
|
789 | |||
790 | 53 | View Code Duplication | foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) { |
791 | 2 | if (! in_array($foreignKey, $diff->changedForeignKeys, true)) { |
|
792 | 2 | $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName); |
|
793 | } |
||
794 | } |
||
795 | |||
796 | 53 | return $sql; |
|
797 | } |
||
798 | |||
799 | /** |
||
800 | * Returns the remaining foreign key constraints that require one of the renamed indexes. |
||
801 | * |
||
802 | * "Remaining" here refers to the diff between the foreign keys currently defined in the associated |
||
803 | * table and the foreign keys to be removed. |
||
804 | * |
||
805 | * @param TableDiff $diff The table diff to evaluate. |
||
806 | * |
||
807 | * @return array |
||
808 | */ |
||
809 | 53 | private function getRemainingForeignKeyConstraintsRequiringRenamedIndexes(TableDiff $diff) |
|
810 | { |
||
811 | 53 | if (empty($diff->renamedIndexes) || ! $diff->fromTable instanceof Table) { |
|
812 | 43 | return []; |
|
813 | } |
||
814 | |||
815 | 10 | $foreignKeys = []; |
|
816 | /** @var \Doctrine\DBAL\Schema\ForeignKeyConstraint[] $remainingForeignKeys */ |
||
817 | 10 | $remainingForeignKeys = array_diff_key( |
|
818 | 10 | $diff->fromTable->getForeignKeys(), |
|
819 | 10 | $diff->removedForeignKeys |
|
820 | ); |
||
821 | |||
822 | 10 | foreach ($remainingForeignKeys as $foreignKey) { |
|
823 | 2 | foreach ($diff->renamedIndexes as $index) { |
|
824 | 2 | if ($foreignKey->intersectsIndexColumns($index)) { |
|
825 | 2 | $foreignKeys[] = $foreignKey; |
|
826 | |||
827 | 2 | break; |
|
828 | } |
||
829 | } |
||
830 | } |
||
831 | |||
832 | 10 | return $foreignKeys; |
|
833 | } |
||
834 | |||
835 | /** |
||
836 | * {@inheritdoc} |
||
837 | */ |
||
838 | 79 | protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff) |
|
839 | { |
||
840 | 79 | return array_merge( |
|
841 | 79 | parent::getPostAlterTableIndexForeignKeySQL($diff), |
|
842 | 79 | $this->getPostAlterTableRenameIndexForeignKeySQL($diff) |
|
843 | ); |
||
844 | } |
||
845 | |||
846 | /** |
||
847 | * @param TableDiff $diff The table diff to gather the SQL for. |
||
848 | * |
||
849 | * @return array |
||
850 | */ |
||
851 | 53 | protected function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff) |
|
852 | { |
||
853 | 53 | $sql = []; |
|
854 | 53 | $tableName = (false !== $diff->newName) |
|
855 | 4 | ? $diff->getNewName()->getQuotedName($this) |
|
856 | 53 | : $diff->getName($this)->getQuotedName($this); |
|
857 | |||
858 | 53 | View Code Duplication | foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) { |
859 | 2 | if (! in_array($foreignKey, $diff->changedForeignKeys, true)) { |
|
860 | 2 | $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName); |
|
861 | } |
||
862 | } |
||
863 | |||
864 | 53 | return $sql; |
|
865 | } |
||
866 | |||
867 | /** |
||
868 | * {@inheritDoc} |
||
869 | */ |
||
870 | 45 | View Code Duplication | protected function getCreateIndexSQLFlags(Index $index) |
871 | { |
||
872 | 45 | $type = ''; |
|
873 | 45 | if ($index->isUnique()) { |
|
874 | 9 | $type .= 'UNIQUE '; |
|
875 | 36 | } elseif ($index->hasFlag('fulltext')) { |
|
876 | 3 | $type .= 'FULLTEXT '; |
|
877 | 33 | } elseif ($index->hasFlag('spatial')) { |
|
878 | 3 | $type .= 'SPATIAL '; |
|
879 | } |
||
880 | |||
881 | 45 | return $type; |
|
882 | } |
||
883 | |||
884 | /** |
||
885 | * {@inheritDoc} |
||
886 | */ |
||
887 | 56 | public function getIntegerTypeDeclarationSQL(array $field) |
|
888 | { |
||
889 | 56 | return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
|
890 | } |
||
891 | |||
892 | /** |
||
893 | * {@inheritDoc} |
||
894 | */ |
||
895 | public function getBigIntTypeDeclarationSQL(array $field) |
||
896 | { |
||
897 | return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
||
898 | } |
||
899 | |||
900 | /** |
||
901 | * {@inheritDoc} |
||
902 | */ |
||
903 | public function getSmallIntTypeDeclarationSQL(array $field) |
||
904 | { |
||
905 | return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
||
906 | } |
||
907 | |||
908 | /** |
||
909 | * {@inheritdoc} |
||
910 | */ |
||
911 | 18 | public function getFloatDeclarationSQL(array $field) |
|
912 | { |
||
913 | 18 | return 'DOUBLE PRECISION' . $this->getUnsignedDeclaration($field); |
|
914 | } |
||
915 | |||
916 | /** |
||
917 | * {@inheritdoc} |
||
918 | */ |
||
919 | 18 | public function getDecimalTypeDeclarationSQL(array $columnDef) |
|
923 | |||
924 | /** |
||
925 | * Get unsigned declaration for a column. |
||
926 | * |
||
927 | * @param array $columnDef |
||
928 | * |
||
929 | * @return string |
||
930 | */ |
||
931 | 92 | private function getUnsignedDeclaration(array $columnDef) |
|
935 | |||
936 | /** |
||
937 | * {@inheritDoc} |
||
938 | */ |
||
939 | 56 | protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
|
940 | { |
||
941 | 56 | $autoinc = ''; |
|
942 | 56 | if ( ! empty($columnDef['autoincrement'])) { |
|
943 | 9 | $autoinc = ' AUTO_INCREMENT'; |
|
944 | } |
||
945 | |||
946 | 56 | return $this->getUnsignedDeclaration($columnDef) . $autoinc; |
|
947 | } |
||
948 | |||
949 | /** |
||
950 | * {@inheritDoc} |
||
951 | */ |
||
952 | 21 | public function getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey) |
|
962 | |||
963 | /** |
||
964 | * {@inheritDoc} |
||
965 | */ |
||
966 | 32 | View Code Duplication | public function getDropIndexSQL($index, $table=null) |
967 | { |
||
968 | 32 | if ($index instanceof Index) { |
|
969 | 22 | $indexName = $index->getQuotedName($this); |
|
970 | 10 | } elseif (is_string($index)) { |
|
971 | 10 | $indexName = $index; |
|
972 | } else { |
||
973 | throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.'); |
||
974 | } |
||
975 | |||
976 | 32 | if ($table instanceof Table) { |
|
977 | $table = $table->getQuotedName($this); |
||
978 | 32 | } elseif (!is_string($table)) { |
|
979 | throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.'); |
||
980 | } |
||
981 | |||
982 | 32 | if ($index instanceof Index && $index->isPrimary()) { |
|
983 | // mysql primary keys are always named "PRIMARY", |
||
984 | // so we cannot use them in statements because of them being keyword. |
||
985 | 19 | return $this->getDropPrimaryKeySQL($table); |
|
986 | } |
||
987 | |||
988 | 13 | return 'DROP INDEX ' . $indexName . ' ON ' . $table; |
|
989 | } |
||
990 | |||
991 | /** |
||
992 | * @param string $table |
||
993 | * |
||
994 | * @return string |
||
995 | */ |
||
996 | 19 | protected function getDropPrimaryKeySQL($table) |
|
997 | { |
||
998 | 19 | return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY'; |
|
999 | } |
||
1000 | |||
1001 | /** |
||
1002 | * {@inheritDoc} |
||
1003 | */ |
||
1004 | 3 | public function getSetTransactionIsolationSQL($level) |
|
1005 | { |
||
1006 | 3 | return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); |
|
1007 | } |
||
1008 | |||
1009 | /** |
||
1010 | * {@inheritDoc} |
||
1011 | */ |
||
1012 | 6 | public function getName() |
|
1013 | { |
||
1014 | 6 | return 'mysql'; |
|
1015 | } |
||
1016 | |||
1017 | /** |
||
1018 | * {@inheritDoc} |
||
1019 | */ |
||
1020 | public function getReadLockSQL() |
||
1021 | { |
||
1022 | return 'LOCK IN SHARE MODE'; |
||
1023 | } |
||
1024 | |||
1025 | /** |
||
1026 | * {@inheritDoc} |
||
1027 | */ |
||
1028 | 17 | protected function initializeDoctrineTypeMappings() |
|
1063 | |||
1064 | /** |
||
1065 | * {@inheritDoc} |
||
1066 | */ |
||
1067 | 45 | public function getVarcharMaxLength() |
|
1068 | { |
||
1069 | 45 | return 65535; |
|
1070 | } |
||
1071 | |||
1072 | /** |
||
1073 | * {@inheritdoc} |
||
1074 | */ |
||
1075 | 6 | public function getBinaryMaxLength() |
|
1076 | { |
||
1077 | 6 | return 65535; |
|
1078 | } |
||
1079 | |||
1080 | /** |
||
1081 | * {@inheritDoc} |
||
1082 | */ |
||
1083 | 62 | protected function getReservedKeywordsClass() |
|
1084 | { |
||
1087 | |||
1088 | /** |
||
1089 | * {@inheritDoc} |
||
1090 | * |
||
1091 | * MySQL commits a transaction implicitly when DROP TABLE is executed, however not |
||
1092 | * if DROP TEMPORARY TABLE is executed. |
||
1093 | */ |
||
1094 | View Code Duplication | public function getDropTemporaryTableSQL($table) |
|
1104 | |||
1105 | /** |
||
1106 | * Gets the SQL Snippet used to declare a BLOB column type. |
||
1107 | * TINYBLOB : 2 ^ 8 - 1 = 255 |
||
1108 | * BLOB : 2 ^ 16 - 1 = 65535 |
||
1109 | * MEDIUMBLOB : 2 ^ 24 - 1 = 16777215 |
||
1110 | * LONGBLOB : 2 ^ 32 - 1 = 4294967295 |
||
1111 | * |
||
1112 | * @param array $field |
||
1113 | * |
||
1114 | * @return string |
||
1115 | */ |
||
1116 | 9 | View Code Duplication | public function getBlobTypeDeclarationSQL(array $field) |
1136 | |||
1137 | /** |
||
1138 | * {@inheritdoc} |
||
1139 | */ |
||
1140 | 80 | public function quoteStringLiteral($str) |
|
1146 | |||
1147 | /** |
||
1148 | * {@inheritdoc} |
||
1149 | */ |
||
1150 | 1 | public function getDefaultTransactionIsolationLevel() |
|
1154 | } |
||
1155 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.