schemacrawler.schema.Table Java Examples

The following examples show how to use schemacrawler.schema.Table. 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: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected NodeSpec toNodeSpec(EntitySpec entitySpec, Table table, Column column) {
    NodeSpec nodeSpec = new NodeSpec();
    nodeSpec.setName( getNodeName( column ));
    if (isPrimaryKey(table, column)) {
        nodeSpec.setPrimaryKey(true);
    }
    nodeSpec.setColumnName( column.getName());
    nodeSpec.setJdbcType( getNodeJdbcType( column ));
    nodeSpec.setJavaType( getJavaType( nodeSpec.getJdbcType() ));
    nodeSpec.setNullable( column.isNullable() ? Nullable.NULL : Nullable.NOT_NULL);
    if (nodeSpec.getJavaType() == JavaType.STRING) {
        nodeSpec.setLength( column.getSize() );
    }
    else if (nodeSpec.getJavaType() == JavaType.BIGDECIMAL) {
        nodeSpec.setPrecision( column.getSize() );
        nodeSpec.setScale( column.getDecimalDigits());
    }
    nodeSpec.setEntity(entitySpec);
    nodeSpecs.put(column, nodeSpec);
    return nodeSpec;
}
 
Example #2
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 #3
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 #4
Source File: AvroSchema.java    From avro-schema-generator with MIT License 5 votes vote down vote up
public AvroSchema(Table table, AvroConfig avroConfig) {
    this.name = avroConfig.getSchemaNameMapper().apply(table.getName());
    this.namespace = avroConfig.getNamespace();
    this.fields = new ArrayList<>(table.getColumns().size());

    for (Column column : table.getColumns()) {
        this.fields.add(new AvroField(column, avroConfig));
    }

    avroConfig.getAvroSchemaPostProcessor().accept(this, table);
}
 
Example #5
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected boolean isPrimaryKey(Table table, Column column) {
  if (table.getPrimaryKey() != null) {
    for (IndexColumn col : table.getPrimaryKey().getColumns()) {
      if (col.getName().equalsIgnoreCase(column.getName())) {
        return true;
      }
    }
  } else {
    LOG.warn("No primary key constraints for {}", table.getName());
  }
  return false;
}
 
Example #6
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected EntitySpec toEntitySpec(Table table) {
    EntitySpec entitySpec = new EntitySpec();
    entitySpec.setTableName( table.getName());
    entitySpec.setClassName( generateClassName(table) );
    entitySpec.setAbstractEntity(false);
    entitySpec.setQueryClassName( generateQueryClassName(table) );
    entitySpec.setDtoClassName( generateDtoClassName(table) );

    for (Column column: table.getColumns()) {
        if (exclusionRules.excludeColumn( column )) {
          continue;
        }
        LOG.debug("Processing column {}", column.getName());
        NodeSpec nodeSpec = toNodeSpec(entitySpec, table, column);
        entitySpec.add( nodeSpec );

        //set PK contraints
        Collection<NodeSpec> key = new LinkedList<>();
        for (NodeSpec ns: entitySpec.getNodeSpecs()) {
            if (ns.isPrimaryKey()) {
                key.add(ns);
            }
        }
        if (!key.isEmpty()) {
            createPrimaryKeyConstraint(entitySpec, key);
        }

    }
    return entitySpec;
}
 
Example #7
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public FromDatabaseSchemaToSpecification(String namespace) {
  this(namespace, new ExclusionRules() {
    @Override
    public boolean excludeTable(Table tableName) {
      return false;
    }

    @Override
    public boolean excludeColumn(Column column) {
      return false;
    }
  });
}
 
Example #8
Source File: DaCatalogImpl.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public ImmutableCollection<DaTable> getTables() {
    return CollectionAdapter.adapt(delegate.getTables())
            .collect(new Function<Table, DaTable>() {
                @Override
                public DaTable valueOf(Table object) {
                    if (object instanceof View) {
                        return new DaViewImpl((View) object, schemaStrategy, extraViewInfoMap.get(object.getName()));
                    } else {
                        return new DaTableImpl(object, schemaStrategy, extraIndexes);
                    }
                }
            })
            .toImmutable();
}
 
Example #9
Source File: DaIndexImpl.java    From obevo with Apache License 2.0 5 votes vote down vote up
private DaIndexImpl(DependantObject<Table> index, List<? extends Column> columns, SchemaStrategy schemaStrategy, boolean unique, DaIndexType indexType) {
    this.index = Validate.notNull(index);
    Validate.notNull(schemaStrategy);
    this.columns = ListAdapter.adapt(columns)
            .collect((Function<Column, DaColumn>) object -> new DaColumnImpl(object, schemaStrategy))
            .toImmutable();
    this.schemaStrategy = Validate.notNull(schemaStrategy);
    this.unique = unique;
    this.indexType = indexType;
}
 
Example #10
Source File: DaTableImpl.java    From obevo with Apache License 2.0 5 votes vote down vote up
public DaTableImpl(Table table, SchemaStrategy schemaStrategy, Multimap<String, ExtraIndexInfo> extraIndexes) {
    this.table = Validate.notNull(table);
    this.schemaStrategy = schemaStrategy;
    this.extraIndexInfoMap = extraIndexes.get(table.getName()).groupByUniqueKey(new Function<ExtraIndexInfo, String>() {
        @Override
        public String valueOf(ExtraIndexInfo extraIndexInfo) {
            return extraIndexInfo.getIndexName();
        }
    });
}
 
Example #11
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 #12
Source File: DaTableImpl.java    From obevo with Apache License 2.0 4 votes vote down vote up
public DaTableImpl(Table table, SchemaStrategy schemaStrategy) {
    this(table, schemaStrategy, Multimaps.immutable.list.<String, ExtraIndexInfo>empty());
}
 
Example #13
Source File: AvroConfig.java    From avro-schema-generator with MIT License 4 votes vote down vote up
public BiConsumer<AvroSchema, Table> getAvroSchemaPostProcessor() {
    return avroSchemaPostProcessor;
}
 
Example #14
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected String generateQueryClassName(Table table) {
    String ccName = "Q" + removePrefixes( toCamelCase(table.getName()) );
    return  namespace + ".query." + ccName;
}
 
Example #15
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected String generateDtoClassName(Table table) {
    String ccName = toCamelCase(table.getName());
    ccName = Character.toUpperCase( ccName.charAt(0) ) + ccName.substring(1, ccName.length());
    return namespace + ".dto." + removePrefixes( ccName )+ "Dto";
}
 
Example #16
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected String generateClassName(Table table) {
    String ccName = toCamelCase(table.getName());
    ccName = Character.toUpperCase( ccName.charAt(0) ) + ccName.substring(1, ccName.length());
    return namespace + ".model." + removePrefixes( ccName );
}
 
Example #17
Source File: AvroConfig.java    From avro-schema-generator with MIT License 4 votes vote down vote up
/**
 * Set a callback that will be called after avro model was built.
 * Schema model is ready by this point, but you can still modify it by adding custom properties.
 */
public AvroConfig setAvroSchemaPostProcessor(BiConsumer<AvroSchema, Table> avroSchemaPostProcessor) {
    this.avroSchemaPostProcessor = avroSchemaPostProcessor;
    return this;
}
 
Example #18
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 votes vote down vote up
boolean excludeTable(Table tableName);