| @@ 660-695 (lines=36) @@ | ||
| 657 | /** |
|
| 658 | * {@inheritdoc} |
|
| 659 | */ |
|
| 660 | public function dropForeignKey($tableName, $columns, $constraint = null) |
|
| 661 | { |
|
| 662 | if (is_string($columns)) { |
|
| 663 | $columns = [$columns]; // str to array |
|
| 664 | } |
|
| 665 | ||
| 666 | if ($constraint) { |
|
| 667 | $this->execute( |
|
| 668 | sprintf( |
|
| 669 | 'ALTER TABLE %s DROP FOREIGN KEY %s', |
|
| 670 | $this->quoteTableName($tableName), |
|
| 671 | $constraint |
|
| 672 | ) |
|
| 673 | ); |
|
| 674 | ||
| 675 | return; |
|
| 676 | } else { |
|
| 677 | foreach ($columns as $column) { |
|
| 678 | $rows = $this->fetchAll(sprintf( |
|
| 679 | "SELECT |
|
| 680 | CONSTRAINT_NAME |
|
| 681 | FROM information_schema.KEY_COLUMN_USAGE |
|
| 682 | WHERE REFERENCED_TABLE_SCHEMA = DATABASE() |
|
| 683 | AND REFERENCED_TABLE_NAME IS NOT NULL |
|
| 684 | AND TABLE_NAME = '%s' |
|
| 685 | AND COLUMN_NAME = '%s' |
|
| 686 | ORDER BY POSITION_IN_UNIQUE_CONSTRAINT", |
|
| 687 | $tableName, |
|
| 688 | $column |
|
| 689 | )); |
|
| 690 | foreach ($rows as $row) { |
|
| 691 | $this->dropForeignKey($tableName, $columns, $row['CONSTRAINT_NAME']); |
|
| 692 | } |
|
| 693 | } |
|
| 694 | } |
|
| 695 | } |
|
| 696 | ||
| 697 | /** |
|
| 698 | * {@inheritdoc} |
|
| @@ 683-705 (lines=23) @@ | ||
| 680 | /** |
|
| 681 | * {@inheritdoc} |
|
| 682 | */ |
|
| 683 | public function dropIndex($tableName, $columns) |
|
| 684 | { |
|
| 685 | if (is_string($columns)) { |
|
| 686 | $columns = [$columns]; // str to array |
|
| 687 | } |
|
| 688 | ||
| 689 | $indexes = $this->getIndexes($tableName); |
|
| 690 | $columns = array_map('strtolower', $columns); |
|
| 691 | ||
| 692 | foreach ($indexes as $indexName => $index) { |
|
| 693 | $a = array_diff($columns, $index['columns']); |
|
| 694 | if (empty($a)) { |
|
| 695 | $this->execute( |
|
| 696 | sprintf( |
|
| 697 | 'DROP INDEX %s ON %s', |
|
| 698 | $this->quoteColumnName($indexName), |
|
| 699 | $this->quoteTableName($tableName) |
|
| 700 | ) |
|
| 701 | ); |
|
| 702 | ||
| 703 | return; |
|
| 704 | } |
|
| 705 | } |
|
| 706 | } |
|
| 707 | ||
| 708 | /** |
|
| @@ 807-844 (lines=38) @@ | ||
| 804 | /** |
|
| 805 | * {@inheritdoc} |
|
| 806 | */ |
|
| 807 | public function dropForeignKey($tableName, $columns, $constraint = null) |
|
| 808 | { |
|
| 809 | if (is_string($columns)) { |
|
| 810 | $columns = [$columns]; // str to array |
|
| 811 | } |
|
| 812 | ||
| 813 | if ($constraint) { |
|
| 814 | $this->execute( |
|
| 815 | sprintf( |
|
| 816 | 'ALTER TABLE %s DROP CONSTRAINT %s', |
|
| 817 | $this->quoteTableName($tableName), |
|
| 818 | $constraint |
|
| 819 | ) |
|
| 820 | ); |
|
| 821 | ||
| 822 | return; |
|
| 823 | } else { |
|
| 824 | foreach ($columns as $column) { |
|
| 825 | $rows = $this->fetchAll(sprintf( |
|
| 826 | "SELECT |
|
| 827 | tc.constraint_name, |
|
| 828 | tc.table_name, kcu.column_name, |
|
| 829 | ccu.table_name AS referenced_table_name, |
|
| 830 | ccu.column_name AS referenced_column_name |
|
| 831 | FROM |
|
| 832 | information_schema.table_constraints AS tc |
|
| 833 | JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name |
|
| 834 | JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name |
|
| 835 | WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = '%s' and ccu.column_name='%s' |
|
| 836 | ORDER BY kcu.ordinal_position", |
|
| 837 | $tableName, |
|
| 838 | $column |
|
| 839 | )); |
|
| 840 | foreach ($rows as $row) { |
|
| 841 | $this->dropForeignKey($tableName, $columns, $row['constraint_name']); |
|
| 842 | } |
|
| 843 | } |
|
| 844 | } |
|
| 845 | } |
|
| 846 | ||
| 847 | /** |
|
| @@ 720-753 (lines=34) @@ | ||
| 717 | /** |
|
| 718 | * {@inheritdoc} |
|
| 719 | */ |
|
| 720 | public function dropForeignKey($tableName, $columns, $constraint = null) |
|
| 721 | { |
|
| 722 | if (is_string($columns)) { |
|
| 723 | $columns = [$columns]; // str to array |
|
| 724 | } |
|
| 725 | ||
| 726 | if ($constraint) { |
|
| 727 | $this->execute( |
|
| 728 | sprintf( |
|
| 729 | 'ALTER TABLE %s DROP CONSTRAINT %s', |
|
| 730 | $this->quoteTableName($tableName), |
|
| 731 | $constraint |
|
| 732 | ) |
|
| 733 | ); |
|
| 734 | } else { |
|
| 735 | foreach ($columns as $column) { |
|
| 736 | $rows = $this->fetchAll(sprintf( |
|
| 737 | "SELECT CONSTRAINT_NAME |
|
| 738 | FROM information_schema.KEY_COLUMN_USAGE |
|
| 739 | WHERE TABLE_SCHEMA = CURRENT_SCHEMA() |
|
| 740 | AND TABLE_NAME IS NOT NULL |
|
| 741 | AND TABLE_NAME = '%s' |
|
| 742 | AND COLUMN_NAME = '%s' |
|
| 743 | ORDER BY POSITION_IN_UNIQUE_CONSTRAINT", |
|
| 744 | $tableName, |
|
| 745 | $column |
|
| 746 | )); |
|
| 747 | ||
| 748 | foreach ($rows as $row) { |
|
| 749 | $this->dropForeignKey($tableName, $columns, $row['constraint_name']); |
|
| 750 | } |
|
| 751 | } |
|
| 752 | } |
|
| 753 | } |
|
| 754 | ||
| 755 | /** |
|
| 756 | * {@inheritdoc} |
|