Skip to content

Commit ab57cde

Browse files
fix: walk SET, DECLARE and ASSERT expressions in StatementFeatureVisitor
The SET and DECLARE visits only reported MODIFIES_SESSION and the ASSERT visit only READS_DATA, so the expressions these statements evaluate were never checked: SET @x = f() and DECLARE @x INT = f() gave mayModifyData() == false with no unresolved reference, while SELECT f() leaves MODIFIES_DATA possible. SET now goes through the adapter walk, DECLARE walks its initial values and ASSERT its condition.
1 parent 34183f7 commit ab57cde

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ public <S> Void visit(PragmaStatement pragmaStatement, S context) {
638638
public <S> Void visit(AssertStatement assertStatement, S context) {
639639
analysis.claimTopLevel();
640640
analysis.certain(StmtFeature.READS_DATA);
641+
assertStatement.getExpression().accept(analysis.expressions, context);
641642
return null;
642643
}
643644

@@ -1112,7 +1113,9 @@ private Void transactionOnly() {
11121113

11131114
@Override
11141115
public <S> Void visit(SetStatement set, S context) {
1115-
return sessionOnly();
1116+
sessionOnly();
1117+
// the assigned values are evaluated, as in SET @x = f()
1118+
return super.visit(set, context);
11161119
}
11171120

11181121
@Override
@@ -1147,7 +1150,14 @@ public <S> Void visit(AlterSession alterSession, S context) {
11471150

11481151
@Override
11491152
public <S> Void visit(DeclareStatement declareStatement, S context) {
1150-
return sessionOnly();
1153+
sessionOnly();
1154+
// the initial values are evaluated, as in DECLARE @x INT = f()
1155+
for (DeclareStatement.TypeDefExpr typeDefExpr : declareStatement.getTypeDefExprList()) {
1156+
if (typeDefExpr.getDefaultExpr() != null) {
1157+
typeDefExpr.getDefaultExpr().accept(analysis.expressions, context);
1158+
}
1159+
}
1160+
return null;
11511161
}
11521162

11531163
@Override

‎src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,12 @@ private StatementFeatures analyseAllPureExcept(String sql, String impure)
708708
"TABLE t ORDER BY pg_read_file('x')",
709709
"TABLE t LIMIT length(pg_read_file('x'))",
710710
"TABLE t OFFSET length(pg_read_file('x'))",
711-
"TABLE t FETCH FIRST length(pg_read_file('x')) ROWS ONLY"})
711+
"TABLE t FETCH FIRST length(pg_read_file('x')) ROWS ONLY",
712+
"SET @x = pg_read_file('x')",
713+
"SET @a = 1, @b = pg_read_file('x')",
714+
"SET x = pg_read_file('x')",
715+
"DECLARE @x INT = length(pg_read_file('x'))",
716+
"ASSERT length(pg_read_file('x')) > 0"})
712717
void functionIsChecked(String sql) throws JSQLParserException {
713718
StatementFeatures impure = analyseAllPureExcept(sql, "pg_read_file");
714719
assertThat(impure.getUnresolvedReferences()).containsExactly("pg_read_file");
@@ -765,6 +770,17 @@ void columnOutsideTheSequenceSyntaxIsNotChecked() throws JSQLParserException {
765770
assertThat(f.getUnresolvedReferences()).isEmpty();
766771
}
767772

773+
@ParameterizedTest(name = "{0}")
774+
@ValueSource(strings = {
775+
"SET @x = (SELECT a FROM t)",
776+
"DECLARE @x INT = (SELECT a FROM t)"})
777+
void assignedSubqueryIsRead(String sql) throws JSQLParserException {
778+
StatementFeatures f = analyse(sql);
779+
assertThat(f.is(StmtFeature.MODIFIES_SESSION)).isTrue();
780+
assertThat(f.is(StmtFeature.READS_DATA)).isTrue();
781+
assertThat(f.returnsResultSet()).isFalse();
782+
}
783+
768784
@Test
769785
void topLevelValuesReturnsRowsWithoutReading() throws JSQLParserException {
770786
StatementFeatures f = analyse("VALUES (1, 2)");

0 commit comments

Comments
 (0)