io.seventytwo.vaadinjooq.repository.JooqRepository Maven / Gradle / Ivy
package io.seventytwo.vaadinjooq.repository;
import org.jooq.*;
import org.jooq.impl.DSL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.name;
public class JooqRepository {
private final DSLContext dslContext;
public JooqRepository(DSLContext dslContext) {
this.dslContext = dslContext;
}
public List findAll(Table table, Condition condition, Map, Boolean> orderBy, int offset, int limit) {
SelectConditionStep where;
if (condition == null) {
where = dslContext.selectFrom(table)
.where(DSL.noCondition());
} else {
where = dslContext.selectFrom(table)
.where(condition);
}
if (orderBy != null && !orderBy.isEmpty()) {
return createOrderBy(table, where, orderBy)
.offset(offset)
.limit(limit)
.fetch();
} else {
return where
.offset(offset)
.limit(limit)
.fetch();
}
}
public int count(Table table, Condition condition) {
if (condition == null) {
return dslContext.fetchCount(dslContext.selectFrom(table));
} else {
return dslContext.fetchCount(dslContext.selectFrom(table).where(condition));
}
}
private SelectSeekStepN createOrderBy(Table table, SelectConditionStep where, Map, Boolean> orderColumns) {
List> orderFields = new ArrayList<>();
orderColumns.forEach((key, value) -> {
List qualifiers = new ArrayList<>();
qualifiers.add(table.getSchema().getName());
qualifiers.addAll(Arrays.asList(table.getQualifiedName().getName()));
qualifiers.add(key.getName());
Name column = name(qualifiers);
Field