Search in sources :

Example 1 with DatabaseMetadata

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;
}
Also used : DatabaseMetadata(org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata)

Example 2 with DatabaseMetadata

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);
}
Also used : CatalogMetadata(org.kie.workbench.common.screens.datasource.management.metadata.CatalogMetadata) SchemaMetadata(org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata) SQLException(java.sql.SQLException) DatabaseMetadata(org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata) Before(org.junit.Before)

Example 3 with DatabaseMetadata

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);
}
Also used : DatabaseMetadata(org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata)

Example 4 with DatabaseMetadata

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.
        }
    }
}
Also used : CatalogMetadata(org.kie.workbench.common.screens.datasource.management.metadata.CatalogMetadata) SchemaMetadata(org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DatabaseMetaData(java.sql.DatabaseMetaData) DatabaseMetadata(org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata)

Example 5 with DatabaseMetadata

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();
}
Also used : DatabaseMetadata(org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata) Test(org.junit.Test)

Aggregations

DatabaseMetadata (org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata)5 CatalogMetadata (org.kie.workbench.common.screens.datasource.management.metadata.CatalogMetadata)2 SchemaMetadata (org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 Test (org.junit.Test)1