From 0e480564a35e1067bca40121dd047537aaffb67b Mon Sep 17 00:00:00 2001 From: Zoubair BAKKALI Date: Fri, 2 Jun 2023 15:00:19 +0200 Subject: [PATCH] feat: add isNull opearator --- README.md | 4 +++- .../rsqlbuilder/impls/AbstractField.java | 12 +++++++++++- .../impls/AbstractPropertyField.java | 19 ++++++++++--------- .../talanlabs/rsqlbuilder/impls/Operator.java | 3 +++ .../impls/properties/AbstractProperty.java | 4 ++++ .../impls/properties/Property.java | 10 ++++++++++ .../rsqlbuilder/test/MainRsqlBuilder.java | 1 + .../test/unit/RsqlBuilderTest.java | 7 +++++++ 8 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/talanlabs/rsqlbuilder/impls/properties/Property.java diff --git a/README.md b/README.md index b12940f..7fe950c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Use with [rsql-parser](https://github.com/jirutka/rsql-parser) com.talanlabs rsql-builder - 1.0.0 + 1.0.1 ``` @@ -35,6 +35,8 @@ RsqlBuilder.newBuilder().string("name").in("gaby","sandra").query(); // name=in=('gaby','sandra') RsqlBuilder.newBuilder().temporal("birthday").before(LocalDate.parse("1990-05-10"), true).query(); // birthday=le='1990-05-10' +RsqlBuilder.newBuilder().property("address").isNull().query(); +// address=na='' ``` Group : diff --git a/src/main/java/com/talanlabs/rsqlbuilder/impls/AbstractField.java b/src/main/java/com/talanlabs/rsqlbuilder/impls/AbstractField.java index 92ca9c6..3c173ec 100644 --- a/src/main/java/com/talanlabs/rsqlbuilder/impls/AbstractField.java +++ b/src/main/java/com/talanlabs/rsqlbuilder/impls/AbstractField.java @@ -59,6 +59,16 @@ public B neq(String field, String value) { return nextBuilder(); } + /** + * Is Null to, use =na= + * + * @param field name + */ + public B isNull(String field) { + context.append(field).append(Operator.IsNull.getSymbol()).append("''"); + return nextBuilder(); + } + /** * Less, use ==lt== * @@ -176,4 +186,4 @@ private B condition(String symbol, Collection ls) { .collect(Collectors.joining(symbol, rsqlBuilder.getOpenGroupSymbol(), rsqlBuilder.getCloseGroupSymbol()))); return nextBuilder(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/talanlabs/rsqlbuilder/impls/AbstractPropertyField.java b/src/main/java/com/talanlabs/rsqlbuilder/impls/AbstractPropertyField.java index 220020f..92742d7 100644 --- a/src/main/java/com/talanlabs/rsqlbuilder/impls/AbstractPropertyField.java +++ b/src/main/java/com/talanlabs/rsqlbuilder/impls/AbstractPropertyField.java @@ -1,15 +1,7 @@ package com.talanlabs.rsqlbuilder.impls; import com.talanlabs.rsqlbuilder.RsqlBuilder; -import com.talanlabs.rsqlbuilder.impls.properties.BooleanProperty; -import com.talanlabs.rsqlbuilder.impls.properties.DoubleProperty; -import com.talanlabs.rsqlbuilder.impls.properties.EnumProperty; -import com.talanlabs.rsqlbuilder.impls.properties.FloatProperty; -import com.talanlabs.rsqlbuilder.impls.properties.IntegerProperty; -import com.talanlabs.rsqlbuilder.impls.properties.LongProperty; -import com.talanlabs.rsqlbuilder.impls.properties.ShortProperty; -import com.talanlabs.rsqlbuilder.impls.properties.StringProperty; -import com.talanlabs.rsqlbuilder.impls.properties.TemporalProperty; +import com.talanlabs.rsqlbuilder.impls.properties.*; import java.time.temporal.Temporal; @@ -82,6 +74,15 @@ public LongProperty longNum(String field) { return new LongProperty(field, this); } + /** + * String field + * + * @param field a name + */ + public Property property(String field) { + return new Property<>(field, this); + } + /** * Enum field * diff --git a/src/main/java/com/talanlabs/rsqlbuilder/impls/Operator.java b/src/main/java/com/talanlabs/rsqlbuilder/impls/Operator.java index 281d9af..a212907 100644 --- a/src/main/java/com/talanlabs/rsqlbuilder/impls/Operator.java +++ b/src/main/java/com/talanlabs/rsqlbuilder/impls/Operator.java @@ -10,6 +10,9 @@ public enum Operator { NotEqual("!="), /** * Less than */ + IsNull("=na="), /** + * Less than + */ Less("=lt="), /** * Less than or equal to */ diff --git a/src/main/java/com/talanlabs/rsqlbuilder/impls/properties/AbstractProperty.java b/src/main/java/com/talanlabs/rsqlbuilder/impls/properties/AbstractProperty.java index 3ed1553..7119a4e 100644 --- a/src/main/java/com/talanlabs/rsqlbuilder/impls/properties/AbstractProperty.java +++ b/src/main/java/com/talanlabs/rsqlbuilder/impls/properties/AbstractProperty.java @@ -13,4 +13,8 @@ public abstract class AbstractProperty { this.field = field; this.fieldBuilder = fieldBuilder; } + + public final B isNull() { + return fieldBuilder.isNull(field); + } } diff --git a/src/main/java/com/talanlabs/rsqlbuilder/impls/properties/Property.java b/src/main/java/com/talanlabs/rsqlbuilder/impls/properties/Property.java new file mode 100644 index 0000000..b5e6874 --- /dev/null +++ b/src/main/java/com/talanlabs/rsqlbuilder/impls/properties/Property.java @@ -0,0 +1,10 @@ +package com.talanlabs.rsqlbuilder.impls.properties; + +import com.talanlabs.rsqlbuilder.impls.AbstractField; + +public class Property extends AbstractProperty { + + public Property(String field, AbstractField fieldBuilder) { + super(field, fieldBuilder); + } +} diff --git a/src/test/java/com/talanlabs/rsqlbuilder/test/MainRsqlBuilder.java b/src/test/java/com/talanlabs/rsqlbuilder/test/MainRsqlBuilder.java index 7507f62..070eb20 100644 --- a/src/test/java/com/talanlabs/rsqlbuilder/test/MainRsqlBuilder.java +++ b/src/test/java/com/talanlabs/rsqlbuilder/test/MainRsqlBuilder.java @@ -10,6 +10,7 @@ public class MainRsqlBuilder { public static void main(String[] args) { + System.out.println(RsqlBuilder.rsql().property("address").isNull().query()); System.out.println(RsqlBuilder.rsql().bool("name").isTrue().query()); System.out.println(RsqlBuilder.rsql().string("name").neq("t'es là").query()); System.out.println(RsqlBuilder.rsql().string("name").eq("gaby").and().openGroup().intNum("age").gt(20).or().intNum("age").lte(40).closeGroup().query()); diff --git a/src/test/java/com/talanlabs/rsqlbuilder/test/unit/RsqlBuilderTest.java b/src/test/java/com/talanlabs/rsqlbuilder/test/unit/RsqlBuilderTest.java index a21a82e..7209509 100644 --- a/src/test/java/com/talanlabs/rsqlbuilder/test/unit/RsqlBuilderTest.java +++ b/src/test/java/com/talanlabs/rsqlbuilder/test/unit/RsqlBuilderTest.java @@ -13,6 +13,13 @@ public class RsqlBuilderTest { + @Test + public void testRsqlPropertyBuilder() { + SoftAssertions softAssertions = new SoftAssertions(); + softAssertions.assertThat(RsqlBuilder.rsql().property("address").isNull().query()).isEqualTo("address=na=''"); + softAssertions.assertAll(); + } + @Test public void testRsqlLongBuilder() { SoftAssertions softAssertions = new SoftAssertions();