Search in sources :

Example 1 with ReferentialConstraint

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;
}
Also used : CodeTable(org.openforis.collect.relational.model.CodeTable) DataTable(org.openforis.collect.relational.model.DataTable) Table(mondrian.olap.MondrianDef.Table) ReferentialConstraint(org.openforis.collect.relational.model.ReferentialConstraint) ForeignKey(mondrian.olap.MondrianDef.ForeignKey) PrimaryKeyConstraint(org.openforis.collect.relational.model.PrimaryKeyConstraint) PhysicalSchema(mondrian.olap.MondrianDef.PhysicalSchema) Column(mondrian.olap.MondrianDef.Column) CodeValueFKColumn(org.openforis.collect.relational.model.CodeValueFKColumn) DataColumn(org.openforis.collect.relational.model.DataColumn) UniquenessConstraint(org.openforis.collect.relational.model.UniquenessConstraint) Link(mondrian.olap.MondrianDef.Link) DimensionLink(mondrian.olap.MondrianDef.DimensionLink) ForeignKeyLink(mondrian.olap.MondrianDef.ForeignKeyLink) FactLink(mondrian.olap.MondrianDef.FactLink) NoLink(mondrian.olap.MondrianDef.NoLink)

Example 2 with ReferentialConstraint

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');
}
Also used : Column(org.openforis.collect.relational.model.Column) PrimaryKeyColumn(org.openforis.collect.relational.model.PrimaryKeyColumn) ReferentialConstraint(org.openforis.collect.relational.model.ReferentialConstraint) PrimaryKeyConstraint(org.openforis.collect.relational.model.PrimaryKeyConstraint) ReferentialConstraint(org.openforis.collect.relational.model.ReferentialConstraint) UniquenessConstraint(org.openforis.collect.relational.model.UniquenessConstraint)

Example 3 with ReferentialConstraint

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);
        }
    }
}
Also used : Table(liquibase.database.structure.Table) ReferentialConstraint(org.openforis.collect.relational.model.ReferentialConstraint) UniquenessConstraint(org.openforis.collect.relational.model.UniquenessConstraint) ForeignKey(liquibase.database.structure.ForeignKey)

Example 4 with ReferentialConstraint

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++;
            }
        }
    }
}
Also used : DataTable(org.openforis.collect.relational.model.DataTable) ReferentialConstraint(org.openforis.collect.relational.model.ReferentialConstraint) Record(org.jooq.Record) ReferentialConstraint(org.openforis.collect.relational.model.ReferentialConstraint) DSL.constraint(org.jooq.impl.DSL.constraint) PrimaryKeyConstraint(org.openforis.collect.relational.model.PrimaryKeyConstraint)

Example 5 with ReferentialConstraint

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();
        }
    }
}
Also used : ReferentialConstraint(org.openforis.collect.relational.model.ReferentialConstraint) Field(org.jooq.Field) DataAncestorFKColumn(org.openforis.collect.relational.model.DataAncestorFKColumn) Column(org.openforis.collect.relational.model.Column) CodeListCodeColumn(org.openforis.collect.relational.model.CodeListCodeColumn) Record(org.jooq.Record)

Aggregations

ReferentialConstraint (org.openforis.collect.relational.model.ReferentialConstraint)8 PrimaryKeyConstraint (org.openforis.collect.relational.model.PrimaryKeyConstraint)6 UniquenessConstraint (org.openforis.collect.relational.model.UniquenessConstraint)6 Column (org.openforis.collect.relational.model.Column)3 Record (org.jooq.Record)2 DataTable (org.openforis.collect.relational.model.DataTable)2 PrimaryKeyColumn (org.openforis.collect.relational.model.PrimaryKeyColumn)2 ForeignKey (liquibase.database.structure.ForeignKey)1 Table (liquibase.database.structure.Table)1 Column (mondrian.olap.MondrianDef.Column)1 DimensionLink (mondrian.olap.MondrianDef.DimensionLink)1 FactLink (mondrian.olap.MondrianDef.FactLink)1 ForeignKey (mondrian.olap.MondrianDef.ForeignKey)1 ForeignKeyLink (mondrian.olap.MondrianDef.ForeignKeyLink)1 Link (mondrian.olap.MondrianDef.Link)1 NoLink (mondrian.olap.MondrianDef.NoLink)1 PhysicalSchema (mondrian.olap.MondrianDef.PhysicalSchema)1 Table (mondrian.olap.MondrianDef.Table)1 Field (org.jooq.Field)1 DSL.constraint (org.jooq.impl.DSL.constraint)1