Skip to content

Commit 94e84cc

Browse files
committed
Allow count queries to start with whitespace.
See #2393.
1 parent c053f08 commit 94e84cc

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public abstract class QueryUtils {
110110

111111
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
112112
private static final Pattern ORDER_BY = Pattern.compile("(order\\s+by\\s+)", CASE_INSENSITIVE);
113-
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern.compile("(\\(\\s*[a-z0-9 ,.*]*order\\s+by\\s+[a-z0-9 ,.]*\\s*\\))", CASE_INSENSITIVE);
113+
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern
114+
.compile("(\\(\\s*[a-z0-9 ,.*]*order\\s+by\\s+[a-z0-9 ,.]*\\s*\\))", CASE_INSENSITIVE);
114115

115116
private static final Pattern NAMED_PARAMETER = Pattern.compile(COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER,
116117
CASE_INSENSITIVE);
@@ -144,6 +145,7 @@ public abstract class QueryUtils {
144145
ALIAS_MATCH = compile(builder.toString(), CASE_INSENSITIVE);
145146

146147
builder = new StringBuilder();
148+
builder.append("\\s*");
147149
builder.append("(select\\s+((distinct)?((?s).+?)?)\\s+)?(from\\s+");
148150
builder.append(IDENTIFIER);
149151
builder.append("(?:\\s+as)?\\s+)");
@@ -279,8 +281,8 @@ public static String applySorting(String query, Sort sort, @Nullable String alia
279281
}
280282

281283
/**
282-
* Returns {@code true} if the query has {@code order by} clause.
283-
* The query has {@code order by} clause if there is an {@code order by} which is not part of window clause.
284+
* Returns {@code true} if the query has {@code order by} clause. The query has {@code order by} clause if there is an
285+
* {@code order by} which is not part of window clause.
284286
*
285287
* @param query the analysed query string
286288
* @return {@code true} if the query has {@code order by} clause, {@code false} otherwise
@@ -297,9 +299,13 @@ private static boolean hasOrderByClause(String query) {
297299
* @return the number of occurences of the pattern in the string
298300
*/
299301
private static int countOccurences(Pattern pattern, String string) {
302+
300303
Matcher matcher = pattern.matcher(string);
304+
301305
int occurences = 0;
302-
while (matcher.find()) occurences++;
306+
while (matcher.find()) {
307+
occurences++;
308+
}
303309
return occurences;
304310
}
305311

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,16 @@ void countProjectionDistinctQueryIncludesNewLineAfterEntityAndBeforeWhere() {
550550
"select count(DISTINCT entity1) FROM Entity1 entity1 LEFT JOIN Entity2 entity2 ON entity1.key = entity2.key\nwhere entity1.id = 1799");
551551
}
552552

553+
@Test // GH-2393
554+
void createCountQueryStartsWithWhitespace() {
555+
556+
assertThat(createCountQueryFor(" \nselect * from User u where u.age > :age"))
557+
.isEqualTo("select count(u) from User u where u.age > :age");
558+
559+
assertThat(createCountQueryFor(" \nselect u from User u where u.age > :age"))
560+
.isEqualTo("select count(u) from User u where u.age > :age");
561+
}
562+
553563
private static void assertCountQuery(String originalQuery, String countQuery) {
554564
assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
555565
}

0 commit comments

Comments
 (0)