in core/src/main/java/org/apache/hop/core/database/DatabaseMetaInformation.java [61:310]
public void getData(ILoggingObject parentLoggingObject, IProgressMonitor monitor)
throws HopDatabaseException {
if (monitor == null) {
monitor = new ProgressNullMonitorListener();
}
monitor.beginTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.GettingInfoFromDb"), 8);
Database db = new Database(parentLoggingObject, variables, databaseMeta);
try {
monitor.subTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.ConnectingDb"));
db.connect();
monitor.worked(1);
if (monitor.isCanceled()) {
return;
}
monitor.subTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.GettingMetaData"));
DatabaseMetaData dbmd = db.getDatabaseMetaData();
monitor.worked(1);
if (monitor.isCanceled()) {
return;
}
// Get catalogs
//
monitor.subTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.GettingInfo"));
Map<String, String> connectionExtraOptions = databaseMeta.getExtraOptions();
if (databaseMeta.supportsCatalogs() && dbmd.supportsCatalogsInTableDefinitions()) {
ArrayList<Catalog> catalogList = new ArrayList<>();
String catalogFilterKey = databaseMeta.getPluginId() + "." + FILTER_CATALOG_LIST;
if ((connectionExtraOptions != null)
&& connectionExtraOptions.containsKey(catalogFilterKey)) {
String catsFilterCommaList = connectionExtraOptions.get(catalogFilterKey);
String[] catsFilterArray = catsFilterCommaList.split(",");
for (int i = 0; i < catsFilterArray.length; i++) {
catalogList.add(new Catalog(catsFilterArray[i].trim()));
}
}
if (catalogList.isEmpty()) {
ResultSet catalogResultSet = dbmd.getCatalogs();
// Grab all the catalog names and put them in an array list
// Then we can close the resultset as soon as possible.
// This is the safest route to take for a lot of databases
//
while (catalogResultSet != null && catalogResultSet.next()) {
String catalogName = catalogResultSet.getString(1);
catalogList.add(new Catalog(catalogName));
}
// Close the catalogs resultset immediately
//
catalogResultSet.close();
}
// Now loop over the catalogs...
//
for (Catalog catalog : catalogList) {
ArrayList<String> catalogTables = new ArrayList<>();
try {
ResultSet catalogTablesResultSet =
dbmd.getTables(catalog.getCatalogName(), null, null, null);
while (catalogTablesResultSet.next()) {
String tableName = catalogTablesResultSet.getString(3);
if (!db.isSystemTable(tableName)) {
catalogTables.add(tableName);
}
}
// Immediately close the catalog tables ResultSet
//
catalogTablesResultSet.close();
// Sort the tables by names
Collections.sort(catalogTables);
} catch (Exception e) {
// Obviously, we're not allowed to snoop around in this catalog.
// Just ignore it!
}
// Save the list of tables in the catalog (can be empty)
//
catalog.setItems(catalogTables.toArray(new String[catalogTables.size()]));
}
// Save for later...
setCatalogs(catalogList.toArray(new Catalog[catalogList.size()]));
}
monitor.worked(1);
if (monitor.isCanceled()) {
return;
}
// Get schemas
//
monitor.subTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.GettingSchemaInfo"));
if (databaseMeta.supportsSchemas() && dbmd.supportsSchemasInTableDefinitions()) {
ArrayList<Schema> schemaList = new ArrayList<>();
try {
String schemaFilterKey = databaseMeta.getPluginId() + "." + FILTER_SCHEMA_LIST;
if ((connectionExtraOptions != null)
&& connectionExtraOptions.containsKey(schemaFilterKey)) {
String schemasFilterCommaList = connectionExtraOptions.get(schemaFilterKey);
String[] schemasFilterArray = schemasFilterCommaList.split(",");
for (int i = 0; i < schemasFilterArray.length; i++) {
schemaList.add(new Schema(schemasFilterArray[i].trim()));
}
}
if (schemaList.isEmpty()) {
// Support schemas for MS SQL server
//
String sql = databaseMeta.getSqlListOfSchemas();
if (!Utils.isEmpty(sql)) {
try (Statement schemaStatement = db.getConnection().createStatement()) {
ResultSet schemaResultSet = schemaStatement.executeQuery(sql);
while (schemaResultSet != null && schemaResultSet.next()) {
String schemaName = schemaResultSet.getString("name");
schemaList.add(new Schema(schemaName));
}
if (schemaResultSet != null) {
schemaResultSet.close();
}
}
} else {
ResultSet schemaResultSet = dbmd.getSchemas();
while (schemaResultSet != null && schemaResultSet.next()) {
String schemaName = schemaResultSet.getString(1);
schemaList.add(new Schema(schemaName));
}
// Close the schema ResultSet immediately
//
if (schemaResultSet != null) {
schemaResultSet.close();
}
}
}
for (Schema schema : schemaList) {
ArrayList<String> schemaTables = new ArrayList<>();
try {
ResultSet schemaTablesResultSet = null;
String schemaName = null;
String catalogName = null;
if (!schema.getSchemaName().contains(".")) {
schemaTablesResultSet = dbmd.getTables(null, schema.getSchemaName(), null, null);
} else {
catalogName =
schema.getSchemaName().substring(0, schema.getSchemaName().indexOf('.'));
schemaName =
schema.getSchemaName().substring(schema.getSchemaName().indexOf('.') + 1);
schemaTablesResultSet = dbmd.getTables(catalogName, schemaName, null, null);
}
while (schemaTablesResultSet.next()) {
String tableName = schemaTablesResultSet.getString(3);
if (!db.isSystemTable(tableName)) {
schemaTables.add(tableName);
}
}
// Immediately close the schema tables ResultSet
//
schemaTablesResultSet.close();
schemaTablesResultSet = null;
// Sort the tables by names
Collections.sort(schemaTables);
} catch (Exception e) {
// Obviously, we're not allowed to snoop around in this catalog.
// Just ignore it!
}
schema.setItems(schemaTables.toArray(new String[schemaTables.size()]));
if (monitor.isCanceled()) {
return;
}
}
} catch (Exception e) {
// Typically an unsupported feature, security issue etc.
// Ignore it to avoid excessive spamming
}
// Sort the schemas by names
Collections.sort(
schemaList,
(s1, s2) -> {
return s1.getSchemaName() == null
? -1
: s1.getSchemaName().compareToIgnoreCase(s2.getSchemaName());
});
// Save for later...
setSchemas(schemaList.toArray(new Schema[schemaList.size()]));
}
monitor.worked(1);
if (monitor.isCanceled()) {
return;
}
// Get tables
//
monitor.subTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.GettingTables"));
setTables(db.getTablenames(databaseMeta.supportsSchemas())); // legacy call
setTableMap(db.getTableMap());
monitor.worked(1);
if (monitor.isCanceled()) {
return;
}
// Get views
//
monitor.subTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.GettingViews"));
if (databaseMeta.supportsViews()) {
setViews(db.getViews(databaseMeta.supportsSchemas())); // legacy call
setViewMap(db.getViewMap());
}
monitor.worked(1);
if (monitor.isCanceled()) {
return;
}
// Get synonyms
//
monitor.subTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.GettingSynonyms"));
if (databaseMeta.supportsSynonyms()) {
setSynonyms(db.getSynonyms(databaseMeta.supportsSchemas())); // legacy call
setSynonymMap(db.getSynonymMap());
}
monitor.worked(1);
if (monitor.isCanceled()) {
return;
}
// Get procedures
//
monitor.subTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.GettingProcedures"));
setProcedures(db.getProcedures());
monitor.worked(1);
} catch (Exception e) {
throw new HopDatabaseException(
BaseMessages.getString(PKG, "DatabaseMeta.Error.UnableRetrieveDbInfo"), e);
} finally {
monitor.subTask(BaseMessages.getString(PKG, "DatabaseMeta.Info.ClosingDbConnection"));
db.disconnect();
monitor.done();
}
}