use of org.kie.workbench.common.screens.datasource.management.metadata.CatalogMetadata in project kie-wb-common by kiegroup.
the class DatabaseMetadataUtilTest method setup.
@Before
public void setup() {
// setup the expected catalogs
catalogs.add(new CatalogMetadata("catalog1"));
catalogs.add(new CatalogMetadata("catalog2"));
// setup the expected schemas
schemas.add(new SchemaMetadata("catalog1", "schema1.1"));
schemas.add(new SchemaMetadata("catalog1", "schema1.2"));
schemas.add(new SchemaMetadata("catalog2", "schema2.1"));
schemas.add(new SchemaMetadata("catalog2", "schema2.2"));
// setup the expected metadata
databaseMetadata = new DatabaseMetadata();
databaseMetadata.setDatabaseType(DatabaseMetadata.DatabaseType.POSTGRESQL);
databaseMetadata.setDatabaseProductName(DATA_BASE_PRODUCT_NAME);
databaseMetadata.setDatabaseProductVersion(DATA_BASE_PRODUCT_VERSION);
databaseMetadata.setDriverName(DRIVER_NAME);
databaseMetadata.setDriverVersion(DRIVER_VERSION);
databaseMetadata.setDriverMajorVersion(DRIVER_MAJOR_VERSION);
databaseMetadata.setDriverMinorVersion(DRIVER_MINOR_VERSION);
databaseMetadata.setCatalogs(catalogs);
databaseMetadata.setSchemas(schemas);
// mocks the catalogs returned by the sql database metadata
catalogsRs = new ResultSetMock<CatalogMetadata>(catalogs.iterator()) {
@Override
public String getString(String columnLabel) throws SQLException {
if ("TABLE_CAT".equals(columnLabel)) {
return current.getCatalogName();
} else {
throw new SQLException("unexpected column name: " + columnLabel);
}
}
};
// mocks the schemas returned by the sql database metadata
schemasRs = new ResultSetMock<SchemaMetadata>(schemas.iterator()) {
@Override
public String getString(String columnLabel) throws SQLException {
if ("TABLE_CATALOG".equals(columnLabel)) {
return current.getCatalogName();
} else if ("TABLE_SCHEM".equals(columnLabel)) {
return current.getSchemaName();
} else {
throw new SQLException("unexpected colum name: " + columnLabel);
}
}
};
// setup the tables metadata
tables.add(S1_TBL1);
tables.add(S1_TBL2);
tables.add(S1_TBL3);
tables.add(S1_TBL4);
tables.add(S2_TBL1);
tables.add(S2_TBL2);
tables.add(S2_TBL3);
}
use of org.kie.workbench.common.screens.datasource.management.metadata.CatalogMetadata in project kie-wb-common by kiegroup.
the class DatabaseMetadataUtil method getMetadata.
/**
* Gets the metadata for a given database.
* @param conn A valid connection to the target database.
* @param includeCatalogs If true the database catalogs metadata will be included in the results.
* @param includeSchemas If true the database schemas metadata will be included in the results.
* @return The metadata for the given database.
* @throws Exception if a database error is produced.
*/
public static DatabaseMetadata getMetadata(Connection conn, boolean includeCatalogs, boolean includeSchemas) throws Exception {
try {
DatabaseMetadata result = new DatabaseMetadata();
ResultSet rs;
DatabaseMetaData sqlMetadata = conn.getMetaData();
result.setDatabaseType(DatabaseMetadataUtil.getDatabaseType(sqlMetadata.getDatabaseProductName()));
result.setDatabaseProductName(sqlMetadata.getDatabaseProductName());
result.setDatabaseProductVersion(sqlMetadata.getDatabaseProductVersion());
result.setDriverName(sqlMetadata.getDriverName());
result.setDriverVersion(sqlMetadata.getDriverVersion());
result.setDriverMajorVersion(sqlMetadata.getDriverMajorVersion());
result.setDriverMinorVersion(sqlMetadata.getDriverMinorVersion());
if (includeCatalogs) {
List<CatalogMetadata> catalogs = new ArrayList<>();
rs = sqlMetadata.getCatalogs();
while (rs.next()) {
catalogs.add(new CatalogMetadata(rs.getString("TABLE_CAT")));
}
rs.close();
result.setCatalogs(catalogs);
}
if (includeSchemas) {
List<SchemaMetadata> schemas = new ArrayList<>();
rs = sqlMetadata.getSchemas();
while (rs.next()) {
schemas.add(new SchemaMetadata(rs.getString("TABLE_CATALOG"), rs.getString("TABLE_SCHEM")));
}
rs.close();
result.setSchemas(schemas);
}
return result;
} catch (Exception e) {
throw new Exception("It was not possible to read database metadata due to the following error: " + e.getMessage());
} finally {
try {
conn.close();
} catch (Exception e) {
// we are not interested in raising this error case.
}
}
}
Aggregations