Search in sources :

Example 1 with CatalogMetadata

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);
}
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 2 with CatalogMetadata

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.
        }
    }
}
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)

Aggregations

CatalogMetadata (org.kie.workbench.common.screens.datasource.management.metadata.CatalogMetadata)2 DatabaseMetadata (org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata)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