Skip to content

Commit

Permalink
Allow to use null value to omit filter
Browse files Browse the repository at this point in the history
  • Loading branch information
uwburn committed Jun 29, 2021
1 parent c990bdb commit 0b52166
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>it.mgt.util</groupId>
<artifactId>jpa-json-search</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static public Operator parse(String placeholder) {

private String name;
private Operator operator;
private boolean omit = false;
private JpaJsonSearchParameter parameter;
private Object value;

Expand Down Expand Up @@ -114,6 +115,9 @@ static public Operator parse(String placeholder) {
JpaJsonSearchJpqlAndParams buildJpql() {
logger.trace("Building JPQL");

if (omit)
return new JpaJsonSearchJpqlAndParams();

StringBuilder jpql = new StringBuilder();

switch (operator) {
Expand Down Expand Up @@ -188,6 +192,12 @@ else if (jsonNode.size() == 1) {
Map.Entry<String, JsonNode> child = jsonNode.fields().next();

operator = Operator.parse(child.getKey());

if (child.getValue().isNull()) {
this.omit = true;
return this;
}

if (child.getValue().isArray()) {
ArrayNode arrayNode = (ArrayNode) child.getValue();
value = StreamSupport.stream(arrayNode.spliterator(), false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ JpaJsonSearchJpqlAndParams buildJpql() {

boolean first = true;
for (JpaJsonSearchFilter filter : filters) {
JpaJsonSearchJpqlAndParams filterJpqlAndParams = filter.buildJpql();

if (filterJpqlAndParams.isEmpty())
continue;

if (first)
first = false;
else
jpqlAndParams.append(conjunction.jpql);

jpqlAndParams.append(filter.buildJpql());
jpqlAndParams.append(filterJpqlAndParams);
}

jpqlAndParams.append(JpaJsonSearch.CLOSE_PARENTHESIS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ JpaJsonSearchJpqlAndParams append(JpaJsonSearchJpqlAndParams jpqlAndParams) {
return this;
}

boolean isEmpty() {
return this.jpql.length() == 0 && this.params.size() == 0;
}

}

0 comments on commit 0b52166

Please sign in to comment.