Skip to content

Commit 7a6f534

Browse files
committed
Polishing.
Also added more test cases from related pull requests. Closes #2582. Related: #2563, #2557, #2603
1 parent 0dec0b0 commit 7a6f534

File tree

2 files changed

+80
-63
lines changed

2 files changed

+80
-63
lines changed

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

+5-17
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,10 @@
2626
import java.util.regex.Pattern;
2727
import java.util.stream.Collectors;
2828

29-
import javax.persistence.EntityManager;
30-
import javax.persistence.ManyToOne;
31-
import javax.persistence.OneToOne;
32-
import javax.persistence.Parameter;
33-
import javax.persistence.Query;
34-
import javax.persistence.criteria.CriteriaBuilder;
35-
import javax.persistence.criteria.Expression;
36-
import javax.persistence.criteria.Fetch;
37-
import javax.persistence.criteria.From;
38-
import javax.persistence.criteria.Join;
39-
import javax.persistence.criteria.JoinType;
40-
import javax.persistence.metamodel.Attribute;
29+
import javax.persistence.*;
30+
import javax.persistence.criteria.*;
31+
import javax.persistence.metamodel.*;
4132
import javax.persistence.metamodel.Attribute.PersistentAttributeType;
42-
import javax.persistence.metamodel.Bindable;
43-
import javax.persistence.metamodel.ManagedType;
44-
import javax.persistence.metamodel.PluralAttribute;
45-
import javax.persistence.metamodel.SingularAttribute;
4633

4734
import org.springframework.core.annotation.AnnotationUtils;
4835
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -105,7 +92,8 @@ public abstract class QueryUtils {
10592
private static final Pattern ALIAS_MATCH;
10693
private static final Pattern COUNT_MATCH;
10794
private static final Pattern STARTS_WITH_PAREN = Pattern.compile("^\\s*\\(");
108-
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))", CASE_INSENSITIVE | DOTALL | MULTILINE);
95+
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))",
96+
CASE_INSENSITIVE | DOTALL | MULTILINE);
10997
private static final Pattern PROJECTION_CLAUSE = Pattern.compile("select\\s+(?:distinct\\s+)?(.+)\\s+from",
11098
Pattern.CASE_INSENSITIVE);
11199

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

+75-46
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import static org.assertj.core.api.Assertions.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1920
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
2021

2122
import java.util.Collections;
@@ -46,6 +47,7 @@
4647
* @author Jędrzej Biedrzycki
4748
* @author Darin Manica
4849
* @author Chris Fraser
50+
* @author Michał Pachucki
4951
*/
5052
class QueryUtilsUnitTests {
5153

@@ -182,55 +184,82 @@ void testRemoveSubqueries() throws Exception {
182184
@Test // GH-2581
183185
void testRemoveMultilineSubqueries() {
184186

185-
assertThat(normalizeWhitespace(removeSubqueries("select u from User u\n"
186-
+ " where not exists (\n"
187-
+ " from User u2\n"
188-
+ " )")))
189-
.isEqualTo("select u from User u where not exists");
190-
assertThat(normalizeWhitespace(removeSubqueries("(\n"
191-
+ " select u from User u \n"
192-
+ " where not exists (\n"
193-
+ " from User u2\n"
194-
+ " )\n"
195-
+ ")")))
196-
.isEqualTo("( select u from User u where not exists )");
197-
assertThat(normalizeWhitespace(
198-
removeSubqueries("select u from User u \n"
199-
+ " where not exists (\n"
200-
+ " from User u2 \n"
201-
+ " where not exists (\n"
202-
+ " from User u3\n"
203-
+ " )\n"
204-
+ " )")))
205-
.isEqualTo("select u from User u where not exists");
206-
assertThat(normalizeWhitespace(
207-
removeSubqueries("select u from User u \n"
208-
+ " where not exists (\n"
209-
+ " (\n"
210-
+ " from User u2 \n"
211-
+ " where not exists (\n"
212-
+ " from User u3\n"
213-
+ " )\n"
214-
+ " )\n"
215-
+ " )")))
216-
.isEqualTo("select u from User u where not exists ( )");
217-
assertThat(normalizeWhitespace(
218-
removeSubqueries("(\n"
219-
+ " select u from User u \n"
220-
+ " where not exists (\n"
221-
+ " (\n"
222-
+ " from User u2 \n"
223-
+ " where not exists (\n"
224-
+ " from User u3\n"
225-
+ " )\n"
226-
+ " )\n"
227-
+ " )\n"
228-
+ ")")))
229-
.isEqualTo("( select u from User u where not exists ( ) )");
187+
assertThat(normalizeWhitespace(removeSubqueries("select u from User u\n" //
188+
+ " where not exists (\n" //
189+
+ " from User u2\n" //
190+
+ " )"))).isEqualTo("select u from User u where not exists");
191+
192+
assertThat(normalizeWhitespace(removeSubqueries("(\n" //
193+
+ " select u from User u \n" //
194+
+ " where not exists (\n" //
195+
+ " from User u2\n" //
196+
+ " )\n" //
197+
+ ")"))).isEqualTo("( select u from User u where not exists )");
198+
199+
assertThat(normalizeWhitespace(removeSubqueries("select u from User u \n" //
200+
+ " where not exists (\n" //
201+
+ " from User u2 \n" //
202+
+ " where not exists (\n" //
203+
+ " from User u3\n" //
204+
+ " )\n" //
205+
+ " )"))).isEqualTo("select u from User u where not exists");
206+
207+
assertThat(normalizeWhitespace(removeSubqueries("select u from User u \n" //
208+
+ " where not exists (\n" //
209+
+ " (\n" //
210+
+ " from User u2 \n" //
211+
+ " where not exists (\n" //
212+
+ " from User u3\n" //
213+
+ " )\n" //
214+
+ " )\n" //
215+
+ " )"))).isEqualTo("select u from User u where not exists ( )");
216+
217+
assertThat(normalizeWhitespace(removeSubqueries("(\n" //
218+
+ " select u from User u \n" //
219+
+ " where not exists (\n" //
220+
+ " (\n" //
221+
+ " from User u2 \n" //
222+
+ " where not exists (\n" //
223+
+ " from User u3\n" //
224+
+ " )\n" //
225+
+ " )\n" //
226+
+ " )\n" //
227+
+ ")"))).isEqualTo("( select u from User u where not exists ( ) )");
228+
}
229+
230+
@Test // GH-2557
231+
void applySortingAccountsForNewlinesInSubselect() {
232+
233+
Sort sort = Sort.by(Order.desc("age"));
234+
235+
assertThat(QueryUtils.applySorting("select u\n" + //
236+
"from user u\n" + //
237+
"where exists (select u2\n" + //
238+
"from user u2\n" + //
239+
")\n" + //
240+
"", sort)).isEqualTo("select u\n" + //
241+
"from user u\n" + //
242+
"where exists (select u2\n" + //
243+
"from user u2\n" + //
244+
")\n" + //
245+
" order by u.age desc");
246+
}
247+
248+
@Test // GH-2563
249+
void aliasDetectionProperlyHandlesNewlinesInSubselects() {
250+
251+
assertThat(detectAlias("SELECT o\n" + //
252+
"FROM Order o\n" + //
253+
"AND EXISTS(SELECT 1\n" + //
254+
"FROM Vehicle vehicle\n" + //
255+
"WHERE vehicle.vehicleOrderId = o.id\n" + //
256+
"AND LOWER(COALESCE(vehicle.make, '')) LIKE :query)")).isEqualTo("o");
230257
}
231258

232259
private String normalizeWhitespace(String s) {
260+
233261
Matcher matcher = MULTI_WHITESPACE.matcher(s);
262+
234263
if (matcher.find()) {
235264
return matcher.replaceAll(" ").trim();
236265
}

0 commit comments

Comments
 (0)