Java Code Examples for java.sql.DatabaseMetaData#tableIndexStatistic()

The following examples show how to use java.sql.DatabaseMetaData#tableIndexStatistic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JDBCUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static IndexType getIndexType(short sqlIndexType) {
    switch (sqlIndexType) {
        case DatabaseMetaData.tableIndexHashed:
            return IndexType.HASHED;
        case DatabaseMetaData.tableIndexClustered:
            return IndexType.CLUSTERED;
        case DatabaseMetaData.tableIndexOther:
            return IndexType.OTHER;
        case DatabaseMetaData.tableIndexStatistic:
            LOGGER.log(Level.INFO, "Got unexpected index type of tableIndexStatistic, marking as 'other'");
            return IndexType.OTHER;
        default:
            LOGGER.log(Level.INFO, "Unexpected index type code from database metadata: " + sqlIndexType);
            return IndexType.OTHER;
    }
}
 
Example 2
Source File: TableMetadata.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void initIndexes(DatabaseMetaData meta) throws SQLException {
	ResultSet rs = null;

	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();
		}
	}
}
 
Example 3
Source File: Table.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the number of rows, or -1 if unknown. Note that some database drivers returns 0,
 * so it is better to consider 0 as "unknown" too. We do not cache this count because it may
 * change at any time.
 *
 * @param  metadata     information about the database.
 * @param  approximate  whether approximate or outdated values are acceptable.
 * @return number of rows (may be approximate), or -1 if unknown.
 */
final long countRows(final DatabaseMetaData metadata, final boolean approximate) throws SQLException {
    long count = -1;
    final String[] names = TableReference.splitName(featureType.getName());
    try (ResultSet reflect = metadata.getIndexInfo(names[2], names[1], names[0], false, approximate)) {
        while (reflect.next()) {
            final long n = reflect.getLong(Reflection.CARDINALITY);
            if (!reflect.wasNull()) {
                if (reflect.getShort(Reflection.TYPE) == DatabaseMetaData.tableIndexStatistic) {
                    return n;       // "Index statistic" type provides the number of rows in the table.
                }
                if (n > count) {    // Other index types may be inaccurate.
                    count = n;
                }
            }
        }
    }
    return count;
}
 
Example 4
Source File: TableMetadata.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void initIndexes(DatabaseMetaData meta) throws SQLException {
	ResultSet rs = null;

	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();
	}
}
 
Example 5
Source File: JDBCTable.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void createIndexes() {
    Map<String, Index> newIndexes = new LinkedHashMap<String, Index>();
    try {
        ResultSet rs = MetadataUtilities.getIndexInfo(
                jdbcSchema.getJDBCCatalog().getJDBCMetadata().getDmd(),
                jdbcSchema.getJDBCCatalog().getName(), jdbcSchema.getName(),
                name, false, true);
        if (rs != null) {
            try {
                JDBCIndex index = null;
                String currentIndexName = null;
                while (rs.next()) {
                    // Ignore Indices marked statistic
                    // explicit: TYPE == DatabaseMetaData or
                    // implicit: ORDINAL_POSITION == 0
                    // @see java.sql.DatabaseMetaData#getIndexInfo
                    if (rs.getShort("TYPE") //NOI18N
                            == DatabaseMetaData.tableIndexStatistic
                            || rs.getInt("ORDINAL_POSITION") == 0) { //NOI18N
                        continue;
                    }

                    String indexName = MetadataUtilities.trimmed(rs.getString("INDEX_NAME")); //NOI18N
                    if (index == null || !(currentIndexName.equals(indexName))) {
                        index = createJDBCIndex(indexName, rs);
                        LOGGER.log(Level.FINE, "Created index {0}", index); //NOI18N

                        newIndexes.put(index.getName(), index.getIndex());
                        currentIndexName = indexName;
                    }

                    JDBCIndexColumn idx = createJDBCIndexColumn(index, rs);
                    if (idx == null) {
                        LOGGER.log(Level.INFO, "Cannot create index column for {0} from {1}",  //NOI18N
                                new Object[]{indexName, rs});
                    } else {
                        IndexColumn col = idx.getIndexColumn();
                        index.addColumn(col);
                        LOGGER.log(Level.FINE, "Added column {0} to index {1}",   //NOI18N
                                new Object[]{col.getName(), indexName});
                    }
                }
            } finally {
                rs.close();
            }
        }
    } catch (SQLException e) {
        filterSQLException(e);
    }

    indexes = Collections.unmodifiableMap(newIndexes);
}
 
Example 6
Source File: JdbcModelReader.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Reads the next index spec from the result set.
 * 
 * @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);
    }
}
 
Example 7
Source File: JdbcModelReader.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Reads the next index spec from the result set.
 * 
 * @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);
    }
}