Logo Search packages:      
Sourcecode: mysql-connector-java version File versions

java.sql.ResultSet com::mysql::jdbc::DatabaseMetaData::getCrossReference ( String  primaryCatalog,
String  primarySchema,
String  primaryTable,
String  foreignCatalog,
String  foreignSchema,
String  foreignTable 
) throws SQLException [inline]

Get a description of the foreign key columns in the foreign key table that reference the primary key columns of the primary key table (describe how one table imports another's key.) This should normally return a single foreign key/primary key pair (most tables only import a foreign key from a table once.) They are ordered by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, and KEY_SEQ.

Each foreign key column description has the following columns:

  1. PKTABLE_CAT String => primary key table catalog (may be null)
  2. PKTABLE_SCHEM String => primary key table schema (may be null)
  3. PKTABLE_NAME String => primary key table name
  4. PKCOLUMN_NAME String => primary key column name
  5. FKTABLE_CAT String => foreign key table catalog (may be null) being exported (may be null)
  6. FKTABLE_SCHEM String => foreign key table schema (may be null) being exported (may be null)
  7. FKTABLE_NAME String => foreign key table name being exported
  8. FKCOLUMN_NAME String => foreign key column name being exported
  9. KEY_SEQ short => sequence number within foreign key
  10. UPDATE_RULE short => What happens to foreign key when primary is updated:

    • importedKeyCascade - change imported key to agree with primary key update
    • importedKeyRestrict - do not allow update of primary key if it has been imported
    • importedKeySetNull - change imported key to NULL if its primary key has been updated

  11. DELETE_RULE short => What happens to the foreign key when primary is deleted.

    • importedKeyCascade - delete rows that import a deleted key
    • importedKeyRestrict - do not allow delete of primary key if it has been imported
    • importedKeySetNull - change imported key to NULL if its primary key has been deleted

  12. FK_NAME String => foreign key identifier (may be null)
  13. PK_NAME String => primary key identifier (may be null)

Parameters:
primaryCatalog a catalog name; "" retrieves those without a catalog
primarySchema a schema name pattern; "" retrieves those without a schema
primaryTable a table name
foreignCatalog a catalog name; "" retrieves those without a catalog
foreignSchema a schema name pattern; "" retrieves those without a schema
foreignTable a table name
Returns:
ResultSet each row is a foreign key column description
Exceptions:
SQLException if a database access error occurs

Definition at line 1100 of file DatabaseMetaData.java.

References com::mysql::jdbc::Statement::close(), com::mysql::jdbc::ResultSet::close(), conn, com::mysql::jdbc::Connection::createStatement(), database, com::mysql::jdbc::Statement::executeQuery(), extractForeignKeyFromCreateTable(), getForeignKeyActions(), com::mysql::jdbc::ResultSet::getString(), com::mysql::jdbc::ResultSet::next(), s2b(), com::mysql::jdbc::Statement::setEscapeProcessing(), SUPPORTS_FK, and com::mysql::jdbc::Connection::versionMeetsMinimum().

                                                                       {
        if (primaryTable == null) {
            throw new SQLException("Table not specified.",
                SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
        }

        Field[] fields = new Field[14];
        fields[0] = new Field("", "PKTABLE_CAT", Types.CHAR, 255);
        fields[1] = new Field("", "PKTABLE_SCHEM", Types.CHAR, 0);
        fields[2] = new Field("", "PKTABLE_NAME", Types.CHAR, 255);
        fields[3] = new Field("", "PKCOLUMN_NAME", Types.CHAR, 32);
        fields[4] = new Field("", "FKTABLE_CAT", Types.CHAR, 255);
        fields[5] = new Field("", "FKTABLE_SCHEM", Types.CHAR, 0);
        fields[6] = new Field("", "FKTABLE_NAME", Types.CHAR, 255);
        fields[7] = new Field("", "FKCOLUMN_NAME", Types.CHAR, 32);
        fields[8] = new Field("", "KEY_SEQ", Types.SMALLINT, 2);
        fields[9] = new Field("", "UPDATE_RULE", Types.SMALLINT, 2);
        fields[10] = new Field("", "DELETE_RULE", Types.SMALLINT, 2);
        fields[11] = new Field("", "FK_NAME", Types.CHAR, 0);
        fields[12] = new Field("", "PK_NAME", Types.CHAR, 0);
        fields[13] = new Field("", "DEFERRABILITY", Types.INTEGER, 2);

        if (this.conn.versionMeetsMinimum(3, 23, 0)) {
            Statement stmt = null;
            ResultSet fkresults = null;

            try {
                stmt = this.conn.createStatement();
                stmt.setEscapeProcessing(false);

                /*
                 * Get foreign key information for table
                 */
                if (this.conn.versionMeetsMinimum(3, 23, 50)) {
                    // we can use 'SHOW CREATE TABLE'
                    String db = this.database;

                    if (foreignCatalog != null) {
                        if (!foreignCatalog.equals("")) {
                            db = foreignCatalog;
                        }
                    }

                    fkresults = extractForeignKeyFromCreateTable(this.conn,
                            this, db, null);
                } else {
                    String databasePart = "";

                    if (foreignCatalog != null) {
                        if (!foreignCatalog.equals("")) {
                            databasePart = " FROM " + foreignCatalog;
                        }
                    } else {
                        databasePart = " FROM " + this.database;
                    }

                    fkresults = stmt.executeQuery("show table status " +
                            databasePart);
                }

                String foreignTableWithCase = getTableNameWithCase(foreignTable);
                String primaryTableWithCase = getTableNameWithCase(primaryTable);

                /*
                * Parse imported foreign key information
                */
                ArrayList tuples = new ArrayList();
                String dummy;

                while (fkresults.next()) {
                    String tableType = fkresults.getString("Type");

                    if ((tableType != null) &&
                            (tableType.equalsIgnoreCase("innodb") ||
                            tableType.equalsIgnoreCase(SUPPORTS_FK))) {
                        String comment = fkresults.getString("Comment").trim();

                        if (comment != null) {
                            StringTokenizer commentTokens = new StringTokenizer(comment,
                                    ";", false);

                            if (commentTokens.hasMoreTokens()) {
                                dummy = commentTokens.nextToken();

                                // Skip InnoDB comment
                            }

                            while (commentTokens.hasMoreTokens()) {
                                String keys = commentTokens.nextToken();

                                // simple-columned keys: (m) REFER airline/tt(a)
                                // multi-columned keys : (m n) REFER airline/vv(a b)
                                int firstLeftParenIndex = keys.indexOf('(');
                                int firstRightParenIndex = keys.indexOf(')');
                                String referencingColumns = keys.substring(firstLeftParenIndex +
                                        1, firstRightParenIndex);
                                StringTokenizer referencingColumnsTokenizer = new StringTokenizer(referencingColumns,
                                        ", ");
                                int secondLeftParenIndex = keys.indexOf('(',
                                        firstRightParenIndex + 1);
                                int secondRightParenIndex = keys.indexOf(')',
                                        firstRightParenIndex + 1);
                                String referencedColumns = keys.substring(secondLeftParenIndex +
                                        1, secondRightParenIndex);
                                StringTokenizer referencedColumnsTokenizer = new StringTokenizer(referencedColumns,
                                        ", ");
                                int slashIndex = keys.indexOf('/');
                                String referencedTable = keys.substring(slashIndex +
                                        1, secondLeftParenIndex);
                                int keySeq = 0;

                                while (referencingColumnsTokenizer.hasMoreTokens()) {
                                    String referencingColumn = referencingColumnsTokenizer.nextToken();

                                    // one tuple for each table between parenthesis
                                    byte[][] tuple = new byte[14][];
                                    tuple[4] = ((foreignCatalog == null) ? null
                                                                         : s2b(foreignCatalog));
                                    tuple[5] = ((foreignSchema == null) ? null
                                                                        : s2b(foreignSchema));
                                    dummy = fkresults.getString("Name"); // FKTABLE_NAME

                                    if (dummy.compareTo(foreignTableWithCase) != 0) {
                                        continue;
                                    }

                                    tuple[6] = s2b(dummy);

                                    tuple[7] = s2b(referencingColumn); // FKCOLUMN_NAME
                                    tuple[0] = ((primaryCatalog == null) ? null
                                                                         : s2b(primaryCatalog));
                                    tuple[1] = ((primarySchema == null) ? null
                                                                        : s2b(primarySchema));

                                    // Skip foreign key if it doesn't refer to the right table
                                    if (referencedTable.compareTo(
                                                primaryTableWithCase) != 0) {
                                        continue;
                                    }

                                    tuple[2] = s2b(referencedTable); // PKTABLE_NAME
                                    tuple[3] = s2b(referencedColumnsTokenizer.nextToken()); // PKCOLUMN_NAME
                                    tuple[8] = Integer.toString(keySeq)
                                                      .getBytes(); // KEY_SEQ

                                    int[] actions = getForeignKeyActions(keys);

                                    tuple[9] = Integer.toString(actions[1])
                                                      .getBytes();
                                    tuple[10] = Integer.toString(actions[0])
                                                       .getBytes();
                                    tuple[11] = null; // FK_NAME
                                    tuple[12] = null; // PK_NAME
                                    tuple[13] = Integer.toString(java.sql.DatabaseMetaData.importedKeyNotDeferrable)
                                                       .getBytes();
                                    tuples.add(tuple);
                                    keySeq++;
                                }
                            }
                        }
                    }
                }

                return buildResultSet(fields, tuples);
            } finally {
                if (fkresults != null) {
                    try {
                        fkresults.close();
                    } catch (Exception sqlEx) {
                        AssertionFailedException.shouldNotHappen(sqlEx);
                    }

                    fkresults = null;
                }

                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception ex) {
                        ;
                    }

                    stmt = null;
                }
            }
        }

        return buildResultSet(fields, new ArrayList());
    }


Generated by  Doxygen 1.6.0   Back to index