Skip to content

Commit

Permalink
Merge pull request #541 from eclipse/jnosql-like-540
Browse files Browse the repository at this point in the history
[BUG] JDQL queries with like operator not working #540
  • Loading branch information
otaviojava authored Aug 9, 2024
2 parents 3a64137 + 714132c commit 2878b85
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
- Make the JDQL return the correct type when the select is by field
- Invalid deserialization of maps with generic values
- Make sure at the serialization to the field, the API does not return any communication layer, but standard Java types
- Fix the like query at the JDQL

=== Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ comparison_expression : scalar_expression comparison_operator scalar_expression;
comparison_operator : EQ | GT | GTEQ | LT | LTEQ | NEQ;

between_expression : scalar_expression NOT? BETWEEN scalar_expression AND scalar_expression;
like_expression : scalar_expression NOT? LIKE STRING;
like_expression : scalar_expression NOT? LIKE (STRING | input_parameter);

in_expression : state_field_path_expression NOT? IN '(' in_item (',' in_item)* ')';
in_item : literal | enum_literal | input_parameter; // could simplify to just literal
Expand Down
2 changes: 1 addition & 1 deletion jnosql-communication/jnosql-communication-query/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr4.version}</version>
<configuration>
<sourceDirectory>../../antlr4</sourceDirectory>
<sourceDirectory>antlr4</sourceDirectory>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,12 @@ public void exitLike_expression(JDQLParser.Like_expressionContext ctx) {
var contexts = ctx.scalar_expression();
var name = contexts.getText();
var contextCondition = Condition.LIKE;
var likeValueIndex = ctx.getChildCount() - 1;
var likeValue = contexts.getParent().getChild(likeValueIndex).getText();
var literal = StringQueryValue.of(likeValue.substring(1, likeValue.length() - 1));
QueryValue<?> value = likeQueryValue(ctx, contexts);

if (this.condition != null && this.condition.value() instanceof ConditionQueryValue) {
and = andCondition;
}
checkCondition(new DefaultQueryCondition(name, contextCondition, literal), hasNot);
checkCondition(new DefaultQueryCondition(name, contextCondition, value), hasNot);
and = andCondition;
}

Expand Down Expand Up @@ -274,4 +273,14 @@ private QueryCondition checkNotCondition(QueryCondition condition, boolean hasNo
return condition;
}
}

private static QueryValue<?> likeQueryValue(JDQLParser.Like_expressionContext ctx, JDQLParser.Scalar_expressionContext contexts) {
if(ctx.input_parameter() != null){
return DefaultQueryValue.of(ctx.input_parameter().getText());
} else {
var likeValueIndex = ctx.getChildCount() - 1;
var likeValue = contexts.getParent().getChild(likeValueIndex).getText();
return StringQueryValue.of(likeValue.substring(1, likeValue.length() - 1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -538,4 +538,122 @@ void shouldReturnErrorWhenUseParenthesis(String query) {
selectProvider.apply(query, "entity");
});
}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = "where employeeName LIKE ?1")
void shouldUseLike(String query) {
var selectQuery = selectProvider.apply(query, "entity");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(selectQuery.fields()).isEmpty();
soft.assertThat(selectQuery.entity()).isEqualTo("entity");
soft.assertThat(selectQuery.orderBy()).isEmpty();
soft.assertThat(selectQuery.where()).isNotEmpty();
var where = selectQuery.where().orElseThrow();
var condition = where.condition();
soft.assertThat(condition.condition()).isEqualTo(Condition.LIKE);
soft.assertThat(condition.name()).isEqualTo("employeeName");
soft.assertThat(condition.value()).isEqualTo(DefaultQueryValue.of("?1"));

});
}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = "where employeeName LIKE :employeeName")
void shouldUseLike2(String query) {
var selectQuery = selectProvider.apply(query, "entity");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(selectQuery.fields()).isEmpty();
soft.assertThat(selectQuery.entity()).isEqualTo("entity");
soft.assertThat(selectQuery.orderBy()).isEmpty();
soft.assertThat(selectQuery.where()).isNotEmpty();
var where = selectQuery.where().orElseThrow();
var condition = where.condition();
soft.assertThat(condition.condition()).isEqualTo(Condition.LIKE);
soft.assertThat(condition.name()).isEqualTo("employeeName");
soft.assertThat(condition.value()).isEqualTo(DefaultQueryValue.of(":employeeName"));
});
}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = "where employeeName LIKE 'employeeName'")
void shouldUseLike3(String query) {
var selectQuery = selectProvider.apply(query, "entity");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(selectQuery.fields()).isEmpty();
soft.assertThat(selectQuery.entity()).isEqualTo("entity");
soft.assertThat(selectQuery.orderBy()).isEmpty();
soft.assertThat(selectQuery.where()).isNotEmpty();
var where = selectQuery.where().orElseThrow();
var condition = where.condition();
soft.assertThat(condition.condition()).isEqualTo(Condition.LIKE);
soft.assertThat(condition.name()).isEqualTo("employeeName");
soft.assertThat(condition.value()).isEqualTo(StringQueryValue.of("employeeName"));
});
}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = "where employeeName NOT LIKE 'employeeName'")
void shouldUseNotLike(String query) {
var selectQuery = selectProvider.apply(query, "entity");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(selectQuery.fields()).isEmpty();
soft.assertThat(selectQuery.entity()).isEqualTo("entity");
soft.assertThat(selectQuery.orderBy()).isEmpty();
soft.assertThat(selectQuery.where()).isNotEmpty();
var where = selectQuery.where().orElseThrow();
var condition = where.condition();
soft.assertThat(condition.condition()).isEqualTo(Condition.NOT);
var notCondition = (ConditionQueryValue) condition.value();
QueryCondition queryCondition = notCondition.get().get(0);
soft.assertThat(queryCondition.name()).isEqualTo("employeeName");
soft.assertThat(queryCondition.value()).isEqualTo(StringQueryValue.of("employeeName"));
});
}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = "where employeeName NOT LIKE ?1")
void shouldUseNotLike2(String query) {
var selectQuery = selectProvider.apply(query, "entity");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(selectQuery.fields()).isEmpty();
soft.assertThat(selectQuery.entity()).isEqualTo("entity");
soft.assertThat(selectQuery.orderBy()).isEmpty();
soft.assertThat(selectQuery.where()).isNotEmpty();
var where = selectQuery.where().orElseThrow();
var condition = where.condition();
soft.assertThat(condition.condition()).isEqualTo(Condition.NOT);
var notCondition = (ConditionQueryValue) condition.value();
QueryCondition queryCondition = notCondition.get().get(0);
soft.assertThat(queryCondition.condition()).isEqualTo(Condition.LIKE);
soft.assertThat(queryCondition.name()).isEqualTo("employeeName");
soft.assertThat(queryCondition.value()).isEqualTo(DefaultQueryValue.of("?1"));

});
}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = "where employeeName NOT LIKE :employeeName")
void shouldUseNotLike3(String query) {
var selectQuery = selectProvider.apply(query, "entity");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(selectQuery.fields()).isEmpty();
soft.assertThat(selectQuery.entity()).isEqualTo("entity");
soft.assertThat(selectQuery.orderBy()).isEmpty();
soft.assertThat(selectQuery.where()).isNotEmpty();
var where = selectQuery.where().orElseThrow();
var condition = where.condition();
soft.assertThat(condition.condition()).isEqualTo(Condition.NOT);
var notCondition = (ConditionQueryValue) condition.value();
QueryCondition queryCondition = notCondition.get().get(0);
soft.assertThat(queryCondition.condition()).isEqualTo(Condition.LIKE);
soft.assertThat(queryCondition.name()).isEqualTo("employeeName");
soft.assertThat(queryCondition.value()).isEqualTo(DefaultQueryValue.of(":employeeName"));
});
}
}

0 comments on commit 2878b85

Please sign in to comment.