DATAJPA-726 - Fixed alias detection for queries containing joins.

We now not only detect alias in left (outer) joins but basically all of them to find out whether or not to prefix sort expressions with the root alias.
This commit is contained in:
Oliver Gierke
2015-05-22 14:18:43 +02:00
parent 2f5f269932
commit 67b93c2073
2 changed files with 22 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -83,15 +83,15 @@ public abstract class QueryUtils {
private static final String IDENTIFIER = "[\\p{Alnum}._$]+";
private static final String IDENTIFIER_GROUP = String.format("(%s)", IDENTIFIER);
private static final String LEFT_JOIN = "left (outer )?join " + IDENTIFIER + " (as )?" + IDENTIFIER_GROUP;
private static final Pattern LEFT_JOIN_PATTERN = Pattern.compile(LEFT_JOIN, Pattern.CASE_INSENSITIVE);
private static final String JOIN = "join " + IDENTIFIER + " (as )?" + IDENTIFIER_GROUP;
private static final Pattern JOIN_PATTERN = Pattern.compile(JOIN, Pattern.CASE_INSENSITIVE);
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
private static final Pattern ORDER_BY = Pattern.compile(".*order\\s+by\\s+.*", CASE_INSENSITIVE);
private static final Map<PersistentAttributeType, Class<? extends Annotation>> ASSOCIATION_TYPES;
private static final int QUERY_JOIN_ALIAS_GROUP_INDEX = 3;
private static final int QUERY_JOIN_ALIAS_GROUP_INDEX = 2;
private static final int VARIABLE_NAME_GROUP_INDEX = 4;
static {
@@ -252,7 +252,7 @@ public abstract class QueryUtils {
static Set<String> getOuterJoinAliases(String query) {
Set<String> result = new HashSet<String>();
Matcher matcher = LEFT_JOIN_PATTERN.matcher(query);
Matcher matcher = JOIN_PATTERN.matcher(query);
while (matcher.find()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -150,11 +150,13 @@ public class QueryUtilsUnitTests {
assertThat(aliases, hasSize(1));
assertThat(aliases, hasItems("b2_$ar"));
aliases = getOuterJoinAliases("select p from Person p left outer join x.foo as b2_$ar, left join x.bar as foo where …");
aliases = getOuterJoinAliases(
"select p from Person p left outer join x.foo as b2_$ar, left join x.bar as foo where …");
assertThat(aliases, hasSize(2));
assertThat(aliases, hasItems("b2_$ar", "foo"));
aliases = getOuterJoinAliases("select p from Person p left join x.foo as b2_$ar, left outer join x.bar foo where …");
aliases = getOuterJoinAliases(
"select p from Person p left join x.foo as b2_$ar, left outer join x.bar foo where …");
assertThat(aliases, hasSize(2));
assertThat(aliases, hasItems("b2_$ar", "foo"));
}
@@ -281,6 +283,18 @@ public class QueryUtilsUnitTests {
is("select count(p.lastname) from Person p"));
}
/**
* @see DATAJPA-726
*/
@Test
public void detectsAliassesInPlainJoins() {
String query = "select p from Customer c join c.productOrder p where p.delayed = true";
Sort sort = new Sort("p.lineItems");
assertThat(applySorting(query, sort, "c"), endsWith("order by p.lineItems asc"));
}
private void assertCountQuery(String originalQuery, String countQuery) {
assertThat(createCountQueryFor(originalQuery), is(countQuery));
}