From 4681cb40e2fba4c787aad9e283ee0e4565392a29 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 1 Sep 2024 05:29:57 +0100 Subject: [PATCH 1/2] test: create method by query at ordinal Signed-off-by: Otavio Santana --- .../query/method/SelectMethodQueryProviderTest.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/method/SelectMethodQueryProviderTest.java b/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/method/SelectMethodQueryProviderTest.java index fa3d6a560..f5544d3ed 100644 --- a/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/method/SelectMethodQueryProviderTest.java +++ b/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/method/SelectMethodQueryProviderTest.java @@ -13,6 +13,7 @@ import jakarta.data.Direction; import jakarta.data.Sort; +import org.assertj.core.api.SoftAssertions; import org.eclipse.jnosql.communication.Condition; import org.eclipse.jnosql.communication.query.BooleanQueryValue; import org.eclipse.jnosql.communication.query.ConditionQueryValue; @@ -573,8 +574,17 @@ void shouldReturnUnsupportedOperationExceptionQueryWithNegation(String query) { String entity = "entity"; Assertions.assertThrows(UnsupportedOperationException.class, () -> queryProvider.apply(query, entity)); } + @ParameterizedTest(name = "Should parser the query {0}") + @ValueSource(strings = {"findByIdBetweenOrderByNumTypeOrdinalAsc"}) + void shouldFindByIdBetweenOrderByNumTypeOrdinalAsc(String query){ + String entity = "entity"; + SelectQuery selectQuery = queryProvider.apply(query, entity); - + SoftAssertions.assertSoftly(soft ->{ + soft.assertThat(selectQuery).isNotNull(); + soft.assertThat(selectQuery.entity()).isEqualTo(entity); + }); + } private void checkOrderBy(String query, Direction direction, Direction direction2) { String entity = "entity"; From 27d22a03fca4f459bd9fb717ae3523794b69454e Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sun, 1 Sep 2024 05:44:53 +0100 Subject: [PATCH 2/2] fix: update structure including order by Signed-off-by: Otavio Santana --- .../query/method/QueryTokenizer.java | 40 ++++++++++++++++++- .../method/SelectMethodQueryProviderTest.java | 2 + 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/jnosql-communication/jnosql-communication-query/src/main/java/org/eclipse/jnosql/communication/query/method/QueryTokenizer.java b/jnosql-communication/jnosql-communication-query/src/main/java/org/eclipse/jnosql/communication/query/method/QueryTokenizer.java index 86570f4d9..349e939ce 100644 --- a/jnosql-communication/jnosql-communication-query/src/main/java/org/eclipse/jnosql/communication/query/method/QueryTokenizer.java +++ b/jnosql-communication/jnosql-communication-query/src/main/java/org/eclipse/jnosql/communication/query/method/QueryTokenizer.java @@ -78,8 +78,9 @@ public int hashCode() { public static QueryTokenizer of(String query) { Objects.requireNonNull(query, "query is required"); return new QueryTokenizer(CACHE.computeIfAbsent(query, q -> { - String tokenized = TOKENIZER_PATTERN.matcher(q).replaceAll(" $0 ").trim().replaceAll("\\s+", " "); - return adjustFirstKeywordPosition(tokenized); + var tokenized = TOKENIZER_PATTERN.matcher(q).replaceAll(" $0 ").trim().replaceAll("\\s+", " "); + tokenized = adjustFirstKeywordPosition(tokenized); + return processOrderBy(tokenized); })); } @@ -96,4 +97,39 @@ private static String adjustFirstKeywordPosition(String query) { } return result.toString().trim(); } + + private static String processOrderBy(String query) { + StringBuilder result = new StringBuilder(); + String[] tokens = query.split(" "); + boolean afterOrderBy = false; + + for (int i = 0; i < tokens.length; i++) { + String token = tokens[i]; + + if (token.equals("OrderBy")) { + afterOrderBy = true; + result.append(token).append(" "); + } else if (afterOrderBy) { + // Only separate Asc and Desc, keep everything else intact + if (token.equals("Asc") || token.equals("Desc")) { + result.append(token).append(" "); + } else { + // Combine all tokens until "Asc" or "Desc" + while (i < tokens.length && !tokens[i].equals("Asc") && !tokens[i].equals("Desc")) { + result.append(tokens[i]); + i++; + } + // Add the final Asc or Desc if present + if (i < tokens.length) { + result.append(" ").append(tokens[i]).append(" "); + } + } + afterOrderBy = false; // Processed the relevant tokens after OrderBy + } else { + result.append(token).append(" "); + } + } + + return result.toString().trim(); + } } diff --git a/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/method/SelectMethodQueryProviderTest.java b/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/method/SelectMethodQueryProviderTest.java index f5544d3ed..ea57adbb3 100644 --- a/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/method/SelectMethodQueryProviderTest.java +++ b/jnosql-communication/jnosql-communication-query/src/test/java/org/eclipse/jnosql/communication/query/method/SelectMethodQueryProviderTest.java @@ -574,6 +574,8 @@ void shouldReturnUnsupportedOperationExceptionQueryWithNegation(String query) { String entity = "entity"; Assertions.assertThrows(UnsupportedOperationException.class, () -> queryProvider.apply(query, entity)); } + + @ParameterizedTest(name = "Should parser the query {0}") @ValueSource(strings = {"findByIdBetweenOrderByNumTypeOrdinalAsc"}) void shouldFindByIdBetweenOrderByNumTypeOrdinalAsc(String query){