Java源码示例:schemacrawler.schema.Table
示例1
/**
* 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 );
}
}
}
示例2
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;
}
示例3
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);
}
示例4
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();
}
});
}
示例5
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;
}
示例6
@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();
}
示例7
public FromDatabaseSchemaToSpecification(String namespace) {
this(namespace, new ExclusionRules() {
@Override
public boolean excludeTable(Table tableName) {
return false;
}
@Override
public boolean excludeColumn(Column column) {
return false;
}
});
}
示例8
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;
}
示例9
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;
}
示例10
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());
}
}
}
}
示例11
/**
* 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;
}
示例12
public BiConsumer<AvroSchema, Table> getAvroSchemaPostProcessor() {
return avroSchemaPostProcessor;
}
示例13
public DaTableImpl(Table table, SchemaStrategy schemaStrategy) {
this(table, schemaStrategy, Multimaps.immutable.list.<String, ExtraIndexInfo>empty());
}
示例14
/**
* 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);
}
}
}
}
}
示例15
protected String generateQueryClassName(Table table) {
String ccName = "Q" + removePrefixes( toCamelCase(table.getName()) );
return namespace + ".query." + ccName;
}
示例16
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";
}
示例17
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 );
}
示例18
boolean excludeTable(Table tableName);