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 fa3d6a560..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 @@ -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; @@ -575,6 +576,17 @@ void shouldReturnUnsupportedOperationExceptionQueryWithNegation(String query) { } + @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";