use of org.openforis.collect.relational.model.ReferentialConstraint in project collect by openforis.
the class Mondrian4SchemaGenerator method generatePhysicalSchema.
// private String getDateFieldLevelType(String fieldName) {
// if (DateAttributeDefinition.YEAR_FIELD_NAME.equals(fieldName)) {
// return "TimeYears";
// } else if (DateAttributeDefinition.MONTH_FIELD_NAME.equals(fieldName)) {
// return "TimeMonths";
// } else if(DateAttributeDefinition.DAY_FIELD_NAME.equals(fieldName)) {
// return "TimeDays";
// } else {
// throw new IllegalArgumentException("Unexpected date attribute field name: " + fieldName);
// }
// }
private PhysicalSchema generatePhysicalSchema() {
PhysicalSchema physicalSchema = new PhysicalSchema();
for (org.openforis.collect.relational.model.Table<?> rdbTable : rdbSchema.getTables()) {
Table table = new Table();
table.name = rdbTable.getName();
PrimaryKeyConstraint pkConstraint = rdbTable.getPrimaryKeyConstraint();
table.keyColumn = pkConstraint.getPrimaryKeyColumn().getName();
physicalSchema.children.add(table);
// add foreign keys
for (ReferentialConstraint referentialConstraint : rdbTable.getReferentialContraints()) {
UniquenessConstraint referencedKey = referentialConstraint.getReferencedKey();
org.openforis.collect.relational.model.Table<?> referencedRdbTable = referencedKey.getTable();
Link link = new Link();
link.source = rdbTable.getName();
link.target = referencedRdbTable.getName();
ForeignKey foreignKey = new ForeignKey();
for (org.openforis.collect.relational.model.Column<?> referencedRdbColumn : referencedKey.getColumns()) {
Column fkColumn = new Column();
fkColumn.name = referencedRdbColumn.getName();
foreignKey.list().add(fkColumn);
}
link.foreignKey = foreignKey;
physicalSchema.children.add(link);
}
}
return physicalSchema;
}
use of org.openforis.collect.relational.model.ReferentialConstraint in project collect by openforis.
the class SqlSchemaWriter method writeTable.
private void writeTable(Table<?> table) throws IOException {
writer.write("CREATE TABLE ");
if (!isSchemaless()) {
writer.write(doubleQuote(schema.getName()));
writer.write('.');
}
writer.write(doubleQuote(table.getName()));
writer.write(" (");
writer.write('\n');
List<Column<?>> columns = table.getColumns();
for (int i = 0; i < columns.size(); i++) {
if (i > 0) {
writer.write(',');
writer.write('\n');
}
Column<?> column = columns.get(i);
writeColumn(column);
}
if (includeForeignKeysInCreateTable && !table.getReferentialContraints().isEmpty()) {
writer.write(',');
writer.write('\n');
List<ReferentialConstraint> fks = table.getReferentialContraints();
for (int i = 0; i < fks.size(); i++) {
ReferentialConstraint fk = fks.get(i);
writer.write('\t');
writeForeignKeyConstraint(fk);
if (i < fks.size() - 1) {
writer.write(", \n");
}
}
}
writer.write('\n');
writer.write(");");
writer.write('\n');
}
use of org.openforis.collect.relational.model.ReferentialConstraint in project collect by openforis.
the class LiquidbaseDatabaseSnapshotBuilder method createForeignKeys.
protected void createForeignKeys() {
for (org.openforis.collect.relational.model.Table<?> itable : schema.getTables()) {
Table ltable = snapshot.getTable(itable.getName());
List<ReferentialConstraint> ifks = itable.getReferentialContraints();
for (ReferentialConstraint ifk : ifks) {
ForeignKey lfk = new ForeignKey();
lfk.setName(ifk.getName());
// set base table columns
lfk.setForeignKeyTable(ltable);
for (org.openforis.collect.relational.model.Column<?> ifcCol : ifk.getColumns()) {
lfk.addForeignKeyColumn(ifcCol.getName());
}
// set referenced key columns
UniquenessConstraint iReferencedKey = ifk.getReferencedKey();
org.openforis.collect.relational.model.Table<?> iReferencedTable = iReferencedKey.getTable();
Table lReferencedTable = snapshot.getTable(iReferencedTable.getName());
lfk.setPrimaryKeyTable(lReferencedTable);
for (org.openforis.collect.relational.model.Column<?> refCol : iReferencedKey.getColumns()) {
lfk.addPrimaryKeyColumn(refCol.getName());
}
// Add fk
snapshot.getForeignKeys().add(lfk);
}
}
}
use of org.openforis.collect.relational.model.ReferentialConstraint in project collect by openforis.
the class JooqRelationalSchemaCreator method addFKIndexes.
private void addFKIndexes(RelationalSchema schema, CollectDSLContext dsl) {
for (Table<?> table : schema.getTables()) {
if (table instanceof DataTable) {
org.jooq.Table<Record> jooqTable = jooqTable(schema, table, !dsl.isSchemaLess());
int idxCount = 1;
for (ReferentialConstraint referentialConstraint : table.getReferentialContraints()) {
String idxName = String.format("%s_%d_idx", table.getName(), idxCount);
dsl.createIndex(idxName).on(jooqTable, toJooqFields(referentialConstraint.getColumns())).execute();
idxCount++;
}
}
}
}
use of org.openforis.collect.relational.model.ReferentialConstraint in project collect by openforis.
the class JooqRelationalSchemaCreator method createForeignKeys.
private void createForeignKeys(RelationalSchema schema, CollectDSLContext dsl) {
for (org.openforis.collect.relational.model.Table<?> table : schema.getTables()) {
List<ReferentialConstraint> fks = table.getReferentialContraints();
for (ReferentialConstraint fk : fks) {
Field<?>[] fields = toJooqFields(fk.getColumns());
org.jooq.Table<Record> jooqTable = jooqTable(schema, table, !dsl.isSchemaLess());
org.jooq.Table<Record> referencedJooqTable = jooqTable(schema, fk.getReferencedKey().getTable(), !dsl.isSchemaLess());
List<Column<?>> referencedColumns = fk.getReferencedKey().getColumns();
dsl.alterTable(jooqTable).add(constraint(fk.getName()).foreignKey(fields).references(referencedJooqTable, toJooqFields(referencedColumns))).execute();
}
}
}
Aggregations