Skip to content

Commit

Permalink
feature/fix: parsing inserts/updates/delete within CTEs (#2055)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nicky6s authored Aug 4, 2024
1 parent 21c605e commit 82470e5
Show file tree
Hide file tree
Showing 36 changed files with 1,313 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public <S> T visit(GeometryDistance geometryDistance, S context) {
public <S> T visit(Select select, S context) {
if (selectVisitor != null) {
if (select.getWithItemsList() != null) {
for (WithItem item : select.getWithItemsList()) {
for (WithItem<?> item : select.getWithItemsList()) {
item.accept(selectVisitor, context);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement;

import net.sf.jsqlparser.expression.Alias;

public interface ParenthesedStatement extends Statement {

<T, S> T accept(StatementVisitor<T> statementVisitor, S context);

default void accept(StatementVisitor<?> statementVisitor) {
this.accept(statementVisitor, null);
}

Alias getAlias();

void setAlias(Alias alias);

default ParenthesedStatement withAlias(Alias alias) {
setAlias(alias);
return this;
}

}
22 changes: 22 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@
import net.sf.jsqlparser.statement.create.view.AlterView;
import net.sf.jsqlparser.statement.create.view.CreateView;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.delete.ParenthesedDelete;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.grant.Grant;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.insert.ParenthesedInsert;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.show.ShowIndexStatement;
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
import net.sf.jsqlparser.statement.truncate.Truncate;
import net.sf.jsqlparser.statement.update.ParenthesedUpdate;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.upsert.Upsert;

Expand Down Expand Up @@ -302,4 +305,23 @@ default void visit(AlterSystemStatement alterSystemStatement) {
default void visit(UnsupportedStatement unsupportedStatement) {
this.visit(unsupportedStatement, null);
}

<S> T visit(ParenthesedInsert parenthesedInsert, S context);

default void visit(ParenthesedInsert parenthesedInsert) {
this.visit(parenthesedInsert, null);
}

<S> T visit(ParenthesedUpdate parenthesedUpdate, S context);

default void visit(ParenthesedUpdate parenthesedUpdate) {
this.visit(parenthesedUpdate, null);
}

<S> T visit(ParenthesedDelete parenthesedDelete, S context);

default void visit(ParenthesedDelete parenthesedDelete) {
this.visit(parenthesedDelete, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@
import net.sf.jsqlparser.statement.create.view.AlterView;
import net.sf.jsqlparser.statement.create.view.CreateView;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.delete.ParenthesedDelete;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.grant.Grant;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.insert.ParenthesedInsert;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.show.ShowIndexStatement;
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
import net.sf.jsqlparser.statement.truncate.Truncate;
import net.sf.jsqlparser.statement.update.ParenthesedUpdate;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.upsert.Upsert;

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

@Override
public <S> T visit(ParenthesedDelete delete, S context) {

return null;
}

@Override
public <S> T visit(Update update, S context) {

return null;
}

@Override
public <S> T visit(ParenthesedUpdate update, S context) {

return null;
}

@Override
public <S> T visit(Insert insert, S context) {

return null;
}

@Override
public <S> T visit(ParenthesedInsert insert, S context) {

return null;
}

@Override
public <S> T visit(Drop drop, S context) {

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/net/sf/jsqlparser/statement/delete/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class Delete implements Statement {

private List<WithItem> withItemsList;
private List<WithItem<?>> withItemsList;
private Table table;
private OracleHint oracleHint = null;
private List<Table> tables;
Expand Down Expand Up @@ -67,28 +67,28 @@ public Delete setReturningClause(ReturningClause returningClause) {
return this;
}

public List<WithItem> getWithItemsList() {
public List<WithItem<?>> getWithItemsList() {
return withItemsList;
}

public void setWithItemsList(List<WithItem> withItemsList) {
public void setWithItemsList(List<WithItem<?>> withItemsList) {
this.withItemsList = withItemsList;
}

public Delete withWithItemsList(List<WithItem> withItemsList) {
public Delete withWithItemsList(List<WithItem<?>> withItemsList) {
this.setWithItemsList(withItemsList);
return this;
}

public Delete addWithItemsList(WithItem... withItemsList) {
List<WithItem> collection =
public Delete addWithItemsList(WithItem<?>... withItemsList) {
List<WithItem<?>> collection =
Optional.ofNullable(getWithItemsList()).orElseGet(ArrayList::new);
Collections.addAll(collection, withItemsList);
return this.withWithItemsList(collection);
}

public Delete addWithItemsList(Collection<? extends WithItem> withItemsList) {
List<WithItem> collection =
public Delete addWithItemsList(Collection<? extends WithItem<?>> withItemsList) {
List<WithItem<?>> collection =
Optional.ofNullable(getWithItemsList()).orElseGet(ArrayList::new);
collection.addAll(withItemsList);
return this.withWithItemsList(collection);
Expand Down Expand Up @@ -177,8 +177,8 @@ public String toString() {
StringBuilder b = new StringBuilder();
if (withItemsList != null && !withItemsList.isEmpty()) {
b.append("WITH ");
for (Iterator<WithItem> iter = withItemsList.iterator(); iter.hasNext();) {
WithItem withItem = iter.next();
for (Iterator<WithItem<?>> iter = withItemsList.iterator(); iter.hasNext();) {
WithItem<?> withItem = iter.next();
b.append(withItem);
if (iter.hasNext()) {
b.append(",");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2023 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.delete;

import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.statement.ParenthesedStatement;
import net.sf.jsqlparser.statement.StatementVisitor;

public class ParenthesedDelete extends Delete implements ParenthesedStatement {

Alias alias;
Delete delete;

@Override
public Alias getAlias() {
return alias;
}

@Override
public void setAlias(Alias alias) {
this.alias = alias;
}

public ParenthesedDelete withAlias(Alias alias) {
this.setAlias(alias);
return this;
}

public Delete getDelete() {
return delete;
}

public void setDelete(Delete delete) {
this.delete = delete;
}

public ParenthesedDelete withDelete(Delete delete) {
setDelete(delete);
return this;
}

public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("(").append(delete).append(")");
if (alias != null) {
builder.append(alias);
}
return builder.toString();
}

@Override
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statementVisitor.visit(this, context);
}
}
12 changes: 6 additions & 6 deletions src/main/java/net/sf/jsqlparser/statement/insert/Insert.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Insert implements Statement {
private boolean modifierIgnore = false;
private ReturningClause returningClause;
private List<UpdateSet> setUpdateSets = null;
private List<WithItem> withItemsList;
private List<WithItem<?>> withItemsList;
private OutputClause outputClause;
private InsertConflictTarget conflictTarget;
private InsertConflictAction conflictAction;
Expand Down Expand Up @@ -168,11 +168,11 @@ public boolean isUseSet() {
return setUpdateSets != null && !setUpdateSets.isEmpty();
}

public List<WithItem> getWithItemsList() {
public List<WithItem<?>> getWithItemsList() {
return withItemsList;
}

public void setWithItemsList(List<WithItem> withItemsList) {
public void setWithItemsList(List<WithItem<?>> withItemsList) {
this.withItemsList = withItemsList;
}

Expand Down Expand Up @@ -221,8 +221,8 @@ public String toString() {
StringBuilder sql = new StringBuilder();
if (withItemsList != null && !withItemsList.isEmpty()) {
sql.append("WITH ");
for (Iterator<WithItem> iter = withItemsList.iterator(); iter.hasNext();) {
WithItem withItem = iter.next();
for (Iterator<WithItem<?>> iter = withItemsList.iterator(); iter.hasNext();) {
WithItem<?> withItem = iter.next();
sql.append(withItem);
if (iter.hasNext()) {
sql.append(",");
Expand Down Expand Up @@ -293,7 +293,7 @@ public String toString() {
return sql.toString();
}

public Insert withWithItemsList(List<WithItem> withList) {
public Insert withWithItemsList(List<WithItem<?>> withList) {
this.withItemsList = withList;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2023 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.insert;

import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.statement.ParenthesedStatement;
import net.sf.jsqlparser.statement.StatementVisitor;

public class ParenthesedInsert extends Insert implements ParenthesedStatement {
Alias alias;
Insert insert;

@Override
public Alias getAlias() {
return alias;
}

@Override
public void setAlias(Alias alias) {
this.alias = alias;
}

public ParenthesedInsert withAlias(Alias alias) {
this.setAlias(alias);
return this;
}

public Insert getInsert() {
return insert;
}

public void setInsert(Insert insert) {
this.insert = insert;
}

public ParenthesedInsert withInsert(Insert insert) {
setInsert(insert);
return this;
}

public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("(").append(insert).append(")");
if (alias != null) {
builder.append(alias);
}
return builder.toString();
}

@Override
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statementVisitor.visit(this, context);
}
}
Loading

0 comments on commit 82470e5

Please sign in to comment.