Skip to content

Commit 288b177

Browse files
feat: Consolidate the ExpressionList, removing many redundant List alike Classes and Productions
- `ExpressionList` extends a `List<Expression>` directly and implements `Expression` - `ExpressionList` has no Brackets - introduce `ParenthesedExpressionList` which extends `ExpressionList` and has Brackets - refactor `MultiExpressionList` to extend `List<ExpressionList>` - replace any occurrence of `List<Expression>` with `ExpressionList` and remove lots of redundant Productions - `RowConstructor` extends `ExpressionList` - remove redundant `ValueExpressionList` (it was just an `ExpressionList` - get rid of any `useBrackets` flags - consolidate the `Cast` Functions - use `ExpressionListDeparser` as much as possible BREAKING-CHANGE: All `List<Expression>` and `List<Column>` related methods have changed. No `useBrackets` flags, instead use `ParenthesedExpressionList` when brackets are needed.
1 parent a5140c7 commit 288b177

66 files changed

Lines changed: 1143 additions & 1626 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/net/sf/jsqlparser/expression/AnalyticExpression.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
*/
1010
package net.sf.jsqlparser.expression;
1111

12-
import java.util.List;
13-
import static java.util.stream.Collectors.joining;
1412
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
1513
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
1614
import net.sf.jsqlparser.statement.select.OrderByElement;
1715

16+
import java.util.List;
17+
18+
import static java.util.stream.Collectors.joining;
19+
1820
/**
19-
* Analytic function. The name of the function is variable but the parameters following the special analytic function
20-
* path. e.g. row_number() over (order by test). Additional there can be an expression for an analytical aggregate like
21-
* sum(col) or the "all collumns" wildcard like count(*).
21+
* Analytic function. The name of the function is variable but the parameters following the special
22+
* analytic function path. e.g. row_number() over (order by test). Additional there can be an
23+
* expression for an analytical aggregate like sum(col) or the "all collumns" wildcard like
24+
* count(*).
2225
*
2326
* @author tw
2427
*/
@@ -33,15 +36,15 @@ public class AnalyticExpression extends ASTNodeAccessImpl implements Expression
3336
private AnalyticType type = AnalyticType.OVER;
3437
private boolean distinct = false;
3538
private boolean unique = false;
36-
private boolean ignoreNulls = false; //IGNORE NULLS inside function parameters
37-
private boolean ignoreNullsOutside = false; //IGNORE NULLS outside function parameters
39+
private boolean ignoreNulls = false; // IGNORE NULLS inside function parameters
40+
private boolean ignoreNullsOutside = false; // IGNORE NULLS outside function parameters
3841
private Expression filterExpression = null;
3942
private List<OrderByElement> funcOrderBy = null;
40-
private String windowName = null; // refers to an external window definition (paritionBy, orderBy, windowElement)
43+
private String windowName = null; // refers to an external window definition (paritionBy,
44+
// orderBy, windowElement)
4145
private WindowDefinition windowDef = new WindowDefinition();
4246

43-
public AnalyticExpression() {
44-
}
47+
public AnalyticExpression() {}
4548

4649
public AnalyticExpression(Function function) {
4750
name = function.getName();
@@ -50,18 +53,19 @@ public AnalyticExpression(Function function) {
5053
unique = function.isUnique();
5154
funcOrderBy = function.getOrderByElements();
5255

53-
ExpressionList list = function.getParameters();
56+
ExpressionList<Expression> list = function.getParameters();
5457
if (list != null) {
5558
if (list.getExpressions().size() > 3) {
56-
throw new IllegalArgumentException("function object not valid to initialize analytic expression");
59+
throw new IllegalArgumentException(
60+
"function object not valid to initialize analytic expression");
5761
}
5862

59-
expression = list.getExpressions().get(0);
63+
expression = list.get(0);
6064
if (list.getExpressions().size() > 1) {
61-
offset = list.getExpressions().get(1);
65+
offset = list.get(1);
6266
}
6367
if (list.getExpressions().size() > 2) {
64-
defaultValue = list.getExpressions().get(2);
68+
defaultValue = list.get(2);
6569
}
6670
}
6771
ignoreNulls = function.isIgnoreNulls();
@@ -97,7 +101,8 @@ public void setPartitionExpressionList(ExpressionList partitionExpressionList) {
97101
setPartitionExpressionList(partitionExpressionList, false);
98102
}
99103

100-
public void setPartitionExpressionList(ExpressionList partitionExpressionList, boolean brackets) {
104+
public void setPartitionExpressionList(ExpressionList partitionExpressionList,
105+
boolean brackets) {
101106
windowDef.partitionBy.setPartitionExpressionList(partitionExpressionList, brackets);
102107
}
103108

@@ -202,7 +207,8 @@ public void setWindowDefinition(WindowDefinition windowDef) {
202207
}
203208

204209
@Override
205-
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity", "PMD.MissingBreakInSwitch"})
210+
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity",
211+
"PMD.MissingBreakInSwitch"})
206212
public String toString() {
207213
StringBuilder b = new StringBuilder();
208214

@@ -266,7 +272,7 @@ public String toString() {
266272

267273
if (windowName != null) {
268274
b.append(" ").append(windowName);
269-
} else if (type!=AnalyticType.WITHIN_GROUP_OVER) {
275+
} else if (type != AnalyticType.WITHIN_GROUP_OVER) {
270276
b.append(" ");
271277
b.append(windowDef.toString());
272278
}

src/main/java/net/sf/jsqlparser/expression/CastExpression.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,41 @@
1111

1212
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
1313
import net.sf.jsqlparser.statement.create.table.ColDataType;
14+
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
15+
import net.sf.jsqlparser.statement.select.Select;
16+
17+
import java.util.ArrayList;
1418

1519
public class CastExpression extends ASTNodeAccessImpl implements Expression {
20+
public String keyword;
1621

1722
private Expression leftExpression;
18-
private ColDataType type;
19-
private RowConstructor rowConstructor;
23+
private ColDataType colDataType = null;
24+
private ArrayList<ColumnDefinition> columnDefinitions = new ArrayList<>();
2025
private boolean useCastKeyword = true;
21-
22-
public RowConstructor getRowConstructor() {
23-
return rowConstructor;
24-
}
25-
26-
public void setRowConstructor(RowConstructor rowConstructor) {
27-
this.rowConstructor = rowConstructor;
28-
this.type = null;
29-
}
30-
31-
public CastExpression withRowConstructor(RowConstructor rowConstructor) {
32-
setRowConstructor(rowConstructor);
33-
return this;
26+
27+
public CastExpression(String keyword) {
28+
this.keyword = keyword;
29+
}
30+
31+
public CastExpression() {
32+
this("CAST");
33+
}
34+
35+
public ColDataType getColDataType() {
36+
return colDataType;
37+
}
38+
39+
public ArrayList<ColumnDefinition> getColumnDefinitions() {
40+
return columnDefinitions;
3441
}
3542

36-
public ColDataType getType() {
37-
return type;
43+
public void setColDataType(ColDataType colDataType) {
44+
this.colDataType = colDataType;
3845
}
3946

40-
public void setType(ColDataType type) {
41-
this.type = type;
42-
this.rowConstructor = null;
47+
public void addColumnDefinition(ColumnDefinition columnDefinition) {
48+
this.columnDefinitions.add(columnDefinition);
4349
}
4450

4551
public Expression getLeftExpression() {
@@ -66,16 +72,17 @@ public void setUseCastKeyword(boolean useCastKeyword) {
6672
@Override
6773
public String toString() {
6874
if (useCastKeyword) {
69-
return rowConstructor!=null
70-
? "CAST(" + leftExpression + " AS " + rowConstructor.toString() + ")"
71-
: "CAST(" + leftExpression + " AS " + type.toString() + ")";
75+
return columnDefinitions.size() > 1
76+
? keyword + "(" + leftExpression + " AS ROW("
77+
+ Select.getStringList(columnDefinitions) + "))"
78+
: keyword + "(" + leftExpression + " AS " + colDataType.toString() + ")";
7279
} else {
73-
return leftExpression + "::" + type.toString();
80+
return leftExpression + "::" + colDataType.toString();
7481
}
7582
}
7683

7784
public CastExpression withType(ColDataType type) {
78-
this.setType(type);
85+
this.setColDataType(type);
7986
return this;
8087
}
8188

src/main/java/net/sf/jsqlparser/expression/DateTimeLiteralExpression.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ public DateTimeLiteralExpression withType(DateTime type) {
5353
}
5454

5555
public enum DateTime {
56-
DATE("DATE")
57-
, TIME("TIME")
58-
, TIMESTAMP("TIMESTAMP")
59-
, TIMESTAMPTZ("TIMESTAMPTZ");
56+
DATE("DATE"), TIME("TIME"), TIMESTAMP("TIMESTAMP"), TIMESTAMPTZ("TIMESTAMPTZ");
6057

6158
public String getDateTime() {
6259
return dateTime;
@@ -68,7 +65,7 @@ public String getDateTime() {
6865
this.dateTime = dateTime;
6966
}
7067

71-
private final DateTime from(String dateTimeStr) {
68+
public static DateTime from(String dateTimeStr) {
7269
return Enum.valueOf(DateTime.class, dateTimeStr.toUpperCase());
7370
}
7471
}

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitor.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.sf.jsqlparser.expression.operators.relational.Between;
2828
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
2929
import net.sf.jsqlparser.expression.operators.relational.ExistsExpression;
30+
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
3031
import net.sf.jsqlparser.expression.operators.relational.FullTextSearch;
3132
import net.sf.jsqlparser.expression.operators.relational.GeometryDistance;
3233
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
@@ -148,10 +149,6 @@ public interface ExpressionVisitor {
148149

149150
void visit(CastExpression cast);
150151

151-
void visit(TryCastExpression cast);
152-
153-
void visit(SafeCastExpression cast);
154-
155152
void visit(Modulo modulo);
156153

157154
void visit(AnalyticExpression aexpr);
@@ -178,9 +175,9 @@ public interface ExpressionVisitor {
178175

179176
void visit(MySQLGroupConcat groupConcat);
180177

181-
void visit(ValueListExpression valueList);
178+
void visit(ExpressionList<?> expressionList);
182179

183-
void visit(RowConstructor rowConstructor);
180+
void visit(RowConstructor<?> rowConstructor);
184181

185182
void visit(RowGetExpression rowGetExpression);
186183

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
5050
import net.sf.jsqlparser.expression.operators.relational.SimilarToExpression;
5151
import net.sf.jsqlparser.schema.Column;
52-
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
5352
import net.sf.jsqlparser.statement.select.AllColumns;
5453
import net.sf.jsqlparser.statement.select.AllTableColumns;
5554
import net.sf.jsqlparser.statement.select.ExpressionListItem;
@@ -88,7 +87,7 @@ public void visit(NullValue value) {
8887
@Override
8988
public void visit(Function function) {
9089
if (function.getParameters() != null) {
91-
function.getParameters().accept(this);
90+
function.getParameters().accept((ItemsListVisitor) this);
9291
}
9392
if (function.getKeep() != null) {
9493
function.getKeep().accept(this);
@@ -198,8 +197,8 @@ public void visit(Between expr) {
198197
}
199198

200199
public void visit(OverlapsCondition overlapsCondition) {
201-
overlapsCondition.getLeft().accept(this);
202-
overlapsCondition.getRight().accept(this);
200+
overlapsCondition.getLeft().accept((ItemsListVisitor) this);
201+
overlapsCondition.getRight().accept((ItemsListVisitor) this);
203202
}
204203

205204

@@ -346,16 +345,6 @@ public void visit(CastExpression expr) {
346345
expr.getLeftExpression().accept(this);
347346
}
348347

349-
@Override
350-
public void visit(TryCastExpression expr) {
351-
expr.getLeftExpression().accept(this);
352-
}
353-
354-
@Override
355-
public void visit(SafeCastExpression expr) {
356-
expr.getLeftExpression().accept(this);
357-
}
358-
359348
@Override
360349
public void visit(Modulo expr) {
361350
visitBinaryExpression(expr);
@@ -406,8 +395,15 @@ public void visit(RegExpMatchOperator expr) {
406395
}
407396

408397
@Override
409-
public void visit(ExpressionList expressionList) {
410-
for (Expression expr : expressionList.getExpressions()) {
398+
public void visit(ExpressionList<?> expressionList) {
399+
for (Expression expr : expressionList) {
400+
expr.accept(this);
401+
}
402+
}
403+
404+
@Override
405+
public void visit(RowConstructor<?> rowConstructor) {
406+
for (Expression expr : rowConstructor) {
411407
expr.accept(this);
412408
}
413409
}
@@ -420,8 +416,8 @@ public void visit(NamedExpressionList namedExpressionList) {
420416
}
421417

422418
@Override
423-
public void visit(MultiExpressionList multiExprList) {
424-
for (ExpressionList list : multiExprList.getExprList()) {
419+
public void visit(MultiExpressionList<?> multiExprList) {
420+
for (ExpressionList<?> list : multiExprList.getExprList()) {
425421
visit(list);
426422
}
427423
}
@@ -490,13 +486,6 @@ public void visit(MySQLGroupConcat groupConcat) {
490486
}
491487
}
492488

493-
@Override
494-
public void visit(ValueListExpression valueListExpression) {
495-
for (Expression expr : valueListExpression.getExpressionList().getExpressions()) {
496-
expr.accept(this);
497-
}
498-
}
499-
500489
@Override
501490
public void visit(Pivot pivot) {
502491
for (FunctionItem item : pivot.getFunctionItems()) {
@@ -513,7 +502,7 @@ public void visit(Pivot pivot) {
513502

514503
if (pivot.getMultiInItems() != null) {
515504
for (ExpressionListItem item : pivot.getMultiInItems()) {
516-
item.getExpressionList().accept(this);
505+
item.getExpressionList().accept((ItemsListVisitor) this);
517506
}
518507
}
519508
}
@@ -555,19 +544,6 @@ public void visit(SelectItem selectExpressionItem) {
555544
selectExpressionItem.getExpression().accept(this);
556545
}
557546

558-
@Override
559-
public void visit(RowConstructor rowConstructor) {
560-
if (rowConstructor.getColumnDefinitions().isEmpty()) {
561-
for (Expression expression : rowConstructor.getExprList().getExpressions()) {
562-
expression.accept(this);
563-
}
564-
} else {
565-
for (ColumnDefinition columnDefinition : rowConstructor.getColumnDefinitions()) {
566-
columnDefinition.accept(this);
567-
}
568-
}
569-
}
570-
571547
@Override
572548
public void visit(RowGetExpression rowGetExpression) {
573549
rowGetExpression.getExpression().accept(this);
@@ -693,8 +669,4 @@ public void visit(TranscodingFunction transcodingFunction) {
693669
public void visit(TrimFunction trimFunction) {
694670

695671
}
696-
697-
public void visit(ColumnDefinition columnDefinition) {
698-
columnDefinition.accept(this);
699-
}
700672
}

src/main/java/net/sf/jsqlparser/expression/Function.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
1515
import net.sf.jsqlparser.schema.Column;
1616
import net.sf.jsqlparser.statement.select.OrderByElement;
17-
import net.sf.jsqlparser.statement.select.PlainSelect;
1817

1918
import java.util.Arrays;
2019
import java.util.List;
@@ -206,7 +205,7 @@ public String toString() {
206205
if (isAllColumns()) {
207206
b.append("ALL ");
208207
}
209-
b.append(PlainSelect.getStringList(parameters.getExpressions(), true, false));
208+
b.append(parameters);
210209
if (orderByElements != null) {
211210
b.append(" ORDER BY ");
212211
boolean comma = false;
@@ -274,9 +273,7 @@ public Function withParameters(ExpressionList parameters) {
274273
}
275274

276275
public Function withParameters(Expression... parameters) {
277-
ExpressionList expressionList = new ExpressionList(parameters).withUsingBrackets(false);
278-
this.setParameters(expressionList);
279-
return this;
276+
return withParameters(new ExpressionList(parameters));
280277
}
281278

282279
public Function withNamedParameters(NamedExpressionList namedParameters) {

0 commit comments

Comments
 (0)