Skip to content

Better detection of "from" clauses in alias detection. #2520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-gh-2508-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-gh-2508-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-gh-2508-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-gh-2508-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-gh-2508-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-gh-2508-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public abstract class QueryUtils {
static {

StringBuilder builder = new StringBuilder();
builder.append("(?<=from)"); // from as starting delimiter
builder.append("(?<=[^a-zA-Z0-9]+from)"); // from as starting delimiter (with no characters prefixing it)
builder.append("(?:\\s)+"); // at least one space separating
builder.append(IDENTIFIER_GROUP); // Entity name, can be qualified (any
builder.append("(?:\\sas)*"); // exclude possible "as" keyword
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DefaultQueryUtilsUnitTests {

private static final String QUERY = "select u from User u";
private static final String FQ_QUERY = "select u from org.acme.domain.User$Foo_Bar u";
private static final String SIMPLE_QUERY = "from User u";
private static final String SIMPLE_QUERY = " from User u";
private static final String COUNT_QUERY = "select count(u) from User u";

private static final String QUERY_WITH_AS = "select u from User as u where u.username = ?";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class QueryEnhancerUnitTests {

private static final String QUERY = "select u from User u";
private static final String FQ_QUERY = "select u from org.acme.domain.User$Foo_Bar u";
private static final String SIMPLE_QUERY = "from User u";
private static final String SIMPLE_QUERY = " from User u";
private static final String COUNT_QUERY = "select count(u) from User u";

private static final String QUERY_WITH_AS = "select u from User as u where u.username = ?";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.jpa.repository.query;

import static java.util.regex.Pattern.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.jpa.repository.query.QueryUtils.*;

Expand Down Expand Up @@ -50,7 +51,7 @@ class QueryUtilsUnitTests {

private static final String QUERY = "select u from User u";
private static final String FQ_QUERY = "select u from org.acme.domain.User$Foo_Bar u";
private static final String SIMPLE_QUERY = "from User u";
private static final String SIMPLE_QUERY = " from User u";
private static final String COUNT_QUERY = "select count(u) from User u";

private static final String QUERY_WITH_AS = "select u from User as u where u.username = ?";
Expand Down Expand Up @@ -123,13 +124,52 @@ void detectsAliasCorrectly() {
assertThat(detectAlias("(select u from User u where not exists ((from User u2 where not exists (from User u3))))"))
.isEqualTo("u");
assertThat(detectAlias(
"from Foo f left join f.bar b with type(b) = BarChild where (f.id = (select max(f.id) from Foo f2 where type(f2) = FooChild) or 1 <> 1) and 1=1"))
" from Foo f left join f.bar b with type(b) = BarChild where (f.id = (select max(f.id) from Foo f2 where type(f2) = FooChild) or 1 <> 1) and 1=1"))
.isEqualTo("f");
assertThat(detectAlias(
"(from Foo f max(f) ((((select * from Foo f2 (from Foo f3) max(*)) (from Foo f4)) max(f5)) (f6)) (from Foo f7))"))
.isEqualTo("f");
}

@Test // GH-2508
void detectAliasWithCastCorrectly() {

assertThat(detectAlias(" from User u where (cast(:effective as date) is null) OR :effective >= u.createdAt"))
.isEqualTo("u");
assertThat(
detectAlias(" from User u where (cast(:effectiveDate as date) is null) OR :effectiveDate >= u.createdAt"))
.isEqualTo("u");
assertThat(
detectAlias(" from User u where (cast(:effectiveFrom as date) is null) OR :effectiveFrom >= u.createdAt"))
.isEqualTo("u");
assertThat(
detectAlias(" from User u where (cast(:e1f2f3ectiveFrom as date) is null) OR :effectiveFrom >= u.createdAt"))
.isEqualTo("u");
}

@Test
void foo() {

StringBuilder builder = new StringBuilder();
builder.append("(?<=[^a-zA-Z]+from)");

Pattern pattern = compile(builder.toString(), CASE_INSENSITIVE);

Matcher matcher = pattern.matcher("select * from User u where (cast(:effectiveDate as date) is null)");
int count = 0;
while (matcher.find()) {
count++;
}
assertThat(count).isEqualTo(1);

matcher = pattern.matcher("select * from User u where (cast(:effectiveFrom as date) is null)");
count = 0;
while (matcher.find()) {
count++;
}
assertThat(count).isEqualTo(1);
}

@Test // GH-2260
void testRemoveSubqueries() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,18 +373,18 @@ void checkProjection(String query, String expected, String description, boolean
@Test // DATAJPA-1235
void getAlias() {

checkAlias("from User u", "u", "simple query", false);
checkAlias(" from User u", "u", "simple query", false);
checkAlias("select count(u) from User u", "u", "count query", true);
checkAlias("select u from User as u where u.username = ?", "u", "with as", true);
checkAlias("SELECT FROM USER U", "U", "uppercase", false);
checkAlias("select u from User u", "u", "simple query", true);
checkAlias("select u from com.acme.User u", "u", "fully qualified package name", true);
checkAlias("select u from T05User u", "u", "interesting entity name", true);
checkAlias("from User ", null, "trailing space", false);
checkAlias("from User", null, "no trailing space", false);
checkAlias("from User as bs", "bs", "ignored as", false);
checkAlias("from User as AS", "AS", "ignored as using the second", false);
checkAlias("from User asas", "asas", "asas is weird but legal", false);
checkAlias(" from User ", null, "trailing space", false);
checkAlias(" from User", null, "no trailing space", false);
checkAlias(" from User as bs", "bs", "ignored as", false);
checkAlias(" from User as AS", "AS", "ignored as using the second", false);
checkAlias(" from User asas", "asas", "asas is weird but legal", false);

softly.assertAll();
}
Expand Down