Skip to content

Commit

Permalink
added tables PostAction to update schema and table metadata
Browse files Browse the repository at this point in the history
renamed TableDescHandler to TablesInputHandler and augment to read both
schema and table docs
augmented PutAction to create schema; owner from http header for now
various minor fixes and improvements
  • Loading branch information
pdowler committed Sep 27, 2024
1 parent daa0e2d commit e1b694b
Show file tree
Hide file tree
Showing 17 changed files with 847 additions and 375 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
import java.util.Set;
import javax.security.auth.Subject;
import javax.security.auth.x500.X500Principal;
import javax.sql.DataSource;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Assert;
Expand Down Expand Up @@ -143,14 +142,14 @@ public void testReadTapSchemaSelf() {
for (SchemaDesc sd : ts.getSchemaDescs()) {
if (sd.getSchemaName().equalsIgnoreCase("tap_schema")) {
foundTS = true;
TableDesc ts_schemas = sd.getTable("tap_schema.schemas");
Assert.assertNotNull("found tap_schema.schemas", ts_schemas);
TableDesc td1 = sd.getTable("tap_schema.schemas");
Assert.assertNotNull("found tap_schema.schemas", td1);

TableDesc ts_tables = sd.getTable("tap_schema.tables");
Assert.assertNotNull("found tap_schema.tables", ts_tables);
TableDesc td2 = sd.getTable("tap_schema.tables");
Assert.assertNotNull("found tap_schema.tables", td2);

TableDesc ts_columns = sd.getTable("tap_schema.columns");
Assert.assertNotNull("found tap_schema.columns", ts_columns);
TableDesc td3 = sd.getTable("tap_schema.columns");
Assert.assertNotNull("found tap_schema.columns", td3);
}
}

Expand All @@ -160,6 +159,31 @@ public void testReadTapSchemaSelf() {
Assert.fail("unexpected exception: " + unexpected);
}
}

@Test
public void testGetSchema() {
try {
TapSchemaDAO dao = new TapSchemaDAO();
dao.setDataSource(dataSource);
SchemaDesc sd = dao.getSchema("tap_schema", 0);
Assert.assertNotNull(sd);
log.info("found: " + sd);
Assert.assertTrue(sd.getTableDescs().isEmpty());

sd = dao.getSchema("tap_schema", 1);
Assert.assertNotNull(sd);
Assert.assertFalse(sd.getTableDescs().isEmpty());
for (TableDesc td : sd.getTableDescs()) {
log.info("found: " + td);
Assert.assertTrue(td.getColumnDescs().isEmpty());
}

// TODO: depth==2
} catch (Exception unexpected) {
log.error("unexpected exception", unexpected);
Assert.fail("unexpected exception: " + unexpected);
}
}

@Test
public void testGetTable() {
Expand Down Expand Up @@ -389,7 +413,7 @@ public void testSetGetPermisions() {
TapSchemaDAO dao = new TapSchemaDAO();
dao.setDataSource(dataSource);

SchemaDesc sd = dao.getSchema("intTest", true);
SchemaDesc sd = dao.getSchema("intTest", 0);
if (sd == null) {
sd = new SchemaDesc("intTest");
dao.put(sd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,10 @@ public class TableIngester {

private final DataSource dataSource;
private final DatabaseDataType databaseDataType;
private final TapSchemaDAO tapSchemaDAO;

public TableIngester(DataSource dataSource) {
this.dataSource = dataSource;
PluginFactory pluginFactory = new PluginFactory();
this.tapSchemaDAO = pluginFactory.getTapSchemaDAO();
this.tapSchemaDAO.setDataSource(dataSource);
this.databaseDataType = pluginFactory.getDatabaseDataType();
log.debug("loaded: " + databaseDataType.getClass().getName());
}
Expand Down Expand Up @@ -140,19 +137,19 @@ public TableDesc getTableDesc(String schemaName, String tableName)

private TableDesc createTableDesc(String schemaName, String tableName)
throws SQLException, ResourceNotFoundException {
log.debug(String.format("creating TableDesc for %s %s", schemaName, tableName));
// get the table metadata
String unqualifiedTableName = getUnqualifiedTableNameFromTable(tableName);
log.debug(String.format("creating TableDesc for %s %s aka %s", schemaName, unqualifiedTableName, tableName));
DatabaseMetaData databaseMetaData = dataSource.getConnection().getMetaData();
ResultSet rs = databaseMetaData.getTables(null, schemaName, unqualifiedTableName, null);
ResultSet rs = databaseMetaData.getTables(null, schemaName.toLowerCase(), unqualifiedTableName.toLowerCase(), null);
if (rs != null && !rs.next()) {
log.debug("table does not exist: " + tableName);
throw new ResourceNotFoundException("database table not found: " + tableName);
}

log.debug(String.format("querying DatabaseMetadata for schema=%s table=%s", schemaName, unqualifiedTableName));
//TODO too pg specific? table names are stored lower case in the system tables queried for the metadata
ResultSet indexInfo = databaseMetaData.getIndexInfo(null, schemaName, unqualifiedTableName.toLowerCase(), false, false);
ResultSet indexInfo = databaseMetaData.getIndexInfo(null, schemaName.toLowerCase(), unqualifiedTableName.toLowerCase(), false, false);
// get column names for indexed columns
List<String> indexedColumns = new ArrayList<String>();
while (indexInfo.next()) {
Expand All @@ -166,7 +163,7 @@ private TableDesc createTableDesc(String schemaName, String tableName)
tableDesc.tableType = TableDesc.TableType.TABLE;
log.debug(String.format("creating TableDesc %s %s", schemaName, tableName));
//TODO too pg specific? table names are stored lower case in the system tables queried for the metadata
ResultSet columnInfo = databaseMetaData.getColumns(null, schemaName, unqualifiedTableName.toLowerCase(), null);
ResultSet columnInfo = databaseMetaData.getColumns(null, schemaName.toLowerCase(), unqualifiedTableName.toLowerCase(), null);
while (columnInfo.next()) {
String columnName = columnInfo.getString("COLUMN_NAME");
String columnType = columnInfo.getString("TYPE_NAME");
Expand Down
Loading

0 comments on commit e1b694b

Please sign in to comment.