diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index c60830a1a..1bd801924 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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 diff --git a/antlr4/org/eclipse/jnosql/query/grammar/data/JDQL.g4 b/jnosql-communication/jnosql-communication-query/antlr4/org/eclipse/jnosql/query/grammar/data/JDQL.g4 similarity index 98% rename from antlr4/org/eclipse/jnosql/query/grammar/data/JDQL.g4 rename to jnosql-communication/jnosql-communication-query/antlr4/org/eclipse/jnosql/query/grammar/data/JDQL.g4 index 67722070f..2cdb92cb8 100644 --- a/antlr4/org/eclipse/jnosql/query/grammar/data/JDQL.g4 +++ b/jnosql-communication/jnosql-communication-query/antlr4/org/eclipse/jnosql/query/grammar/data/JDQL.g4 @@ -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 diff --git a/antlr4/org/eclipse/jnosql/query/grammar/method/Method.g4 b/jnosql-communication/jnosql-communication-query/antlr4/org/eclipse/jnosql/query/grammar/method/Method.g4 similarity index 100% rename from antlr4/org/eclipse/jnosql/query/grammar/method/Method.g4 rename to jnosql-communication/jnosql-communication-query/antlr4/org/eclipse/jnosql/query/grammar/method/Method.g4 diff --git a/jnosql-communication/jnosql-communication-query/pom.xml b/jnosql-communication/jnosql-communication-query/pom.xml index 84819245d..49a03aff0 100644 --- a/jnosql-communication/jnosql-communication-query/pom.xml +++ b/jnosql-communication/jnosql-communication-query/pom.xml @@ -48,7 +48,7 @@ antlr4-maven-plugin ${antlr4.version} - ../../antlr4 + antlr4 diff --git a/jnosql-communication/jnosql-communication-query/src/main/java/org/eclipse/jnosql/communication/query/data/AbstractWhere.java b/jnosql-communication/jnosql-communication-query/src/main/java/org/eclipse/jnosql/communication/query/data/AbstractWhere.java index c8305522c..1b79a3398 100644 --- a/jnosql-communication/jnosql-communication-query/src/main/java/org/eclipse/jnosql/communication/query/data/AbstractWhere.java +++ b/jnosql-communication/jnosql-communication-query/src/main/java/org/eclipse/jnosql/communication/query/data/AbstractWhere.java @@ -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; } @@ -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)); + } + } } diff --git a/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/data/SelectJakartaDataQueryProviderTest.java b/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/data/SelectJakartaDataQueryProviderTest.java index e88b18b87..72498adeb 100644 --- a/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/data/SelectJakartaDataQueryProviderTest.java +++ b/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/data/SelectJakartaDataQueryProviderTest.java @@ -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")); + }); + } }