Добавил поддержку join

This commit is contained in:
Struchkov Mark 2022-02-17 16:29:25 +03:00
parent cdd2026b12
commit c25fbdedc9
3 changed files with 150 additions and 127 deletions

View File

@ -6,7 +6,7 @@
<groupId>dev.struchkov.haiti.filter</groupId>
<artifactId>haiti-filter-quarkus-reactive</artifactId>
<version>0.4.0-SNAPSHOT</version>
<version>0.6.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Haiti Filter Reactive Quarkus</name>
@ -31,7 +31,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.version>2.5.1.Final</quarkus.platform.version>
<haiti.filter.jooq.version>0.4.0-SNAPSHOT</haiti.filter.jooq.version>
<haiti.filter.jooq.version>0.5.0-SNAPSHOT</haiti.filter.jooq.version>
<plugin.maven.compiler.ver>3.9.0</plugin.maven.compiler.ver>
<plugin.nexus.staging.ver>1.6.8</plugin.nexus.staging.ver>

View File

@ -1,125 +0,0 @@
package dev.struchkov.haiti.filter.quarkus;
import dev.struchkov.haiti.filter.jooq.CriteriaJooqFilter;
import dev.struchkov.haiti.filter.jooq.CriteriaJooqQuery;
import dev.struchkov.haiti.filter.jooq.SortContainer;
import dev.struchkov.haiti.filter.jooq.SortType;
import dev.struchkov.haiti.filter.jooq.page.PageableOffset;
import dev.struchkov.haiti.filter.jooq.page.PageableSeek;
import io.smallrye.mutiny.Uni;
import io.vertx.mutiny.pgclient.PgPool;
import io.vertx.mutiny.sqlclient.Row;
import org.jooq.DSLContext;
import org.jooq.Query;
import org.jooq.conf.ParamType;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class CriteriaFilter<T> {
private final PgPool pgPool;
private final CriteriaJooqFilter jooqFilter;
private final Function<Row, T> mapper;
private PageableOffset offset;
private CriteriaFilter(PgPool pgPool, String table, DSLContext dslContext, Function<Row, T> mapper) {
dslContext.settings().withParamType(ParamType.NAMED_OR_INLINED);
this.pgPool = pgPool;
this.mapper = mapper;
this.jooqFilter = CriteriaJooqFilter.create(table, dslContext);
}
public static <T> CriteriaFilter<T> create(PgPool pgPool, String table, DSLContext dslContext, Function<Row, T> mapper) {
return new CriteriaFilter<>(pgPool, table, dslContext, mapper);
}
public CriteriaFilter<T> and(CriteriaJooqQuery filterQuery) {
jooqFilter.and(filterQuery);
return this;
}
public CriteriaFilter<T> and(Consumer<CriteriaJooqQuery> query) {
jooqFilter.and(query);
return this;
}
public CriteriaFilter<T> or(CriteriaJooqQuery filterQuery) {
jooqFilter.or(filterQuery);
return this;
}
public CriteriaFilter<T> or(Consumer<CriteriaJooqQuery> query) {
jooqFilter.or(query);
return this;
}
public CriteriaFilter<T> page(PageableOffset offset) {
jooqFilter.page(offset);
this.offset = offset;
return this;
}
public CriteriaFilter<T> page(PageableSeek seek) {
jooqFilter.page(seek);
return this;
}
public CriteriaFilter<T> sort(SortContainer container) {
jooqFilter.sort(container);
return this;
}
public CriteriaFilter<T> sort(String field, SortType sortType) {
jooqFilter.sort(field, sortType);
return this;
}
public CriteriaFilter<T> sort(String field) {
jooqFilter.sort(field);
return this;
}
public Uni<List<T>> build() {
final Query query = jooqFilter.build();
return pgPool.preparedQuery(query.getSQL())
.execute()
.map(rows ->
StreamSupport.stream(rows.spliterator(), false)
.map(mapper)
.collect(Collectors.toList())
);
}
public Uni<Long> count() {
final Query query = jooqFilter.count();
return pgPool.preparedQuery(query.getSQL())
.execute()
.map(t -> t.iterator().next().getLong("count"));
}
public Uni<FilterResult<T>> filterResult() {
final Uni<Long> count = count();
final Uni<List<T>> content = build();
return Uni.combine().all()
.unis(count, content)
.asTuple()
.map(
t -> {
final Long totalElements = t.getItem1();
final List<T> results = t.getItem2();
return FilterResult.builder(
totalElements, results.size(), results
)
.page(offset.getPageNumber())
.totalPages(results.isEmpty() ? offset.getPageNumber() : totalElements / results.size())
.build();
}
);
}
}

View File

@ -0,0 +1,148 @@
package dev.struchkov.haiti.filter.quarkus;
import dev.struchkov.haiti.filter.jooq.CriteriaJooqFilter;
import dev.struchkov.haiti.filter.jooq.CriteriaJooqQuery;
import dev.struchkov.haiti.filter.jooq.join.JoinTable;
import dev.struchkov.haiti.filter.jooq.page.PageableOffset;
import dev.struchkov.haiti.filter.jooq.page.PageableSeek;
import dev.struchkov.haiti.filter.jooq.sort.SortContainer;
import dev.struchkov.haiti.filter.jooq.sort.SortType;
import dev.struchkov.haiti.utils.Assert;
import io.smallrye.mutiny.Uni;
import io.vertx.mutiny.pgclient.PgPool;
import org.jooq.DSLContext;
import org.jooq.Query;
import org.jooq.conf.ParamType;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class QuarkusFilter<ID> {
public static final String COLUMN_COUNT = "count";
private final String tableName;
private boolean distinctCount;
private final PgPool pgPool;
private final CriteriaJooqFilter jooqFilter;
private PageableOffset offset;
private QuarkusFilter(PgPool pgPool, String table, DSLContext dslContext) {
dslContext.settings().withParamType(ParamType.NAMED_OR_INLINED);
this.tableName = table;
this.pgPool = pgPool;
this.jooqFilter = CriteriaJooqFilter.create(table, dslContext);
}
public static <ID> QuarkusFilter<ID> create(PgPool pgPool, String table, DSLContext dslContext) {
return new QuarkusFilter<>(pgPool, table, dslContext);
}
public QuarkusFilter<ID> and(CriteriaJooqQuery filterQuery) {
jooqFilter.and(filterQuery);
return this;
}
public QuarkusFilter<ID> and(Consumer<CriteriaJooqQuery> query) {
jooqFilter.and(query);
return this;
}
public QuarkusFilter<ID> or(CriteriaJooqQuery filterQuery) {
jooqFilter.or(filterQuery);
return this;
}
public QuarkusFilter<ID> or(Consumer<CriteriaJooqQuery> query) {
jooqFilter.or(query);
return this;
}
public QuarkusFilter<ID> join(JoinTable... joinTables) {
Assert.isNotNull(joinTables);
jooqFilter.join(joinTables);
return this;
}
public QuarkusFilter<ID> page(PageableOffset offset) {
jooqFilter.page(offset);
this.offset = offset;
return this;
}
public QuarkusFilter<ID> page(PageableSeek seek) {
jooqFilter.page(seek);
return this;
}
public QuarkusFilter<ID> sort(SortContainer container) {
jooqFilter.sort(container);
return this;
}
public QuarkusFilter<ID> sort(String field, SortType sortType) {
jooqFilter.sort(field, sortType);
return this;
}
public QuarkusFilter<ID> sort(String field) {
jooqFilter.sort(field);
return this;
}
public Uni<List<ID>> build(Class<ID> idType, String idField) {
final Query query = jooqFilter.generateQuery(idField);
final String sql = query.getSQL();
return pgPool.preparedQuery(sql)
.execute()
.map(rows ->
StreamSupport.stream(rows.spliterator(), false)
.map(row -> row.get(idType, idField))
.collect(Collectors.toList())
);
}
public Uni<Long> count() {
final Query query = jooqFilter.generateCount();
final String sql = query.getSQL();
return pgPool.preparedQuery(sql)
.execute()
.map(t -> {
final long count = StreamSupport.stream(t.spliterator(), false).count();
if (distinctCount) {
return count;
} else {
final Optional<Long> optCount = StreamSupport.stream(t.spliterator(), false)
.map(row -> row.getLong(COLUMN_COUNT))
.reduce(((a, b) -> a * b));
return optCount.orElse(0L);
}
});
}
public Uni<FilterResult<ID>> filterResult(Class<ID> idType, String idField) {
jooqFilter.groupBy(idField);
distinctCount = true;
final Uni<Long> count = count();
final Uni<List<ID>> content = build(idType, idField);
return Uni.combine().all()
.unis(count, content)
.asTuple()
.map(
t -> {
final Long totalElements = t.getItem1();
final List<ID> results = t.getItem2();
return FilterResult.builder(
totalElements, results.size(), results
)
.page(offset.getPageNumber())
.totalPages(results.isEmpty() ? offset.getPageNumber() : totalElements / results.size())
.build();
}
);
}
}