Code Duplication    Length = 23-26 lines in 4 locations

src/Phinx/Db/Adapter/OracleAdapter.php 1 location

@@ 634-656 (lines=23) @@
631
     * @param string $type Type of Constraint Type (R, P)
632
     * @return array
633
     */
634
    protected function getForeignKeys($tableName, $type = 'R')
635
    {
636
        $foreignKeys = [];
637
        $rows = $this->fetchAll(sprintf(
638
            "SELECT a.CONSTRAINT_NAME, a.TABLE_NAME, b.COLUMN_NAME, 
639
                    (SELECT c.TABLE_NAME from ALL_CONS_COLUMNS c 
640
                    WHERE c.CONSTRAINT_NAME = a.R_CONSTRAINT_NAME) referenced_table_name,
641
                    (SELECT c.COLUMN_NAME from ALL_CONS_COLUMNS c 
642
                    WHERE c.CONSTRAINT_NAME = a.R_CONSTRAINT_NAME) referenced_column_name
643
                    FROM all_constraints a JOIN ALL_CONS_COLUMNS b ON a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
644
                    WHERE a.table_name = '%s'
645
                    AND CONSTRAINT_TYPE = '%s'",
646
            $tableName,
647
            $type
648
        ));
649
650
        foreach ($rows as $row) {
651
            $foreignKeys[$row['CONSTRAINT_NAME']]['TABLE'] = $row['TABLE_NAME'];
652
            $foreignKeys[$row['CONSTRAINT_NAME']]['COLUMNS'][] = $row['COLUMN_NAME'];
653
            $foreignKeys[$row['CONSTRAINT_NAME']]['REFERENCED_TABLE'] = $row['REFERENCED_TABLE_NAME'];
654
            $foreignKeys[$row['CONSTRAINT_NAME']]['REFERENCED_COLUMNS'][] = $row['REFERENCED_COLUMN_NAME'];
655
        }
656
657
        return $foreignKeys;
658
    }
659

src/Phinx/Db/Adapter/PostgresAdapter.php 1 location

@@ 677-701 (lines=25) @@
674
     * @param string $tableName Table Name
675
     * @return array
676
     */
677
    protected function getForeignKeys($tableName)
678
    {
679
        $foreignKeys = [];
680
        $rows = $this->fetchAll(sprintf(
681
            "SELECT
682
                    tc.constraint_name,
683
                    tc.table_name, kcu.column_name,
684
                    ccu.table_name AS referenced_table_name,
685
                    ccu.column_name AS referenced_column_name
686
                FROM
687
                    information_schema.table_constraints AS tc
688
                    JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
689
                    JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
690
                WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = '%s'
691
                ORDER BY kcu.position_in_unique_constraint",
692
            $tableName
693
        ));
694
        foreach ($rows as $row) {
695
            $foreignKeys[$row['constraint_name']]['table'] = $row['table_name'];
696
            $foreignKeys[$row['constraint_name']]['columns'][] = $row['column_name'];
697
            $foreignKeys[$row['constraint_name']]['referenced_table'] = $row['referenced_table_name'];
698
            $foreignKeys[$row['constraint_name']]['referenced_columns'][] = $row['referenced_column_name'];
699
        }
700
701
        return $foreignKeys;
702
    }
703
704
    /**

src/Phinx/Db/Adapter/MysqlAdapter.php 1 location

@@ 607-631 (lines=25) @@
604
     * @param string $tableName Table Name
605
     * @return array
606
     */
607
    protected function getForeignKeys($tableName)
608
    {
609
        $foreignKeys = [];
610
        $rows = $this->fetchAll(sprintf(
611
            "SELECT
612
              CONSTRAINT_NAME,
613
              TABLE_NAME,
614
              COLUMN_NAME,
615
              REFERENCED_TABLE_NAME,
616
              REFERENCED_COLUMN_NAME
617
            FROM information_schema.KEY_COLUMN_USAGE
618
            WHERE REFERENCED_TABLE_SCHEMA = DATABASE()
619
              AND REFERENCED_TABLE_NAME IS NOT NULL
620
              AND TABLE_NAME = '%s'
621
            ORDER BY POSITION_IN_UNIQUE_CONSTRAINT",
622
            $tableName
623
        ));
624
        foreach ($rows as $row) {
625
            $foreignKeys[$row['CONSTRAINT_NAME']]['table'] = $row['TABLE_NAME'];
626
            $foreignKeys[$row['CONSTRAINT_NAME']]['columns'][] = $row['COLUMN_NAME'];
627
            $foreignKeys[$row['CONSTRAINT_NAME']]['referenced_table'] = $row['REFERENCED_TABLE_NAME'];
628
            $foreignKeys[$row['CONSTRAINT_NAME']]['referenced_columns'][] = $row['REFERENCED_COLUMN_NAME'];
629
        }
630
631
        return $foreignKeys;
632
    }
633
634
    /**

src/Phinx/Db/Adapter/SqlServerAdapter.php 1 location

@@ 791-816 (lines=26) @@
788
     * @param string $tableName Table Name
789
     * @return array
790
     */
791
    protected function getForeignKeys($tableName)
792
    {
793
        $foreignKeys = [];
794
        $rows = $this->fetchAll(sprintf(
795
            "SELECT
796
                    tc.constraint_name,
797
                    tc.table_name, kcu.column_name,
798
                    ccu.table_name AS referenced_table_name,
799
                    ccu.column_name AS referenced_column_name
800
                FROM
801
                    information_schema.table_constraints AS tc
802
                    JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
803
                    JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
804
                WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = '%s'
805
                ORDER BY kcu.ordinal_position",
806
            $tableName
807
        ));
808
        foreach ($rows as $row) {
809
            $foreignKeys[$row['constraint_name']]['table'] = $row['table_name'];
810
            $foreignKeys[$row['constraint_name']]['columns'][] = $row['column_name'];
811
            $foreignKeys[$row['constraint_name']]['referenced_table'] = $row['referenced_table_name'];
812
            $foreignKeys[$row['constraint_name']]['referenced_columns'][] = $row['referenced_column_name'];
813
        }
814
815
        return $foreignKeys;
816
    }
817
818
    /**
819
     * {@inheritdoc}