Skip to content

Commit 2a35efd

Browse files
committed
Modify DeParsers to output the same result as statement.toString();
1 parent 666cac2 commit 2a35efd

File tree

3 files changed

+283
-75
lines changed

3 files changed

+283
-75
lines changed

src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void visit(Division division) {
117117
}
118118

119119
public void visit(DoubleValue doubleValue) {
120-
buffer.append(doubleValue.getValue());
120+
buffer.append(doubleValue.toString());
121121

122122
}
123123

@@ -165,7 +165,10 @@ public void visit(JdbcParameter jdbcParameter) {
165165

166166
public void visit(LikeExpression likeExpression) {
167167
visitBinaryExpression(likeExpression, " LIKE ");
168-
168+
String escape = likeExpression.getEscape();
169+
if(escape!=null){
170+
buffer.append(" ESCAPE '").append(escape).append('\'');
171+
}
169172
}
170173

171174
public void visit(ExistsExpression existsExpression) {
@@ -324,29 +327,32 @@ public void visit(TimeValue timeValue) {
324327
public void visit(CaseExpression caseExpression) {
325328
buffer.append("CASE ");
326329
Expression switchExp = caseExpression.getSwitchExpression();
327-
if (switchExp != null) {
330+
if( switchExp != null ) {
328331
switchExp.accept(this);
332+
buffer.append(" ");
329333
}
330334

331-
List<Expression> clauses = caseExpression.getWhenClauses();
332-
for (Iterator<Expression> iter = clauses.iterator(); iter.hasNext();) {
333-
Expression exp = iter.next();
335+
for (Iterator<Expression> iter = caseExpression.getWhenClauses().iterator(); iter.hasNext();) {
336+
Expression exp = (Expression) iter.next();
334337
exp.accept(this);
335338
}
336339

337340
Expression elseExp = caseExpression.getElseExpression();
338-
if (elseExp != null) {
341+
if( elseExp != null ) {
342+
buffer.append("ELSE ");
339343
elseExp.accept(this);
344+
buffer.append(" ");
340345
}
341346

342-
buffer.append(" END");
347+
buffer.append("END");
343348
}
344349

345350
public void visit(WhenClause whenClause) {
346-
buffer.append(" WHEN ");
351+
buffer.append("WHEN ");
347352
whenClause.getWhenExpression().accept(this);
348353
buffer.append(" THEN ");
349354
whenClause.getThenExpression().accept(this);
355+
buffer.append(" ");
350356
}
351357

352358
public void visit(AllComparisonExpression allComparisonExpression) {

src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void visit(PlainSelect plainSelect) {
5252
buffer.append("SELECT ");
5353
Top top = plainSelect.getTop();
5454
if (top != null)
55-
top.toString();
55+
buffer.append(top).append(" ");
5656
if (plainSelect.getDistinct() != null) {
5757
buffer.append("DISTINCT ");
5858
if (plainSelect.getDistinct().getOnSelectItems() != null) {
@@ -130,6 +130,9 @@ public void visit(Union union) {
130130
buffer.append(")");
131131
if (iter.hasNext()) {
132132
buffer.append(" UNION ");
133+
if(union.isAll()){
134+
buffer.append("ALL ");//should UNION be a BinaryExpression ?
135+
}
133136
}
134137

135138
}
@@ -146,9 +149,7 @@ public void visit(Union union) {
146149

147150
public void visit(OrderByElement orderBy) {
148151
orderBy.getExpression().accept(expressionVisitor);
149-
if (orderBy.isAsc())
150-
buffer.append(" ASC");
151-
else
152+
if (!orderBy.isAsc())
152153
buffer.append(" DESC");
153154
}
154155

@@ -176,6 +177,10 @@ public void visit(SubSelect subSelect) {
176177
buffer.append("(");
177178
subSelect.getSelectBody().accept(this);
178179
buffer.append(")");
180+
String alias = subSelect.getAlias();
181+
if(alias != null){
182+
buffer.append(" AS ").append(alias);
183+
}
179184
}
180185

181186
public void visit(Table tableName) {
@@ -199,18 +204,12 @@ public void deparseOrderBy(List<OrderByElement> orderByElements) {
199204

200205
public void deparseLimit(Limit limit) {
201206
// LIMIT n OFFSET skip
202-
buffer.append(" LIMIT ");
203207
if (limit.isRowCountJdbcParameter()) {
208+
buffer.append(" LIMIT ");
204209
buffer.append("?");
205210
} else if (limit.getRowCount() != 0) {
211+
buffer.append(" LIMIT ");
206212
buffer.append(limit.getRowCount());
207-
} else {
208-
/*
209-
* from mysql docs: For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset
210-
* syntax. To retrieve all rows from a certain offset up to the end of the result set, you can use some
211-
* large number for the second parameter.
212-
*/
213-
buffer.append("18446744073709551615");
214213
}
215214

216215
if (limit.isOffsetJdbcParameter()) {
@@ -240,7 +239,6 @@ public void setExpressionVisitor(ExpressionVisitor visitor) {
240239
public void visit(SubJoin subjoin) {
241240
buffer.append("(");
242241
subjoin.getLeft().accept(this);
243-
buffer.append(" ");
244242
deparseJoin(subjoin.getJoin());
245243
buffer.append(")");
246244
}
@@ -251,20 +249,20 @@ public void deparseJoin(Join join) {
251249
else {
252250

253251
if (join.isRight())
254-
buffer.append("RIGHT ");
252+
buffer.append(" RIGHT");
255253
else if (join.isNatural())
256-
buffer.append("NATURAL ");
254+
buffer.append(" NATURAL");
257255
else if (join.isFull())
258-
buffer.append("FULL ");
256+
buffer.append(" FULL");
259257
else if (join.isLeft())
260-
buffer.append("LEFT ");
258+
buffer.append(" LEFT");
261259

262260
if (join.isOuter())
263-
buffer.append("OUTER ");
261+
buffer.append(" OUTER");
264262
else if (join.isInner())
265-
buffer.append("INNER ");
263+
buffer.append(" INNER");
266264

267-
buffer.append("JOIN ");
265+
buffer.append(" JOIN ");
268266

269267
}
270268

@@ -275,12 +273,12 @@ else if (join.isInner())
275273
join.getOnExpression().accept(expressionVisitor);
276274
}
277275
if (join.getUsingColumns() != null) {
278-
buffer.append(" USING ( ");
276+
buffer.append(" USING (");
279277
for (Iterator<Column> iterator = join.getUsingColumns().iterator(); iterator.hasNext();) {
280278
Column column = iterator.next();
281279
buffer.append(column.getWholeColumnName());
282280
if (iterator.hasNext()) {
283-
buffer.append(" ,");
281+
buffer.append(", ");
284282
}
285283
}
286284
buffer.append(")");

0 commit comments

Comments
 (0)