Skip to content

Commit

Permalink
Unparenthesized SubSelect as FromItem (#2073)
Browse files Browse the repository at this point in the history
* feat: Implement FromItem in Select

Implement FromItem interface in Select to allow Select being an unparenthesized FromItem

Refs: #2071

* feat: Implement grammar to support Select as FromItem

Refs: #2071

* style: Run gradle :spotlessApply

* style: Run gradle :spotlessApply

---------

Co-authored-by: Stefan Steinhauser <[email protected]>
  • Loading branch information
ssteinhauser and Stefan Steinhauser authored Sep 10, 2024
1 parent 59593fa commit 60f4d74
Show file tree
Hide file tree
Showing 22 changed files with 184 additions and 44 deletions.
21 changes: 21 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/FromItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,26 @@ default FromItem withUnPivot(UnPivot unpivot) {
return this;
}

default StringBuilder appendTo(StringBuilder builder, Alias alias) {
return appendTo(builder, alias, null, null);
}

default StringBuilder appendTo(StringBuilder builder, Alias alias, Pivot pivot,
UnPivot unPivot) {
if (alias != null) {
builder.append(alias);
}

if (pivot != null) {
builder.append(" ").append(pivot);
}

if (unPivot != null) {
builder.append(" ").append(unPivot);
}

return builder;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ default void visit(ParenthesedFromItem parenthesedFromItem) {
default void visit(Values values) {
this.visit(values, null);
}

<S> T visit(PlainSelect plainSelect, S context);

default void visit(PlainSelect plainSelect) {
this.visit(plainSelect, null);
}

<S> T visit(SetOperationList setOperationList, S context);

default void visit(SetOperationList setOperationList) {
this.visit(setOperationList, null);
}

<S> T visit(TableStatement tableStatement, S context);

default void visit(TableStatement tableStatement) {
this.visit(tableStatement, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,22 @@ public <S> T visit(Values values, S context) {

return null;
}

@Override
public <S> T visit(PlainSelect plainSelect, S context) {

return null;
}

@Override
public <S> T visit(SetOperationList setOperationList, S context) {

return null;
}

@Override
public <S> T visit(TableStatement tableStatement, S context) {

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,7 @@ public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {

public StringBuilder appendSelectBodyTo(StringBuilder builder) {
builder.append("(").append(select).append(")");
if (alias != null) {
builder.append(alias);
}
if (pivot != null) {
builder.append(" ").append(pivot);
}
if (unPivot != null) {
builder.append(" ").append(unPivot);
}
appendTo(builder, alias, pivot, unPivot);
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ public <T, S> T accept(SelectVisitor<T> selectVisitor, S context) {
return selectVisitor.visit(this, context);
}

@Override
public <T, S> T accept(FromItemVisitor<T> fromItemVisitor, S context) {
return fromItemVisitor.visit(this, context);
}

public OptimizeFor getOptimizeFor() {
return optimizeFor;
}
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/net/sf/jsqlparser/statement/select/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
Expand All @@ -23,7 +24,7 @@
import java.util.List;
import java.util.Optional;

public abstract class Select extends ASTNodeAccessImpl implements Statement, Expression {
public abstract class Select extends ASTNodeAccessImpl implements Statement, Expression, FromItem {
protected Table forUpdateTable = null;
List<WithItem<?>> withItemsList;
Limit limitBy;
Expand All @@ -40,6 +41,9 @@ public abstract class Select extends ASTNodeAccessImpl implements Statement, Exp
private boolean skipLocked;
private Wait wait;
private boolean noWait = false;
Alias alias;
Pivot pivot;
UnPivot unPivot;

public static String orderByToString(List<OrderByElement> orderByElements) {
return orderByToString(false, orderByElements);
Expand Down Expand Up @@ -322,6 +326,39 @@ public void setSkipLocked(boolean skipLocked) {
this.skipLocked = skipLocked;
}

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

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

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

@Override
public Pivot getPivot() {
return pivot;
}

@Override
public void setPivot(Pivot pivot) {
this.pivot = pivot;
}

public UnPivot getUnPivot() {
return unPivot;
}

public void setUnPivot(UnPivot unPivot) {
this.unPivot = unPivot;
}

public abstract StringBuilder appendSelectBodyTo(StringBuilder builder);

@SuppressWarnings({"PMD.CyclomaticComplexity"})
Expand Down Expand Up @@ -381,6 +418,8 @@ public StringBuilder appendTo(StringBuilder builder) {
}
}

appendTo(builder, alias, pivot, unPivot);

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public <T, S> T accept(SelectVisitor<T> selectVisitor, S context) {
return selectVisitor.visit(this, context);
}

@Override
public <T, S> T accept(FromItemVisitor<T> fromItemVisitor, S context) {
return fromItemVisitor.visit(this, context);
}

public List<OrderByElement> getOrderByElements() {
return orderByElements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public StringBuilder appendTo(StringBuilder builder) {
public <T, S> T accept(SelectVisitor<T> selectVisitor, S context) {
return selectVisitor.visit(this, context);
}

@Override
public <T, S> T accept(FromItemVisitor<T> fromItemVisitor, S context) {
return fromItemVisitor.visit(this, context);
}
}
4 changes: 1 addition & 3 deletions src/main/java/net/sf/jsqlparser/statement/select/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public void setExpressions(ExpressionList<Expression> expressions) {
public StringBuilder appendSelectBodyTo(StringBuilder builder) {
builder.append("VALUES ");
builder.append(expressions.toString());
if (alias != null) {
builder.append(" ").append(alias);
}
appendTo(builder, alias);
return builder;
}

Expand Down
34 changes: 31 additions & 3 deletions src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public <S> StringBuilder visit(ParenthesedSelect select, S context) {
}

buffer.append("(");
select.getSelect().accept(this, context);
select.getSelect().accept((SelectVisitor<StringBuilder>) this, context);
buffer.append(")");

if (select.getOrderByElements() != null) {
Expand Down Expand Up @@ -330,6 +330,20 @@ public <S> StringBuilder visit(PlainSelect plainSelect, S context) {
if (plainSelect.isUseWithNoLog()) {
buffer.append(" WITH NO LOG");
}

Alias alias = plainSelect.getAlias();
if (alias != null) {
buffer.append(alias);
}
Pivot pivot = plainSelect.getPivot();
if (pivot != null) {
pivot.accept(this, context);
}
UnPivot unpivot = plainSelect.getUnPivot();
if (unpivot != null) {
unpivot.accept(this, context);
}

return buffer;
}

Expand Down Expand Up @@ -618,7 +632,7 @@ public <S> StringBuilder visit(SetOperationList list, S context) {
if (i != 0) {
buffer.append(' ').append(list.getOperations().get(i - 1)).append(' ');
}
list.getSelects().get(i).accept(this, context);
list.getSelects().get(i).accept((SelectVisitor<StringBuilder>) this, context);
}
if (list.getOrderByElements() != null) {
new OrderByDeParser(expressionVisitor, buffer).deParse(list.getOrderByElements());
Expand All @@ -636,6 +650,20 @@ public <S> StringBuilder visit(SetOperationList list, S context) {
if (list.getIsolation() != null) {
buffer.append(list.getIsolation().toString());
}

Alias alias = list.getAlias();
if (alias != null) {
buffer.append(alias);
}
Pivot pivot = list.getPivot();
if (pivot != null) {
pivot.accept(this, context);
}
UnPivot unpivot = list.getUnPivot();
if (unpivot != null) {
unpivot.accept(this, context);
}

return buffer;
}

Expand Down Expand Up @@ -779,7 +807,7 @@ private void deparseOptimizeFor(OptimizeFor optimizeFor) {

@Override
void deParse(PlainSelect statement) {
statement.accept(this, null);
statement.accept((SelectVisitor<StringBuilder>) this, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private <S> StringBuilder addWithItemsToBuffer(List<WithItem<?>> withItemsList,

@Override
public <S> StringBuilder visit(Select select, S context) {
select.accept(selectDeParser, context);
select.accept((SelectVisitor<StringBuilder>) selectDeParser, context);
return buffer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@
package net.sf.jsqlparser.util.deparser;

import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.statement.select.LateralSubSelect;
import net.sf.jsqlparser.statement.select.Offset;
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.statement.select.SetOperationList;
import net.sf.jsqlparser.statement.select.TableStatement;
import net.sf.jsqlparser.statement.select.Values;
import net.sf.jsqlparser.statement.select.WithItem;
import net.sf.jsqlparser.statement.select.*;

/**
* @author jxnu-liguobin
Expand Down Expand Up @@ -101,6 +93,10 @@ public <S> StringBuilder visit(TableStatement tableStatement, S context) {
}

// TODO UNION

tableStatement.appendTo(buffer, tableStatement.getAlias(), tableStatement.getPivot(),
tableStatement.getUnPivot());

return buffer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void deParse(Upsert upsert) {

if (upsert.getSelect() != null) {
buffer.append(" ");
upsert.getSelect().accept(selectVisitor, null);
upsert.getSelect().accept((SelectVisitor<StringBuilder>) selectVisitor, null);
}

if (upsert.getDuplicateUpdateSets() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.statement.create.view.AlterView;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.util.validation.ValidationCapability;
import net.sf.jsqlparser.util.validation.metadata.NamedObject;

Expand All @@ -27,7 +28,8 @@ public void validate(AlterView alterView) {
validateName(c, NamedObject.view, alterView.getView().getFullyQualifiedName());
validateOptionalColumnNames(c, alterView.getColumnNames());
}
alterView.getSelect().accept(getValidator(SelectValidator.class), null);
alterView.getSelect().accept((SelectVisitor<Void>) getValidator(SelectValidator.class),
null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.sf.jsqlparser.statement.create.view.ForceOption;
import net.sf.jsqlparser.statement.create.view.TemporaryOption;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.util.validation.ValidationCapability;
import net.sf.jsqlparser.util.validation.metadata.NamedObject;

Expand All @@ -38,7 +39,7 @@ public void validate(CreateView createView) {
}
SelectValidator v = getValidator(SelectValidator.class);
Select select = createView.getSelect();
select.accept(v, null);
select.accept((SelectVisitor<Void>) v, null);
}

}
Loading

0 comments on commit 60f4d74

Please sign in to comment.