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

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

Get a description of a table's primary key columns. They are ordered by COLUMN_NAME.

Each column description has the following columns:

  1. TABLE_CAT String => table catalog (may be null)
  2. TABLE_SCHEM String => table schema (may be null)
  3. TABLE_NAME String => table name
  4. COLUMN_NAME String => column name
  5. KEY_SEQ short => sequence number within primary key
  6. PK_NAME String => primary key name (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 primary key column description
Exceptions:
SQLException DOCUMENT ME!

Definition at line 2366 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(), com::mysql::jdbc::ResultSet::getString(), com::mysql::jdbc::ResultSet::next(), quotedId, s2b(), and com::mysql::jdbc::Statement::setEscapeProcessing().

                                          {
        Field[] fields = new Field[6];
        fields[0] = new Field("", "TABLE_CAT", Types.CHAR, 255);
        fields[1] = new Field("", "TABLE_SCHEM", Types.CHAR, 0);
        fields[2] = new Field("", "TABLE_NAME", Types.CHAR, 255);
        fields[3] = new Field("", "COLUMN_NAME", Types.CHAR, 32);
        fields[4] = new Field("", "KEY_SEQ", Types.SMALLINT, 5);
        fields[5] = new Field("", "PK_NAME", Types.CHAR, 32);

        String dbSub = "";

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

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

        Statement stmt = null;
        ResultSet rs = null;

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

            StringBuffer queryBuf = new StringBuffer("SHOW KEYS FROM ");
            queryBuf.append(this.quotedId);
            queryBuf.append(table);
            queryBuf.append(this.quotedId);
            queryBuf.append(dbSub);

            rs = stmt.executeQuery(queryBuf.toString());

            ArrayList tuples = new ArrayList();
            TreeMap sortMap = new TreeMap();

            while (rs.next()) {
                String keyType = rs.getString("Key_name");

                if (keyType != null) {
                    if (keyType.equalsIgnoreCase("PRIMARY") ||
                            keyType.equalsIgnoreCase("PRI")) {
                        byte[][] tuple = new byte[6][];
                        tuple[0] = ((catalog == null) ? new byte[0] : s2b(catalog));
                        tuple[1] = null;
                        tuple[2] = s2b(table);

                        String columnName = rs.getString("Column_name");
                        tuple[3] = s2b(columnName);
                        tuple[4] = s2b(rs.getString("Seq_in_index"));
                        tuple[5] = s2b(keyType);
                        sortMap.put(columnName, tuple);
                    }
                }
            }

            // Now pull out in column name sorted order
            Iterator sortedIterator = sortMap.values().iterator();

            while (sortedIterator.hasNext()) {
                tuples.add(sortedIterator.next());
            }

            return buildResultSet(fields, tuples);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex) {
                    ;
                }

                rs = null;
            }

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

                stmt = null;
            }
        }
    }


Generated by  Doxygen 1.6.0   Back to index