Search in sources :

Example 1 with SchemaMetadata

use of org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata in project kie-wb-common by kiegroup.

the class DatabaseSchemaExplorerTest method testInitialize.

@Test
public void testInitialize() {
    DatabaseSchemaExplorer.Settings settings = new DatabaseSchemaExplorer.Settings().dataSourceUuid(DATASOURCE_ID);
    when(metadataService.getMetadata(settings.dataSourceUuid(), false, true)).thenReturn(metadata);
    when(metadata.getSchemas()).thenReturn(schemas);
    when(translationService.getTranslation(DataSourceManagementConstants.DatabaseSchemaExplorerViewImpl_loadingDbSchemas)).thenReturn(LOADING_MESSAGE1);
    schemaExplorer.initialize(settings);
    // expected rows
    List<DatabaseSchemaRow> rows = new ArrayList<>();
    for (SchemaMetadata schema : schemas) {
        rows.add(new DatabaseSchemaRow(schema.getSchemaName()));
    }
    assertEquals(rows, schemaExplorer.getItems());
    // the UI should have been updated.
    verify(view, times(1)).setDataProvider(any(AsyncDataProvider.class));
    verify(view, times(1)).showBusyIndicator(LOADING_MESSAGE1);
    verify(view, times(1)).hideBusyIndicator();
    verify(view, times(2)).redraw();
}
Also used : SchemaMetadata(org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata) AsyncDataProvider(com.google.gwt.view.client.AsyncDataProvider) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with SchemaMetadata

use of org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata 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 SchemaMetadata

use of org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata in project kie-wb-common by kiegroup.

the class DatabaseObjectExplorer method loadSchemas.

private void loadSchemas(DatabaseMetadata metadata, String selectedSchema) {
    String currentSchema = null;
    List<Pair<String, String>> options = new ArrayList<>();
    for (SchemaMetadata schemaMetadata : metadata.getSchemas()) {
        if (schemaMetadata.getSchemaName().equals(selectedSchema)) {
            currentSchema = selectedSchema;
        }
        options.add(new Pair<>(schemaMetadata.getSchemaName(), schemaMetadata.getSchemaName()));
    }
    if (currentSchema == null && !metadata.getSchemas().isEmpty()) {
        currentSchema = metadata.getSchemas().get(0).getSchemaName();
    }
    view.loadSchemaOptions(options, currentSchema);
}
Also used : SchemaMetadata(org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata) ArrayList(java.util.ArrayList) Pair(org.uberfire.commons.data.Pair)

Example 4 with SchemaMetadata

use of org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata 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 SchemaMetadata

use of org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata in project kie-wb-common by kiegroup.

the class DatabaseSchemaExplorerTest method setup.

@Before
public void setup() {
    schemaExplorer = new DatabaseSchemaExplorer(view, new CallerMock<>(metadataService), translationService);
    // emulate the @PostConstruct invocation
    schemaExplorer.init();
    schemas.add(new SchemaMetadata("schema1"));
    schemas.add(new SchemaMetadata("schema2"));
}
Also used : SchemaMetadata(org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata) CallerMock(org.uberfire.mocks.CallerMock) Before(org.junit.Before)

Aggregations

SchemaMetadata (org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata)6 ArrayList (java.util.ArrayList)3 Before (org.junit.Before)3 CatalogMetadata (org.kie.workbench.common.screens.datasource.management.metadata.CatalogMetadata)2 DatabaseMetadata (org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata)2 CallerMock (org.uberfire.mocks.CallerMock)2 AsyncDataProvider (com.google.gwt.view.client.AsyncDataProvider)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Test (org.junit.Test)1 TableMetadata (org.kie.workbench.common.screens.datasource.management.metadata.TableMetadata)1 Pair (org.uberfire.commons.data.Pair)1