use of org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata in project kie-wb-common by kiegroup.
the class DataManagementServiceImpl method buildDataSetTableName.
private String buildDataSetTableName(String dataSourceUuid, String table) throws Exception {
String result = table;
DatabaseMetadata metadata = databaseMetadataService.getMetadata(dataSourceUuid, false, false);
if (metadata.getDatabaseType() != null) {
switch(metadata.getDatabaseType()) {
case POSTGRESQL:
result = "\"" + table + "\"";
break;
case H2:
case MYSQL:
case MARIADB:
case ORACLE:
case SQLSERVER:
case DB2:
result = table;
}
}
return result;
}
use of org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata 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.DatabaseMetadata in project kie-wb-common by kiegroup.
the class DatabaseMetadataServiceTest method getMetadata.
/**
* Tests the execution of the getMetadata method.
*/
public void getMetadata() throws Exception {
PowerMockito.mockStatic(DatabaseMetadataUtil.class);
boolean includeCatalogs = true;
boolean includeSchemas = true;
PowerMockito.when(DatabaseMetadataUtil.getMetadata(conn, true, true)).thenReturn(metadata);
DatabaseMetadata result = metadataService.getMetadata(DATASOURCE_UUID, includeCatalogs, includeSchemas);
// the result metadata should be the same as the returned by he DatabaseMetadataUtil class.
assertEquals(metadata, result);
}
use of org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata 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.
}
}
}
use of org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata in project kie-wb-common by kiegroup.
the class DatabaseMetadataUtilTest method testGetMetadata.
@Test
public void testGetMetadata() throws Exception {
when(conn.getMetaData()).thenReturn(sqlDatabaseMetaData);
when(sqlDatabaseMetaData.getDatabaseProductName()).thenReturn(DATA_BASE_PRODUCT_NAME);
when(sqlDatabaseMetaData.getDatabaseProductVersion()).thenReturn(DATA_BASE_PRODUCT_VERSION);
when(sqlDatabaseMetaData.getDriverName()).thenReturn(DRIVER_NAME);
when(sqlDatabaseMetaData.getDriverVersion()).thenReturn(DRIVER_VERSION);
when(sqlDatabaseMetaData.getDriverMajorVersion()).thenReturn(DRIVER_MAJOR_VERSION);
when(sqlDatabaseMetaData.getDriverMinorVersion()).thenReturn(DRIVER_MINOR_VERSION);
when(sqlDatabaseMetaData.getCatalogs()).thenReturn(catalogsRs);
when(sqlDatabaseMetaData.getSchemas()).thenReturn(schemasRs);
DatabaseMetadata result = DatabaseMetadataUtil.getMetadata(conn, true, true);
assertEquals(databaseMetadata, result);
assertTrue(catalogsRs.isClosed());
assertTrue(schemasRs.isClosed());
verify(conn, times(1)).close();
}
Aggregations