|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.statement; |
| 11 | + |
| 12 | +import net.sf.jsqlparser.JSQLParserException; |
| 13 | +import net.sf.jsqlparser.expression.ExpressionVisitorAdapter; |
| 14 | +import net.sf.jsqlparser.expression.Function; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.statement.select.Select; |
| 17 | +import net.sf.jsqlparser.statement.select.SelectVisitorAdapter; |
| 18 | +import org.junit.jupiter.api.DisplayName; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 26 | + |
| 27 | +/** |
| 28 | + * Regressions for two adapter traversal defects. |
| 29 | + */ |
| 30 | +class AdapterRegressionTest { |
| 31 | + |
| 32 | + /** |
| 33 | + * {@code SelectVisitorAdapter#visit(WithItem)} used to call {@code withItem.getSelect()}, an |
| 34 | + * unchecked cast to {@code ParenthesedSelect}. Any data-modifying CTE reached through the |
| 35 | + * select path threw {@link ClassCastException} - and the select path is taken for every |
| 36 | + * sub-select, because {@code ExpressionVisitorAdapter#visit(Select)} loops the with-items into |
| 37 | + * the select visitor. |
| 38 | + */ |
| 39 | + @Test |
| 40 | + @DisplayName("a data-modifying CTE inside a sub-select no longer throws ClassCastException") |
| 41 | + void dataModifyingCteOnTheSelectPath() throws JSQLParserException { |
| 42 | + Statement statement = CCJSqlParserUtil.parse("SELECT * FROM u WHERE id IN (" |
| 43 | + + "WITH c AS (DELETE FROM t RETURNING id) SELECT id FROM c)"); |
| 44 | + |
| 45 | + assertThatCode(() -> statement.accept(new StatementVisitorAdapter<>(), null)) |
| 46 | + .doesNotThrowAnyException(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * The CTE body must actually be traversed, not merely survived. |
| 51 | + * |
| 52 | + * <p> |
| 53 | + * This is the test that catches <em>both</em> defects, and it is the one that exposed the |
| 54 | + * larger of the two. Reaching {@code visit(Delete)} here requires: |
| 55 | + * |
| 56 | + * <ol> |
| 57 | + * <li>the expression chain to be connected at all. {@code StatementVisitorAdapter()} used to |
| 58 | + * build its {@code ExpressionVisitorAdapter} with the no-arg constructor, which leaves |
| 59 | + * {@code selectVisitor} null - and {@code ExpressionVisitorAdapter#visit(Select)} returns |
| 60 | + * immediately when it is. Every sub-select inside an expression (IN, EXISTS, scalar) was |
| 61 | + * silently dropped, so no subclass could observe anything below one. Its |
| 62 | + * {@code FromItemVisitorAdapter} had the mirror problem: the no-arg constructor builds a |
| 63 | + * throwaway select/expression chain of its own.</li> |
| 64 | + * <li>{@code SelectVisitorAdapter#visit(WithItem)} to dispatch the CTE body through |
| 65 | + * {@code getParenthesedStatement()} rather than casting it to ParenthesedSelect, and to have a |
| 66 | + * statement visitor to hand a DELETE body to.</li> |
| 67 | + * </ol> |
| 68 | + * |
| 69 | + * <p> |
| 70 | + * Defect (1) is also why (2) never surfaced as a ClassCastException in practice: the traversal |
| 71 | + * never got that far. Fixing the wiring is what makes the cast reachable, so the two fixes have |
| 72 | + * to land together. |
| 73 | + * |
| 74 | + * <p> |
| 75 | + * Note this can only be asserted through its effects - {@code StatementVisitorAdapter} exposes |
| 76 | + * no getters for its sub-visitors, so the wiring cannot be inspected directly. Adding them |
| 77 | + * would be a reasonable follow-up. |
| 78 | + */ |
| 79 | + @Test |
| 80 | + @DisplayName("the CTE body is traversed, not skipped") |
| 81 | + void dataModifyingCteBodyIsVisited() throws JSQLParserException { |
| 82 | + List<String> visited = new ArrayList<>(); |
| 83 | + |
| 84 | + StatementVisitorAdapter<Void> visitor = new StatementVisitorAdapter<Void>() { |
| 85 | + @Override |
| 86 | + public <S> Void visit(net.sf.jsqlparser.statement.delete.Delete delete, S context) { |
| 87 | + visited.add("delete"); |
| 88 | + return super.visit(delete, context); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + CCJSqlParserUtil |
| 93 | + .parse("SELECT * FROM u WHERE id IN (" |
| 94 | + + "WITH c AS (DELETE FROM t RETURNING id) SELECT id FROM c)") |
| 95 | + .accept(visitor, null); |
| 96 | + |
| 97 | + assertThat(visited).containsExactly("delete"); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * A bare SelectVisitorAdapter has no statement visitor, so it cannot accept a DELETE body. It |
| 102 | + * must skip it rather than fail - the old cast made this case fatal. |
| 103 | + */ |
| 104 | + @Test |
| 105 | + @DisplayName("a standalone SelectVisitorAdapter skips a DML CTE instead of failing") |
| 106 | + void standaloneSelectVisitorSkipsDmlCte() throws JSQLParserException { |
| 107 | + Select select = (Select) CCJSqlParserUtil |
| 108 | + .parse("WITH c AS (DELETE FROM t RETURNING id) SELECT id FROM c"); |
| 109 | + |
| 110 | + SelectVisitorAdapter<Void> selects = new SelectVisitorAdapter<>(); |
| 111 | + assertThat(selects.getStatementVisitor()).isNull(); |
| 112 | + assertThatCode(() -> select.accept(selects, null)).doesNotThrowAnyException(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * {@code StatementVisitorAdapter#visit(Delete)} never called {@code visitReturningClause}, |
| 117 | + * unlike its Insert and Update counterparts, so expressions in the RETURNING list were never |
| 118 | + * handed to the expression visitor. |
| 119 | + */ |
| 120 | + @Test |
| 121 | + @DisplayName("DELETE .. RETURNING expressions are traversed") |
| 122 | + void deleteReturningExpressionsAreVisited() throws JSQLParserException { |
| 123 | + List<String> functions = new ArrayList<>(); |
| 124 | + |
| 125 | + ExpressionVisitorAdapter<Void> expressions = new ExpressionVisitorAdapter<Void>() { |
| 126 | + @Override |
| 127 | + public <S> Void visit(Function function, S context) { |
| 128 | + functions.add(function.getName().toLowerCase()); |
| 129 | + return super.visit(function, context); |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + StatementVisitorAdapter<Void> visitor = |
| 134 | + new StatementVisitorAdapter<>(new SelectVisitorAdapter<>(expressions)); |
| 135 | + |
| 136 | + CCJSqlParserUtil.parse("DELETE FROM t WHERE a = 1 RETURNING upper(name)") |
| 137 | + .accept(visitor, null); |
| 138 | + |
| 139 | + assertThat(functions).contains("upper"); |
| 140 | + } |
| 141 | +} |
0 commit comments