Example usage for java.sql DatabaseMetaData tableIndexStatistic

List of usage examples for java.sql DatabaseMetaData tableIndexStatistic

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData tableIndexStatistic.

Prototype

short tableIndexStatistic

To view the source code for java.sql DatabaseMetaData tableIndexStatistic.

Click Source Link

Document

Indicates that this column contains table statistics that are returned in conjunction with a table's index descriptions.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    ResultSet indexInformation = null;
    DatabaseMetaData meta = conn.getMetaData();

    // The '_' character represents any single character.
    // The '%' character represents any sequence of zero
    // or more characters.
    indexInformation = meta.getIndexInfo(conn.getCatalog(), null, "survey", true, true);
    while (indexInformation.next()) {
        short type = indexInformation.getShort("TYPE");
        switch (type) {
        case DatabaseMetaData.tableIndexClustered:
            System.out.println("tableIndexClustered");
        case DatabaseMetaData.tableIndexHashed:
            System.out.println("tableIndexHashed");
        case DatabaseMetaData.tableIndexOther:
            System.out.println("tableIndexOther");
        case DatabaseMetaData.tableIndexStatistic:
            System.out.println("tableIndexStatistic");
        default://  ww  w.j  a  v a 2s. c o  m
            System.out.println("tableIndexOther");
        }

    }

    st.close();
    conn.close();
}

From source file:com.clican.pluto.orm.tool.TableMetadata.java

private void initIndexes(DatabaseMetaData meta) throws SQLException {
    ResultSet rs = null;/*  w w  w.jav  a 2 s  .c  o  m*/

    try {
        rs = meta.getIndexInfo(catalog, schema, name, false, true);

        while (rs.next()) {
            if (rs.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic)
                continue;
            addIndex(rs);
        }
    } finally {
        if (rs != null)
            rs.close();
    }
}

From source file:com.nextep.designer.sqlgen.helpers.CaptureHelper.java

/**
 * Converts a JDBC index type code into a neXtep {@link IndexType} enumeration.
 * /*  w  ww.  j a v  a 2s  .  c o m*/
 * @param type JDBC code of the index type
 * @return a corresponding {@link IndexType}
 */
public static IndexType getIndexType(short type) {
    switch (type) {
    case DatabaseMetaData.tableIndexHashed:
        return IndexType.HASH;
    case DatabaseMetaData.tableIndexStatistic:
    case DatabaseMetaData.tableIndexClustered:
    case DatabaseMetaData.tableIndexOther:
    default:
        return IndexType.NON_UNIQUE;
    }
}

From source file:org.apache.ddlutils.platform.JdbcModelReader.java

/**
 * Reads the next index spec from the result set.
 * /*from   www  . j  a v a  2 s. co  m*/
 * @param metaData     The database meta data
 * @param values       The index meta data as defined by {@link #getColumnsForIndex()}
 * @param knownIndices The already read indices for the current table
 */
protected void readIndex(DatabaseMetaDataWrapper metaData, Map values, Map knownIndices) throws SQLException {
    Short indexType = (Short) values.get("TYPE");

    // we're ignoring statistic indices
    if ((indexType != null) && (indexType.shortValue() == DatabaseMetaData.tableIndexStatistic)) {
        return;
    }

    String indexName = (String) values.get("INDEX_NAME");

    if (indexName != null) {
        Index index = (Index) knownIndices.get(indexName);

        if (index == null) {
            if (((Boolean) values.get("NON_UNIQUE")).booleanValue()) {
                index = new NonUniqueIndex();
            } else {
                index = new UniqueIndex();
            }

            index.setName(indexName);
            knownIndices.put(indexName, index);
        }

        IndexColumn indexColumn = new IndexColumn();

        indexColumn.setName((String) values.get("COLUMN_NAME"));
        if (values.containsKey("ORDINAL_POSITION")) {
            indexColumn.setOrdinalPosition(((Short) values.get("ORDINAL_POSITION")).intValue());
        }
        index.addColumn(indexColumn);
    }
}

From source file:org.apache.ddlutils.task.DumpMetadataTask.java

/**
 * Dumps the indexes of the indicated table.
 * /*from w  w w.j  a  v  a2 s  .  c  o  m*/
 * @param xmlWriter   The xml writer to write to
 * @param metaData    The database metadata
 * @param catalogName The catalog name
 * @param schemaName  The schema name
 * @param tableName   The table name
 */
private void dumpIndexes(PrettyPrintingXmlWriter xmlWriter, final DatabaseMetaData metaData,
        final String catalogName, final String schemaName, final String tableName) throws SQLException {
    performResultSetXmlOperation(xmlWriter, null, new ResultSetXmlOperation() {
        public ResultSet getResultSet() throws SQLException {
            return metaData.getIndexInfo(catalogName, schemaName, tableName, false, false);
        }

        public void handleRow(PrettyPrintingXmlWriter xmlWriter, ResultSet result) throws SQLException {
            Set columns = getColumnsInResultSet(result);

            xmlWriter.writeElementStart(null, "index");

            addStringAttribute(xmlWriter, "name", result, columns, "INDEX_NAME");
            addBooleanAttribute(xmlWriter, "nonUnique", result, columns, "NON_UNIQUE");
            addStringAttribute(xmlWriter, "indexCatalog", result, columns, "INDEX_QUALIFIER");
            if (columns.contains("TYPE")) {
                try {
                    switch (result.getShort("TYPE")) {
                    case DatabaseMetaData.tableIndexStatistic:
                        xmlWriter.writeAttribute(null, "type", "table statistics");
                        break;
                    case DatabaseMetaData.tableIndexClustered:
                        xmlWriter.writeAttribute(null, "type", "clustered");
                        break;
                    case DatabaseMetaData.tableIndexHashed:
                        xmlWriter.writeAttribute(null, "type", "hashed");
                        break;
                    case DatabaseMetaData.tableIndexOther:
                        xmlWriter.writeAttribute(null, "type", "other");
                        break;
                    default:
                        xmlWriter.writeAttribute(null, "type", "unknown");
                        break;
                    }
                } catch (SQLException ex) {
                    log("Could not read the TYPE value for an index of table '" + tableName
                            + "' from the result set: " + ex.getStackTrace(), Project.MSG_ERR);
                }
            }
            addStringAttribute(xmlWriter, "column", result, columns, "COLUMN_NAME");
            addShortAttribute(xmlWriter, "sequenceNumberInIndex", result, columns, "ORDINAL_POSITION");
            if (columns.contains("ASC_OR_DESC")) {
                try {
                    String value = result.getString("ASC_OR_DESC");

                    if ("A".equalsIgnoreCase(value)) {
                        xmlWriter.writeAttribute(null, "sortOrder", "ascending");
                    } else if ("D".equalsIgnoreCase(value)) {
                        xmlWriter.writeAttribute(null, "sortOrder", "descending");
                    } else {
                        xmlWriter.writeAttribute(null, "sortOrder", "unknown");
                    }
                } catch (SQLException ex) {
                    log("Could not read the ASC_OR_DESC value for an index of table '" + tableName
                            + "' from the result set: " + ex.getStackTrace(), Project.MSG_ERR);
                }
            }
            addIntAttribute(xmlWriter, "cardinality", result, columns, "CARDINALITY");
            addIntAttribute(xmlWriter, "pages", result, columns, "PAGES");
            addStringAttribute(xmlWriter, "filter", result, columns, "FILTER_CONDITION");
        }

        public void handleError(SQLException ex) {
            log("Could not read the indexes for table '" + tableName + "' from the result set: "
                    + ex.getStackTrace(), Project.MSG_ERR);
        }
    });
}

From source file:org.executequery.databaseobjects.impl.TableColumnIndex.java

private String translateType(Short value) {

    String translated = String.valueOf(value);
    switch (value) {
    case DatabaseMetaData.tableIndexStatistic:
        return translated + " - tableIndexStatistic";

    case DatabaseMetaData.tableIndexClustered:
        return translated + " - tableIndexClustered";

    case DatabaseMetaData.tableIndexHashed:
        return translated + " - tableIndexHashed";

    case DatabaseMetaData.tableIndexOther:
        return translated + " - tableIndexOther";
    }//from www. ja v a  2s. c  om
    return translated;
}

From source file:org.jumpmind.db.platform.AbstractJdbcDdlReader.java

protected void readIndex(DatabaseMetaDataWrapper metaData, Map<String, Object> values,
        Map<String, IIndex> knownIndices) throws SQLException {
    Short indexType = (Short) values.get("TYPE");

    // we're ignoring statistic indices
    if ((indexType != null) && (indexType.shortValue() == DatabaseMetaData.tableIndexStatistic)) {
        return;//from  ww  w  .  j av  a  2  s.co  m
    }

    String indexName = (String) values.get("INDEX_NAME");

    if (indexName != null) {
        IIndex index = (IIndex) knownIndices.get(indexName);

        if (index == null) {
            if (((Boolean) values.get("NON_UNIQUE")).booleanValue()) {
                index = new NonUniqueIndex();
            } else {
                index = new UniqueIndex();
            }

            index.setName(indexName);
            knownIndices.put(indexName, index);
        }

        IndexColumn indexColumn = new IndexColumn();

        String columnName = (String) values.get("COLUMN_NAME");
        if (columnName.startsWith("\"") && columnName.endsWith("\"")) {
            columnName = columnName.substring(1, columnName.length() - 1);
        }
        indexColumn.setName(columnName);
        if (values.containsKey("ORDINAL_POSITION")) {
            indexColumn.setOrdinalPosition(((Short) values.get("ORDINAL_POSITION")).intValue());
        }
        index.addColumn(indexColumn);
    }
}