Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add support for drop column if exists
  • Loading branch information
rrrship committed Jul 19, 2022
commit a319fca363da8403370ee131a3e6f1f9454efd7f
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 @@ -5861,6 +5861,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");
}
}