Skip to content

Commit

Permalink
add support for drop column if exists (#1594)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrship authored Jul 19, 2022
1 parent 191b9fd commit fcfdfb7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ public String toString() {
if (hasColumn) {
b.append("COLUMN ");
}
if (usingIfExists) {
b.append("IF EXISTS ");
}
if (operation == AlterOperation.RENAME) {
b.append(columnOldName).append(" TO ");
}
Expand Down
1 change: 1 addition & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5991,6 +5991,7 @@ AlterExpression AlterExpression():
|
(
( LOOKAHEAD(2) <K_COLUMN> { alterExp.hasColumn(true); } )?
[<K_IF> <K_EXISTS> { alterExp.setUsingIfExists(true); } ]
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { alterExp.setColumnName(tk.image); }

[ "INVALIDATE" { alterExp.addParameters("INVALIDATE"); } ]
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -789,4 +789,18 @@ public void testAlterTableChangeColumnDropDefault() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE a MODIFY (COLUMN b DROP DEFAULT, COLUMN b DROP NOT NULL)", true);
}

@Test
public void testAlterTableDropColumnIfExists() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE test DROP COLUMN IF EXISTS name");
}

@Test
public void testAlterTableDropMultipleColumnsIfExists() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE test DROP COLUMN IF EXISTS name, DROP COLUMN IF EXISTS surname");
}

@Test
public void testAlterTableDropMultipleColumnsIfExistsWithParams() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE test DROP COLUMN IF EXISTS name CASCADE, DROP COLUMN IF EXISTS surname CASCADE");
}
}

0 comments on commit fcfdfb7

Please sign in to comment.