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();
}
Aggregations