Skip to content

support custom DeParser #2013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ public void deParse(Delete delete) {
}
}

if (delete.getWhere() != null) {
buffer.append(" WHERE ");
delete.getWhere().accept(expressionVisitor);
}
deparseWhereClause(delete);

if (delete.getOrderByElements() != null) {
new OrderByDeParser(expressionVisitor, buffer).deParse(delete.getOrderByElements());
Expand All @@ -108,6 +105,13 @@ public void deParse(Delete delete) {

}

protected void deparseWhereClause(Delete delete) {
if (delete.getWhere() != null) {
buffer.append(" WHERE ");
delete.getWhere().accept(expressionVisitor);
}
}

public ExpressionVisitor getExpressionVisitor() {
return expressionVisitor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MergeDeParser(ExpressionDeParser expressionDeParser, SelectDeParser selec
}

@Override
void deParse(Merge merge) {
public void deParse(Merge merge) {
List<WithItem> withItemsList = merge.getWithItemsList();
if (withItemsList != null && !withItemsList.isEmpty()) {
buffer.append("WITH ");
Expand Down Expand Up @@ -115,4 +115,11 @@ public void visit(MergeInsert mergeInsert) {
}
}

public ExpressionDeParser getExpressionDeParser() {
return expressionDeParser;
}

public SelectDeParser getSelectDeParser() {
return selectDeParser;
}
}
94 changes: 56 additions & 38 deletions src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.sf.jsqlparser.expression.WindowDefinition;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Distinct;
import net.sf.jsqlparser.statement.select.Fetch;
import net.sf.jsqlparser.statement.select.First;
import net.sf.jsqlparser.statement.select.FromItem;
Expand All @@ -27,6 +28,7 @@
import net.sf.jsqlparser.statement.select.LateralView;
import net.sf.jsqlparser.statement.select.Offset;
import net.sf.jsqlparser.statement.select.OptimizeFor;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.ParenthesedFromItem;
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
import net.sf.jsqlparser.statement.select.Pivot;
Expand Down Expand Up @@ -158,26 +160,7 @@ public void visit(PlainSelect plainSelect) {
buffer.append(first).append(" ");
}

if (plainSelect.getDistinct() != null) {
if (plainSelect.getDistinct().isUseUnique()) {
buffer.append("UNIQUE ");
} else {
buffer.append("DISTINCT ");
}
if (plainSelect.getDistinct().getOnSelectItems() != null) {
buffer.append("ON (");
for (Iterator<SelectItem<?>> iter =
plainSelect.getDistinct().getOnSelectItems().iterator(); iter.hasNext();) {
SelectItem<?> selectItem = iter.next();
selectItem.accept(this);
if (iter.hasNext()) {
buffer.append(", ");
}
}
buffer.append(") ");
}

}
deparseDistinctClause(plainSelect, plainSelect.getDistinct());

Top top = plainSelect.getTop();
if (top != null) {
Expand All @@ -192,16 +175,7 @@ public void visit(PlainSelect plainSelect) {
buffer.append("SQL_CALC_FOUND_ROWS").append(" ");
}

final List<SelectItem<?>> selectItems = plainSelect.getSelectItems();
if (selectItems != null) {
for (Iterator<SelectItem<?>> iter = selectItems.iterator(); iter.hasNext();) {
SelectItem<?> selectItem = iter.next();
selectItem.accept(this);
if (iter.hasNext()) {
buffer.append(", ");
}
}
}
deparseSelectItemsClause(plainSelect, plainSelect.getSelectItems());

if (plainSelect.getIntoTables() != null) {
buffer.append(" INTO ");
Expand Down Expand Up @@ -249,10 +223,7 @@ public void visit(PlainSelect plainSelect) {
buffer.append(plainSelect.getKsqlWindow().toString());
}

if (plainSelect.getWhere() != null) {
buffer.append(" WHERE ");
plainSelect.getWhere().accept(expressionVisitor);
}
deparseWhereClause(plainSelect);

if (plainSelect.getOracleHierarchical() != null) {
plainSelect.getOracleHierarchical().accept(expressionVisitor);
Expand Down Expand Up @@ -280,10 +251,7 @@ public void visit(PlainSelect plainSelect) {
plainSelect.getForClause().appendTo(buffer);
}

if (plainSelect.getOrderByElements() != null) {
new OrderByDeParser(expressionVisitor, buffer).deParse(plainSelect.isOracleSiblings(),
plainSelect.getOrderByElements());
}
deparseOrderByElementsClause(plainSelect, plainSelect.getOrderByElements());
if (plainSelect.isEmitChanges()) {
buffer.append(" EMIT CHANGES");
}
Expand Down Expand Up @@ -334,6 +302,56 @@ public void visit(PlainSelect plainSelect) {

}

protected void deparseWhereClause(PlainSelect plainSelect) {
if (plainSelect.getWhere() != null) {
buffer.append(" WHERE ");
plainSelect.getWhere().accept(expressionVisitor);
}
}

protected void deparseDistinctClause(PlainSelect plainSelect, Distinct distinct) {
if (distinct != null) {
if (distinct.isUseUnique()) {
buffer.append("UNIQUE ");
} else {
buffer.append("DISTINCT ");
}
if (distinct.getOnSelectItems() != null) {
buffer.append("ON (");
for (Iterator<SelectItem<?>> iter =
distinct.getOnSelectItems().iterator(); iter.hasNext();) {
SelectItem<?> selectItem = iter.next();
selectItem.accept(this);
if (iter.hasNext()) {
buffer.append(", ");
}
}
buffer.append(") ");
}
}
}

protected void deparseSelectItemsClause(PlainSelect plainSelect,
List<SelectItem<?>> selectItems) {
if (selectItems != null) {
for (Iterator<SelectItem<?>> iter = selectItems.iterator(); iter.hasNext();) {
SelectItem<?> selectItem = iter.next();
selectItem.accept(this);
if (iter.hasNext()) {
buffer.append(", ");
}
}
}
}

protected void deparseOrderByElementsClause(PlainSelect plainSelect,
List<OrderByElement> orderByElements) {
if (orderByElements != null) {
new OrderByDeParser(expressionVisitor, buffer).deParse(plainSelect.isOracleSiblings(),
orderByElements);
}
}

@Override
public void visit(SelectItem selectExpressionItem) {
selectExpressionItem.getExpression().accept(expressionVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,12 @@ public void visit(AlterSystemStatement alterSystemStatement) {
public void visit(UnsupportedStatement unsupportedStatement) {
unsupportedStatement.appendTo(buffer);
}

public ExpressionDeParser getExpressionDeParser() {
return expressionDeParser;
}

public SelectDeParser getSelectDeParser() {
return selectDeParser;
}
}
20 changes: 15 additions & 5 deletions src/main/java/net/sf/jsqlparser/util/deparser/UpdateDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void deParse(Update update) {
}
buffer.append(" SET ");

deparseUpdateSets(update.getUpdateSets(), buffer, expressionVisitor);
deparseUpdateSetsClause(update);

if (update.getOutputClause() != null) {
update.getOutputClause().appendTo(buffer);
Expand All @@ -88,10 +88,8 @@ public void deParse(Update update) {
}
}

if (update.getWhere() != null) {
buffer.append(" WHERE ");
update.getWhere().accept(expressionVisitor);
}
deparseWhereClause(update);

if (update.getOrderByElements() != null) {
new OrderByDeParser(expressionVisitor, buffer).deParse(update.getOrderByElements());
}
Expand All @@ -104,6 +102,18 @@ public void deParse(Update update) {
}
}

protected void deparseWhereClause(Update update) {
if (update.getWhere() != null) {
buffer.append(" WHERE ");
update.getWhere().accept(expressionVisitor);
}
}

protected void deparseUpdateSetsClause(Update update) {
deparseUpdateSets(update.getUpdateSets(), buffer, expressionVisitor);
}


public ExpressionVisitor getExpressionVisitor() {
return expressionVisitor;
}
Expand Down
Loading