From 7279aa6fd8b5859082ff169e528209c8bf447b05 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Thu, 10 Feb 2022 22:29:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BB=D0=BE=D0=BC=D0=B1=D0=BE=D0=BA=D0=B0=20=D0=B8=20?= =?UTF-8?q?=D1=81=D0=B2=D1=8F=D0=B7=D0=B8=20=D1=81=20haiti-filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 68 +++++--- .../haiti/filter/jooq/CriteriaJooqFilter.java | 159 +++++++++++------- .../haiti/filter/jooq/CriteriaJooqQuery.java | 49 ++---- .../haiti/filter/jooq/JoinTable.java | 36 +++- .../haiti/filter/jooq/SortContainer.java | 33 ++-- .../filter/jooq/page/PageableOffset.java | 29 +++- .../haiti/filter/jooq/page/PageableSeek.java | 32 ++-- 7 files changed, 241 insertions(+), 165 deletions(-) diff --git a/pom.xml b/pom.xml index e64fa1e..896d9c3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.haiti.filter haiti-filter-jooq - 0.2.0 + 0.3.0 jar Haiti Filter JOOQ @@ -18,6 +18,10 @@ https://raw.githubusercontent.com/haiti-projects/haiti-filter-jooq/master/LICENSE + + GitHub + https://github.com/haiti-projects/haiti-filter-jooq/issues + 11 @@ -36,7 +40,7 @@ dev.struchkov.haiti haiti-bom - 0.0.2 + 1.0.0 pom import @@ -46,11 +50,11 @@ dev.struchkov.haiti - haiti-filter + haiti-utils dev.struchkov.haiti - haiti-utils + haiti-exception org.jooq @@ -59,28 +63,6 @@ - - scm:git:https://github.com/haiti-projects/haiti-filter-jooq.git - https://github.com/haiti-projects/haiti-filter-jooq - scm:git:https://github.com/haiti-projects/haiti-filter-jooq.git - - - - - ossrh - https://s01.oss.sonatype.org/content/repositories/snapshots - - - - - - uPagge - Struchkov Mark - https://mark.struchkov.dev - mark@struchkov.dev - - - @@ -153,6 +135,14 @@ 11 + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + @@ -171,15 +161,37 @@ org.apache.maven.plugins - maven-gpg-plugin + maven-javadoc-plugin org.apache.maven.plugins - maven-javadoc-plugin + maven-gpg-plugin + + scm:git:https://github.com/haiti-projects/haiti-filter-jooq.git + https://github.com/haiti-projects/haiti-filter-jooq + scm:git:https://github.com/haiti-projects/haiti-filter-jooq.git + + + + + ossrh + https://s01.oss.sonatype.org/content/repositories/snapshots + + + + + + uPagge + Struchkov Mark + https://mark.struchkov.dev + mark@struchkov.dev + + + \ No newline at end of file diff --git a/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqFilter.java b/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqFilter.java index 9e20e1c..5caec92 100644 --- a/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqFilter.java +++ b/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqFilter.java @@ -1,12 +1,9 @@ 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; import org.jooq.Operator; @@ -21,15 +18,16 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; +import static dev.struchkov.haiti.filter.jooq.exception.FilterJooqHaitiException.filterJooqException; import static org.jooq.impl.DSL.condition; import static org.jooq.impl.DSL.field; -public class CriteriaJooqFilter implements Filter { +public class CriteriaJooqFilter { private final List andConditions = new ArrayList<>(); private final List orConditions = new ArrayList<>(); private final List notConditions = new ArrayList<>(); - private final List joinTables = new ArrayList<>(); +// private final List joinTables = new ArrayList<>(); private final String table; private final DSLContext dsl; @@ -47,84 +45,83 @@ public class CriteriaJooqFilter implements Filter { return new CriteriaJooqFilter(table, dsl); } - @Override - public Filter and(FilterQuery filterQuery) { - generateAnd((CriteriaJooqQuery) filterQuery); + public CriteriaJooqFilter and(CriteriaJooqQuery filterQuery) { + generateAnd(filterQuery); return this; } - @Override - public Filter and(Consumer query) { + public CriteriaJooqFilter and(Consumer query) { final CriteriaJooqQuery criteriaQuery = CriteriaJooqQuery.create(); query.accept(criteriaQuery); generateAnd(criteriaQuery); return this; } - @Override - public Filter or(FilterQuery filterQuery) { - generateOr((CriteriaJooqQuery) filterQuery); + public CriteriaJooqFilter or(CriteriaJooqQuery filterQuery) { + generateOr(filterQuery); return this; } - public Filter or(Consumer query) { + public CriteriaJooqFilter or(Consumer query) { final CriteriaJooqQuery criteriaQuery = CriteriaJooqQuery.create(); query.accept(criteriaQuery); generateOr(criteriaQuery); return this; } - @Override - public Filter not(FilterQuery filterQuery) { - // FIXME: Добавить поддержку - throw new IllegalStateException("Операция отрицания пока не поддерживается"); - } +// FIXME: Добавить поддержку +// public CriteriaJooqFilter not(FilterQuery filterQuery) { +// throw new IllegalStateException("Операция отрицания пока не поддерживается"); +// } - @Override - public Filter not(Consumer query) { - // FIXME: Добавить поддержку - throw new IllegalStateException("Операция отрицания пока не поддерживается"); - } - public Filter page(@NonNull PageableOffset offset) { - Assert.isNull(seek, () -> new FilterJooqHaitiException("Нельзя установить два типа пагинации одновременно")); +// FIXME: Добавить поддержку +// public CriteriaJooqFilter not(Consumer query) { +// throw new IllegalStateException("Операция отрицания пока не поддерживается"); +// } + + public CriteriaJooqFilter page(PageableOffset offset) { + Assert.isNotNull(offset); + Assert.isNull(seek, filterJooqException("Нельзя установить два типа пагинации одновременно")); this.offset = offset; return this; } - public Filter page(@NonNull PageableSeek seek) { - Assert.isNull(offset, () -> new FilterJooqHaitiException("Нельзя установить два типа пагинации одновременно")); + public CriteriaJooqFilter page(PageableSeek seek) { + Assert.isNotNull(seek); + Assert.isNull(offset, filterJooqException("Нельзя установить два типа пагинации одновременно")); this.seek = seek; return this; } - public Filter sort(@NonNull SortContainer container) { + public CriteriaJooqFilter sort(SortContainer container) { + Assert.isNotNull(container); if (container.getFieldName() != null) { this.sorts.add(container); } return this; } - public Filter sort(String field, SortType sortType) { + public CriteriaJooqFilter sort(String field, SortType sortType) { if (field != null) { this.sorts.add(SortContainer.of(field, sortType)); } return this; } - public Filter sort(String field) { + public CriteriaJooqFilter sort(String field) { if (field != null) { this.sorts.add(SortContainer.of(field)); } return this; } - public Filter join(@NonNull JoinTable... joinTables) { - // FIXME: Добавить поддержку - throw new IllegalStateException("Операция пока не поддерживается"); +// FIXME: Добавить поддержку +// public CriteriaJooqFilter join(@NonNull JoinTable... joinTables) { +// throw new IllegalStateException("Операция пока не поддерживается"); // this.joinTables.addAll(Arrays.stream(joinTables).collect(Collectors.toList())); // return this; - } +// } private void generateAnd(CriteriaJooqQuery criteriaQuery) { andConditions.addAll(criteriaQuery.getConditions()); @@ -134,8 +131,38 @@ public class CriteriaJooqFilter implements Filter { orConditions.addAll(criteriaQuery.getConditions()); } - @Override public Query build() { + final List conditions = getConditions(); + SelectJoinStep from = dsl.select().from(table); +// if (!joinTables.isEmpty()) { +// for (JoinTable joinTable : joinTables) { +// final String tableName = joinTable.getTableName(); +// final JoinTypeOperation joinType = joinTable.getJoinTypeOperation(); +// final String fieldReference = joinTable.getFieldReference(); +// final String fieldBase = joinTable.getFieldBase(); +// final Condition on = field(fieldReference).eq(field(fieldBase)); +// switch (joinType) { +// case LEFT: +// from = from.leftJoin(tableName).on(on); +// break; +// case INNER: +// from = from.innerJoin(tableName).on(on); +// break; +// case RIGHT: +// from = from.rightJoin(tableName).on(on); +// break; +// } +// } +// } + final SelectConditionStep where = from.where(conditions); + final SelectSeekStepN sort = setSort(where); + setPaginationOffset(where); + setPaginationSeek(sort); + + return where; + } + + private List getConditions() { final List conditions = new ArrayList<>(); if (!andConditions.isEmpty()) { conditions.add(condition(Operator.AND, andConditions)); @@ -143,35 +170,38 @@ public class CriteriaJooqFilter implements Filter { if (!orConditions.isEmpty()) { conditions.add(condition(Operator.OR, orConditions)); } - SelectJoinStep from = dsl.select().from(table); - if (!joinTables.isEmpty()) { - for (JoinTable joinTable : joinTables) { - final String tableName = joinTable.getTableName(); - final JoinTypeOperation joinType = joinTable.getJoinTypeOperation(); - final String fieldReference = joinTable.getFieldReference(); - final String fieldBase = joinTable.getFieldBase(); - final Condition on = field(fieldReference).eq(field(fieldBase)); - switch (joinType) { - case LEFT: - from = from.leftJoin(tableName).on(on); - break; - case INNER: - from = from.innerJoin(tableName).on(on); - break; - case RIGHT: - from = from.rightJoin(tableName).on(on); - break; - } - } - } - final SelectConditionStep where = from.where(conditions); - final SelectSeekStepN sort = setSort(where); - setPaginationOffset(where); - setPaginationSeek(sort); + return conditions; + } + + public Query count() { + final List conditions = getConditions(); + SelectJoinStep from = dsl.selectCount().from(table); +// if (!joinTables.isEmpty()) { +// for (JoinTable joinTable : joinTables) { +// final String tableName = joinTable.getTableName(); +// final JoinTypeOperation joinType = joinTable.getJoinTypeOperation(); +// final String fieldReference = joinTable.getFieldReference(); +// final String fieldBase = joinTable.getFieldBase(); +// final Condition on = field(fieldReference).eq(field(fieldBase)); +// switch (joinType) { +// case LEFT: +// from = from.leftJoin(tableName).on(on); +// break; +// case INNER: +// from = from.innerJoin(tableName).on(on); +// break; +// case RIGHT: +// from = from.rightJoin(tableName).on(on); +// break; +// } +// } +// } + final SelectConditionStep where = from.where(conditions); + setSort(where); return where; } - private SelectSeekStepN setSort(SelectConditionStep where) { + private SelectSeekStepN setSort(SelectConditionStep where) { if (!sorts.isEmpty()) { final List> newSorts = new ArrayList<>(); for (SortContainer sort : sorts) { @@ -188,7 +218,7 @@ public class CriteriaJooqFilter implements Filter { return null; } - private void setPaginationSeek(SelectSeekStepN sort) { + private void setPaginationSeek(SelectSeekStepN sort) { if (seek != null) { Assert.isNotNull(sort, () -> new FilterJooqHaitiException("При использовании пагинации типа seek необходимо указать сортировку")); final Integer pageSize = seek.getPageSize(); @@ -206,7 +236,7 @@ public class CriteriaJooqFilter implements Filter { } } - private void setPaginationOffset(SelectConditionStep where) { + private void setPaginationOffset(SelectConditionStep where) { if (offset != null) { final int pageNumber = offset.getPageNumber(); final int pageSize = offset.getPageSize(); @@ -214,4 +244,5 @@ public class CriteriaJooqFilter implements Filter { where.limit(pageSize).offset(offsetNumber); } } + } diff --git a/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqQuery.java b/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqQuery.java index 312bccf..5de0b31 100644 --- a/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqQuery.java +++ b/src/main/java/dev/struchkov/haiti/filter/jooq/CriteriaJooqQuery.java @@ -1,7 +1,6 @@ package dev.struchkov.haiti.filter.jooq; -import dev.struchkov.haiti.filter.FilterQuery; -import lombok.NonNull; +import dev.struchkov.haiti.utils.Assert; import org.jooq.Condition; import org.jooq.Field; import org.jooq.impl.DSL; @@ -14,7 +13,7 @@ import java.util.Set; import static org.jooq.impl.DSL.condition; import static org.jooq.impl.DSL.field; -public class CriteriaJooqQuery implements FilterQuery { +public class CriteriaJooqQuery { private final List conditions = new ArrayList<>(); @@ -22,12 +21,12 @@ public class CriteriaJooqQuery implements FilterQuery { return new CriteriaJooqQuery(); } - protected List getConditions() { + List getConditions() { return conditions; } - @Override - public > FilterQuery between(@NonNull String field, Y from, Y to) { + public > CriteriaJooqQuery between(String field, Y from, Y to) { + Assert.isNotNull(field); if (from != null && to != null) { Condition condition = DSL.field(field).between(from, to); conditions.add(condition); @@ -35,20 +34,8 @@ public class CriteriaJooqQuery implements FilterQuery { return this; } - @Override - public > FilterQuery greaterThan(@NonNull String field, Y value) { - // FIXME: Добавить поддержку - throw new IllegalStateException("Операция пока не поддерживается"); - } - - @Override - public > FilterQuery lessThan(@NonNull String field, Y value) { - // FIXME: Добавить поддержку - throw new IllegalStateException("Операция пока не поддерживается"); - } - - @Override - public FilterQuery matchPhrase(@NonNull String field, Object value) { + public CriteriaJooqQuery matchPhrase(String field, Object value) { + Assert.isNotNull(field); if (value != null) { final Condition condition = condition(Map.of(field(field), value)); conditions.add(condition); @@ -56,24 +43,23 @@ public class CriteriaJooqQuery implements FilterQuery { return this; } - @Override - public FilterQuery matchPhrase(@NonNull String field, Set values) { + public CriteriaJooqQuery matchPhrase(String field, Set values) { + Assert.isNotNull(field); if (values != null && !values.isEmpty()) { conditions.add(DSL.field(field).in(values)); } return this; } - @Override - public FilterQuery exists(String field) { + public CriteriaJooqQuery exists(String field) { if (field != null) { conditions.add(DSL.field(field).isNotNull()); } return this; } - @Override - public FilterQuery like(@NonNull String field, String value, boolean ignoreCase) { + public CriteriaJooqQuery like(String field, String value, boolean ignoreCase) { + Assert.isNotNull(field); final Field query = field(field); if (value != null) { if (ignoreCase) { @@ -85,15 +71,4 @@ public class CriteriaJooqQuery implements FilterQuery { return this; } - @Override - public FilterQuery checkBoolInt(@NonNull String field, Boolean flag) { - // FIXME: Добавить поддержку - throw new IllegalStateException("Операция пока не поддерживается"); - } - - @Override - public List build() { - return conditions; - } - } diff --git a/src/main/java/dev/struchkov/haiti/filter/jooq/JoinTable.java b/src/main/java/dev/struchkov/haiti/filter/jooq/JoinTable.java index 422a7a2..ed466ff 100644 --- a/src/main/java/dev/struchkov/haiti/filter/jooq/JoinTable.java +++ b/src/main/java/dev/struchkov/haiti/filter/jooq/JoinTable.java @@ -1,17 +1,12 @@ package dev.struchkov.haiti.filter.jooq; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; +import dev.struchkov.haiti.utils.Assert; /** * Контейнер для соединения с таблицами. * * @author upagge 15.04.2021 */ -@Getter -@RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class JoinTable { private final String tableName; @@ -19,12 +14,37 @@ public class JoinTable { private final String fieldReference; private final JoinTypeOperation joinTypeOperation; - public static JoinTable ofLeft(@NonNull String tableName, @NonNull String fieldBase, @NonNull String fieldReference) { + private JoinTable(String tableName, String fieldBase, String fieldReference, JoinTypeOperation joinTypeOperation) { + this.tableName = tableName; + this.fieldBase = fieldBase; + this.fieldReference = fieldReference; + this.joinTypeOperation = joinTypeOperation; + } + + public static JoinTable ofLeft(String tableName, String fieldBase, String fieldReference) { + Assert.isNotNull(tableName, fieldBase, fieldReference); return new JoinTable(tableName, fieldBase, fieldReference, JoinTypeOperation.LEFT); } - public static JoinTable of(@NonNull String tableName, @NonNull String fieldBase, @NonNull String fieldReference, JoinTypeOperation joinType) { + public static JoinTable of(String tableName, String fieldBase, String fieldReference, JoinTypeOperation joinType) { + Assert.isNotNull(tableName, fieldBase, fieldReference); return new JoinTable(tableName, fieldBase, fieldReference, joinType); } + public String getTableName() { + return tableName; + } + + public String getFieldBase() { + return fieldBase; + } + + public String getFieldReference() { + return fieldReference; + } + + public JoinTypeOperation getJoinTypeOperation() { + return joinTypeOperation; + } + } diff --git a/src/main/java/dev/struchkov/haiti/filter/jooq/SortContainer.java b/src/main/java/dev/struchkov/haiti/filter/jooq/SortContainer.java index 4f97afb..c8b020d 100644 --- a/src/main/java/dev/struchkov/haiti/filter/jooq/SortContainer.java +++ b/src/main/java/dev/struchkov/haiti/filter/jooq/SortContainer.java @@ -1,29 +1,42 @@ 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) +import dev.struchkov.haiti.utils.Assert; + public class SortContainer { private final String fieldName; private SortType type = SortType.ASC; + private SortContainer(String fieldName) { + this.fieldName = fieldName; + } + + private SortContainer(String fieldName, SortType type) { + this.fieldName = fieldName; + this.type = type; + } + public static SortContainer empty() { return new SortContainer(null); } - public static SortContainer of(@NonNull String fieldName) { + public static SortContainer of(String fieldName) { + Assert.isNotNull(fieldName); return new SortContainer(fieldName); } - public static SortContainer of(@NonNull String fieldName, SortType sortType) { + public static SortContainer of(String fieldName, SortType sortType) { + Assert.isNotNull(fieldName); return new SortContainer(fieldName, sortType == null ? SortType.ASC : sortType); } + public String getFieldName() { + return fieldName; + } + + public SortType getType() { + return type; + } + } diff --git a/src/main/java/dev/struchkov/haiti/filter/jooq/page/PageableOffset.java b/src/main/java/dev/struchkov/haiti/filter/jooq/page/PageableOffset.java index af0fd16..dff01db 100644 --- a/src/main/java/dev/struchkov/haiti/filter/jooq/page/PageableOffset.java +++ b/src/main/java/dev/struchkov/haiti/filter/jooq/page/PageableOffset.java @@ -1,18 +1,19 @@ 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; + private PageableOffset(int pageNumber) { + this.pageNumber = pageNumber; + } + + private PageableOffset(int pageNumber, int pageSize) { + this.pageNumber = pageNumber; + this.pageSize = pageSize; + } + public static PageableOffset of(int pageNumber) { return new PageableOffset(pageNumber); } @@ -21,4 +22,16 @@ public class PageableOffset { return new PageableOffset(pageNumber, pageSize); } + public int getPageNumber() { + return pageNumber; + } + + public int getPageSize() { + return pageSize; + } + + public void setPageSize(int pageSize) { + this.pageSize = pageSize; + } + } diff --git a/src/main/java/dev/struchkov/haiti/filter/jooq/page/PageableSeek.java b/src/main/java/dev/struchkov/haiti/filter/jooq/page/PageableSeek.java index b00ee0a..cdb387a 100644 --- a/src/main/java/dev/struchkov/haiti/filter/jooq/page/PageableSeek.java +++ b/src/main/java/dev/struchkov/haiti/filter/jooq/page/PageableSeek.java @@ -1,19 +1,21 @@ package dev.struchkov.haiti.filter.jooq.page; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; +import dev.struchkov.haiti.utils.Assert; -@Getter -@AllArgsConstructor(access = AccessLevel.PRIVATE) -@RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class PageableSeek { private final Object lastId; private int pageSize = 30; + private PageableSeek(Object lastId) { + this.lastId = lastId; + } + + private PageableSeek(Object lastId, int pageSize) { + this.lastId = lastId; + this.pageSize = pageSize; + } + public static PageableSeek empty() { return new PageableSeek(null); } @@ -22,12 +24,22 @@ public class PageableSeek { return new PageableSeek(null, pageSize); } - public static PageableSeek of(@NonNull Object lastId) { + public static PageableSeek of(Object lastId) { + Assert.isNotNull(lastId); return new PageableSeek(lastId); } - public static PageableSeek of(@NonNull Object lastId, int pageSize) { + public static PageableSeek of(Object lastId, int pageSize) { + Assert.isNotNull(lastId); return new PageableSeek(lastId, pageSize); } + public Object getLastId() { + return lastId; + } + + public int getPageSize() { + return pageSize; + } + }