@Override public Optional<? extends Table> lookupTable(final Schema schema, final String name) { return catalog.lookupTable(schema, name); }
@Override public Optional<? extends Table> lookupTable(final Schema schema, final String name) { return catalog.lookupTable(schema, name); }
private void assertReferencedColumnExists(final Catalog catalog, final Column column) { assertThat(column, notNullValue()); final Table table = column.getParent(); assertThat("Table references do not match - " + table.getName(), table == catalog.lookupTable(table.getSchema(), table.getName()) .get()); assertThat("Column references do not match", column == table.lookupColumn(column.getName()).get()); }
@Test public void grepColumnsAndIncludeChildTables(final Connection connection) throws Exception { SchemaCrawlerOptions schemaCrawlerOptions = SchemaCrawlerOptionsBuilder .builder() .includeGreppedColumns(new RegularExpressionInclusionRule(".*\\.BOOKAUTHORS\\..*")) .toOptions(); Catalog catalog; Schema schema; Table table; catalog = getCatalog(connection, schemaCrawlerOptions); schema = catalog.lookupSchema("PUBLIC.BOOKS").get(); assertThat("Schema PUBLIC.BOOKS not found", schema, notNullValue()); assertThat(catalog.getTables(schema), hasSize(1)); table = catalog.lookupTable(schema, "BOOKAUTHORS").get(); assertThat("Table BOOKAUTHORS not found", table, notNullValue()); schemaCrawlerOptions = SchemaCrawlerOptionsBuilder.builder() .fromOptions(schemaCrawlerOptions).parentTableFilterDepth(1).toOptions(); catalog = getCatalog(connection, schemaCrawlerOptions); schema = catalog.lookupSchema("PUBLIC.BOOKS").get(); assertThat("Schema PUBLIC.BOOKS not found", schema, notNullValue()); assertThat(catalog.getTables(schema).size(), is(3)); table = catalog.lookupTable(schema, "BOOKAUTHORS").get(); assertThat("Table BOOKAUTHORS not found", table, notNullValue()); table = catalog.lookupTable(schema, "BOOKS").get(); assertThat("Table BOOKS not found", table, notNullValue()); table = catalog.lookupTable(schema, "AUTHORS").get(); assertThat("Table AUTHORS not found", table, notNullValue()); }
@Test public void columnLookup(final TestContext testContext, final Connection connection) throws Exception { final SchemaCrawlerOptions schemaCrawlerOptions = SchemaCrawlerOptionsBuilder .newSchemaCrawlerOptions(); final Catalog catalog = getCatalog(connection, schemaCrawlerOptions); assertThat(catalog, notNullValue()); final Schema schema = catalog.lookupSchema("PUBLIC.BOOKS").get(); assertThat(schema, notNullValue()); final Table table = catalog.lookupTable(schema, "AUTHORS").get(); assertThat(table, notNullValue()); assertThat(table.lookupColumn(null), is(emptyOptional())); assertThat(table.lookupColumn(""), is(emptyOptional())); assertThat(table.lookupColumn("NO_COLUMN"), is(emptyOptional())); assertThat(table.lookupColumn("ID"), is(not(emptyOptional()))); }
@Test public void tableUtilities(final TestContext testContext, final Connection connection) throws Exception { final SchemaCrawlerOptionsBuilder schemaCrawlerOptionsBuilder = SchemaCrawlerOptionsBuilder .builder().withSchemaInfoLevel(SchemaInfoLevelBuilder.maximum()); final SchemaCrawlerOptions schemaCrawlerOptions = schemaCrawlerOptionsBuilder .toOptions(); final Catalog catalog = getCatalog(connection, schemaCrawlerOptions); final Schema schema = catalog.lookupSchema("PUBLIC.BOOKS").get(); assertThat("BOOKS Schema not found", schema, notNullValue()); final Table table = catalog.lookupTable(schema, "BOOKS").get(); assertThat("BOOKS Table not found", table, notNullValue()); assertThat(MetaDataUtility.allIndexCoumnNames(table).stream() .flatMap(List::stream).collect(Collectors.toSet()), containsInAnyOrder("PUBLIC.BOOKS.BOOKS.ID", "PUBLIC.BOOKS.BOOKS.PREVIOUSEDITIONID")); assertThat(MetaDataUtility.uniqueIndexCoumnNames(table).stream() .flatMap(List::stream).collect(Collectors.toSet()), containsInAnyOrder("PUBLIC.BOOKS.BOOKS.ID", "PUBLIC.BOOKS.BOOKS.PREVIOUSEDITIONID")); final Index index = table.getIndexes().toArray(new Index[0])[0]; assertThat("Index not found", index, notNullValue()); assertThat(MetaDataUtility.columnNames(index), containsInAnyOrder("PUBLIC.BOOKS.BOOKS.ID")); assertThat(MetaDataUtility.containsGeneratedColumns(index), is(false)); }
assertThat("Schema not found", schema, notNullValue()); final Table table = catalog.lookupTable(schema, tableName).orElse(null); assertThat("Table " + tableName + " not found", table, notNullValue()); if (table.getName().equals(tableName))
@Test public void fkUtilities(final TestContext testContext, final Connection connection) throws Exception { final SchemaCrawlerOptionsBuilder schemaCrawlerOptionsBuilder = SchemaCrawlerOptionsBuilder .builder().withSchemaInfoLevel(SchemaInfoLevelBuilder.maximum()); final SchemaCrawlerOptions schemaCrawlerOptions = schemaCrawlerOptionsBuilder .toOptions(); final Catalog catalog = getCatalog(connection, schemaCrawlerOptions); final Schema schema = catalog.lookupSchema("PUBLIC.BOOKS").get(); assertThat("BOOKS Schema not found", schema, notNullValue()); final Table table = catalog.lookupTable(schema, "BOOKS").get(); assertThat("BOOKS Table not found", table, notNullValue()); final ForeignKey fk = table.getForeignKeys().toArray(new ForeignKey[0])[0]; assertThat("Foreign key not found", fk, notNullValue()); final ColumnReference columnReference = fk.getColumnReferences() .toArray(new ColumnReference[0])[0]; assertThat("Column reference not found", columnReference, notNullValue()); assertThat(MetaDataUtility .constructForeignKeyName(columnReference.getForeignKeyColumn(), columnReference.getPrimaryKeyColumn()), is("SC_AA4376_AFD2BA21")); assertThat(MetaDataUtility.findForeignKeyCardinality(fk), is(ForeignKeyCardinality.zero_many)); assertThat(MetaDataUtility.foreignKeyColumnNames(fk), containsInAnyOrder("PUBLIC.BOOKS.BOOKAUTHORS.BOOKID")); }
@Override public Optional<? extends Table> lookupTable(final Schema schema, final String name) { return catalog.lookupTable(schema, name); }
@Override public Optional<? extends Table> lookupTable(final Schema schema, final String name) { return catalog.lookupTable(schema, name); }
@Test public void viewDefinitions(final TestContext testContext, final Connection connection) throws Exception { final Config config = loadHsqldbConfig(); final SchemaRetrievalOptions schemaRetrievalOptions = SchemaRetrievalOptionsBuilder .newSchemaRetrievalOptions(config); final SchemaCrawlerOptionsBuilder schemaCrawlerOptionsBuilder = SchemaCrawlerOptionsBuilder .builder(); schemaCrawlerOptionsBuilder.tableTypes("VIEW"); schemaCrawlerOptionsBuilder .withSchemaInfoLevel(SchemaInfoLevelBuilder.maximum()); final Catalog catalog = getCatalog(connection, schemaRetrievalOptions, schemaCrawlerOptionsBuilder.toOptions()); final Schema schema = new SchemaReference("PUBLIC", "BOOKS"); final View view = (View) catalog.lookupTable(schema, "AUTHORSLIST").get(); assertThat("View not found", view, notNullValue()); assertThat("View definition not found", view.getDefinition(), notNullValue()); assertThat("View definition not found", isBlank(view.getDefinition()), is(false)); }
assertThat("Primary key table table should not be in the database - " + table.getName(), catalog.lookupTable(table.getSchema(), table.getName()), emptyOptional()); assertThat("Column references do not match",