schemacrawler.schema.Catalog Java Examples

The following examples show how to use schemacrawler.schema.Catalog. 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: DaCatalogImpl.java    From obevo with Apache License 2.0 6 votes vote down vote up
public DaCatalogImpl(Catalog delegate, SchemaStrategy schemaStrategy, ImmutableCollection<DaUserType> userTypes, ImmutableCollection<DaRule> rules, ImmutableCollection<RuleBinding> ruleBindings, ImmutableCollection<DaRoutine> extraRoutines, Multimap<String, ExtraIndexInfo> extraIndexes, ImmutableCollection<ExtraRerunnableInfo> extraViewInfo, DaRoutineType routineOverrideValue, ImmutableCollection<DaPackage> packages) {
    this.delegate = Validate.notNull(delegate);
    this.userTypes = userTypes;
    this.rules = rules;
    this.ruleBindings = ruleBindings;
    this.extraRoutines = extraRoutines;
    this.extraIndexes = extraIndexes;
    this.extraViewInfoMap = extraViewInfo.groupByUniqueKey(new Function<ExtraRerunnableInfo, String>() {
        @Override
        public String valueOf(ExtraRerunnableInfo extraRerunnableInfo) {
            return extraRerunnableInfo.getName();
        }
    });
    this.routineOverrideValue = routineOverrideValue;
    this.schemaStrategy = schemaStrategy;
    this.packages = packages;
}
 
Example #2
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 6 votes vote down vote up
public SpecRegistry generateSpecification(DataSource dataSource, String schemaName) throws Exception {
    SchemaCrawlerOptions options = new SchemaCrawlerOptions();
    // Set what details are required in the schema - this affects the
    // time taken to crawl the schema
    options.setSchemaInfoLevel(SchemaInfoLevelBuilder.detailed());
    if (schemaName != null) {
      options.setSchemaInclusionRule(s -> s.equals( schemaName ));
    }

    Catalog catalog = loadCatalog(dataSource, options);

    firstPass(catalog);
    secondPass(catalog);
    postProcess(catalog);
    return registry;
}
 
Example #3
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * create the entityspecs and nodespecs.
 * @param catalog
 */
private void firstPass(Catalog catalog) {
    LOG.debug("First pass...");
    for (Schema schema: catalog.getSchemas()) {
      LOG.debug("Processing schema...");
      for (Table table: catalog.getTables(schema)) {
         if (exclusionRules.excludeTable(table)) {
          continue;
         }
        LOG.debug("Processing table {}", table.getName());
        EntitySpec espec = toEntitySpec(table);
        entitySpecs.put(table, espec);
        spec.add( espec );
      }
    }

}
 
Example #4
Source File: MsSqlMetadataDialect.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public void validateDatabase(Catalog database, final PhysicalSchema physicalSchema) {
    MutableCollection<Schema> schemasWithIncorrectCatalog = CollectionAdapter.adapt(database.getSchemas()).reject(each -> each.getCatalogName().equals(physicalSchema.getPhysicalName()));

    if (schemasWithIncorrectCatalog.notEmpty()) {
        throw new IllegalArgumentException("Returned ASE schemas should be in " + physicalSchema.getPhysicalName() + " catalog; however, these were not: " + schemasWithIncorrectCatalog);
    }
}
 
Example #5
Source File: AbstractMetadataDialect.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public void validateDatabase(Catalog database, PhysicalSchema physicalSchema) {
    if (database.getSchemas().size() != 1) {
        throw new IllegalArgumentException("Should find 1 schema only for schema " + physicalSchema + "; found "
                + CollectionAdapter.adapt(database.getSchemas()).makeString(", "));
    }
}
 
Example #6
Source File: SybaseAseMetadataDialect.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public void validateDatabase(Catalog database, final PhysicalSchema physicalSchema) {
    MutableCollection<Schema> schemasWithIncorrectCatalog = CollectionAdapter.adapt(database.getSchemas()).reject(new Predicate<Schema>() {
        @Override
        public boolean accept(Schema each) {
            return each.getCatalogName().equals(physicalSchema.getPhysicalName());
        }
    });

    if (schemasWithIncorrectCatalog.notEmpty()) {
        throw new IllegalArgumentException("Returned ASE schemas should be in " + physicalSchema.getPhysicalName() + " catalog; however, these were not: " + schemasWithIncorrectCatalog);
    }
}
 
Example #7
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 #8
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 #9
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * process the foreign key relations and constraints.
 */
private void secondPass(Catalog catalog) {
    for (Schema schema: catalog.getSchemas()) {
        LOG.debug(schema.toString());
        for (Table table: catalog.getTables(schema)) {
            for (ForeignKey fk: table.getForeignKeys()) {
                for (ForeignKeyColumnReference fkRef: fk.getColumnReferences()) {
                    Column fkCol = fkRef.getForeignKeyColumn();
                    Column pkCol = fkRef.getPrimaryKeyColumn();
                    Table pkTable = pkCol.getParent();

                    EntitySpec pkEspec = entitySpecs.get(pkTable);
                    EntitySpec fkEspec = entitySpecs.get(fkCol.getParent());
                    NodeSpec fkNSpec = nodeSpecs.get(fkCol);
                    if (pkEspec == null || fkEspec == null || fkNSpec == null) {
                      continue;
                    }

                    if (!processedFks.add(new ProcessedFk(fkNSpec, pkEspec))) {
                        continue;
                    }

                    /*
                     *  Do the N:1 natuarl foreign key relation *
                     */
                    fkNSpec.setName( genRealtionNodeName(fkNSpec) );
                    //java type is null, as the relation defines the type.
                    fkNSpec.setJavaType(null);
                    RelationSpec rspec = new RelationSpec();
                    rspec.setEntitySpec(pkEspec);
                    rspec.setJoinType(JoinTypeSpec.LEFT_OUTER_JOIN);
                    rspec.setType(RelationType.REFERS);
                    fkNSpec.setRelation(rspec);

                    LOG.debug("Added FK relation from {}.{} to {}", fkEspec.getClassName(), fkNSpec.getName(), pkEspec.getClassName());

                    createForeignKeyConstraint(fkEspec, fkNSpec, rspec);

                    /*
                     * do the opposite 1:N relation
                     */
                    //create the nodespec as there is no dbcolumn whch created a node for us
                    LOG.debug("Creating tomany node for N relation from {} to {}", pkEspec.getClassName(), fkEspec.getClassName());
                    NodeSpec toManyNodeSpec = new NodeSpec();
                    String nodeName = genRealtionNodeName(fkEspec, true);
                    while (pkEspec.getNodeSpec(nodeName) != null) {
                        nodeName = incrementNodeName(nodeName);
                    }
                    toManyNodeSpec.setName( nodeName );
                    toManyNodeSpec.setEntity(pkEspec);

                    rspec = new RelationSpec();
                    rspec.setBackReference(fkNSpec);
                    rspec.setEntitySpec(fkEspec);
                    rspec.setJoinType(JoinTypeSpec.LEFT_OUTER_JOIN);
                    rspec.setType(RelationType.REFERS);
                    toManyNodeSpec.setRelation(rspec);
                    pkEspec.add(toManyNodeSpec);
                }
            }
        }
    }
}
 
Example #10
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * provides the oppertunity for further processing.
 * @param catalog
 */
protected void postProcess(Catalog catalog ) {
}
 
Example #11
Source File: DbMetadataDialect.java    From obevo with Apache License 2.0 votes vote down vote up
void validateDatabase(Catalog database, PhysicalSchema physicalSchema);