Skip to content

Commit

Permalink
Support Alter Table Drop Constraint If Exists (#709)
Browse files Browse the repository at this point in the history
* Support Alter Table Drop Constraint If Exists

* #709 add constraintIfExists flag
  • Loading branch information
rfscholte authored and wumpz committed Dec 13, 2018
1 parent b3263c8 commit 08fedf7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class AlterExpression {
private String ukName;
private Index index = null;
private String constraintName;
private boolean constraintIfExists;
private boolean onDeleteRestrict;
private boolean onDeleteSetNull;
private boolean onDeleteCascade;
Expand Down Expand Up @@ -143,6 +144,14 @@ public String getConstraintName() {
public void setConstraintName(final String constraintName) {
this.constraintName = constraintName;
}

public boolean isConstraintIfExists() {
return constraintIfExists;
}

public void setConstraintIfExists( boolean constraintIfExists ) {
this.constraintIfExists = constraintIfExists;
}

public List<String> getPkColumns() {
return pkColumns;
Expand Down Expand Up @@ -215,7 +224,11 @@ public String toString() {
b.append(")");
}
} else if (constraintName != null) {
b.append("CONSTRAINT ").append(constraintName);
b.append("CONSTRAINT ");
if(constraintIfExists) {
b.append("IF EXISTS ");
}
b.append(constraintName);
} else if (pkColumns != null) {
b.append("PRIMARY KEY (").append(PlainSelect.getStringList(pkColumns)).append(')');
} else if (ukColumns != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -3864,7 +3864,7 @@ AlterExpression AlterExpression():

)
|
( <K_CONSTRAINT>
( <K_CONSTRAINT> [<K_IF> <K_EXISTS> { alterExp.setConstraintIfExists(true); } ]
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>)
{
alterExp.setConstraintName(tk.image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public void testAlterTableDropConstraint() throws JSQLParserException {
assertEquals(alterExpression.getConstraintName(), "YYY");
}

@Test
public void testAlterTableDropConstraintIfExists() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE Persons DROP CONSTRAINT IF EXISTS UC_Person");
}

@Test
public void testAlterTablePK() throws JSQLParserException {
final String sql = "ALTER TABLE `Author` ADD CONSTRAINT `AuthorPK` PRIMARY KEY (`ID`)";
Expand Down

0 comments on commit 08fedf7

Please sign in to comment.