DATAJPA-1500 - Fix RegEx to support whitespace characters at the end.

When using the creteCountQueryFor method before, the order by clause did not get removed when having a specific combination of whitespace characters at the end of the input.
By removing the $ for matching the end of the line, this is now fixed.

Original pull request: #380.
This commit is contained in:
Florian Lüdiger
2019-05-17 11:43:21 +02:00
committed by Jens Schauder
parent f21461000d
commit f1dc4fe242
2 changed files with 10 additions and 1 deletions

View File

@@ -95,7 +95,7 @@ public abstract class QueryUtils {
private static final String COUNT_REPLACEMENT_TEMPLATE = "select count(%s) $5$6$7";
private static final String SIMPLE_COUNT_VALUE = "$2";
private static final String COMPLEX_COUNT_VALUE = "$3$6";
private static final String ORDER_BY_PART = "(?iu)\\s+order\\s+by\\s+.*$";
private static final String ORDER_BY_PART = "(?iu)\\s+order\\s+by\\s+.*";
private static final Pattern ALIAS_MATCH;
private static final Pattern COUNT_MATCH;

View File

@@ -413,6 +413,15 @@ public class QueryUtilsUnitTests {
assertThat(detectAlias("select * from User u order by name")).isEqualTo("u");
}
@Test // DATAJPA-1500
public void createCountQuerySupportsWhitespaceCharacters() {
assertThat(createCountQueryFor("select * from User user\n" +
" where user.age = 18\n" +
" order by user.name\n "),
is("select count(user) from User user\n" +
" where user.age = 18\n "));
}
private static void assertCountQuery(String originalQuery, String countQuery) {
assertThat(createCountQueryFor(originalQuery), is(countQuery));
}