Java源码示例:schemacrawler.schema.Catalog
示例1
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;
}
示例2
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;
}
示例3
/**
* 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 );
}
}
}
示例4
@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);
}
}
示例5
@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(", "));
}
}
示例6
@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);
}
}
示例7
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());
}
}
}
}
示例8
protected Catalog loadCatalog(DataSource dataSource, SchemaCrawlerOptions options) throws SchemaCrawlerException, SQLException {
return SchemaCrawlerUtility.getCatalog( dataSource.getConnection(),
options);
}
示例9
/**
* 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);
}
}
}
}
}
示例10
/**
* provides the oppertunity for further processing.
* @param catalog
*/
protected void postProcess(Catalog catalog ) {
}
示例11
void validateDatabase(Catalog database, PhysicalSchema physicalSchema);