Skip to content

Commit f53c256

Browse files
committed
DATAJPA-798 - Alias detection now also works for JPQL strings containing line breaks.
We tweaked the regular expression to detect query aliases to correctly use the whitespace character class \s instead of a simple space to make sure we don't get fooled by other whitespace characters like tabs and line breaks.
1 parent b18a8bc commit f53c256

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public abstract class QueryUtils {
8484
private static final String IDENTIFIER = "[\\p{Lu}\\P{InBASIC_LATIN}\\p{Alnum}._$]+";
8585
private static final String IDENTIFIER_GROUP = String.format("(%s)", IDENTIFIER);
8686

87-
private static final String JOIN = "join " + IDENTIFIER + " (as )?" + IDENTIFIER_GROUP;
87+
private static final String JOIN = "join\\s" + IDENTIFIER + "\\s(as\\s)?" + IDENTIFIER_GROUP;
8888
private static final Pattern JOIN_PATTERN = Pattern.compile(JOIN, Pattern.CASE_INSENSITIVE);
8989

9090
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
@@ -102,10 +102,10 @@ public abstract class QueryUtils {
102102

103103
StringBuilder builder = new StringBuilder();
104104
builder.append("(?<=from)"); // from as starting delimiter
105-
builder.append("(?: )+"); // at least one space separating
105+
builder.append("(?:\\s)+"); // at least one space separating
106106
builder.append(IDENTIFIER_GROUP); // Entity name, can be qualified (any
107-
builder.append("(?: as)*"); // exclude possible "as" keyword
108-
builder.append("(?: )+"); // at least one space separating
107+
builder.append("(?:\\sas)*"); // exclude possible "as" keyword
108+
builder.append("(?:\\s)+"); // at least one space separating
109109
builder.append("(\\w*)"); // the actual alias
110110

111111
ALIAS_MATCH = compile(builder.toString(), CASE_INSENSITIVE);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ public void supportsNonAsciiCharactersInEntityNames() {
303303
assertThat(createCountQueryFor("select u from Usèr u"), is("select count(u) from Usèr u"));
304304
}
305305

306+
/**
307+
* @see DATAJPA-798
308+
*/
309+
@Test
310+
public void detectsAliasInQueryContainingLineBreaks() {
311+
assertThat(detectAlias("select \n u \n from \n User \nu"), is("u"));
312+
}
313+
306314
private void assertCountQuery(String originalQuery, String countQuery) {
307315
assertThat(createCountQueryFor(originalQuery), is(countQuery));
308316
}

0 commit comments

Comments
 (0)