Skip to content

Commit 44c0fe5

Browse files
authored
fix(parser): support IF EXISTS in ALTER TABLE MODIFY COLUMN (#2494)
- accept both IF EXISTS and IF NOT EXISTS with optional COLUMN - preserve existence clauses when deparsing - add round-trip and AST regression tests
1 parent 158b580 commit 44c0fe5

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,9 @@ protected void toStringGeneral(StringBuilder b) {
11181118
} else if (hasColumns) {
11191119
b.append("COLUMNS ");
11201120
}
1121-
if (useIfNotExists
1122-
&& operation == AlterOperation.ADD) {
1121+
if (usingIfExists) {
1122+
b.append("IF EXISTS ");
1123+
} else if (useIfNotExists) {
11231124
b.append("IF NOT EXISTS ");
11241125
}
11251126
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12661,7 +12661,14 @@ AlterExpression AlterExpressionAddAlterModify():
1266112661
<K_COLUMNS> { alterExp.hasColumns(true); }
1266212662
)
1266312663
)?
12664-
[ LOOKAHEAD(2) <K_IF> <K_NOT> <K_EXISTS> { alterExp.setUseIfNotExists(true); } ]
12664+
[
12665+
LOOKAHEAD(2) <K_IF>
12666+
(
12667+
<K_NOT> <K_EXISTS> { alterExp.setUseIfNotExists(true); }
12668+
|
12669+
<K_EXISTS> { alterExp.setUsingIfExists(true); }
12670+
)
12671+
]
1266512672
(
1266612673
LOOKAHEAD(3) AlterExpressionColumnChanges(alterExp)
1266712674
|

src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,25 @@ public void testIssue1875() throws JSQLParserException {
11091109
assertSqlCanBeParsedAndDeparsed(stmt);
11101110
}
11111111

1112+
@ParameterizedTest
1113+
@MethodSource("provideModifyColumnExistenceClauses")
1114+
public void testModifyColumnExistenceClauses(String sql, boolean usingIfExists,
1115+
boolean useIfNotExists) throws JSQLParserException {
1116+
Alter alter = (Alter) assertSqlCanBeParsedAndDeparsed(sql);
1117+
AlterExpression expression = alter.getAlterExpressions().get(0);
1118+
1119+
assertEquals(usingIfExists, expression.isUsingIfExists());
1120+
assertEquals(useIfNotExists, expression.isUseIfNotExists());
1121+
}
1122+
1123+
private static Stream<Arguments> provideModifyColumnExistenceClauses() {
1124+
return Stream.of(
1125+
Arguments.of("ALTER TABLE t MODIFY COLUMN IF EXISTS c INT", true, false),
1126+
Arguments.of("ALTER TABLE t MODIFY IF EXISTS c INT", true, false),
1127+
Arguments.of("ALTER TABLE t MODIFY COLUMN IF NOT EXISTS c INT", false, true),
1128+
Arguments.of("ALTER TABLE t MODIFY IF NOT EXISTS c INT", false, true));
1129+
}
1130+
11121131
@Test
11131132
public void testIssue2027() throws JSQLParserException {
11141133
String sql = "ALTER TABLE `foo_bar` ADD COLUMN `baz` text";

0 commit comments

Comments
 (0)