Skip to content

Commit 4b26700

Browse files
committed
Polishing.
Related: #2641.
1 parent 7b69aa6 commit 4b26700

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

Diff for: spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JSqlParserQueryEnhancer.java

+21-26
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,9 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import static org.springframework.data.jpa.repository.query.JSqlParserUtils.*;
19-
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
20-
21-
import java.util.ArrayList;
22-
import java.util.Collections;
23-
import java.util.HashSet;
24-
import java.util.List;
25-
import java.util.Objects;
26-
import java.util.Set;
27-
import java.util.stream.Collectors;
28-
29-
import org.springframework.data.domain.Sort;
30-
import org.springframework.lang.Nullable;
31-
import org.springframework.util.Assert;
32-
import org.springframework.util.CollectionUtils;
33-
import org.springframework.util.StringUtils;
18+
import static org.springframework.data.jpa.repository.query.JSqlParserUtils.getJSqlCount;
19+
import static org.springframework.data.jpa.repository.query.JSqlParserUtils.getJSqlLower;
20+
import static org.springframework.data.jpa.repository.query.QueryUtils.checkSortExpression;
3421

3522
import net.sf.jsqlparser.JSQLParserException;
3623
import net.sf.jsqlparser.expression.Alias;
@@ -42,17 +29,19 @@
4229
import net.sf.jsqlparser.statement.delete.Delete;
4330
import net.sf.jsqlparser.statement.insert.Insert;
4431
import net.sf.jsqlparser.statement.merge.Merge;
45-
import net.sf.jsqlparser.statement.select.OrderByElement;
46-
import net.sf.jsqlparser.statement.select.PlainSelect;
47-
import net.sf.jsqlparser.statement.select.Select;
48-
import net.sf.jsqlparser.statement.select.SelectBody;
49-
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
50-
import net.sf.jsqlparser.statement.select.SelectItem;
51-
import net.sf.jsqlparser.statement.select.SetOperationList;
52-
import net.sf.jsqlparser.statement.select.WithItem;
32+
import net.sf.jsqlparser.statement.select.*;
5333
import net.sf.jsqlparser.statement.update.Update;
5434
import net.sf.jsqlparser.statement.values.ValuesStatement;
5535

36+
import java.util.*;
37+
import java.util.stream.Collectors;
38+
39+
import org.springframework.data.domain.Sort;
40+
import org.springframework.lang.Nullable;
41+
import org.springframework.util.Assert;
42+
import org.springframework.util.CollectionUtils;
43+
import org.springframework.util.StringUtils;
44+
5645
/**
5746
* The implementation of {@link QueryEnhancer} using JSqlParser.
5847
*
@@ -147,7 +136,7 @@ public String applySorting(Sort sort, @Nullable String alias) {
147136

148137
/**
149138
* Returns the {@link SetOperationList} as a string query with {@link Sort}s applied in the right order.
150-
*
139+
*
151140
* @param setOperationListStatement
152141
* @param sort
153142
* @return
@@ -305,14 +294,16 @@ public String detectAlias() {
305294
private String detectAlias(String query) {
306295

307296
if (ParsedType.MERGE.equals(this.parsedType)) {
297+
308298
Merge mergeStatement = parseSelectStatement(query, Merge.class);
309299
return detectAlias(mergeStatement);
310300

311301
} else if (ParsedType.SELECT.equals(this.parsedType)) {
302+
312303
Select selectStatement = parseSelectStatement(query);
313304

314305
/*
315-
For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
306+
For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
316307
alias since:
317308
* ValuesStatement has no alias
318309
* SetOperation can have multiple alias for each operation item
@@ -354,6 +345,7 @@ private String detectAlias(PlainSelect selectBody) {
354345
*/
355346
@Nullable
356347
private String detectAlias(Merge mergeStatement) {
348+
357349
Alias alias = mergeStatement.getUsingAlias();
358350
return alias == null ? null : alias.getName();
359351
}
@@ -382,6 +374,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
382374
selectBody.setOrderByElements(null);
383375

384376
if (StringUtils.hasText(countProjection)) {
377+
385378
Function jSqlCount = getJSqlCount(Collections.singletonList(countProjection), false);
386379
selectBody.setSelectItems(Collections.singletonList(new SelectExpressionItem(jSqlCount)));
387380
return selectBody.toString();
@@ -396,6 +389,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
396389
List<SelectItem> selectItems = selectBody.getSelectItems();
397390

398391
if (onlyASingleColumnProjection(selectItems)) {
392+
399393
SelectExpressionItem singleProjection = (SelectExpressionItem) selectItems.get(0);
400394

401395
Column column = (Column) singleProjection.getExpression();
@@ -440,6 +434,7 @@ public String getProjection() {
440434
SelectBody selectBody = selectStatement.getSelectBody();
441435

442436
if (selectStatement.getSelectBody()instanceof SetOperationList setOperationList) {
437+
443438
// using the first one since for setoperations the projection has to be the same
444439
selectBody = setOperationList.getSelects().get(0);
445440

Diff for: spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -3008,15 +3008,17 @@ void mergeWithNativeStatement() {
30083008

30093009
flushTestUsers();
30103010

3011-
Optional<User> byIdUser = repository.findById(firstUser.getId());
3012-
assertThat(byIdUser).isPresent().map(User::getAge).get().isEqualTo(28);
3011+
assertThat(repository.findById(firstUser.getId())) //
3012+
.isPresent() //
3013+
.map(User::getAge).contains(28);
30133014

30143015
// when
30153016
repository.mergeNativeStatement();
30163017

30173018
// then
3018-
Optional<User> afterUpdate = repository.findById(firstUser.getId());
3019-
assertThat(afterUpdate).isPresent().map(User::getAge).get().isEqualTo(30);
3019+
assertThat(repository.findById(firstUser.getId())) //
3020+
.isPresent() //
3021+
.map(User::getAge).contains(30);
30203022
}
30213023

30223024
private Page<User> executeSpecWithSort(Sort sort) {

Diff for: spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/QueryEnhancerUnitTests.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@ void insertStatementIsProcessedSameAsDefault(String insertQuery) {
924924
@ParameterizedTest // GH-2641
925925
@MethodSource("mergeStatementWorksWithJSqlParserSource")
926926
void mergeStatementWorksWithJSqlParser(String query, String alias) {
927+
927928
StringQuery stringQuery = new StringQuery(query, true);
928929
QueryEnhancer queryEnhancer = QueryEnhancerFactory.forQuery(stringQuery);
929930

@@ -937,16 +938,19 @@ void mergeStatementWorksWithJSqlParser(String query, String alias) {
937938
}
938939

939940
public static Stream<Arguments> insertStatementIsProcessedSameAsDefaultSource() {
941+
940942
return Stream.of( //
941943
Arguments.of("INSERT INTO FOO(A) VALUES('A')"), //
942944
Arguments.of("INSERT INTO randomsecondTable(A,B,C,D) VALUES('A','B','C','D')") //
943945
);
944946
}
945947

946948
public static Stream<Arguments> mergeStatementWorksWithJSqlParserSource() {
947-
return Stream.of(Arguments.of(
948-
"merge into a using (select id, value from b) query on (a.id = query.id) when matched then update set a.value = value",
949-
"query"),
949+
950+
return Stream.of( //
951+
Arguments.of(
952+
"merge into a using (select id, value from b) query on (a.id = query.id) when matched then update set a.value = value",
953+
"query"),
950954
Arguments.of(
951955
"merge into a using (select id2, value from b) on (id = id2) when matched then update set a.value = value",
952956
null));

Diff for: spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -684,11 +684,9 @@ List<String> findAllAndSortByFunctionResultNamedParameter(@Param("namedParameter
684684

685685
// GH-2641
686686
@Modifying(clearAutomatically = true)
687-
@Query(value = "merge into sd_user " +
688-
"using (select id from sd_user where age < 30) request " +
689-
"on (sd_user.id = request.id) " +
690-
"when matched then " +
691-
" update set sd_user.age = 30",
687+
@Query(
688+
value = "merge into sd_user " + "using (select id from sd_user where age < 30) request "
689+
+ "on (sd_user.id = request.id) " + "when matched then " + " update set sd_user.age = 30",
692690
nativeQuery = true)
693691
int mergeNativeStatement();
694692

0 commit comments

Comments
 (0)