Search in sources :

Example 1 with TableLike

use of org.jooq.TableLike in project collect by openforis.

the class JooqRelationalSchemaCreator method createDataTableView.

private void createDataTableView(RelationalSchema schema, DataTable dataTable, Connection conn) {
    CollectDSLContext dsl = new CollectDSLContext(conn);
    List<Field<?>> fields = new ArrayList<Field<?>>();
    List<TableLike<?>> tables = new ArrayList<TableLike<?>>();
    List<Condition> conditions = new ArrayList<Condition>();
    DataTable currentTable = dataTable;
    while (currentTable != null) {
        org.jooq.Table<Record> currentJooqTable = jooqTable(schema, currentTable, !dsl.isSchemaLess());
        tables.add(currentJooqTable);
        List<Column<?>> columns = currentTable.getColumns();
        for (Column<?> column : columns) {
            if (!(column instanceof DataAncestorFKColumn)) {
                fields.add(field(name(currentJooqTable.getName(), column.getName())));
            }
        }
        // add parent table join condition
        DataTable parentTable = currentTable.getParent();
        if (parentTable != null) {
            // names are duplicate, use the table name as prefix in the join condition
            Condition parentTableJoinCondition = field(currentJooqTable.getName() + "." + currentTable.getParentFKColumn().getName()).eq(field(parentTable.getName() + "." + parentTable.getPrimaryKeyColumn().getName()));
            conditions.add(parentTableJoinCondition);
        }
        currentTable = parentTable;
    }
    Select<?> select = dsl.select(fields).from(tables).where(conditions);
    Name name;
    if (dsl.isSchemaLess()) {
        name = name(dataTable.getName() + "_view");
    } else {
        name = name(schema.getName(), dataTable.getName() + "_view");
    }
    dsl.createView(DSL.table(name), fields.toArray(new Field[fields.size()])).as(select).execute();
}
Also used : Condition(org.jooq.Condition) DataTable(org.openforis.collect.relational.model.DataTable) TableLike(org.jooq.TableLike) ArrayList(java.util.ArrayList) CollectDSLContext(org.openforis.collect.persistence.jooq.CollectDSLContext) DataAncestorFKColumn(org.openforis.collect.relational.model.DataAncestorFKColumn) Name(org.jooq.Name) 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

ArrayList (java.util.ArrayList)1 Condition (org.jooq.Condition)1 Field (org.jooq.Field)1 Name (org.jooq.Name)1 Record (org.jooq.Record)1 TableLike (org.jooq.TableLike)1 CollectDSLContext (org.openforis.collect.persistence.jooq.CollectDSLContext)1 CodeListCodeColumn (org.openforis.collect.relational.model.CodeListCodeColumn)1 Column (org.openforis.collect.relational.model.Column)1 DataAncestorFKColumn (org.openforis.collect.relational.model.DataAncestorFKColumn)1 DataTable (org.openforis.collect.relational.model.DataTable)1