/** * TODO Change to List for return value * * @param sortSpecification Sort from Spring Data * @return Collection of fields to sort (can be empty if {@code sortSpecification} is null or no sort values) * @throws SortingApiException if any sort field given cannot be found (it should result in a 400 error message) or sort contains duplicate keys */ @NotNull default Collection<SortField<?>> buildOrderBy(@Nullable final Sort sortSpecification) { Collection<SortField<?>> querySortFields = new ArrayList<>(10); if (sortSpecification == null) { return getDefaultSorting(); } List<String> usedSortKey = new ArrayList<>(10); for (final Sort.Order specifiedField : sortSpecification) { String sortFieldName = specifiedField.getProperty(); Sort.Direction sortDirection = specifiedField.getDirection(); if (usedSortKey.contains(sortFieldName)) throw new SortingApiException("Cannot sort on duplicate keys"); SortValue sortValue = getTableField(sortFieldName); SortField<?> querySortField = convertTableFieldToSortField(sortValue, sortDirection); querySortFields.add(querySortField); usedSortKey.add(sortFieldName); } return querySortFields; }
@Test void getSortFieldWorks() { final SortField sortFieldAscId = intSortValue.getSortField(SortOrder.ASC); final SortField sortFieldDescId = intSortValue.getSortField(SortOrder.DESC); Assertions.assertEquals("id", sortFieldAscId.getName()); Assertions.assertEquals(SortOrder.ASC, sortFieldAscId.getOrder()); Assertions.assertEquals("id", sortFieldDescId.getName()); Assertions.assertEquals(SortOrder.DESC, sortFieldDescId.getOrder()); final SortField sortFieldAscName = stringSortValue.getSortField(SortOrder.ASC); final SortField sortFieldDescName = stringSortValue.getSortField(SortOrder.DESC); Assertions.assertEquals("name", sortFieldAscName.getName()); Assertions.assertEquals(SortOrder.ASC, sortFieldAscName.getOrder()); Assertions.assertEquals("name", sortFieldDescName.getName()); Assertions.assertEquals(SortOrder.DESC, sortFieldDescName.getOrder()); }
private SortField<?>[] addSortFields(QueryParams queryParams) { ArrayList<SortField<?>> sortFields = new ArrayList<SortField<?>>(); for (QueryParam queryParam : queryParams) { switch (queryParam.getOperator()) { case SORT_ASCENDING: sortFields.add(org.jooq.impl.DSL.field(queryParam.getKey()).asc()); break; case SORT_DESCENDING: sortFields.add(org.jooq.impl.DSL.field(queryParam.getKey()).desc()); break; default: break; } } SortField<?>[] rs = new SortField<?>[sortFields.size()]; return sortFields.toArray(rs); }
private SelectQuery<R> getQuery(Sort sort) { SelectQuery<R> query = dsl.selectFrom(table).getQuery(); // Do not sort if specified sort condition. if (sort == null) { return query; } for (Sort.Order order : sort) { // It's currently only allowed column name of lowercase. Field<?> field = table.field(name(LOWER_CAMEL.to(LOWER_UNDERSCORE, order.getProperty()))); if (field == null) { // TODO Consider later that can't find the field which has sort condition. continue; } SortField<?> sortField; if (order.getDirection() == Sort.Direction.ASC) { sortField = field.asc(); } else { sortField = field.desc(); } query.addOrderBy(sortField); } return query; }
/** * Is not supposed to be called from external of interface (might be private in Java 9) * * @param sortValue SortValue to get SortField from * @param sortDirection Direction of sorting from request * @return A field sorted for jOOQ */ @NotNull default SortField<?> convertTableFieldToSortField(SortValue sortValue, Sort.Direction sortDirection) { if (sortDirection == Sort.Direction.ASC) { return sortValue.getSortField(SortOrder.ASC); } else { return sortValue.getSortField(SortOrder.DESC); } }
/** * TODO Change to List for return value * <p>Return default sorting. An empty collection if not sorting values set or the first one defined.</p> * <p>Overridden implementation can set many field to sort on by default</p> * * @return Collection of field to sort on (can be empty) */ @NotNull default Collection<SortField<?>> getDefaultSorting() { final Collection<SortValue> values = getSortAliasMapping().values(); if (values.isEmpty()) return emptySorting(); return Collections.singletonList(values.iterator().next().getSortField(SortOrder.DESC)); }
@Test void buildOrderByWorks() { sortingJooqImpl1.getSortAliasMapping().put("id", SortValue.of(DSL.field("col_id", Integer.class))); sortingJooqImpl1.getSortAliasMapping().put("name", SortValue.of(DSL.field("any", String.class))); sortingJooqImpl1.getSortAliasMapping().put("listBla", SortValue.of(DSL.field("a_column", String.class))); sortingJooqImpl1.getSortAliasMapping().put("other", SortValue.of(DSL.field("another", Timestamp.class))); final Collection<SortField<?>> sortFields = sortingJooqImpl1.buildOrderBy(Sort.by(Sort.Order.asc("id"), Sort.Order.desc("listBla"), Sort.Order.asc("other"))); Assertions.assertNotNull(sortFields); Assertions.assertFalse(sortFields.isEmpty()); Assertions.assertEquals(3, sortFields.size()); }
@Test void buildOrderBySortBySortOrderGiven() { sortingJooqImpl1.getSortAliasMapping().put("name", SortValue.of(DSL.field("any", String.class))); sortingJooqImpl1.getSortAliasMapping().put("id", SortValue.of(DSL.field("col_id", Integer.class))); sortingJooqImpl1.getSortAliasMapping().put("listBla", SortValue.of(DSL.field("a_column", String.class))); sortingJooqImpl1.getSortAliasMapping().put("other", SortValue.of(DSL.field("another", Timestamp.class))); final Collection<SortField<?>> sortFields1 = sortingJooqImpl1.buildOrderBy(Sort.by(Sort.Order.asc("id"), Sort.Order.desc("listBla"), Sort.Order.asc("other"))); final Collection<SortField<?>> sortFields2 = sortingJooqImpl1.buildOrderBy(Sort.by(Sort.Order.desc("listBla"), Sort.Order.asc("other"), Sort.Order.asc("id"))); Assertions.assertEquals(3, sortFields1.size()); Assertions.assertEquals(3, sortFields2.size()); final SortField[] sortField1 = sortFields1.toArray(new SortField[3]); final SortField[] sortField2 = sortFields2.toArray(new SortField[3]); Assertions.assertEquals("col_id", sortField1[0].getName()); Assertions.assertEquals("a_column", sortField1[1].getName()); Assertions.assertEquals("another", sortField1[2].getName()); Assertions.assertEquals(SortOrder.ASC, sortField1[0].getOrder()); Assertions.assertEquals(SortOrder.DESC, sortField1[1].getOrder()); Assertions.assertEquals(SortOrder.ASC, sortField1[2].getOrder()); Assertions.assertEquals("a_column", sortField2[0].getName()); Assertions.assertEquals("another", sortField2[1].getName()); Assertions.assertEquals("col_id", sortField2[2].getName()); Assertions.assertEquals(SortOrder.DESC, sortField2[0].getOrder()); Assertions.assertEquals(SortOrder.ASC, sortField2[1].getOrder()); Assertions.assertEquals(SortOrder.ASC, sortField2[2].getOrder()); }
@Test void getSortNullFirst() { final SortField sortFieldNullFirstAscInt = intSortValue.getSortFieldNullFirst(SortOrder.ASC); final SortField sortFieldNullFirstDescInt = intSortValue.getSortFieldNullFirst(SortOrder.DESC); final SortField sortFieldNullFirstAscString = stringSortValue.getSortFieldNullFirst(SortOrder.ASC); final SortField sortFieldNullFirstDescString = stringSortValue.getSortFieldNullFirst(SortOrder.DESC); Assertions.assertNotNull(sortFieldNullFirstAscInt); Assertions.assertNotNull(sortFieldNullFirstDescInt); Assertions.assertNotNull(sortFieldNullFirstAscString); Assertions.assertNotNull(sortFieldNullFirstDescString); }
@Test void getSortNullLast() { final SortField sortFieldNullLastAscInt = intSortValue.getSortFieldNullLast(SortOrder.ASC); final SortField sortFieldNullLastDescInt = intSortValue.getSortFieldNullLast(SortOrder.DESC); final SortField sortFieldNullLastAscString = stringSortValue.getSortFieldNullLast(SortOrder.ASC); final SortField sortFieldNullLastDescString = stringSortValue.getSortFieldNullLast(SortOrder.DESC); Assertions.assertNotNull(sortFieldNullLastAscInt); Assertions.assertNotNull(sortFieldNullLastDescInt); Assertions.assertNotNull(sortFieldNullLastAscString); Assertions.assertNotNull(sortFieldNullLastDescString); }
protected Result<Record> getRecordsByPage(List<Field<Object>> viewer, Condition c, int form, int to, SortField<?>... sorts) { if (sorts != null && sorts.length != 0) { return _r(getProviderTable(), viewer).where(c).orderBy(sorts).limit(form, to).fetch(); } else { return _r(getProviderTable(), viewer).where(c).limit(form, to).fetch(); } }
protected Result<Record> getRecords(List<Field<Object>> viewer, Condition c, SortField<?>... sorts) { if (sorts != null && sorts.length != 0) { return _r(getProviderTable(), viewer).where(c).orderBy(sorts).fetch(); } else { return _r(getProviderTable(), viewer).where(c).fetch(); } }
public Collection<SortField<?>> orderingsToSortFields() { ArrayList<SortField<?>> sorts = new ArrayList<>(sourceQuery.getOrderings().size()); for (Ordering order : sourceQuery.getOrderings()) { sorts.add(getSortField(order).sort(order.getOrder())); } return sorts; }
public boolean containsSortField(String fieldName) { Collection<SortField<?>> sortFields = orderingsToSortFields(); for (SortField<?> field : sortFields) { if (field.getName().equals(fieldName)) { return true; } } return false; }
@Override public SortField getSortField(final SortOrder sortOrder) { return tableField == null ? field.sort(sortOrder) : tableField.sort(sortOrder); }
@Test void buildOrderByWithEmptySort() { final Collection<SortField<?>> sortFields = sortingJooqImpl1.buildOrderBy(Sort.unsorted()); Assertions.assertNotNull(sortFields); Assertions.assertTrue(sortFields.isEmpty()); }
/** * Empty immutable list for sorting * * @return an empty immutable sort list */ @NotNull default Collection<SortField<?>> emptySorting() { return Collections.emptyList(); }
/** * @param sortOrder Sort order to apply to field * @return Field already sorted by given sort order */ SortField getSortField(final SortOrder sortOrder);
/** * @param sortOrder Sort order to apply to field * @return Field sorted and null first */ default SortField getSortFieldNullFirst(final SortOrder sortOrder) { return getSortField(sortOrder).nullsFirst(); }
/** * @param sortOrder Sort order to apply to field * @return Field sorted and null last */ default SortField getSortFieldNullLast(final SortOrder sortOrder) { return getSortField(sortOrder).nullsLast(); }