TableExtRetriever extends AbstractRetriever { void retrieveIndexInformation() throws SQLException { final InformationSchemaViews informationSchemaViews = getRetrieverConnection().getInformationSchemaViews(); if (!informationSchemaViews.hasQuery(EXT_INDEXES)) { LOGGER.log( Level.INFO, "Not retrieving additional index information, since this was not requested"); LOGGER.log(Level.FINE, "Indexes information SQL statement was not provided"); return; } LOGGER.log(Level.INFO, "Retrieving additional index information"); final Query extIndexesInformationSql = informationSchemaViews.getQuery(EXT_INDEXES); final Connection connection = getDatabaseConnection(); try (final Statement statement = connection.createStatement(); final MetadataResultSet results = new MetadataResultSet(extIndexesInformationSql, statement, getSchemaInclusionRule())) { while (results.next()) { final String catalogName = normalizeCatalogName(results.getString("INDEX_CATALOG")); final String schemaName = normalizeSchemaName(results.getString("INDEX_SCHEMA")); final String tableName = results.getString("TABLE_NAME"); final String indexName = results.getString("INDEX_NAME"); final Optional<MutableTable> tableOptional = lookupTable(catalogName, schemaName, tableName); if (!tableOptional.isPresent()) { LOGGER.log( Level.FINE, new StringFormat("Cannot find table <%s.%s.%s>", catalogName, schemaName, indexName)); continue; } LOGGER.log(Level.FINER, new StringFormat("Retrieving index information <%s>", indexName)); final MutableTable table = tableOptional.get(); final Optional<MutableIndex> indexOptional = table.lookupIndex(indexName); if (!indexOptional.isPresent()) { LOGGER.log( Level.FINE, new StringFormat( "Cannot find index <%s.%s.%s.%s>", catalogName, schemaName, tableName, indexName)); continue; } final MutableIndex index = indexOptional.get(); final String definition = results.getString("INDEX_DEFINITION"); final String remarks = results.getString("REMARKS"); index.appendDefinition(definition); index.setRemarks(remarks); index.addAttributes(results.getAttributes()); } } catch (final Exception e) { LOGGER.log(Level.WARNING, "Could not retrieve index information", e); } } TableExtRetriever(
      final RetrieverConnection retrieverConnection,
      final MutableCatalog catalog,
      final SchemaCrawlerOptions options);   }
@Test @DisplayName("Retrieve index definitions from INFORMATION_SCHEMA") public void indexInfo(final Connection connection) throws Exception { final String remarks = "TEST Index remarks"; final String definition = "TEST Index definition"; final InformationSchemaViews informationSchemaViews = InformationSchemaViewsBuilder.builder() .withSql( EXT_INDEXES, String.format( "SELECT DISTINCT TABLE_CAT AS INDEX_CATALOG, TABLE_SCHEM AS INDEX_SCHEMA, " + "TABLE_NAME, INDEX_NAME, '%s' AS REMARKS, '%s' AS INDEX_DEFINITION " + "FROM INFORMATION_SCHEMA.SYSTEM_INDEXINFO", remarks, definition)) .toOptions(); final SchemaRetrievalOptionsBuilder schemaRetrievalOptionsBuilder = SchemaRetrievalOptionsBuilder.builder(); schemaRetrievalOptionsBuilder.withInformationSchemaViews(informationSchemaViews); final SchemaRetrievalOptions schemaRetrievalOptions = schemaRetrievalOptionsBuilder.toOptions(); final RetrieverConnection retrieverConnection = new RetrieverConnection(connection, schemaRetrievalOptions); final SchemaCrawlerOptions options = SchemaCrawlerOptionsBuilder.newSchemaCrawlerOptions(); final TableExtRetriever tableExtRetriever = new TableExtRetriever(retrieverConnection, catalog, options); tableExtRetriever.retrieveIndexInformation(); final Collection<Table> tables = catalog.getTables(); assertThat(tables, hasSize(19)); for (final Table table : tables) { for (final Index index : table.getIndexes()) { assertThat(index.getRemarks(), is(remarks)); assertThat(index.getDefinition(), is(definition)); } } }