Add null order handling to SortContainer

A new enum NullOrderType was added and integrated into the SortContainer class to handle sorting nulls. The CriteriaJooqFilter's sorting methods were updated to use this new functionality as well, which enables the user to specify the nulls order (first or last) when sorting fields.
This commit is contained in:
Struchkov Mark 2024-03-13 13:10:24 +03:00
parent 40302fb406
commit 2e0039adcb
No known key found for this signature in database
GPG Key ID: A3F0AC3F0FA52F3C
3 changed files with 41 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import dev.struchkov.haiti.filter.jooq.join.JoinTable;
import dev.struchkov.haiti.filter.jooq.join.JoinTypeOperation;
import dev.struchkov.haiti.filter.jooq.page.PageableOffset;
import dev.struchkov.haiti.filter.jooq.page.PageableSeek;
import dev.struchkov.haiti.filter.jooq.sort.NullOrderType;
import dev.struchkov.haiti.filter.jooq.sort.SortContainer;
import dev.struchkov.haiti.filter.jooq.sort.SortType;
import dev.struchkov.haiti.utils.Inspector;
@ -112,6 +113,13 @@ public class CriteriaJooqFilter {
return this;
}
public CriteriaJooqFilter sort(String field, SortType sortType, NullOrderType nullsOrder) {
if (field != null) {
this.sorts.add(SortContainer.of(field, sortType, nullsOrder));
}
return this;
}
public CriteriaJooqFilter sort(String field) {
if (field != null) {
this.sorts.add(SortContainer.of(field));
@ -248,12 +256,14 @@ public class CriteriaJooqFilter {
if (!sorts.isEmpty()) {
final List<SortField<Object>> newSorts = new ArrayList<>();
for (SortContainer sort : sorts) {
final NullOrderType nullOrderType = sort.getNullOrderType();
final SortType sortType = sort.getType();
final String fieldName = sort.getFieldName();
if (SortType.ASC.equals(sortType)) {
newSorts.add(field(fieldName).asc());
final SortField<Object> sortField = SortType.ASC.equals(sortType) ? field(fieldName).asc() : field(fieldName).desc();
if (NullOrderType.LAST.equals(nullOrderType)) {
newSorts.add(sortField.nullsLast());
} else {
newSorts.add(field(fieldName).desc());
newSorts.add(sortField.nullsFirst());
}
}
return newSorts;

View File

@ -0,0 +1,8 @@
package dev.struchkov.haiti.filter.jooq.sort;
public enum NullOrderType {
FIRST,
LAST
}

View File

@ -7,6 +7,7 @@ public class SortContainer {
private final String fieldName;
private SortType type = SortType.ASC;
private NullOrderType nullOrderType = NullOrderType.LAST;
private SortContainer(String fieldName) {
this.fieldName = fieldName;
@ -17,6 +18,12 @@ public class SortContainer {
this.type = type;
}
private SortContainer(String fieldName, SortType type, NullOrderType nullsOrder) {
this.fieldName = fieldName;
this.type = type;
this.nullOrderType = nullsOrder;
}
public static SortContainer of(String fieldName) {
Inspector.isNotNull(fieldName);
return new SortContainer(fieldName);
@ -27,6 +34,15 @@ public class SortContainer {
return new SortContainer(fieldName, sortType == null ? SortType.ASC : sortType);
}
public static SortContainer of(String fieldName, SortType sortType, NullOrderType nullOrderType) {
Inspector.isNotNull(fieldName);
return new SortContainer(
fieldName,
sortType == null ? SortType.ASC : sortType,
nullOrderType == null ? NullOrderType.LAST : nullOrderType
);
}
public String getFieldName() {
return fieldName;
}
@ -35,4 +51,8 @@ public class SortContainer {
return type;
}
public NullOrderType getNullOrderType() {
return nullOrderType;
}
}