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.
This commit is contained in:
@@ -84,7 +84,7 @@ public abstract class QueryUtils {
|
||||
private static final String IDENTIFIER = "[\\p{Lu}\\P{InBASIC_LATIN}\\p{Alnum}._$]+";
|
||||
private static final String IDENTIFIER_GROUP = String.format("(%s)", IDENTIFIER);
|
||||
|
||||
private static final String JOIN = "join " + IDENTIFIER + " (as )?" + IDENTIFIER_GROUP;
|
||||
private static final String JOIN = "join\\s" + IDENTIFIER + "\\s(as\\s)?" + IDENTIFIER_GROUP;
|
||||
private static final Pattern JOIN_PATTERN = Pattern.compile(JOIN, Pattern.CASE_INSENSITIVE);
|
||||
|
||||
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
|
||||
@@ -102,10 +102,10 @@ public abstract class QueryUtils {
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("(?<=from)"); // from as starting delimiter
|
||||
builder.append("(?: )+"); // at least one space separating
|
||||
builder.append("(?:\\s)+"); // at least one space separating
|
||||
builder.append(IDENTIFIER_GROUP); // Entity name, can be qualified (any
|
||||
builder.append("(?: as)*"); // exclude possible "as" keyword
|
||||
builder.append("(?: )+"); // at least one space separating
|
||||
builder.append("(?:\\sas)*"); // exclude possible "as" keyword
|
||||
builder.append("(?:\\s)+"); // at least one space separating
|
||||
builder.append("(\\w*)"); // the actual alias
|
||||
|
||||
ALIAS_MATCH = compile(builder.toString(), CASE_INSENSITIVE);
|
||||
|
||||
@@ -303,6 +303,14 @@ public class QueryUtilsUnitTests {
|
||||
assertThat(createCountQueryFor("select u from Usèr u"), is("select count(u) from Usèr u"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-798
|
||||
*/
|
||||
@Test
|
||||
public void detectsAliasInQueryContainingLineBreaks() {
|
||||
assertThat(detectAlias("select \n u \n from \n User \nu"), is("u"));
|
||||
}
|
||||
|
||||
private void assertCountQuery(String originalQuery, String countQuery) {
|
||||
assertThat(createCountQueryFor(originalQuery), is(countQuery));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user