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

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

Get a description of a table's indices and statistics. They are ordered by NON_UNIQUE, TYPE, INDEX_NAME, and ORDINAL_POSITION.

Each index 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. NON_UNIQUE boolean => Can index values be non-unique? false when TYPE is tableIndexStatistic
  5. INDEX_QUALIFIER String => index catalog (may be null); null when TYPE is tableIndexStatistic
  6. INDEX_NAME String => index name; null when TYPE is tableIndexStatistic
  7. TYPE short => index type:

    • tableIndexStatistic - this identifies table statistics that are returned in conjuction with a table's index descriptions
    • tableIndexClustered - this is a clustered index
    • tableIndexHashed - this is a hashed index
    • tableIndexOther - this is some other style of index

  8. ORDINAL_POSITION short => column sequence number within index; zero when TYPE is tableIndexStatistic
  9. COLUMN_NAME String => column name; null when TYPE is tableIndexStatistic
  10. ASC_OR_DESC String => column sort sequence, "A" => ascending, "D" => descending, may be null if sort sequence is not supported; null when TYPE is tableIndexStatistic
  11. CARDINALITY int => When TYPE is tableIndexStatisic then this is the number of rows in the table; otherwise it is the number of unique values in the index.
  12. PAGES int => When TYPE is tableIndexStatisic then this is the number of pages used for the table, otherwise it is the number of pages used for the current index.
  13. FILTER_CONDITION String => Filter condition, if any. (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
unique when true, return only indices for unique values; when false, return indices regardless of whether unique or not
approximate when true, result is allowed to reflect approximate or out of data values; when false, results are requested to be accurate
Returns:
ResultSet each row is an index column description
Exceptions:
SQLException DOCUMENT ME!

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

                            {
        /*
         * MySQL stores index information in the following fields:
         *
         * Table
         * Non_unique
         * Key_name
         * Seq_in_index
         * Column_name
         * Collation
         * Cardinality
         * Sub_part
         */
        String databasePart = "";

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

        Statement stmt = null;
        ResultSet results = null;

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

            StringBuffer queryBuf = new StringBuffer("SHOW INDEX FROM ");
            queryBuf.append(this.quotedId);
            queryBuf.append(table);
            queryBuf.append(this.quotedId);
            queryBuf.append(databasePart);

            try {
                  results = stmt.executeQuery(queryBuf.toString());
            } catch (SQLException sqlEx) {
                  int errorCode = sqlEx.getErrorCode();
                  
                  // If SQLState is 42S02, ignore this SQLException
                  // it means the table doesn't exist....
                  if (!"42S02".equals(sqlEx.getSQLState())) {
                        // Sometimes not mapped correctly for pre-4.1
                        // so use error code instead.
                        if (errorCode != MysqlErrorNumbers.ER_NO_SUCH_TABLE) {
                              throw sqlEx;
                        }
                  }
            }

            Field[] fields = new Field[13];
            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("", "NON_UNIQUE", Types.CHAR, 4);
            fields[4] = new Field("", "INDEX_QUALIFIER", Types.CHAR, 1);
            fields[5] = new Field("", "INDEX_NAME", Types.CHAR, 32);
            fields[6] = new Field("", "TYPE", Types.CHAR, 32);
            fields[7] = new Field("", "ORDINAL_POSITION", Types.SMALLINT, 5);
            fields[8] = new Field("", "COLUMN_NAME", Types.CHAR, 32);
            fields[9] = new Field("", "ASC_OR_DESC", Types.CHAR, 1);
            fields[10] = new Field("", "CARDINALITY", Types.INTEGER, 10);
            fields[11] = new Field("", "PAGES", Types.INTEGER, 10);
            fields[12] = new Field("", "FILTER_CONDITION", Types.CHAR, 32);

            ArrayList rows = new ArrayList();
            
              while (results != null && results.next()) {
                byte[][] row = new byte[14][];
                row[0] = ((catalog == null) ? new byte[0] : s2b(catalog));;
                row[1] = null;
                row[2] = results.getBytes("Table");
                
                boolean indexIsUnique = results.getInt("Non_unique") == 0;
                
                row[3] = (indexIsUnique ? s2b("true") : s2b("false"));
                row[4] = new byte[0];
                row[5] = results.getBytes("Key_name");
                row[6] = Integer.toString(java.sql.DatabaseMetaData.tableIndexOther)
                                .getBytes();
                row[7] = results.getBytes("Seq_in_index");
                row[8] = results.getBytes("Column_name");
                row[9] = results.getBytes("Collation");
                row[10] = results.getBytes("Cardinality");
                row[11] = s2b("0");
                row[12] = null;
                
                if (unique) {
                  if (indexIsUnique) {
                        rows.add(row);
                  }
                } else {
                  // All rows match
                  rows.add(row);
                }
            }

            java.sql.ResultSet indexInfo = buildResultSet(fields, rows);

            return indexInfo;
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (Exception ex) {
                    ;
                }

                results = null;
            }

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

                stmt = null;
            }
        }
    }


Generated by  Doxygen 1.6.0   Back to index