From 86140eac9a703d01ded9bfe1112c067bf43cef4c Mon Sep 17 00:00:00 2001 From: Yves-MENGELLE-OF <154422758+Yves-MENGELLE-OF@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:13:52 +0100 Subject: [PATCH] feat(null): adding operators ISNULL and NOTNULL (#32) * feat(isnull-operator): implement ISNULL operator --- querydsl-postgrest/pom.xml | 5 ++-- .../postgrest/mappers/IsNullMapper.java | 18 +++++++++++ .../querydsl/postgrest/mappers/Operators.java | 4 +++ .../postgrest/mappers/IsNullMapperTest.java | 30 +++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/mappers/IsNullMapper.java create mode 100644 querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/mappers/IsNullMapperTest.java diff --git a/querydsl-postgrest/pom.xml b/querydsl-postgrest/pom.xml index ff22b0a..7ae231e 100644 --- a/querydsl-postgrest/pom.xml +++ b/querydsl-postgrest/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 fr.ouestfrance.querydsl @@ -13,7 +14,7 @@ fr.ouestfrance.querydsl querydsl - 1.2.0 + 1.3.0 diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/mappers/IsNullMapper.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/mappers/IsNullMapper.java new file mode 100644 index 0000000..d65613d --- /dev/null +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/mappers/IsNullMapper.java @@ -0,0 +1,18 @@ +package fr.ouestfrance.querydsl.postgrest.mappers; + +import fr.ouestfrance.querydsl.FilterOperation; +import fr.ouestfrance.querydsl.postgrest.model.Filter; +import fr.ouestfrance.querydsl.postgrest.model.impl.QueryFilter; + +public class IsNullMapper extends AbstractMapper { + + @Override + public Filter getFilter(String field, Object value) { + return QueryFilter.of(field, Boolean.TRUE.equals(value) ? Operators.IS : Operators.IS_NOT, null); + } + + @Override + public Class operation() { + return FilterOperation.ISNULL.class; + } +} diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/mappers/Operators.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/mappers/Operators.java index 91d4132..86f2688 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/mappers/Operators.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/mappers/Operators.java @@ -56,6 +56,10 @@ public final class Operators { * is operation */ public static final String IS = "is"; + /** + * is not operation + */ + public static final String IS_NOT = "not.is"; /** * Contains operation for JSON/Range datatype */ diff --git a/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/mappers/IsNullMapperTest.java b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/mappers/IsNullMapperTest.java new file mode 100644 index 0000000..3b50097 --- /dev/null +++ b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/mappers/IsNullMapperTest.java @@ -0,0 +1,30 @@ +package fr.ouestfrance.querydsl.postgrest.mappers; + +import fr.ouestfrance.querydsl.FilterOperation; +import fr.ouestfrance.querydsl.model.SimpleFilter; +import fr.ouestfrance.querydsl.postgrest.builders.QueryFilterVisitor; +import fr.ouestfrance.querydsl.postgrest.model.Filter; +import org.junit.jupiter.api.Test; + +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class IsNullMapperTest { + + @Test + void shouldMapIsNull() { + IsNullMapper mapper = new IsNullMapper(); + assertNotNull(mapper.operation()); + Stream.of(Boolean.TRUE, Boolean.FALSE).forEach(value -> { + Filter result = mapper.map(new SimpleFilter("name", FilterOperation.ISNULL.class, false, null), value); + assertNotNull(result); + QueryFilterVisitor visitor = new QueryFilterVisitor(); + result.accept(visitor); + assertNotNull(visitor.getValue()); + assertEquals(Boolean.TRUE.equals(value) ? "is.null" : "not.is.null", visitor.getValue()); + }); + + } +}