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

java.sql.ResultSet com::mysql::jdbc::DatabaseMetaData::getExportedKeys ( String  catalog,
String  schema,
String  table 
) throws SQLException [inline]

Get a description of a foreign key columns that reference a table's primary key columns (the foreign keys exported by a table). 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:
catalog a catalog name; "" retrieves those without a catalog
schema a schema name pattern; "" retrieves those without a schema
table a table name
Returns:
ResultSet each row is a foreign key column description
Exceptions:
SQLException if a database access error occurs
See also:
getImportedKeys

Definition at line 1484 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(), getExportKeyResults(), com::mysql::jdbc::ResultSet::getString(), com::mysql::jdbc::ResultSet::next(), com::mysql::jdbc::Statement::setEscapeProcessing(), SUPPORTS_FK, and com::mysql::jdbc::Connection::versionMeetsMinimum().

                                          {
        if (table == 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, 255);
        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 (catalog != null) {
                        if (!catalog.equals("")) {
                            db = catalog;
                        }
                    }

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

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

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

                // lower-case table name might be turned on
                String tableNameWithCase = getTableNameWithCase(table);

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

                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()) {
                                commentTokens.nextToken(); // Skip InnoDB comment

                                while (commentTokens.hasMoreTokens()) {
                                    String keys = commentTokens.nextToken();
                                    getExportKeyResults(catalog,
                                        tableNameWithCase, keys, tuples,
                                        fkresults.getString("Name"));
                                }
                            }
                        }
                    }
                }

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

                    fkresults = null;
                }

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

                    stmt = null;
                }
            }
        }

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


Generated by  Doxygen 1.6.0   Back to index