Skip to content

Commit

Permalink
Merge pull request #549 from eclipse/fix-method-query
Browse files Browse the repository at this point in the history
Fix method query when using order
  • Loading branch information
otaviojava authored Sep 1, 2024
2 parents 5ac0745 + 27d22a0 commit bba661f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
}

Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit bba661f

Please sign in to comment.