Skip to content

Commit c5aa790

Browse files
quaffchristophstrobl
authored andcommitted
Upgrade to JSqlParser 4.8
Closes: #3340 Original Pull Request: #3248
1 parent 6063c73 commit c5aa790

File tree

3 files changed

+28
-40
lines changed

3 files changed

+28
-40
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<hibernate-64-next-snapshots>6.4.3-SNAPSHOT</hibernate-64-next-snapshots>
3535
<hsqldb>2.7.1</hsqldb>
3636
<h2>2.2.220</h2>
37-
<jsqlparser>4.5</jsqlparser>
37+
<jsqlparser>4.8</jsqlparser>
3838
<mysql-connector-java>8.0.33</mysql-connector-java>
3939
<postgresql>42.6.0</postgresql>
4040
<springdata.commons>3.3.0-SNAPSHOT</springdata.commons>

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

+25-37
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@
3131
import net.sf.jsqlparser.statement.select.OrderByElement;
3232
import net.sf.jsqlparser.statement.select.PlainSelect;
3333
import net.sf.jsqlparser.statement.select.Select;
34-
import net.sf.jsqlparser.statement.select.SelectBody;
35-
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
3634
import net.sf.jsqlparser.statement.select.SelectItem;
3735
import net.sf.jsqlparser.statement.select.SetOperationList;
38-
import net.sf.jsqlparser.statement.select.WithItem;
36+
import net.sf.jsqlparser.statement.select.Values;
3937
import net.sf.jsqlparser.statement.update.Update;
40-
import net.sf.jsqlparser.statement.values.ValuesStatement;
4138

4239
import java.util.ArrayList;
4340
import java.util.Collections;
@@ -59,6 +56,7 @@
5956
* @author Diego Krupitza
6057
* @author Greg Turnquist
6158
* @author Geoffrey Deremetz
59+
* @author Yanming Zhou
6260
* @since 2.7.0
6361
*/
6462
public class JSqlParserQueryEnhancer implements QueryEnhancer {
@@ -120,13 +118,13 @@ public String applySorting(Sort sort, @Nullable String alias) {
120118

121119
Select selectStatement = parseSelectStatement(queryString);
122120

123-
if (selectStatement.getSelectBody()instanceof SetOperationList setOperationList) {
121+
if (selectStatement instanceof SetOperationList setOperationList) {
124122
return applySortingToSetOperationList(setOperationList, sort);
125-
} else if (!(selectStatement.getSelectBody() instanceof PlainSelect)) {
126-
return queryString;
127123
}
128124

129-
PlainSelect selectBody = (PlainSelect) selectStatement.getSelectBody();
125+
if (!(selectStatement instanceof PlainSelect selectBody)) {
126+
return queryString;
127+
}
130128

131129
Set<String> joinAliases = getJoinAliases(selectBody);
132130
Set<String> selectionAliases = getSelectionAliases(selectBody);
@@ -155,7 +153,7 @@ public String applySorting(Sort sort, @Nullable String alias) {
155153
private String applySortingToSetOperationList(SetOperationList setOperationListStatement, Sort sort) {
156154

157155
// special case: ValuesStatements are detected as nested OperationListStatements
158-
if (setOperationListStatement.getSelects().stream().anyMatch(ValuesStatement.class::isInstance)) {
156+
if (setOperationListStatement.getSelects().stream().anyMatch(Values.class::isInstance)) {
159157
return setOperationListStatement.toString();
160158
}
161159

@@ -185,8 +183,8 @@ private Set<String> getSelectionAliases(PlainSelect selectBody) {
185183
}
186184

187185
return selectBody.getSelectItems().stream() //
188-
.filter(SelectExpressionItem.class::isInstance) //
189-
.map(item -> ((SelectExpressionItem) item).getAlias()) //
186+
.filter(SelectItem.class::isInstance) //
187+
.map(item -> ((SelectItem) item).getAlias()) //
190188
.filter(Objects::nonNull) //
191189
.map(Alias::getName) //
192190
.collect(Collectors.toSet());
@@ -203,9 +201,7 @@ Set<String> getSelectionAliases() {
203201
return new HashSet<>();
204202
}
205203

206-
Select selectStatement = (Select) statement;
207-
PlainSelect selectBody = (PlainSelect) selectStatement.getSelectBody();
208-
return this.getSelectionAliases(selectBody);
204+
return this.getSelectionAliases((PlainSelect) statement);
209205
}
210206

211207
/**
@@ -221,7 +217,7 @@ private Set<String> getJoinAliases(String query) {
221217
}
222218

223219
Select selectStatement = (Select) statement;
224-
if (selectStatement.getSelectBody()instanceof PlainSelect selectBody) {
220+
if (selectStatement instanceof PlainSelect selectBody) {
225221
return getJoinAliases(selectBody);
226222
}
227223

@@ -319,7 +315,7 @@ private String detectAlias(String query) {
319315
* ValuesStatement has no alias
320316
* SetOperation can have multiple alias for each operation item
321317
*/
322-
if (!(selectStatement.getSelectBody()instanceof PlainSelect selectBody)) {
318+
if (!(selectStatement instanceof PlainSelect selectBody)) {
323319
return null;
324320
}
325321

@@ -374,7 +370,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
374370
/*
375371
We only support count queries for {@link PlainSelect}.
376372
*/
377-
if (!(selectStatement.getSelectBody()instanceof PlainSelect selectBody)) {
373+
if (!(selectStatement instanceof PlainSelect selectBody)) {
378374
return this.query.getQueryString();
379375
}
380376

@@ -384,7 +380,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
384380
if (StringUtils.hasText(countProjection)) {
385381

386382
Function jSqlCount = getJSqlCount(Collections.singletonList(countProjection), false);
387-
selectBody.setSelectItems(Collections.singletonList(new SelectExpressionItem(jSqlCount)));
383+
selectBody.setSelectItems(Collections.singletonList(new SelectItem(jSqlCount)));
388384
return selectBody.toString();
389385
}
390386

@@ -394,34 +390,26 @@ public String createCountQueryFor(@Nullable String countProjection) {
394390
String tableAlias = detectAlias(selectBody);
395391

396392
// is never null
397-
List<SelectItem> selectItems = selectBody.getSelectItems();
393+
List<SelectItem<?>> selectItems = selectBody.getSelectItems();
398394

399395
if (onlyASingleColumnProjection(selectItems)) {
400396

401-
SelectExpressionItem singleProjection = (SelectExpressionItem) selectItems.get(0);
397+
SelectItem<?> singleProjection = (SelectItem<?>) selectItems.get(0);
402398

403399
Column column = (Column) singleProjection.getExpression();
404400
String countProp = column.getFullyQualifiedName();
405401

406402
Function jSqlCount = getJSqlCount(Collections.singletonList(countProp), distinct);
407-
selectBody.setSelectItems(Collections.singletonList(new SelectExpressionItem(jSqlCount)));
403+
selectBody.setSelectItems(Collections.singletonList(new SelectItem<>(jSqlCount)));
408404
return selectBody.toString();
409405
}
410406

411407
String countProp = query.isNativeQuery() ? (distinct ? "*" : "1") : tableAlias == null ? "*" : tableAlias;
412408

413409
Function jSqlCount = getJSqlCount(Collections.singletonList(countProp), distinct);
414-
selectBody.setSelectItems(Collections.singletonList(new SelectExpressionItem(jSqlCount)));
415-
416-
if (CollectionUtils.isEmpty(selectStatement.getWithItemsList())) {
417-
return selectBody.toString();
418-
}
419-
420-
String withStatements = selectStatement.getWithItemsList().stream() //
421-
.map(WithItem::toString) //
422-
.collect(Collectors.joining(","));
410+
selectBody.setSelectItems(Collections.singletonList(new SelectItem(jSqlCount)));
423411

424-
return "with " + withStatements + "\n" + selectBody;
412+
return selectBody.toString();
425413
}
426414

427415
@Override
@@ -435,13 +423,13 @@ public String getProjection() {
435423

436424
Select selectStatement = (Select) statement;
437425

438-
if (selectStatement.getSelectBody() instanceof ValuesStatement) {
426+
if (selectStatement instanceof Values) {
439427
return "";
440428
}
441429

442-
SelectBody selectBody = selectStatement.getSelectBody();
430+
Select selectBody = selectStatement;
443431

444-
if (selectStatement.getSelectBody()instanceof SetOperationList setOperationList) {
432+
if (selectStatement instanceof SetOperationList setOperationList) {
445433

446434
// using the first one since for setoperations the projection has to be the same
447435
selectBody = setOperationList.getSelects().get(0);
@@ -493,11 +481,11 @@ private Select parseSelectStatement(String query) {
493481
* @param projection the projection to analyse
494482
* @return <code>true</code> when the projection only contains a single column definition otherwise <code>false</code>
495483
*/
496-
private boolean onlyASingleColumnProjection(List<SelectItem> projection) {
484+
private boolean onlyASingleColumnProjection(List<SelectItem<?>> projection) {
497485

498486
// this is unfortunately the only way to check without any hacky & hard string regex magic
499-
return projection.size() == 1 && projection.get(0) instanceof SelectExpressionItem
500-
&& (((SelectExpressionItem) projection.get(0)).getExpression()) instanceof Column;
487+
return projection.size() == 1 && projection.get(0) instanceof SelectItem<?>
488+
&& (((SelectItem<?>) projection.get(0)).getExpression()) instanceof Column;
501489
}
502490

503491
@Override

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/JSqlParserQueryEnhancerUnitTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void withStatementsWorks() {
162162
assertThat(stringQuery.hasConstructorExpression()).isFalse();
163163

164164
assertThat(queryEnhancer.createCountQueryFor()).isEqualToIgnoringCase(
165-
"with sample_data (day, value) AS (VALUES ((0, 13), (1, 12), (2, 15), (3, 4), (4, 8), (5, 16)))\n"
165+
"with sample_data (day, value) AS (VALUES ((0, 13), (1, 12), (2, 15), (3, 4), (4, 8), (5, 16))) "
166166
+ "SELECT count(1) FROM sample_data AS a");
167167
assertThat(queryEnhancer.applySorting(Sort.by("day").descending())).endsWith("ORDER BY a.day DESC");
168168
assertThat(queryEnhancer.getJoinAliases()).isEmpty();
@@ -185,7 +185,7 @@ void multipleWithStatementsWorks() {
185185
assertThat(stringQuery.hasConstructorExpression()).isFalse();
186186

187187
assertThat(queryEnhancer.createCountQueryFor()).isEqualToIgnoringCase(
188-
"with sample_data (day, value) AS (VALUES ((0, 13), (1, 12), (2, 15), (3, 4), (4, 8), (5, 16))),test2 AS (VALUES (1, 2, 3))\n"
188+
"with sample_data (day, value) AS (VALUES ((0, 13), (1, 12), (2, 15), (3, 4), (4, 8), (5, 16))), test2 AS (VALUES (1, 2, 3)) "
189189
+ "SELECT count(1) FROM sample_data AS a");
190190
assertThat(queryEnhancer.applySorting(Sort.by("day").descending())).endsWith("ORDER BY a.day DESC");
191191
assertThat(queryEnhancer.getJoinAliases()).isEmpty();

0 commit comments

Comments
 (0)