Skip to content

Commit

Permalink
fix: update structure including order by
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Sep 1, 2024
1 parent 4681cb4 commit 27d22a0
Show file tree
Hide file tree
Showing 2 changed files with 40 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 @@ -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){
Expand Down

0 comments on commit 27d22a0

Please sign in to comment.