DatabaseInfoRetriever extends AbstractRetriever { void retrieveDatabaseUsers() { final InformationSchemaViews informationSchemaViews = getRetrieverConnection().getInformationSchemaViews(); if (!informationSchemaViews.hasQuery(DATABASE_USERS)) { LOGGER.log( Level.INFO, "Not retrieving database users information, since this was not requested"); LOGGER.log(Level.FINE, "Database users SQL statement was not provided"); return; } final Query databaseUsersSql = informationSchemaViews.getQuery(DATABASE_USERS); final Connection connection = getDatabaseConnection(); try (final Statement statement = connection.createStatement(); final MetadataResultSet results = new MetadataResultSet(databaseUsersSql, statement, new IncludeAll())) { results.setDescription("retrieveDatabaseUsers"); while (results.next()) { final String username = results.getString("USERNAME"); if (isBlank(username)) { continue; } LOGGER.log(Level.FINER, new StringFormat("Retrieving database user: %s", username)); final ImmutableDatabaseUser databaseUser = new ImmutableDatabaseUser(username); databaseUser.addAttributes(results.getAttributes()); catalog.addDatabaseUser(databaseUser); } } catch (final Exception e) { LOGGER.log(Level.WARNING, "Could not retrieve database users", e); } } DatabaseInfoRetriever(
      final RetrieverConnection retrieverConnection,
      final MutableCatalog catalog,
      final SchemaCrawlerOptions options);   }
@Test @DisplayName("Retrieve database users") public void databaseUsers(final TestContext testContext, final Connection connection) throws Exception { assertThat(catalog.getDatabaseUsers(), is(empty())); final InformationSchemaViews informationSchemaViews = InformationSchemaViewsBuilder.builder() .withSql( InformationSchemaKey.DATABASE_USERS, "SELECT USER_NAME AS USERNAME, " + "ADMIN, INITIAL_SCHEMA, AUTHENTICATION, PASSWORD_DIGEST " + "FROM INFORMATION_SCHEMA.SYSTEM_USERS") .toOptions(); final SchemaRetrievalOptionsBuilder schemaRetrievalOptionsBuilder = SchemaRetrievalOptionsBuilder.builder().withInformationSchemaViews(informationSchemaViews); final SchemaRetrievalOptions schemaRetrievalOptions = schemaRetrievalOptionsBuilder.toOptions(); final RetrieverConnection retrieverConnection = new RetrieverConnection(connection, schemaRetrievalOptions); final SchemaCrawlerOptions options = SchemaCrawlerOptionsBuilder.newSchemaCrawlerOptions(); final DatabaseInfoRetriever databaseInfoRetriever = new DatabaseInfoRetriever(retrieverConnection, catalog, options); databaseInfoRetriever.retrieveDatabaseUsers(); final List<DatabaseUser> databaseUsers = new ArrayList<>(catalog.getDatabaseUsers()); assertThat(databaseUsers, hasSize(2)); assertThat( databaseUsers.stream().map(DatabaseUser::getName).collect(Collectors.toList()), hasItems("OTHERUSER", "SA")); assertThat( databaseUsers .stream() .map(databaseUser -> databaseUser.getAttributes().size()) .collect(Collectors.toList()), hasItems(4, 4)); assertThat( databaseUsers .stream() .map(databaseUser -> databaseUser.getAttributes().keySet()) .flatMap(Collection::stream) .collect(Collectors.toSet()), hasItems("INITIAL_SCHEMA", "AUTHENTICATION", "PASSWORD_DIGEST", "ADMIN")); }