From fcfdfb7458fd28ff17e02cf088a215f5f8cb059c Mon Sep 17 00:00:00 2001 From: rrrship <107106908+rrrship@users.noreply.github.com> Date: Wed, 20 Jul 2022 00:38:40 +0300 Subject: [PATCH] add support for drop column if exists (#1594) --- .../statement/alter/AlterExpression.java | 3 +++ .../net/sf/jsqlparser/parser/JSqlParserCC.jjt | 1 + .../sf/jsqlparser/statement/alter/AlterTest.java | 14 ++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java b/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java index 0e7422345..b537b4f03 100644 --- a/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java +++ b/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java @@ -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 "); } diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 229a16bff..a5df719b7 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -5991,6 +5991,7 @@ AlterExpression AlterExpression(): | ( ( LOOKAHEAD(2) { alterExp.hasColumn(true); } )? + [ { alterExp.setUsingIfExists(true); } ] (tk= | tk=) { alterExp.setColumnName(tk.image); } [ "INVALIDATE" { alterExp.addParameters("INVALIDATE"); } ] diff --git a/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java b/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java index f977df132..0b233ba4b 100644 --- a/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java @@ -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"); + } }