schemacrawler.utility.SchemaCrawlerUtility Java Examples

The following examples show how to use schemacrawler.utility.SchemaCrawlerUtility. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SchemaCrawlerTest.java    From neo4j-rdbms-import with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws SQLException, SchemaCrawlerException, ClassNotFoundException {

        Driver driver = DriverManager.getDriver("jdbc:derby:memory:test;create=true");
        Connection connection = DriverManager.getConnection("jdbc:derby:memory:test;create=true", new Properties());
        Statement statement = connection.createStatement();
        // id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1)
        statement.execute("CREATE TABLE USERS (id INT NOT NULL, name varchar(20), constraint users_pk_id primary key(id))");
        statement.execute("CREATE TABLE FRIENDS (id1 INT, id2 INT, " +
                " constraint fk_users_id1 foreign key(id1) references users(id)," +
                " constraint fk_users_id2 foreign key(id2) references users(id)" +
                ")");

        final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
        options.setSchemaInfoLevel(SchemaInfoLevel.standard());

        final Catalog catalog = SchemaCrawlerUtility.getCatalog(connection, options);

        for (final Schema schema : catalog.getSchemas()) {
            System.out.println(schema);
            for (final Table table : catalog.getTables(schema)) {
                System.out.println("o--> " + table + " pk " + table.getPrimaryKey() + " fks " + table.getForeignKeys() + " type " + table.getTableType());
                for (final Column column : table.getColumns()) {
                    System.out.println("     o--> " + column + " pk: " + column.isPartOfPrimaryKey() + " fk: " + column.isPartOfForeignKey());
                }
            }
        }
    }
 
Example #2
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected Catalog loadCatalog(DataSource dataSource, SchemaCrawlerOptions options) throws SchemaCrawlerException, SQLException {
    return SchemaCrawlerUtility.getCatalog( dataSource.getConnection(),
            options);
}
 
Example #3
Source File: MetaDataReader.java    From neo4j-rdbms-import with GNU General Public License v3.0 4 votes vote down vote up
static TableInfo[] extractTables(Connection conn, final String schemaName, Rules rules) throws SchemaCrawlerException, SQLException {
        ArrayList<TableInfo> tableList = new ArrayList<TableInfo>(100);

        final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
        options.setSchemaInfoLevel(SchemaInfoLevel.standard());
        System.out.println("my catalog =" + conn.getCatalog());
        options.setSchemaInclusionRule(new InclusionRule() {
            @Override public boolean test(String anObject) {
                return schemaName.equals(anObject);
            }
        });

        final Catalog catalog = SchemaCrawlerUtility.getCatalog(conn, options);
        System.out.println("schem ! ");


        final Schema schema = catalog.getSchema(schemaName);
        System.out.println("Schema: " + schema);

        for (final Table table : catalog.getTables(schema)) {
            String tableName = table.getName();

            System.out.println(table + " pk " + table.getPrimaryKey() + " fks " + table.getForeignKeys() + " type " + table.getTableType());
            if (rules.skipTable(tableName) || table.getTableType().isView()) {
                System.out.println("SKIPPED");
                continue;
            }
            List<Column> columns = table.getColumns();
            List<String> fields = new ArrayList<>(columns.size());
            for (final Column column : columns) {
//                    System.out.println("     o--> " + column + " pk: "+ column.isPartOfPrimaryKey() + " fk: " + column.isPartOfForeignKey());
                String columnName = column.getName();
                if (column.isPartOfPrimaryKey() && rules.skipPrimaryKey(tableName, columnName)) {
                    // skip, todo strategy
                } else if (column.isPartOfForeignKey()) {
                    // skip, todo strategy
                } else {
                    fields.add(columnName);
                }
            }
            Map<List<String>, String> fks = extractForeignKeys(table);
            tableList.add(TableInfo.add(tableName, extractPrimaryKeys(table, fks), fields, fks));
        }

        return tableList.toArray(new TableInfo[tableList.size()]);
    }