Search in sources :

Example 1 with TableMetadata

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

the class DatabaseMetadataUtilTest method includeTable.

private boolean includeTable(TableMetadata tableMetadata, String schema, String tableNamePattern, DatabaseMetadata.TableType... types) {
    if (schema != null && !schema.equals(tableMetadata.getSchemaName())) {
        return false;
    }
    if (tableNamePattern != null) {
        String regex = tableNamePattern.replace("%", "(.*)");
        Pattern pattern = Pattern.compile(regex);
        if (!pattern.matcher(tableMetadata.getTableName()).matches()) {
            return false;
        }
    }
    return types == null || Arrays.stream(types).anyMatch(tableType -> tableType == DatabaseMetadata.TableType.ALL || tableType.name().equals(tableMetadata.getTableType()));
}
Also used : Arrays(java.util.Arrays) SchemaMetadata(org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata) Connection(java.sql.Connection) Time(java.sql.Time) URL(java.net.URL) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) SQLXML(java.sql.SQLXML) Array(java.sql.Array) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) ResultSet(java.sql.ResultSet) Map(java.util.Map) DatabaseMetadata(org.kie.workbench.common.screens.datasource.management.metadata.DatabaseMetadata) NClob(java.sql.NClob) SQLWarning(java.sql.SQLWarning) Clob(java.sql.Clob) Before(org.junit.Before) CatalogMetadata(org.kie.workbench.common.screens.datasource.management.metadata.CatalogMetadata) Iterator(java.util.Iterator) Timestamp(java.sql.Timestamp) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Reader(java.io.Reader) Mockito.when(org.mockito.Mockito.when) Date(java.sql.Date) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) List(java.util.List) RowId(java.sql.RowId) Statement(java.sql.Statement) Mockito.anyObject(org.mockito.Mockito.anyObject) Ref(java.sql.Ref) Pattern(java.util.regex.Pattern) TableMetadata(org.kie.workbench.common.screens.datasource.management.metadata.TableMetadata) Blob(java.sql.Blob) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ResultSetMetaData(java.sql.ResultSetMetaData) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Mockito.eq(org.mockito.Mockito.eq) Pattern(java.util.regex.Pattern)

Example 2 with TableMetadata

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

the class DatabaseMetadataUtil method findTables.

/**
 * Gets a list of database objects metadata for a given database.
 * @param conn A valid connection to the target database.
 * @param schema A schema name for filtering. A null value will query all the available schemas.
 * @param tableNamePattern A table name pattern for filtering the database objects by name, e.g. %INVOICE_%. A null
 * value will include all tables.
 * @param types A list of database object types for filtering. A null value will include all types.
 * @return A list of database objects fulfilling the filtering criteria.
 */
public static List<TableMetadata> findTables(Connection conn, String schema, String tableNamePattern, DatabaseMetadata.TableType... types) throws Exception {
    try {
        List<TableMetadata> result = new ArrayList<>();
        DatabaseMetaData sqlMetadata = conn.getMetaData();
        ResultSet rs = sqlMetadata.getTables(null, schema, tableNamePattern, toSqlTypes(types));
        TableMetadata tableMetadata;
        while (rs.next()) {
            tableMetadata = new TableMetadata(rs.getString("TABLE_CAT"), rs.getString("TABLE_SCHEM"), rs.getString("TABLE_NAME"), rs.getString("TABLE_TYPE"));
            result.add(tableMetadata);
        }
        rs.close();
        return result;
    } catch (Exception e) {
        throw new Exception("It was not possible to read schema tables 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 : TableMetadata(org.kie.workbench.common.screens.datasource.management.metadata.TableMetadata) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 3 with TableMetadata

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

the class DatabaseObjectExplorerTest method setup.

@Before
public void setup() {
    objectExplorer = new DatabaseObjectExplorer(view, new CallerMock<>(metadataService), translationService);
    // emulate the @PostConstruct invocation.
    objectExplorer.init();
    schemas.add(new SchemaMetadata(SCHEMA_NAME));
    schemas.add(new SchemaMetadata("schema2"));
    schemas.add(new SchemaMetadata("schema3"));
    dbObjects.add(new TableMetadata(CATALOG_NAME, SCHEMA_NAME, "table1", DatabaseMetadata.TableType.TABLE.name()));
    dbObjects.add(new TableMetadata(CATALOG_NAME, SCHEMA_NAME, "table2", DatabaseMetadata.TableType.TABLE.name()));
}
Also used : TableMetadata(org.kie.workbench.common.screens.datasource.management.metadata.TableMetadata) SchemaMetadata(org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata) CallerMock(org.uberfire.mocks.CallerMock) Before(org.junit.Before)

Example 4 with TableMetadata

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

the class DatabaseObjectExplorer method loadTables.

private void loadTables(List<TableMetadata> response) {
    rows.clear();
    for (TableMetadata metadata : response) {
        rows.add(new DatabaseObjectRow(metadata.getTableName(), metadata.getTableType()));
    }
    refreshRows();
}
Also used : TableMetadata(org.kie.workbench.common.screens.datasource.management.metadata.TableMetadata)

Aggregations

TableMetadata (org.kie.workbench.common.screens.datasource.management.metadata.TableMetadata)4 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)2 SchemaMetadata (org.kie.workbench.common.screens.datasource.management.metadata.SchemaMetadata)2 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 BigDecimal (java.math.BigDecimal)1 URL (java.net.URL)1 Array (java.sql.Array)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 Date (java.sql.Date)1 NClob (java.sql.NClob)1 Ref (java.sql.Ref)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 RowId (java.sql.RowId)1 SQLException (java.sql.SQLException)1