release-0.2.0
This commit is contained in:
parent
d221d2b25b
commit
5b622514ad
10
pom.xml
10
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>dev.struchkov.haiti.filter</groupId>
|
||||
<artifactId>haiti-filter-jooq</artifactId>
|
||||
<version>0.1.0</version>
|
||||
<version>0.2.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Haiti Filter JOOQ</name>
|
||||
@ -23,7 +23,7 @@
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
|
||||
<jooq.version>3.15.5</jooq.version>
|
||||
<jooq.version>3.16.1</jooq.version>
|
||||
|
||||
<plugin.nexus.staging.ver>1.6.8</plugin.nexus.staging.ver>
|
||||
<plugin.maven.source.ver>3.2.1</plugin.maven.source.ver>
|
||||
@ -36,7 +36,7 @@
|
||||
<dependency>
|
||||
<groupId>dev.struchkov.haiti</groupId>
|
||||
<artifactId>haiti-bom</artifactId>
|
||||
<version>0.0.1-RELEASE</version>
|
||||
<version>0.0.2</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
@ -48,6 +48,10 @@
|
||||
<groupId>dev.struchkov.haiti</groupId>
|
||||
<artifactId>haiti-filter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>dev.struchkov.haiti</groupId>
|
||||
<artifactId>haiti-utils</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq</artifactId>
|
||||
|
@ -2,6 +2,10 @@ package dev.struchkov.haiti.filter.jooq;
|
||||
|
||||
import dev.struchkov.haiti.filter.Filter;
|
||||
import dev.struchkov.haiti.filter.FilterQuery;
|
||||
import dev.struchkov.haiti.filter.jooq.exception.FilterJooqHaitiException;
|
||||
import dev.struchkov.haiti.filter.jooq.page.PageableOffset;
|
||||
import dev.struchkov.haiti.filter.jooq.page.PageableSeek;
|
||||
import dev.struchkov.haiti.utils.Assert;
|
||||
import lombok.NonNull;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.DSLContext;
|
||||
@ -10,12 +14,12 @@ import org.jooq.Query;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.SelectConditionStep;
|
||||
import org.jooq.SelectJoinStep;
|
||||
import org.jooq.SelectSeekStepN;
|
||||
import org.jooq.SortField;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.jooq.impl.DSL.condition;
|
||||
import static org.jooq.impl.DSL.field;
|
||||
@ -27,13 +31,13 @@ public class CriteriaJooqFilter implements Filter {
|
||||
private final List<Condition> notConditions = new ArrayList<>();
|
||||
private final List<JoinTable> joinTables = new ArrayList<>();
|
||||
|
||||
private String fieldOrder;
|
||||
private Object lastId;
|
||||
private Integer pageSize;
|
||||
|
||||
private final String table;
|
||||
private final DSLContext dsl;
|
||||
|
||||
private PageableOffset offset;
|
||||
private PageableSeek seek;
|
||||
private List<SortContainer> sorts = new ArrayList<>();
|
||||
|
||||
protected CriteriaJooqFilter(String table, DSLContext dsl) {
|
||||
this.table = table;
|
||||
this.dsl = dsl;
|
||||
@ -82,10 +86,36 @@ public class CriteriaJooqFilter implements Filter {
|
||||
throw new IllegalStateException("Операция отрицания пока не поддерживается");
|
||||
}
|
||||
|
||||
public Filter page(@NonNull String fieldOrder, Object id, int pageSize) {
|
||||
this.fieldOrder = fieldOrder;
|
||||
this.lastId = id;
|
||||
this.pageSize = pageSize;
|
||||
public Filter page(@NonNull PageableOffset offset) {
|
||||
Assert.isNull(seek, () -> new FilterJooqHaitiException("Нельзя установить два типа пагинации одновременно"));
|
||||
this.offset = offset;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Filter page(@NonNull PageableSeek seek) {
|
||||
Assert.isNull(offset, () -> new FilterJooqHaitiException("Нельзя установить два типа пагинации одновременно"));
|
||||
this.seek = seek;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Filter sort(@NonNull SortContainer container) {
|
||||
if (container.getFieldName() != null) {
|
||||
this.sorts.add(container);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Filter sort(String field, SortType sortType) {
|
||||
if (field != null) {
|
||||
this.sorts.add(SortContainer.of(field, sortType));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Filter sort(String field) {
|
||||
if (field != null) {
|
||||
this.sorts.add(SortContainer.of(field));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -135,17 +165,53 @@ public class CriteriaJooqFilter implements Filter {
|
||||
}
|
||||
}
|
||||
final SelectConditionStep<Record> where = from.where(conditions);
|
||||
if (pageSize != null && fieldOrder != null) {
|
||||
if (lastId != null) {
|
||||
where.orderBy(field(fieldOrder))
|
||||
.seek(lastId)
|
||||
.limit(pageSize);
|
||||
} else {
|
||||
where.orderBy(field(fieldOrder))
|
||||
.limit(pageSize);
|
||||
}
|
||||
}
|
||||
final SelectSeekStepN<Record> sort = setSort(where);
|
||||
setPaginationOffset(where);
|
||||
setPaginationSeek(sort);
|
||||
return where;
|
||||
}
|
||||
|
||||
private SelectSeekStepN<Record> setSort(SelectConditionStep<Record> where) {
|
||||
if (!sorts.isEmpty()) {
|
||||
final List<SortField<Object>> newSorts = new ArrayList<>();
|
||||
for (SortContainer sort : sorts) {
|
||||
final SortType sortType = sort.getType();
|
||||
final String fieldName = sort.getFieldName();
|
||||
if (SortType.ASC.equals(sortType)) {
|
||||
newSorts.add(field(fieldName).asc());
|
||||
} else {
|
||||
newSorts.add(field(fieldName).desc());
|
||||
}
|
||||
}
|
||||
return where.orderBy(newSorts);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void setPaginationSeek(SelectSeekStepN<Record> sort) {
|
||||
if (seek != null) {
|
||||
Assert.isNotNull(sort, () -> new FilterJooqHaitiException("При использовании пагинации типа seek необходимо указать сортировку"));
|
||||
final Integer pageSize = seek.getPageSize();
|
||||
final Object lastId = seek.getLastId();
|
||||
if (pageSize != null) {
|
||||
if (lastId != null) {
|
||||
sort
|
||||
.seek(lastId)
|
||||
.limit(pageSize);
|
||||
} else {
|
||||
sort
|
||||
.limit(pageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setPaginationOffset(SelectConditionStep<Record> where) {
|
||||
if (offset != null) {
|
||||
final int pageNumber = offset.getPageNumber();
|
||||
final int pageSize = offset.getPageSize();
|
||||
final int offsetNumber = (pageNumber + 1) * pageSize - pageSize;
|
||||
where.limit(pageSize).offset(offsetNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
package dev.struchkov.haiti.filter.jooq;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class SortContainer {
|
||||
|
||||
private final String fieldName;
|
||||
private SortType type = SortType.ASC;
|
||||
|
||||
public static SortContainer empty() {
|
||||
return new SortContainer(null);
|
||||
}
|
||||
|
||||
public static SortContainer of(@NonNull String fieldName) {
|
||||
return new SortContainer(fieldName);
|
||||
}
|
||||
|
||||
public static SortContainer of(@NonNull String fieldName, SortType sortType) {
|
||||
return new SortContainer(fieldName, sortType == null ? SortType.ASC : sortType);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package dev.struchkov.haiti.filter.jooq;
|
||||
|
||||
public enum SortType {
|
||||
|
||||
ASC, DESC
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package dev.struchkov.haiti.filter.jooq.exception;
|
||||
|
||||
import dev.struchkov.haiti.filter.exception.FilterException;
|
||||
|
||||
public class FilterJooqHaitiException extends FilterException {
|
||||
|
||||
public FilterJooqHaitiException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package dev.struchkov.haiti.filter.jooq.page;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class PageableOffset {
|
||||
|
||||
private final int pageNumber;
|
||||
private int pageSize = 30;
|
||||
|
||||
public static PageableOffset of(int pageNumber) {
|
||||
return new PageableOffset(pageNumber);
|
||||
}
|
||||
|
||||
public static PageableOffset of(int pageNumber, int pageSize) {
|
||||
return new PageableOffset(pageNumber, pageSize);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package dev.struchkov.haiti.filter.jooq.page;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class PageableSeek {
|
||||
|
||||
private final Object lastId;
|
||||
private int pageSize = 30;
|
||||
|
||||
public static PageableSeek empty() {
|
||||
return new PageableSeek(null);
|
||||
}
|
||||
|
||||
public static PageableSeek of(int pageSize) {
|
||||
return new PageableSeek(null, pageSize);
|
||||
}
|
||||
|
||||
public static PageableSeek of(@NonNull Object lastId) {
|
||||
return new PageableSeek(lastId);
|
||||
}
|
||||
|
||||
public static PageableSeek of(@NonNull Object lastId, int pageSize) {
|
||||
return new PageableSeek(lastId, pageSize);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user