Skip to content

Commit cd27f03

Browse files
darinmanicagregturn
authored andcommitted
Fix case where the from clause is misidentified.
Add a zero-width word boundary to the regex that identifies the from clause. This is used in alias detection. See #2508, #2260.
1 parent 28a2283 commit cd27f03

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public abstract class QueryUtils {
138138
static {
139139

140140
StringBuilder builder = new StringBuilder();
141-
builder.append("(?<=from)"); // from as starting delimiter
141+
builder.append("(?<=\\bfrom)"); // from as starting delimiter
142142
builder.append("(?:\\s)+"); // at least one space separating
143143
builder.append(IDENTIFIER_GROUP); // Entity name, can be qualified (any
144144
builder.append("(?:\\sas)*"); // exclude possible "as" keyword

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

+12
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ void detectsAliasCorrectly() throws Exception {
130130
assertThat(detectAlias(
131131
"(from Foo f max(f) ((((select * from Foo f2 (from Foo f3) max(*)) (from Foo f4)) max(f5)) (f6)) (from Foo f7))"))
132132
.isEqualTo("f");
133+
assertThat(detectAlias(
134+
"SELECT e FROM DbEvent e WHERE (CAST(:modifiedFrom AS date) IS NULL OR e.modificationDate >= :modifiedFrom)"))
135+
.isEqualTo("e");
136+
assertThat(detectAlias("from User u where (cast(:effective as date) is null) OR :effective >= u.createdAt"))
137+
.isEqualTo("u");
138+
assertThat(detectAlias("from User u where (cast(:effectiveDate as date) is null) OR :effectiveDate >= u.createdAt"))
139+
.isEqualTo("u");
140+
assertThat(detectAlias("from User u where (cast(:effectiveFrom as date) is null) OR :effectiveFrom >= u.createdAt"))
141+
.isEqualTo("u");
142+
assertThat(
143+
detectAlias("from User u where (cast(:e1f2f3ectiveFrom as date) is null) OR :effectiveFrom >= u.createdAt"))
144+
.isEqualTo("u");
133145
}
134146

135147
@Test // GH-2260

0 commit comments

Comments
 (0)