Skip to content

Commit 82470e5

Browse files
authored
feature/fix: parsing inserts/updates/delete within CTEs (#2055)
* feature parsing inserts/updates/delete within CTEs * removing System lines * fixing codacy issues * reducing the looping in NestedBracketsPerformanceTest to just 6 * formatting fixes via spotlessApply
1 parent 21c605e commit 82470e5

36 files changed

+1313
-213
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ public <S> T visit(GeometryDistance geometryDistance, S context) {
711711
public <S> T visit(Select select, S context) {
712712
if (selectVisitor != null) {
713713
if (select.getWithItemsList() != null) {
714-
for (WithItem item : select.getWithItemsList()) {
714+
for (WithItem<?> item : select.getWithItemsList()) {
715715
item.accept(selectVisitor, context);
716716
}
717717
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement;
11+
12+
import net.sf.jsqlparser.expression.Alias;
13+
14+
public interface ParenthesedStatement extends Statement {
15+
16+
<T, S> T accept(StatementVisitor<T> statementVisitor, S context);
17+
18+
default void accept(StatementVisitor<?> statementVisitor) {
19+
this.accept(statementVisitor, null);
20+
}
21+
22+
Alias getAlias();
23+
24+
void setAlias(Alias alias);
25+
26+
default ParenthesedStatement withAlias(Alias alias) {
27+
setAlias(alias);
28+
return this;
29+
}
30+
31+
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@
2424
import net.sf.jsqlparser.statement.create.view.AlterView;
2525
import net.sf.jsqlparser.statement.create.view.CreateView;
2626
import net.sf.jsqlparser.statement.delete.Delete;
27+
import net.sf.jsqlparser.statement.delete.ParenthesedDelete;
2728
import net.sf.jsqlparser.statement.drop.Drop;
2829
import net.sf.jsqlparser.statement.execute.Execute;
2930
import net.sf.jsqlparser.statement.grant.Grant;
3031
import net.sf.jsqlparser.statement.insert.Insert;
32+
import net.sf.jsqlparser.statement.insert.ParenthesedInsert;
3133
import net.sf.jsqlparser.statement.merge.Merge;
3234
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
3335
import net.sf.jsqlparser.statement.select.Select;
3436
import net.sf.jsqlparser.statement.show.ShowIndexStatement;
3537
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
3638
import net.sf.jsqlparser.statement.truncate.Truncate;
39+
import net.sf.jsqlparser.statement.update.ParenthesedUpdate;
3740
import net.sf.jsqlparser.statement.update.Update;
3841
import net.sf.jsqlparser.statement.upsert.Upsert;
3942

@@ -302,4 +305,23 @@ default void visit(AlterSystemStatement alterSystemStatement) {
302305
default void visit(UnsupportedStatement unsupportedStatement) {
303306
this.visit(unsupportedStatement, null);
304307
}
308+
309+
<S> T visit(ParenthesedInsert parenthesedInsert, S context);
310+
311+
default void visit(ParenthesedInsert parenthesedInsert) {
312+
this.visit(parenthesedInsert, null);
313+
}
314+
315+
<S> T visit(ParenthesedUpdate parenthesedUpdate, S context);
316+
317+
default void visit(ParenthesedUpdate parenthesedUpdate) {
318+
this.visit(parenthesedUpdate, null);
319+
}
320+
321+
<S> T visit(ParenthesedDelete parenthesedDelete, S context);
322+
323+
default void visit(ParenthesedDelete parenthesedDelete) {
324+
this.visit(parenthesedDelete, null);
325+
}
326+
305327
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitorAdapter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@
2424
import net.sf.jsqlparser.statement.create.view.AlterView;
2525
import net.sf.jsqlparser.statement.create.view.CreateView;
2626
import net.sf.jsqlparser.statement.delete.Delete;
27+
import net.sf.jsqlparser.statement.delete.ParenthesedDelete;
2728
import net.sf.jsqlparser.statement.drop.Drop;
2829
import net.sf.jsqlparser.statement.execute.Execute;
2930
import net.sf.jsqlparser.statement.grant.Grant;
3031
import net.sf.jsqlparser.statement.insert.Insert;
32+
import net.sf.jsqlparser.statement.insert.ParenthesedInsert;
3133
import net.sf.jsqlparser.statement.merge.Merge;
3234
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
3335
import net.sf.jsqlparser.statement.select.Select;
3436
import net.sf.jsqlparser.statement.show.ShowIndexStatement;
3537
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
3638
import net.sf.jsqlparser.statement.truncate.Truncate;
39+
import net.sf.jsqlparser.statement.update.ParenthesedUpdate;
3740
import net.sf.jsqlparser.statement.update.Update;
3841
import net.sf.jsqlparser.statement.upsert.Upsert;
3942

@@ -64,18 +67,36 @@ public <S> T visit(Delete delete, S context) {
6467
return null;
6568
}
6669

70+
@Override
71+
public <S> T visit(ParenthesedDelete delete, S context) {
72+
73+
return null;
74+
}
75+
6776
@Override
6877
public <S> T visit(Update update, S context) {
6978

7079
return null;
7180
}
7281

82+
@Override
83+
public <S> T visit(ParenthesedUpdate update, S context) {
84+
85+
return null;
86+
}
87+
7388
@Override
7489
public <S> T visit(Insert insert, S context) {
7590

7691
return null;
7792
}
7893

94+
@Override
95+
public <S> T visit(ParenthesedInsert insert, S context) {
96+
97+
return null;
98+
}
99+
79100
@Override
80101
public <S> T visit(Drop drop, S context) {
81102

src/main/java/net/sf/jsqlparser/statement/delete/Delete.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
public class Delete implements Statement {
3535

36-
private List<WithItem> withItemsList;
36+
private List<WithItem<?>> withItemsList;
3737
private Table table;
3838
private OracleHint oracleHint = null;
3939
private List<Table> tables;
@@ -67,28 +67,28 @@ public Delete setReturningClause(ReturningClause returningClause) {
6767
return this;
6868
}
6969

70-
public List<WithItem> getWithItemsList() {
70+
public List<WithItem<?>> getWithItemsList() {
7171
return withItemsList;
7272
}
7373

74-
public void setWithItemsList(List<WithItem> withItemsList) {
74+
public void setWithItemsList(List<WithItem<?>> withItemsList) {
7575
this.withItemsList = withItemsList;
7676
}
7777

78-
public Delete withWithItemsList(List<WithItem> withItemsList) {
78+
public Delete withWithItemsList(List<WithItem<?>> withItemsList) {
7979
this.setWithItemsList(withItemsList);
8080
return this;
8181
}
8282

83-
public Delete addWithItemsList(WithItem... withItemsList) {
84-
List<WithItem> collection =
83+
public Delete addWithItemsList(WithItem<?>... withItemsList) {
84+
List<WithItem<?>> collection =
8585
Optional.ofNullable(getWithItemsList()).orElseGet(ArrayList::new);
8686
Collections.addAll(collection, withItemsList);
8787
return this.withWithItemsList(collection);
8888
}
8989

90-
public Delete addWithItemsList(Collection<? extends WithItem> withItemsList) {
91-
List<WithItem> collection =
90+
public Delete addWithItemsList(Collection<? extends WithItem<?>> withItemsList) {
91+
List<WithItem<?>> collection =
9292
Optional.ofNullable(getWithItemsList()).orElseGet(ArrayList::new);
9393
collection.addAll(withItemsList);
9494
return this.withWithItemsList(collection);
@@ -177,8 +177,8 @@ public String toString() {
177177
StringBuilder b = new StringBuilder();
178178
if (withItemsList != null && !withItemsList.isEmpty()) {
179179
b.append("WITH ");
180-
for (Iterator<WithItem> iter = withItemsList.iterator(); iter.hasNext();) {
181-
WithItem withItem = iter.next();
180+
for (Iterator<WithItem<?>> iter = withItemsList.iterator(); iter.hasNext();) {
181+
WithItem<?> withItem = iter.next();
182182
b.append(withItem);
183183
if (iter.hasNext()) {
184184
b.append(",");
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2023 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.delete;
11+
12+
import net.sf.jsqlparser.expression.Alias;
13+
import net.sf.jsqlparser.statement.ParenthesedStatement;
14+
import net.sf.jsqlparser.statement.StatementVisitor;
15+
16+
public class ParenthesedDelete extends Delete implements ParenthesedStatement {
17+
18+
Alias alias;
19+
Delete delete;
20+
21+
@Override
22+
public Alias getAlias() {
23+
return alias;
24+
}
25+
26+
@Override
27+
public void setAlias(Alias alias) {
28+
this.alias = alias;
29+
}
30+
31+
public ParenthesedDelete withAlias(Alias alias) {
32+
this.setAlias(alias);
33+
return this;
34+
}
35+
36+
public Delete getDelete() {
37+
return delete;
38+
}
39+
40+
public void setDelete(Delete delete) {
41+
this.delete = delete;
42+
}
43+
44+
public ParenthesedDelete withDelete(Delete delete) {
45+
setDelete(delete);
46+
return this;
47+
}
48+
49+
public String toString() {
50+
StringBuilder builder = new StringBuilder();
51+
builder.append("(").append(delete).append(")");
52+
if (alias != null) {
53+
builder.append(alias);
54+
}
55+
return builder.toString();
56+
}
57+
58+
@Override
59+
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
60+
return statementVisitor.visit(this, context);
61+
}
62+
}

src/main/java/net/sf/jsqlparser/statement/insert/Insert.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class Insert implements Statement {
4343
private boolean modifierIgnore = false;
4444
private ReturningClause returningClause;
4545
private List<UpdateSet> setUpdateSets = null;
46-
private List<WithItem> withItemsList;
46+
private List<WithItem<?>> withItemsList;
4747
private OutputClause outputClause;
4848
private InsertConflictTarget conflictTarget;
4949
private InsertConflictAction conflictAction;
@@ -168,11 +168,11 @@ public boolean isUseSet() {
168168
return setUpdateSets != null && !setUpdateSets.isEmpty();
169169
}
170170

171-
public List<WithItem> getWithItemsList() {
171+
public List<WithItem<?>> getWithItemsList() {
172172
return withItemsList;
173173
}
174174

175-
public void setWithItemsList(List<WithItem> withItemsList) {
175+
public void setWithItemsList(List<WithItem<?>> withItemsList) {
176176
this.withItemsList = withItemsList;
177177
}
178178

@@ -221,8 +221,8 @@ public String toString() {
221221
StringBuilder sql = new StringBuilder();
222222
if (withItemsList != null && !withItemsList.isEmpty()) {
223223
sql.append("WITH ");
224-
for (Iterator<WithItem> iter = withItemsList.iterator(); iter.hasNext();) {
225-
WithItem withItem = iter.next();
224+
for (Iterator<WithItem<?>> iter = withItemsList.iterator(); iter.hasNext();) {
225+
WithItem<?> withItem = iter.next();
226226
sql.append(withItem);
227227
if (iter.hasNext()) {
228228
sql.append(",");
@@ -293,7 +293,7 @@ public String toString() {
293293
return sql.toString();
294294
}
295295

296-
public Insert withWithItemsList(List<WithItem> withList) {
296+
public Insert withWithItemsList(List<WithItem<?>> withList) {
297297
this.withItemsList = withList;
298298
return this;
299299
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2023 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.insert;
11+
12+
import net.sf.jsqlparser.expression.Alias;
13+
import net.sf.jsqlparser.statement.ParenthesedStatement;
14+
import net.sf.jsqlparser.statement.StatementVisitor;
15+
16+
public class ParenthesedInsert extends Insert implements ParenthesedStatement {
17+
Alias alias;
18+
Insert insert;
19+
20+
@Override
21+
public Alias getAlias() {
22+
return alias;
23+
}
24+
25+
@Override
26+
public void setAlias(Alias alias) {
27+
this.alias = alias;
28+
}
29+
30+
public ParenthesedInsert withAlias(Alias alias) {
31+
this.setAlias(alias);
32+
return this;
33+
}
34+
35+
public Insert getInsert() {
36+
return insert;
37+
}
38+
39+
public void setInsert(Insert insert) {
40+
this.insert = insert;
41+
}
42+
43+
public ParenthesedInsert withInsert(Insert insert) {
44+
setInsert(insert);
45+
return this;
46+
}
47+
48+
public String toString() {
49+
StringBuilder builder = new StringBuilder();
50+
builder.append("(").append(insert).append(")");
51+
if (alias != null) {
52+
builder.append(alias);
53+
}
54+
return builder.toString();
55+
}
56+
57+
@Override
58+
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
59+
return statementVisitor.visit(this, context);
60+
}
61+
}

0 commit comments

Comments
 (0)