Search in sources :

Example 1 with PrimaryKeyConstraint

use of org.openforis.collect.relational.model.PrimaryKeyConstraint 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 PrimaryKeyConstraint

use of org.openforis.collect.relational.model.PrimaryKeyConstraint in project collect by openforis.

the class LiquidbaseDatabaseSnapshotBuilder method createPKIndexes.

private void createPKIndexes() {
    for (org.openforis.collect.relational.model.Table<?> itable : schema.getTables()) {
        Table ltable = snapshot.getTable(itable.getName());
        PrimaryKeyConstraint pkConstraint = itable.getPrimaryKeyConstraint();
        Index index = new Index();
        index.setTable(ltable);
        index.setName(pkConstraint.getName() + "_idx");
        List<org.openforis.collect.relational.model.Column<?>> columns = pkConstraint.getColumns();
        for (org.openforis.collect.relational.model.Column<?> column : columns) {
            index.addAssociatedWith(Index.MARK_PRIMARY_KEY);
            index.getColumns().add(column.getName());
        }
        snapshot.getIndexes().add(index);
    }
}
Also used : Table(liquibase.database.structure.Table) PrimaryKeyColumn(org.openforis.collect.relational.model.PrimaryKeyColumn) Column(liquibase.database.structure.Column) Index(liquibase.database.structure.Index) PrimaryKeyConstraint(org.openforis.collect.relational.model.PrimaryKeyConstraint)

Example 3 with PrimaryKeyConstraint

use of org.openforis.collect.relational.model.PrimaryKeyConstraint in project collect by openforis.

the class SqlSchemaWriter method writeForeignKeyConstraint.

private void writeForeignKeyConstraint(ReferentialConstraint fk) throws IOException {
    writer.write(" FOREIGN KEY ");
    writer.write('(');
    writeColumnNameSet(fk.getColumns());
    writer.write(')');
    writer.write(" REFERENCES ");
    UniquenessConstraint referencedKey = fk.getReferencedKey();
    Table<?> referencedTable = referencedKey.getTable();
    writer.write(getQualifiedTableName(referencedTable));
    PrimaryKeyConstraint referencedTablePK = referencedTable.getPrimaryKeyConstraint();
    writer.write('(');
    writeColumnNameSet(referencedTablePK.getColumns());
    writer.write(")");
}
Also used : UniquenessConstraint(org.openforis.collect.relational.model.UniquenessConstraint) PrimaryKeyConstraint(org.openforis.collect.relational.model.PrimaryKeyConstraint)

Example 4 with PrimaryKeyConstraint

use of org.openforis.collect.relational.model.PrimaryKeyConstraint in project collect by openforis.

the class JooqRelationalSchemaCreator method addPKConstraints.

private void addPKConstraints(RelationalSchema schema, CollectDSLContext dsl) {
    for (Table<?> table : schema.getTables()) {
        org.jooq.Table<Record> jooqTable = jooqTable(schema, table, !dsl.isSchemaLess());
        PrimaryKeyConstraint pkConstraint = table.getPrimaryKeyConstraint();
        String pkColumnName = pkConstraint.getPrimaryKeyColumn().getName();
        String pkConstraintName = table.getName() + "_pk";
        dsl.alterTable(jooqTable).add(constraint(pkConstraintName).primaryKey(pkColumnName)).execute();
    }
}
Also used : Record(org.jooq.Record) PrimaryKeyConstraint(org.openforis.collect.relational.model.PrimaryKeyConstraint)

Example 5 with PrimaryKeyConstraint

use of org.openforis.collect.relational.model.PrimaryKeyConstraint in project collect by openforis.

the class JooqRelationalSchemaCreator method addPKIndexes.

private void addPKIndexes(RelationalSchema schema, CollectDSLContext dsl) {
    for (Table<?> table : schema.getTables()) {
        org.jooq.Table<Record> jooqTable = jooqTable(schema, table, !dsl.isSchemaLess());
        // For SQLite it creates an index on the primary key, for POSTGRESQL it will alter the table to set the primary key columns
        PrimaryKeyConstraint pkConstraint = table.getPrimaryKeyConstraint();
        String pkColumnName = pkConstraint.getPrimaryKeyColumn().getName();
        String pkConstraintName = table.getName() + "_pk";
        dsl.createIndex(pkConstraintName).unique().on(jooqTable, field(pkColumnName)).execute();
    }
}
Also used : Record(org.jooq.Record) PrimaryKeyConstraint(org.openforis.collect.relational.model.PrimaryKeyConstraint)

Aggregations

PrimaryKeyConstraint (org.openforis.collect.relational.model.PrimaryKeyConstraint)6 UniquenessConstraint (org.openforis.collect.relational.model.UniquenessConstraint)3 Record (org.jooq.Record)2 Column (liquibase.database.structure.Column)1 Index (liquibase.database.structure.Index)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 CodeTable (org.openforis.collect.relational.model.CodeTable)1 CodeValueFKColumn (org.openforis.collect.relational.model.CodeValueFKColumn)1 DataColumn (org.openforis.collect.relational.model.DataColumn)1 DataTable (org.openforis.collect.relational.model.DataTable)1 PrimaryKeyColumn (org.openforis.collect.relational.model.PrimaryKeyColumn)1