Skip to content

Commit 0fa2a7f

Browse files
authored
fix: preserve visitor context in expression deparsing (#2671)
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent d4295ce commit 0fa2a7f

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
@@ -182,14 +182,13 @@ public <S> StringBuilder visit(AliasedExpression expression, S context) {
182182

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

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

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

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

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

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

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

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

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

@@ -304,13 +303,13 @@ public <S> StringBuilder deparse(
304303

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

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

315314
return builder;
316315
}
@@ -622,53 +621,51 @@ public <S> StringBuilder visit(LongValue longValue, S context) {
622621

623622
@Override
624623
public <S> StringBuilder visit(MinorThan minorThan, S context) {
625-
deparse(minorThan, " < ", null);
624+
deparse(minorThan, " < ", context);
626625

627626
return builder;
628627
}
629628

630629
@Override
631630
public <S> StringBuilder visit(MinorThanEquals minorThanEquals, S context) {
632-
deparse(minorThanEquals, " <= ", null);
631+
deparse(minorThanEquals, " <= ", context);
633632

634633
return builder;
635634
}
636635

637636
@Override
638637
public <S> StringBuilder visit(Multiplication multiplication, S context) {
639-
deparse(multiplication, " * ", null);
638+
deparse(multiplication, " * ", context);
640639

641640
return builder;
642641
}
643642

644643
@Override
645644
public <S> StringBuilder visit(NotEqualsTo notEqualsTo, S context) {
646645
deparse(notEqualsTo,
647-
" " + notEqualsTo.getStringExpression() + " ", null);
646+
" " + notEqualsTo.getStringExpression() + " ", context);
648647

649648
return builder;
650649
}
651650

652651
@Override
653652
public <S> StringBuilder visit(DoubleAnd doubleAnd, S context) {
654-
deparse(doubleAnd, " " + doubleAnd.getStringExpression() + " ",
655-
null);
653+
deparse(doubleAnd, " " + doubleAnd.getStringExpression() + " ", context);
656654

657655
return builder;
658656
}
659657

660658
@Override
661659
public <S> StringBuilder visit(Contains contains, S context) {
662-
deparse(contains, " " + contains.getStringExpression() + " ",
663-
null);
660+
deparse(contains, " " + contains.getStringExpression() + " ", context);
664661

665662
return builder;
666663
}
667664

668665
@Override
669666
public <S> StringBuilder visit(ContainedBy containedBy, S context) {
670667
deparse(containedBy,
671-
" " + containedBy.getStringExpression() + " ", null);
668+
" " + containedBy.getStringExpression() + " ", context);
672669

673670
return builder;
674671
}
@@ -682,14 +679,14 @@ public <S> StringBuilder visit(NullValue nullValue, S context) {
682679

683680
@Override
684681
public <S> StringBuilder visit(OrExpression orExpression, S context) {
685-
deparse(orExpression, " OR ", null);
682+
deparse(orExpression, " OR ", context);
686683

687684
return builder;
688685
}
689686

690687
@Override
691688
public <S> StringBuilder visit(XorExpression xorExpression, S context) {
692-
deparse(xorExpression, " XOR ", null);
689+
deparse(xorExpression, " XOR ", context);
693690

694691
return builder;
695692
}
@@ -714,7 +711,7 @@ public <S> StringBuilder visit(BooleanValue booleanValue, S context) {
714711

715712
@Override
716713
public <S> StringBuilder visit(Subtraction subtraction, S context) {
717-
deparse(subtraction, " - ", null);
714+
deparse(subtraction, " - ", context);
718715
return builder;
719716
}
720717

@@ -733,7 +730,7 @@ public <S> StringBuilder visit(Select select, S context) {
733730
builder.append("WITH ");
734731
for (Iterator<WithItem<?>> iter = select.getWithItemsList().iterator(); iter
735732
.hasNext();) {
736-
iter.next().accept(selectVisitor, null);
733+
iter.next().accept(selectVisitor, context);
737734
if (iter.hasNext()) {
738735
builder.append(", ");
739736
}
@@ -742,7 +739,7 @@ public <S> StringBuilder visit(Select select, S context) {
742739
builder.append(" ");
743740
}
744741

745-
select.accept(selectVisitor, null);
742+
select.accept(selectVisitor, context);
746743
}
747744
return builder;
748745
}
@@ -893,6 +890,15 @@ public <S> StringBuilder visit(Column tableColumn, S context) {
893890
return builder;
894891
}
895892

893+
private <S> void deParseOrderByElement(OrderByDeParser deParser,
894+
OrderByElement element, S context) {
895+
if (context == null) {
896+
deParser.deParseElement(element);
897+
} else {
898+
deParser.deParseElement(element, context);
899+
}
900+
}
901+
896902
@Override
897903
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
898904
public <S> StringBuilder visit(Function function, S context) {
@@ -951,7 +957,7 @@ public <S> StringBuilder visit(Function function, S context) {
951957
} else {
952958
comma = true;
953959
}
954-
orderByDeParser.deParseElement(orderByElement);
960+
deParseOrderByElement(orderByDeParser, orderByElement, context);
955961
}
956962
}
957963

@@ -960,7 +966,7 @@ public <S> StringBuilder visit(Function function, S context) {
960966
}
961967

962968
if (function.getLimit() != null) {
963-
new LimitDeparser(this, builder).deParse(function.getLimit());
969+
new LimitDeparser(this, builder).deParse(function.getLimit(), context);
964970
}
965971

966972
// Generic keyword arguments (e.g. SEPARATOR ',', USING utf8)
@@ -1081,7 +1087,7 @@ public <S> StringBuilder visit(AnyComparisonExpression anyComparisonExpression,
10811087

10821088
@Override
10831089
public <S> StringBuilder visit(Concat concat, S context) {
1084-
deparse(concat, " || ", null);
1090+
deparse(concat, " || ", context);
10851091
return builder;
10861092
}
10871093

@@ -1136,25 +1142,25 @@ public void visit(Concat concat) {
11361142

11371143
@Override
11381144
public <S> StringBuilder visit(Matches matches, S context) {
1139-
deparse(matches, " @@ ", null);
1145+
deparse(matches, " @@ ", context);
11401146
return builder;
11411147
}
11421148

11431149
@Override
11441150
public <S> StringBuilder visit(BitwiseAnd bitwiseAnd, S context) {
1145-
deparse(bitwiseAnd, " & ", null);
1151+
deparse(bitwiseAnd, " & ", context);
11461152
return builder;
11471153
}
11481154

11491155
@Override
11501156
public <S> StringBuilder visit(BitwiseOr bitwiseOr, S context) {
1151-
deparse(bitwiseOr, " | ", null);
1157+
deparse(bitwiseOr, " | ", context);
11521158
return builder;
11531159
}
11541160

11551161
@Override
11561162
public <S> StringBuilder visit(BitwiseXor bitwiseXor, S context) {
1157-
deparse(bitwiseXor, " ^ ", null);
1163+
deparse(bitwiseXor, " ^ ", context);
11581164
return builder;
11591165
}
11601166

@@ -1187,7 +1193,7 @@ public <S> StringBuilder visit(CastExpression cast, S context) {
11871193

11881194
@Override
11891195
public <S> StringBuilder visit(Modulo modulo, S context) {
1190-
deparse(modulo, " % ", null);
1196+
deparse(modulo, " % ", context);
11911197
return builder;
11921198
}
11931199

@@ -1254,7 +1260,7 @@ public <S> StringBuilder visit(AnalyticExpression analyticExpression, S context)
12541260
}
12551261

12561262
if (analyticExpression.getLimit() != null) {
1257-
new LimitDeparser(this, builder).deParse(analyticExpression.getLimit());
1263+
new LimitDeparser(this, builder).deParse(analyticExpression.getLimit(), context);
12581264
}
12591265

12601266
builder.append(") ");
@@ -1332,7 +1338,7 @@ public <S> StringBuilder visit(AnalyticExpression analyticExpression, S context)
13321338
if (i > 0) {
13331339
builder.append(", ");
13341340
}
1335-
orderByDeParser.deParseElement(orderByElements.get(i));
1341+
deParseOrderByElement(orderByDeParser, orderByElements.get(i), context);
13361342
}
13371343
}
13381344

@@ -1431,7 +1437,8 @@ public <S> StringBuilder visit(OracleHierarchicalExpression hierarchicalExpressi
14311437

14321438
@Override
14331439
public <S> StringBuilder visit(RegExpMatchOperator regExpMatchOperator, S context) {
1434-
deparse(regExpMatchOperator, " " + regExpMatchOperator.getStringExpression() + " ", null);
1440+
deparse(regExpMatchOperator, " " + regExpMatchOperator.getStringExpression() + " ",
1441+
context);
14351442
return builder;
14361443
}
14371444

@@ -1444,7 +1451,7 @@ public <S> StringBuilder visit(JsonExpression jsonExpr, S context) {
14441451

14451452
@Override
14461453
public <S> StringBuilder visit(JsonOperator jsonExpr, S context) {
1447-
deparse(jsonExpr, " " + jsonExpr.getStringExpression() + " ", null);
1454+
deparse(jsonExpr, " " + jsonExpr.getStringExpression() + " ", context);
14481455
return builder;
14491456
}
14501457

@@ -1482,7 +1489,7 @@ public <S> StringBuilder visit(MySQLGroupConcat groupConcat, S context) {
14821489
public <S> StringBuilder visit(ExpressionList<? extends Expression> expressionList, S context) {
14831490
ExpressionListDeParser<?> expressionListDeParser =
14841491
new ExpressionListDeParser<>(this, builder);
1485-
expressionListDeParser.deParse(expressionList);
1492+
expressionListDeParser.deParse(expressionList, context);
14861493
return builder;
14871494
}
14881495

@@ -1493,7 +1500,7 @@ public <S> StringBuilder visit(RowConstructor<?> rowConstructor, S context) {
14931500
}
14941501
ExpressionListDeParser<?> expressionListDeParser =
14951502
new ExpressionListDeParser<>(this, builder);
1496-
expressionListDeParser.deParse(rowConstructor);
1503+
expressionListDeParser.deParse(rowConstructor, context);
14971504
return builder;
14981505
}
14991506

@@ -1812,25 +1819,25 @@ public <S> StringBuilder visit(IsDistinctExpression isDistinctExpression, S cont
18121819
@Override
18131820
public <S> StringBuilder visit(GeometryDistance geometryDistance, S context) {
18141821
deparse(geometryDistance,
1815-
" " + geometryDistance.getStringExpression() + " ", null);
1822+
" " + geometryDistance.getStringExpression() + " ", context);
18161823
return builder;
18171824
}
18181825

18191826
@Override
18201827
public <S> StringBuilder visit(Intersects intersects, S context) {
1821-
deparse(intersects, " # ", null);
1828+
deparse(intersects, " # ", context);
18221829
return builder;
18231830
}
18241831

18251832
@Override
18261833
public <S> StringBuilder visit(TSQLLeftJoin tsqlLeftJoin, S context) {
1827-
this.deparse(tsqlLeftJoin, " *= ", null);
1834+
this.deparse(tsqlLeftJoin, " *= ", context);
18281835
return builder;
18291836
}
18301837

18311838
@Override
18321839
public <S> StringBuilder visit(TSQLRightJoin tsqlRightJoin, S context) {
1833-
this.deparse(tsqlRightJoin, " =* ", null);
1840+
this.deparse(tsqlRightJoin, " =* ", context);
18341841
return builder;
18351842
}
18361843

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)