use of org.jooq.SelectLimitStep in project jOOQ by jOOQ.
the class SelectQueryImpl method distinctOnEmulation.
@SuppressWarnings({ "rawtypes", "unchecked" })
private final Select<?> distinctOnEmulation() {
// [#3564] TODO: Extract and merge this with getSelectResolveSomeAsterisks0()
List<Field<?>> partitionBy = new ArrayList<>(distinctOn.size());
for (SelectFieldOrAsterisk s : distinctOn) if (s instanceof Field)
partitionBy.add((Field<?>) s);
Field<Integer> rn = rowNumber().over(partitionBy(partitionBy).orderBy(orderBy)).as("rn");
SelectQueryImpl<R> copy = copy(x -> {
});
copy.distinctOn = null;
copy.select.add(rn);
copy.orderBy.clear();
copy.limit.clear();
SelectLimitStep<?> s1 = DSL.select(new QualifiedSelectFieldList(table(name("t")), select)).from(copy.asTable("t")).where(rn.eq(one())).orderBy(map(orderBy, o -> unqualified(o)));
if (limit.limit != null) {
SelectLimitPercentStep<?> s2 = s1.limit(limit.limit);
SelectWithTiesStep<?> s3 = limit.percent ? s2.percent() : s2;
SelectOffsetStep<?> s4 = limit.withTies ? s3.withTies() : s3;
return limit.offset != null ? s4.offset(limit.offset) : s4;
} else
return limit.offset != null ? s1.offset(limit.offset) : s1;
}
Aggregations