Skip to content

Commit abd0c85

Browse files
committed
Upgrading to jsqlparser 4.9
Closes #1799
1 parent f1cbdd8 commit abd0c85

File tree

6 files changed

+26
-46
lines changed

6 files changed

+26
-46
lines changed

spring-data-relational/pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<properties>
2121
<java-module-name>spring.data.relational</java-module-name>
2222
<project.root>${basedir}/..</project.root>
23+
<jsqlparser>4.9</jsqlparser>
2324
</properties>
2425

2526
<dependencies>
@@ -100,7 +101,7 @@
100101
<dependency>
101102
<groupId>com.github.jsqlparser</groupId>
102103
<artifactId>jsqlparser</artifactId>
103-
<version>4.6</version>
104+
<version>${jsqlparser}</version>
104105
<scope>test</scope>
105106
</dependency>
106107

spring-data-relational/src/test/java/org/springframework/data/relational/core/sqlgeneration/AliasedPattern.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,21 @@
1616

1717
package org.springframework.data.relational.core.sqlgeneration;
1818

19-
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
2019
import net.sf.jsqlparser.statement.select.SelectItem;
2120

2221
/**
2322
* Matches an expression with an alias.
2423
*
2524
* @param pattern for the expression to match
2625
* @param alias to match
27-
*
2826
* @author Jens Schauder
2927
*/
30-
record AliasedPattern (SelectItemPattern pattern, String alias) implements SelectItemPattern {
28+
record AliasedPattern(SelectItemPattern pattern, String alias) implements SelectItemPattern {
3129

3230
@Override
3331
public boolean matches(SelectItem selectItem) {
34-
35-
if (selectItem instanceof SelectExpressionItem sei) {
36-
return pattern.matches(sei) && sei.getAlias() != null && sei.getAlias().getName().equals(alias);
37-
}
38-
39-
return false;
32+
return pattern.matches(selectItem) && selectItem.getAlias() != null
33+
&& selectItem.getAlias().getName().equals(alias);
4034
}
4135

4236
@Override

spring-data-relational/src/test/java/org/springframework/data/relational/core/sqlgeneration/AnalyticFunctionPattern.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import net.sf.jsqlparser.expression.AnalyticExpression;
2020
import net.sf.jsqlparser.expression.Expression;
21-
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
2221
import net.sf.jsqlparser.statement.select.SelectItem;
2322

2423
import java.util.List;
@@ -44,19 +43,17 @@ public AnalyticFunctionPattern(String rowNumber, ExpressionPattern partitionBy)
4443
@Override
4544
public boolean matches(SelectItem selectItem) {
4645

47-
if (selectItem instanceof SelectExpressionItem sei) {
48-
Expression expression = sei.getExpression();
49-
if (expression instanceof AnalyticExpression analyticExpression) {
50-
return matches(analyticExpression);
51-
}
46+
Expression expression = selectItem.getExpression();
47+
if (expression instanceof AnalyticExpression analyticExpression) {
48+
return matches(analyticExpression);
5249
}
50+
5351
return false;
5452
}
5553

5654
@Override
5755
boolean matches(AnalyticExpression analyticExpression) {
58-
return analyticExpression.getName().toLowerCase().equals(functionName)
59-
&& partitionByMatches(analyticExpression);
56+
return analyticExpression.getName().toLowerCase().equals(functionName) && partitionByMatches(analyticExpression);
6057
}
6158

6259
private boolean partitionByMatches(AnalyticExpression analyticExpression) {

spring-data-relational/src/test/java/org/springframework/data/relational/core/sqlgeneration/LiteralPattern.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.data.relational.core.sqlgeneration;
1818

1919
import net.sf.jsqlparser.expression.Expression;
20-
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
2120
import net.sf.jsqlparser.statement.select.SelectItem;
2221

2322
/**
@@ -30,7 +29,7 @@ record LiteralPattern(Object value) implements SelectItemPattern, ExpressionPatt
3029

3130
@Override
3231
public boolean matches(SelectItem selectItem) {
33-
return selectItem instanceof SelectExpressionItem sei && matches(sei.getExpression());
32+
return matches(selectItem.getExpression());
3433
}
3534

3635
@Override

spring-data-relational/src/test/java/org/springframework/data/relational/core/sqlgeneration/SqlAssert.java

+12-19
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import net.sf.jsqlparser.statement.select.PlainSelect;
2828
import net.sf.jsqlparser.statement.select.Select;
2929
import net.sf.jsqlparser.statement.select.SelectItem;
30-
import net.sf.jsqlparser.statement.select.SpecialSubSelect;
31-
import net.sf.jsqlparser.statement.select.SubSelect;
3230

3331
import java.util.ArrayList;
3432
import java.util.Arrays;
@@ -84,10 +82,11 @@ static AnalyticFunctionPattern count(ExpressionPattern partitionBy) {
8482
return new AnalyticFunctionPattern("count", partitionBy);
8583
}
8684

87-
static FunctionPattern func(String name, ExpressionPattern ... params) {
85+
static FunctionPattern func(String name, ExpressionPattern... params) {
8886
return new FunctionPattern(name, params);
8987
}
90-
static FunctionPattern func(String name, String ... params) {
88+
89+
static FunctionPattern func(String name, String... params) {
9190
return new FunctionPattern(name, Arrays.stream(params).map(p -> col(p)).collect(Collectors.toList()));
9291
}
9392

@@ -104,7 +103,7 @@ SqlAssert hasExactlyColumns(String... columns) {
104103

105104
SqlAssert hasExactlyColumns(SelectItemPattern... columns) {
106105

107-
List<SelectItem> actualSelectItems = actual.getSelectItems();
106+
List<SelectItem<?>> actualSelectItems = actual.getSelectItems();
108107
List<SelectItemPattern> unmatchedPatterns = new ArrayList<>(Arrays.asList(columns));
109108
List<SelectItem> unmatchedSelectItems = new ArrayList<>();
110109

@@ -169,23 +168,26 @@ public StringAssert extractWhereClause() {
169168
Expression where = actual.getWhere();
170169
return new StringAssert(where == null ? "" : where.toString());
171170
}
171+
172172
public JoinAssert hasJoin() {
173173
List<Join> joins = actual.getJoins();
174174

175175
if (joins == null || joins.size() < 1) {
176-
throw failureWithActualExpected(actual, "select with a join", "Expected %s to contain a join but it doesn't.", actual);
176+
throw failureWithActualExpected(actual, "select with a join", "Expected %s to contain a join but it doesn't.",
177+
actual);
177178
}
178179

179180
return new JoinAssert(joins.get(0));
180181
}
182+
181183
private String prepare(SelectItemPattern[] columns) {
182184
return Arrays.toString(columns);
183185
}
184186

185187
SqlAssert hasInlineViewSelectingFrom(String tableName) {
186188

187189
Optional<PlainSelect> matchingSelect = getSubSelects(actual)
188-
.filter(ps -> (ps.getFromItem()instanceof Table t) && t.getName().equals(tableName)).findFirst();
190+
.filter(ps -> (ps.getFromItem() instanceof Table t) && t.getName().equals(tableName)).findFirst();
189191

190192
if (matchingSelect.isEmpty()) {
191193
throw failureWithActualExpected(actual, "Subselect from " + tableName,
@@ -195,13 +197,11 @@ SqlAssert hasInlineViewSelectingFrom(String tableName) {
195197
return new SqlAssert(matchingSelect.get());
196198
}
197199

198-
199200
public SqlAssert hasInlineView() {
200201
Optional<PlainSelect> matchingSelect = getSubSelects(actual).findFirst();
201202

202203
if (matchingSelect.isEmpty()) {
203-
throw failureWithActualExpected(actual, "Subselect",
204-
"%s is expected to contain a subselect", actual);
204+
throw failureWithActualExpected(actual, "Subselect", "%s is expected to contain a subselect", actual);
205205
}
206206

207207
return new SqlAssert(matchingSelect.get());
@@ -227,15 +227,8 @@ private static Stream<PlainSelect> getSubSelects(PlainSelect select) {
227227
}
228228

229229
private static Stream<PlainSelect> subSelects(FromItem fromItem) {
230-
Stream<PlainSelect> fromStream;
231-
if (fromItem instanceof SubSelect ss) {
232-
fromStream = Stream.of((PlainSelect) ss.getSelectBody());
233-
} else if (fromItem instanceof SpecialSubSelect ss) {
234-
fromStream = Stream.of((PlainSelect) ss.getSubSelect().getSelectBody());
235-
} else {
236-
fromStream = Stream.empty();
237-
}
238-
return fromStream;
230+
231+
return fromItem instanceof Select ss ? Stream.of(ss.getPlainSelect()) : Stream.empty();
239232
}
240233

241234
public StringAssert extractOrderBy() {

spring-data-relational/src/test/java/org/springframework/data/relational/core/sqlgeneration/TypedExpressionPattern.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.data.relational.core.sqlgeneration;
1818

1919
import net.sf.jsqlparser.expression.Expression;
20-
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
2120
import net.sf.jsqlparser.statement.select.SelectItem;
2221

2322
/**
@@ -33,15 +32,12 @@ abstract class TypedExpressionPattern<T> implements SelectItemPattern, Expressio
3332

3433
this.type = type;
3534
}
35+
3636
@Override
3737
public boolean matches(SelectItem selectItem) {
3838

39-
if (selectItem instanceof SelectExpressionItem sei) {
40-
41-
Expression expression = sei.getExpression();
42-
return matches(expression);
43-
}
44-
return false;
39+
Expression expression = selectItem.getExpression();
40+
return matches(expression);
4541
}
4642

4743
@Override

0 commit comments

Comments
 (0)