Skip to content

Commit

Permalink
fix: issue1875 (#1957)
Browse files Browse the repository at this point in the history
* fix: issue1875

* fix: code format

---------

Co-authored-by: mjh <majh118@chinaunicom.cn>
  • Loading branch information
SheldonKubor and mjh authored Feb 7, 2024
1 parent cc7aa01 commit 98aa90c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
23 changes: 20 additions & 3 deletions src/main/java/net/sf/jsqlparser/statement/alter/Alter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class Alter implements Statement {
private Table table;
private boolean useOnly = false;

private boolean useTableIfExists = false;

private List<AlterExpression> alterExpressions;

public Table getTable() {
Expand All @@ -42,6 +44,19 @@ public void setUseOnly(boolean useOnly) {
this.useOnly = useOnly;
}

public boolean isUseTableIfExists() {
return useTableIfExists;
}

public void setUseTableIfExists(boolean useTableIfExists) {
this.useTableIfExists = useTableIfExists;
}

public Alter withUseTableIfExists(boolean useTableIfExists) {
this.useTableIfExists = useTableIfExists;
return this;
}

public void addAlterExpression(AlterExpression alterExpression) {
if (alterExpressions == null) {
alterExpressions = new ArrayList<AlterExpression>();
Expand Down Expand Up @@ -71,7 +86,7 @@ public String toString() {
b.append("ONLY ");
}

if (alterExpressions.size()>0 && alterExpressions.get(0).getOperation()==AlterOperation.RENAME_TABLE && alterExpressions.get(0).isUsingIfExists()) {
if (useTableIfExists) {
b.append("IF EXISTS ");
}

Expand Down Expand Up @@ -108,13 +123,15 @@ public Alter withAlterExpressions(List<AlterExpression> alterExpressions) {
}

public Alter addAlterExpressions(AlterExpression... alterExpressions) {
List<AlterExpression> collection = Optional.ofNullable(getAlterExpressions()).orElseGet(ArrayList::new);
List<AlterExpression> collection =
Optional.ofNullable(getAlterExpressions()).orElseGet(ArrayList::new);
Collections.addAll(collection, alterExpressions);
return this.withAlterExpressions(collection);
}

public Alter addAlterExpressions(Collection<? extends AlterExpression> alterExpressions) {
List<AlterExpression> collection = Optional.ofNullable(getAlterExpressions()).orElseGet(ArrayList::new);
List<AlterExpression> collection =
Optional.ofNullable(getAlterExpressions()).orElseGet(ArrayList::new);
collection.addAll(alterExpressions);
return this.withAlterExpressions(collection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class AlterExpression implements Serializable {

private String truncatePartitionName = null;

private boolean useIfNotExists = false;

public Index getOldIndex() {
return oldIndex;
}
Expand Down Expand Up @@ -417,6 +419,19 @@ public AlterExpression withTruncatePartitionName(String truncatePartitionName) {
return this;
}

public boolean isUseIfNotExists() {
return useIfNotExists;
}

public void setUseIfNotExists(boolean useIfNotExists) {
this.useIfNotExists = useIfNotExists;
}

public AlterExpression withUserIfNotExists(boolean userIfNotExists) {
this.useIfNotExists = userIfNotExists;
return this;
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity",
"PMD.ExcessiveMethodLength", "PMD.SwitchStmtsShouldHaveDefault"})
Expand Down Expand Up @@ -493,6 +508,10 @@ public String toString() {
if (hasColumn) {
b.append("COLUMN ");
}
if (useIfNotExists
&& operation == AlterOperation.ADD) {
b.append("IF NOT EXISTS ");
}
}
if (useBrackets && colDataTypeList.size() == 1) {
b.append(" ( ");
Expand Down
10 changes: 3 additions & 7 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -6293,7 +6293,7 @@ AlterExpression AlterExpression():
|
LOOKAHEAD(3) (
( LOOKAHEAD(2) <K_COLUMN> { alterExp.hasColumn(true); } )?

[ <K_IF> <K_NOT> <K_EXISTS> { alterExp.setUseIfNotExists(true); } ]
(
LOOKAHEAD(4) (
"("
Expand Down Expand Up @@ -6625,14 +6625,10 @@ Alter AlterTable():
{
<K_TABLE>
[ <K_ONLY> { alter.setUseOnly(true); } ]
[ LOOKAHEAD(2) <K_IF> <K_EXISTS> { usingIfExists = true; } ]

[ LOOKAHEAD(2) <K_IF> <K_EXISTS> { alter.setUseTableIfExists(true); } ]
table=Table() { alter.setTable(table); }

alterExp=AlterExpression() { if (usingIfExists)
alter.addAlterExpression( alterExp.withUsingIfExists(true) );
else
alter.addAlterExpression(alterExp); }
alterExp=AlterExpression() { alter.addAlterExpression(alterExp); }

("," alterExp=AlterExpression() { alter.addAlterExpression(alterExp); } )*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -988,4 +988,11 @@ public void testIssue1890() throws JSQLParserException {
"ALTER TABLE xdmiddle.ft_mid_sop_sms_send_list_daily TRUNCATE PARTITION sum_date";
assertSqlCanBeParsedAndDeparsed(stmt);
}

@Test
public void testIssue1875() throws JSQLParserException {
String stmt =
"ALTER TABLE IF EXISTS usercenter.dict_surgeries ADD COLUMN IF NOT EXISTS operation_grade_id int8 NULL";
assertSqlCanBeParsedAndDeparsed(stmt);
}
}

0 comments on commit 98aa90c

Please sign in to comment.