Skip to content

Commit 95dc95b

Browse files
committed
fix: parse VALIDATE CONSTRAINT as a structured action in every dialect
ALTER TABLE t VALIDATE CONSTRAINT ck was a structured RelationAlterAction only with the PostgreSQL dialect. Elsewhere the remaining text became an UNSPECIFIC expression, so the constraint name could not be read and the action could not follow an ADD that now parses in every dialect. VALIDATE CONSTRAINT starts no other ALTER TABLE action, so recognise it in every dialect. Its output is unchanged. Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent f257070 commit 95dc95b

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3050,6 +3050,8 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
30503050
&& "EXPRESSION".equalsIgnoreCase(getToken(columnActionOffset + 1).image)) {
30513051
return true;
30523052
}
3053+
// VALIDATE CONSTRAINT starts no other ALTER TABLE action in any dialect.
3054+
if (first.kind == K_VALIDATE && getToken(2).kind == K_CONSTRAINT) { return true; }
30533055
if (!Dialect.POSTGRESQL.name().equals(getAsString(Feature.dialect))) { return false; }
30543056
if (first.kind == K_VALIDATE || first.kind == K_RESET || isKeywordAhead("INHERIT")
30553057
|| isKeywordAhead("REPLICA") || isKeywordAhead("OWNER") || isKeywordAhead("CLUSTER")

‎src/test/java/net/sf/jsqlparser/statement/alter/PostgreSqlRelationAlterTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,23 @@ void bulkTablespaceMovesShareOneModel(String objectType) throws JSQLParserExcept
229229
assertEquals(2, CCJSqlParserUtil.parseStatements(statement + "; SELECT 1").size());
230230
}
231231

232+
@Test
233+
void validateConstraintIsStructuredWithoutThePostgreSqlDialect() throws JSQLParserException {
234+
for (Dialect dialect : new Dialect[] {null, Dialect.ANSI_SQL, Dialect.MYSQL}) {
235+
Alter alter = (Alter) assertSqlCanBeParsedAndDeparsed(
236+
"ALTER TABLE t ADD CHECK (c > 0) NOT VALID, VALIDATE CONSTRAINT ck", true,
237+
p -> {
238+
if (dialect != null) {
239+
p.withDialect(dialect);
240+
}
241+
});
242+
RelationAlterAction validate = assertInstanceOf(RelationAlterAction.class,
243+
alter.getAlterExpressions().get(1));
244+
assertEquals(RelationAlterAction.Kind.VALIDATE_CONSTRAINT, validate.getKind());
245+
assertEquals("ck", validate.getConstraintName());
246+
}
247+
}
248+
232249
private static Statement parse(String sql) throws JSQLParserException {
233250
return CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL));
234251
}

0 commit comments

Comments
 (0)