Skip to content

Commit

Permalink
Update UpdateDeParser.java
Browse files Browse the repository at this point in the history
Extend the deparser to support DB2 Updates with Select clause
  • Loading branch information
CeeKayGit committed Oct 1, 2014
1 parent 232795f commit 3a7bf9d
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions src/main/java/net/sf/jsqlparser/util/deparser/UpdateDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.select.SelectVisitor;

/**
* A class to de-parse (that is, tranform from JSqlParser hierarchy into a
Expand All @@ -36,16 +37,22 @@ public class UpdateDeParser {

private StringBuilder buffer;
private ExpressionVisitor expressionVisitor;
private SelectVisitor selectVisitor;

/**
* @param expressionVisitor a {@link ExpressionVisitor} to de-parse
* expressions. It has to share the same<br>
* StringBuilder (buffer parameter) as this object in order to work
* @param selectVisitor a {@link SelectVisitor} to de-parse
* {@link net.sf.jsqlparser.statement.select.Select}s. It has to share the
* same<br>
* StringBuilder (buffer parameter) as this object in order to work
* @param buffer the buffer that will be filled with the select
*/
public UpdateDeParser(ExpressionVisitor expressionVisitor, StringBuilder buffer) {
public UpdateDeParser(ExpressionVisitor expressionVisitor, SelectVisitor selectVisitor, StringBuilder buffer) {
this.buffer = buffer;
this.expressionVisitor = expressionVisitor;
this.selectVisitor = selectVisitor;
}

public StringBuilder getBuffer() {
Expand All @@ -58,17 +65,39 @@ public void setBuffer(StringBuilder buffer) {

public void deParse(Update update) {
buffer.append("UPDATE ").append(PlainSelect.getStringList(update.getTables(), true, false)).append(" SET ");
for (int i = 0; i < update.getColumns().size(); i++) {
Column column = update.getColumns().get(i);
buffer.append(column.getFullyQualifiedName()).append(" = ");

Expression expression = update.getExpressions().get(i);
expression.accept(expressionVisitor);
if (i < update.getColumns().size() - 1) {
buffer.append(", ");

if (!update.isUseSelect()) {
for (int i = 0; i < update.getColumns().size(); i++) {
Column column = update.getColumns().get(i);
buffer.append(column.getFullyQualifiedName()).append(" = ");

Expression expression = update.getExpressions().get(i);
expression.accept(expressionVisitor);
if (i < update.getColumns().size() - 1) {
buffer.append(", ");
}
}
} else {
if (update.isUseColumnsBrackets()) {
buffer.append("(");
}
for (int i = 0; i < update.getColumns().size(); i++) {
if (i != 0) {
buffer.append(", ");
}
Column column = update.getColumns().get(i);
buffer.append(column.getFullyQualifiedName());
}
if (update.isUseColumnsBrackets()) {
buffer.append(")");
}
buffer.append(" = ");
buffer.append("(");
Select select = update.getSelect();
select.accept(selectVisitor);
buffer.append(")");
}

if (update.getFromItem() != null) {
buffer.append(" FROM ").append(update.getFromItem());
if (update.getJoins() != null) {
Expand Down

0 comments on commit 3a7bf9d

Please sign in to comment.