From c25fbdedc96a8ab96ae9b5ea3fcd2d9cfda246d8 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Thu, 17 Feb 2022 16:29:25 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D1=83=20join?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- .../haiti/filter/quarkus/CriteriaFilter.java | 125 --------------- .../haiti/filter/quarkus/QuarkusFilter.java | 148 ++++++++++++++++++ 3 files changed, 150 insertions(+), 127 deletions(-) delete mode 100644 src/main/java/dev/struchkov/haiti/filter/quarkus/CriteriaFilter.java create mode 100644 src/main/java/dev/struchkov/haiti/filter/quarkus/QuarkusFilter.java diff --git a/pom.xml b/pom.xml index 9a6eb26..72e1c39 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.haiti.filter haiti-filter-quarkus-reactive - 0.4.0-SNAPSHOT + 0.6.0-SNAPSHOT jar Haiti Filter Reactive Quarkus @@ -31,7 +31,7 @@ UTF-8 2.5.1.Final - 0.4.0-SNAPSHOT + 0.5.0-SNAPSHOT 3.9.0 1.6.8 diff --git a/src/main/java/dev/struchkov/haiti/filter/quarkus/CriteriaFilter.java b/src/main/java/dev/struchkov/haiti/filter/quarkus/CriteriaFilter.java deleted file mode 100644 index 83eb310..0000000 --- a/src/main/java/dev/struchkov/haiti/filter/quarkus/CriteriaFilter.java +++ /dev/null @@ -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 { - - private final PgPool pgPool; - private final CriteriaJooqFilter jooqFilter; - private final Function mapper; - private PageableOffset offset; - - private CriteriaFilter(PgPool pgPool, String table, DSLContext dslContext, Function mapper) { - dslContext.settings().withParamType(ParamType.NAMED_OR_INLINED); - this.pgPool = pgPool; - this.mapper = mapper; - this.jooqFilter = CriteriaJooqFilter.create(table, dslContext); - } - - public static CriteriaFilter create(PgPool pgPool, String table, DSLContext dslContext, Function mapper) { - return new CriteriaFilter<>(pgPool, table, dslContext, mapper); - } - - public CriteriaFilter and(CriteriaJooqQuery filterQuery) { - jooqFilter.and(filterQuery); - return this; - } - - public CriteriaFilter and(Consumer query) { - jooqFilter.and(query); - return this; - } - - - public CriteriaFilter or(CriteriaJooqQuery filterQuery) { - jooqFilter.or(filterQuery); - return this; - } - - public CriteriaFilter or(Consumer query) { - jooqFilter.or(query); - return this; - } - - public CriteriaFilter page(PageableOffset offset) { - jooqFilter.page(offset); - this.offset = offset; - return this; - } - - public CriteriaFilter page(PageableSeek seek) { - jooqFilter.page(seek); - return this; - } - - public CriteriaFilter sort(SortContainer container) { - jooqFilter.sort(container); - return this; - } - - public CriteriaFilter sort(String field, SortType sortType) { - jooqFilter.sort(field, sortType); - return this; - } - - public CriteriaFilter sort(String field) { - jooqFilter.sort(field); - return this; - } - - public Uni> 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 count() { - final Query query = jooqFilter.count(); - return pgPool.preparedQuery(query.getSQL()) - .execute() - .map(t -> t.iterator().next().getLong("count")); - } - - public Uni> filterResult() { - final Uni count = count(); - final Uni> content = build(); - return Uni.combine().all() - .unis(count, content) - .asTuple() - .map( - t -> { - final Long totalElements = t.getItem1(); - final List results = t.getItem2(); - return FilterResult.builder( - totalElements, results.size(), results - ) - .page(offset.getPageNumber()) - .totalPages(results.isEmpty() ? offset.getPageNumber() : totalElements / results.size()) - .build(); - } - ); - } - -} diff --git a/src/main/java/dev/struchkov/haiti/filter/quarkus/QuarkusFilter.java b/src/main/java/dev/struchkov/haiti/filter/quarkus/QuarkusFilter.java new file mode 100644 index 0000000..aa85aac --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/filter/quarkus/QuarkusFilter.java @@ -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 { + + 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 QuarkusFilter create(PgPool pgPool, String table, DSLContext dslContext) { + return new QuarkusFilter<>(pgPool, table, dslContext); + } + + public QuarkusFilter and(CriteriaJooqQuery filterQuery) { + jooqFilter.and(filterQuery); + return this; + } + + public QuarkusFilter and(Consumer query) { + jooqFilter.and(query); + return this; + } + + public QuarkusFilter or(CriteriaJooqQuery filterQuery) { + jooqFilter.or(filterQuery); + return this; + } + + public QuarkusFilter or(Consumer query) { + jooqFilter.or(query); + return this; + } + + public QuarkusFilter join(JoinTable... joinTables) { + Assert.isNotNull(joinTables); + jooqFilter.join(joinTables); + return this; + } + + public QuarkusFilter page(PageableOffset offset) { + jooqFilter.page(offset); + this.offset = offset; + return this; + } + + public QuarkusFilter page(PageableSeek seek) { + jooqFilter.page(seek); + return this; + } + + public QuarkusFilter sort(SortContainer container) { + jooqFilter.sort(container); + return this; + } + + public QuarkusFilter sort(String field, SortType sortType) { + jooqFilter.sort(field, sortType); + return this; + } + + public QuarkusFilter sort(String field) { + jooqFilter.sort(field); + return this; + } + + public Uni> build(Class 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 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 optCount = StreamSupport.stream(t.spliterator(), false) + .map(row -> row.getLong(COLUMN_COUNT)) + .reduce(((a, b) -> a * b)); + return optCount.orElse(0L); + } + }); + } + + public Uni> filterResult(Class idType, String idField) { + jooqFilter.groupBy(idField); + distinctCount = true; + final Uni count = count(); + final Uni> content = build(idType, idField); + return Uni.combine().all() + .unis(count, content) + .asTuple() + .map( + t -> { + final Long totalElements = t.getItem1(); + final List results = t.getItem2(); + return FilterResult.builder( + totalElements, results.size(), results + ) + .page(offset.getPageNumber()) + .totalPages(results.isEmpty() ? offset.getPageNumber() : totalElements / results.size()) + .build(); + } + ); + } + +}