Skip to content

Commit d95bd8f

Browse files
committed
fix: preserve visitor context in expression deparsing
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 7f91819 commit d95bd8f

4 files changed

Lines changed: 150 additions & 46 deletions

File tree

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

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,13 @@ public <S> StringBuilder visit(AliasedExpression expression, S context) {
181181

182182
@Override
183183
public <S> StringBuilder visit(Addition addition, S context) {
184-
deparse(addition, " + ", null);
184+
deparse(addition, " + ", context);
185185
return builder;
186186
}
187187

188188
@Override
189189
public <S> StringBuilder visit(AndExpression andExpression, S context) {
190-
deparse(andExpression, andExpression.isUseOperator() ? " && " : " AND ",
191-
null);
190+
deparse(andExpression, andExpression.isUseOperator() ? " && " : " AND ", context);
192191
return builder;
193192
}
194193

@@ -225,19 +224,19 @@ public <S> StringBuilder visit(OverlapsCondition overlapsCondition, S context) {
225224

226225
@Override
227226
public <S> StringBuilder visit(EqualsTo equalsTo, S context) {
228-
deparse(equalsTo, " = ", null);
227+
deparse(equalsTo, " = ", context);
229228
return builder;
230229
}
231230

232231
@Override
233232
public <S> StringBuilder visit(Division division, S context) {
234-
deparse(division, " / ", null);
233+
deparse(division, " / ", context);
235234
return builder;
236235
}
237236

238237
@Override
239238
public <S> StringBuilder visit(IntegerDivision division, S context) {
240-
deparse(division, " DIV ", null);
239+
deparse(division, " DIV ", context);
241240
return builder;
242241
}
243242

@@ -266,13 +265,13 @@ public <S> StringBuilder visit(NotExpression notExpr, S context) {
266265

267266
@Override
268267
public <S> StringBuilder visit(BitwiseRightShift expr, S context) {
269-
deparse(expr, " >> ", null);
268+
deparse(expr, " >> ", context);
270269
return builder;
271270
}
272271

273272
@Override
274273
public <S> StringBuilder visit(BitwiseLeftShift expr, S context) {
275-
deparse(expr, " << ", null);
274+
deparse(expr, " << ", context);
276275
return builder;
277276
}
278277

@@ -303,13 +302,13 @@ public <S> StringBuilder deparse(
303302

304303
@Override
305304
public <S> StringBuilder visit(GreaterThan greaterThan, S context) {
306-
deparse(greaterThan, " > ", null);
305+
deparse(greaterThan, " > ", context);
307306
return builder;
308307
}
309308

310309
@Override
311310
public <S> StringBuilder visit(GreaterThanEquals greaterThanEquals, S context) {
312-
deparse(greaterThanEquals, " >= ", null);
311+
deparse(greaterThanEquals, " >= ", context);
313312

314313
return builder;
315314
}
@@ -616,53 +615,51 @@ public <S> StringBuilder visit(LongValue longValue, S context) {
616615

617616
@Override
618617
public <S> StringBuilder visit(MinorThan minorThan, S context) {
619-
deparse(minorThan, " < ", null);
618+
deparse(minorThan, " < ", context);
620619

621620
return builder;
622621
}
623622

624623
@Override
625624
public <S> StringBuilder visit(MinorThanEquals minorThanEquals, S context) {
626-
deparse(minorThanEquals, " <= ", null);
625+
deparse(minorThanEquals, " <= ", context);
627626

628627
return builder;
629628
}
630629

631630
@Override
632631
public <S> StringBuilder visit(Multiplication multiplication, S context) {
633-
deparse(multiplication, " * ", null);
632+
deparse(multiplication, " * ", context);
634633

635634
return builder;
636635
}
637636

638637
@Override
639638
public <S> StringBuilder visit(NotEqualsTo notEqualsTo, S context) {
640639
deparse(notEqualsTo,
641-
" " + notEqualsTo.getStringExpression() + " ", null);
640+
" " + notEqualsTo.getStringExpression() + " ", context);
642641

643642
return builder;
644643
}
645644

646645
@Override
647646
public <S> StringBuilder visit(DoubleAnd doubleAnd, S context) {
648-
deparse(doubleAnd, " " + doubleAnd.getStringExpression() + " ",
649-
null);
647+
deparse(doubleAnd, " " + doubleAnd.getStringExpression() + " ", context);
650648

651649
return builder;
652650
}
653651

654652
@Override
655653
public <S> StringBuilder visit(Contains contains, S context) {
656-
deparse(contains, " " + contains.getStringExpression() + " ",
657-
null);
654+
deparse(contains, " " + contains.getStringExpression() + " ", context);
658655

659656
return builder;
660657
}
661658

662659
@Override
663660
public <S> StringBuilder visit(ContainedBy containedBy, S context) {
664661
deparse(containedBy,
665-
" " + containedBy.getStringExpression() + " ", null);
662+
" " + containedBy.getStringExpression() + " ", context);
666663

667664
return builder;
668665
}
@@ -676,14 +673,14 @@ public <S> StringBuilder visit(NullValue nullValue, S context) {
676673

677674
@Override
678675
public <S> StringBuilder visit(OrExpression orExpression, S context) {
679-
deparse(orExpression, " OR ", null);
676+
deparse(orExpression, " OR ", context);
680677

681678
return builder;
682679
}
683680

684681
@Override
685682
public <S> StringBuilder visit(XorExpression xorExpression, S context) {
686-
deparse(xorExpression, " XOR ", null);
683+
deparse(xorExpression, " XOR ", context);
687684

688685
return builder;
689686
}
@@ -708,7 +705,7 @@ public <S> StringBuilder visit(BooleanValue booleanValue, S context) {
708705

709706
@Override
710707
public <S> StringBuilder visit(Subtraction subtraction, S context) {
711-
deparse(subtraction, " - ", null);
708+
deparse(subtraction, " - ", context);
712709
return builder;
713710
}
714711

@@ -727,7 +724,7 @@ public <S> StringBuilder visit(Select select, S context) {
727724
builder.append("WITH ");
728725
for (Iterator<WithItem<?>> iter = select.getWithItemsList().iterator(); iter
729726
.hasNext();) {
730-
iter.next().accept(selectVisitor, null);
727+
iter.next().accept(selectVisitor, context);
731728
if (iter.hasNext()) {
732729
builder.append(", ");
733730
}
@@ -736,7 +733,7 @@ public <S> StringBuilder visit(Select select, S context) {
736733
builder.append(" ");
737734
}
738735

739-
select.accept(selectVisitor, null);
736+
select.accept(selectVisitor, context);
740737
}
741738
return builder;
742739
}
@@ -908,6 +905,15 @@ public <S> StringBuilder visit(Column tableColumn, S context) {
908905
return builder;
909906
}
910907

908+
private <S> void deParseOrderByElement(OrderByDeParser deParser,
909+
OrderByElement element, S context) {
910+
if (context == null) {
911+
deParser.deParseElement(element);
912+
} else {
913+
deParser.deParseElement(element, context);
914+
}
915+
}
916+
911917
@Override
912918
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
913919
public <S> StringBuilder visit(Function function, S context) {
@@ -966,7 +972,7 @@ public <S> StringBuilder visit(Function function, S context) {
966972
} else {
967973
comma = true;
968974
}
969-
orderByDeParser.deParseElement(orderByElement);
975+
deParseOrderByElement(orderByDeParser, orderByElement, context);
970976
}
971977
}
972978

@@ -975,7 +981,7 @@ public <S> StringBuilder visit(Function function, S context) {
975981
}
976982

977983
if (function.getLimit() != null) {
978-
new LimitDeparser(this, builder).deParse(function.getLimit());
984+
new LimitDeparser(this, builder).deParse(function.getLimit(), context);
979985
}
980986

981987
// Generic keyword arguments (e.g. SEPARATOR ',', USING utf8)
@@ -1096,7 +1102,7 @@ public <S> StringBuilder visit(AnyComparisonExpression anyComparisonExpression,
10961102

10971103
@Override
10981104
public <S> StringBuilder visit(Concat concat, S context) {
1099-
deparse(concat, " || ", null);
1105+
deparse(concat, " || ", context);
11001106
return builder;
11011107
}
11021108

@@ -1151,25 +1157,25 @@ public void visit(Concat concat) {
11511157

11521158
@Override
11531159
public <S> StringBuilder visit(Matches matches, S context) {
1154-
deparse(matches, " @@ ", null);
1160+
deparse(matches, " @@ ", context);
11551161
return builder;
11561162
}
11571163

11581164
@Override
11591165
public <S> StringBuilder visit(BitwiseAnd bitwiseAnd, S context) {
1160-
deparse(bitwiseAnd, " & ", null);
1166+
deparse(bitwiseAnd, " & ", context);
11611167
return builder;
11621168
}
11631169

11641170
@Override
11651171
public <S> StringBuilder visit(BitwiseOr bitwiseOr, S context) {
1166-
deparse(bitwiseOr, " | ", null);
1172+
deparse(bitwiseOr, " | ", context);
11671173
return builder;
11681174
}
11691175

11701176
@Override
11711177
public <S> StringBuilder visit(BitwiseXor bitwiseXor, S context) {
1172-
deparse(bitwiseXor, " ^ ", null);
1178+
deparse(bitwiseXor, " ^ ", context);
11731179
return builder;
11741180
}
11751181

@@ -1202,7 +1208,7 @@ public <S> StringBuilder visit(CastExpression cast, S context) {
12021208

12031209
@Override
12041210
public <S> StringBuilder visit(Modulo modulo, S context) {
1205-
deparse(modulo, " % ", null);
1211+
deparse(modulo, " % ", context);
12061212
return builder;
12071213
}
12081214

@@ -1269,7 +1275,7 @@ public <S> StringBuilder visit(AnalyticExpression analyticExpression, S context)
12691275
}
12701276

12711277
if (analyticExpression.getLimit() != null) {
1272-
new LimitDeparser(this, builder).deParse(analyticExpression.getLimit());
1278+
new LimitDeparser(this, builder).deParse(analyticExpression.getLimit(), context);
12731279
}
12741280

12751281
builder.append(") ");
@@ -1347,7 +1353,7 @@ public <S> StringBuilder visit(AnalyticExpression analyticExpression, S context)
13471353
if (i > 0) {
13481354
builder.append(", ");
13491355
}
1350-
orderByDeParser.deParseElement(orderByElements.get(i));
1356+
deParseOrderByElement(orderByDeParser, orderByElements.get(i), context);
13511357
}
13521358
}
13531359

@@ -1446,7 +1452,8 @@ public <S> StringBuilder visit(OracleHierarchicalExpression hierarchicalExpressi
14461452

14471453
@Override
14481454
public <S> StringBuilder visit(RegExpMatchOperator regExpMatchOperator, S context) {
1449-
deparse(regExpMatchOperator, " " + regExpMatchOperator.getStringExpression() + " ", null);
1455+
deparse(regExpMatchOperator, " " + regExpMatchOperator.getStringExpression() + " ",
1456+
context);
14501457
return builder;
14511458
}
14521459

@@ -1459,7 +1466,7 @@ public <S> StringBuilder visit(JsonExpression jsonExpr, S context) {
14591466

14601467
@Override
14611468
public <S> StringBuilder visit(JsonOperator jsonExpr, S context) {
1462-
deparse(jsonExpr, " " + jsonExpr.getStringExpression() + " ", null);
1469+
deparse(jsonExpr, " " + jsonExpr.getStringExpression() + " ", context);
14631470
return builder;
14641471
}
14651472

@@ -1497,7 +1504,7 @@ public <S> StringBuilder visit(MySQLGroupConcat groupConcat, S context) {
14971504
public <S> StringBuilder visit(ExpressionList<? extends Expression> expressionList, S context) {
14981505
ExpressionListDeParser<?> expressionListDeParser =
14991506
new ExpressionListDeParser<>(this, builder);
1500-
expressionListDeParser.deParse(expressionList);
1507+
expressionListDeParser.deParse(expressionList, context);
15011508
return builder;
15021509
}
15031510

@@ -1508,7 +1515,7 @@ public <S> StringBuilder visit(RowConstructor<?> rowConstructor, S context) {
15081515
}
15091516
ExpressionListDeParser<?> expressionListDeParser =
15101517
new ExpressionListDeParser<>(this, builder);
1511-
expressionListDeParser.deParse(rowConstructor);
1518+
expressionListDeParser.deParse(rowConstructor, context);
15121519
return builder;
15131520
}
15141521

@@ -1827,25 +1834,25 @@ public <S> StringBuilder visit(IsDistinctExpression isDistinctExpression, S cont
18271834
@Override
18281835
public <S> StringBuilder visit(GeometryDistance geometryDistance, S context) {
18291836
deparse(geometryDistance,
1830-
" " + geometryDistance.getStringExpression() + " ", null);
1837+
" " + geometryDistance.getStringExpression() + " ", context);
18311838
return builder;
18321839
}
18331840

18341841
@Override
18351842
public <S> StringBuilder visit(Intersects intersects, S context) {
1836-
deparse(intersects, " # ", null);
1843+
deparse(intersects, " # ", context);
18371844
return builder;
18381845
}
18391846

18401847
@Override
18411848
public <S> StringBuilder visit(TSQLLeftJoin tsqlLeftJoin, S context) {
1842-
this.deparse(tsqlLeftJoin, " *= ", null);
1849+
this.deparse(tsqlLeftJoin, " *= ", context);
18431850
return builder;
18441851
}
18451852

18461853
@Override
18471854
public <S> StringBuilder visit(TSQLRightJoin tsqlRightJoin, S context) {
1848-
this.deparse(tsqlRightJoin, " =* ", null);
1855+
this.deparse(tsqlRightJoin, " =* ", context);
18491856
return builder;
18501857
}
18511858

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public ExpressionListDeParser(ExpressionVisitor<StringBuilder> expressionVisitor
3131

3232
@Override
3333
public void deParse(ExpressionList<?> expressionList) {
34+
deParse(expressionList, null);
35+
}
36+
37+
public <S> void deParse(ExpressionList<?> expressionList, S context) {
3438
// @todo: remove this NameExpressionList related part
3539
String comma = expressionList instanceof NamedExpressionList
3640
? " "
@@ -55,7 +59,7 @@ public void deParse(ExpressionList<?> expressionList) {
5559
builder.append(name);
5660
builder.append(" ");
5761
}
58-
expression.accept(expressionVisitor, null);
62+
expression.accept(expressionVisitor, context);
5963
i++;
6064
}
6165

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public LimitDeparser(ExpressionVisitor<StringBuilder> expressionVisitor, StringB
2222

2323
@Override
2424
public void deParse(Limit limit) {
25+
deParse(limit, null);
26+
}
27+
28+
public <S> void deParse(Limit limit, S context) {
2529
builder.append(" LIMIT ");
2630
if (limit.isLimitNull()) {
2731
builder.append("NULL");
@@ -30,19 +34,19 @@ public void deParse(Limit limit) {
3034
builder.append("ALL");
3135
} else {
3236
if (null != limit.getOffset()) {
33-
limit.getOffset().accept(expressionVisitor, null);
37+
limit.getOffset().accept(expressionVisitor, context);
3438
builder.append(", ");
3539
}
3640

3741
if (null != limit.getRowCount()) {
38-
limit.getRowCount().accept(expressionVisitor, null);
42+
limit.getRowCount().accept(expressionVisitor, context);
3943
}
4044
}
4145
}
4246

4347
if (limit.getByExpressions() != null) {
4448
builder.append(" BY ");
45-
limit.getByExpressions().accept(expressionVisitor, null);
49+
limit.getByExpressions().accept(expressionVisitor, context);
4650
}
4751
}
4852

0 commit comments

Comments
 (0)