Skip to content

Commit 98aa90c

Browse files
SheldonKubormjh
and
mjh
authored
fix: issue1875 (#1957)
* fix: issue1875 * fix: code format --------- Co-authored-by: mjh <[email protected]>
1 parent cc7aa01 commit 98aa90c

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

src/main/java/net/sf/jsqlparser/statement/alter/Alter.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class Alter implements Statement {
2424
private Table table;
2525
private boolean useOnly = false;
2626

27+
private boolean useTableIfExists = false;
28+
2729
private List<AlterExpression> alterExpressions;
2830

2931
public Table getTable() {
@@ -42,6 +44,19 @@ public void setUseOnly(boolean useOnly) {
4244
this.useOnly = useOnly;
4345
}
4446

47+
public boolean isUseTableIfExists() {
48+
return useTableIfExists;
49+
}
50+
51+
public void setUseTableIfExists(boolean useTableIfExists) {
52+
this.useTableIfExists = useTableIfExists;
53+
}
54+
55+
public Alter withUseTableIfExists(boolean useTableIfExists) {
56+
this.useTableIfExists = useTableIfExists;
57+
return this;
58+
}
59+
4560
public void addAlterExpression(AlterExpression alterExpression) {
4661
if (alterExpressions == null) {
4762
alterExpressions = new ArrayList<AlterExpression>();
@@ -71,7 +86,7 @@ public String toString() {
7186
b.append("ONLY ");
7287
}
7388

74-
if (alterExpressions.size()>0 && alterExpressions.get(0).getOperation()==AlterOperation.RENAME_TABLE && alterExpressions.get(0).isUsingIfExists()) {
89+
if (useTableIfExists) {
7590
b.append("IF EXISTS ");
7691
}
7792

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

110125
public Alter addAlterExpressions(AlterExpression... alterExpressions) {
111-
List<AlterExpression> collection = Optional.ofNullable(getAlterExpressions()).orElseGet(ArrayList::new);
126+
List<AlterExpression> collection =
127+
Optional.ofNullable(getAlterExpressions()).orElseGet(ArrayList::new);
112128
Collections.addAll(collection, alterExpressions);
113129
return this.withAlterExpressions(collection);
114130
}
115131

116132
public Alter addAlterExpressions(Collection<? extends AlterExpression> alterExpressions) {
117-
List<AlterExpression> collection = Optional.ofNullable(getAlterExpressions()).orElseGet(ArrayList::new);
133+
List<AlterExpression> collection =
134+
Optional.ofNullable(getAlterExpressions()).orElseGet(ArrayList::new);
118135
collection.addAll(alterExpressions);
119136
return this.withAlterExpressions(collection);
120137
}

src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java

+19
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public class AlterExpression implements Serializable {
7070

7171
private String truncatePartitionName = null;
7272

73+
private boolean useIfNotExists = false;
74+
7375
public Index getOldIndex() {
7476
return oldIndex;
7577
}
@@ -417,6 +419,19 @@ public AlterExpression withTruncatePartitionName(String truncatePartitionName) {
417419
return this;
418420
}
419421

422+
public boolean isUseIfNotExists() {
423+
return useIfNotExists;
424+
}
425+
426+
public void setUseIfNotExists(boolean useIfNotExists) {
427+
this.useIfNotExists = useIfNotExists;
428+
}
429+
430+
public AlterExpression withUserIfNotExists(boolean userIfNotExists) {
431+
this.useIfNotExists = userIfNotExists;
432+
return this;
433+
}
434+
420435
@Override
421436
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity",
422437
"PMD.ExcessiveMethodLength", "PMD.SwitchStmtsShouldHaveDefault"})
@@ -493,6 +508,10 @@ public String toString() {
493508
if (hasColumn) {
494509
b.append("COLUMN ");
495510
}
511+
if (useIfNotExists
512+
&& operation == AlterOperation.ADD) {
513+
b.append("IF NOT EXISTS ");
514+
}
496515
}
497516
if (useBrackets && colDataTypeList.size() == 1) {
498517
b.append(" ( ");

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

+3-7
Original file line numberDiff line numberDiff line change
@@ -6293,7 +6293,7 @@ AlterExpression AlterExpression():
62936293
|
62946294
LOOKAHEAD(3) (
62956295
( LOOKAHEAD(2) <K_COLUMN> { alterExp.hasColumn(true); } )?
6296-
6296+
[ <K_IF> <K_NOT> <K_EXISTS> { alterExp.setUseIfNotExists(true); } ]
62976297
(
62986298
LOOKAHEAD(4) (
62996299
"("
@@ -6625,14 +6625,10 @@ Alter AlterTable():
66256625
{
66266626
<K_TABLE>
66276627
[ <K_ONLY> { alter.setUseOnly(true); } ]
6628-
[ LOOKAHEAD(2) <K_IF> <K_EXISTS> { usingIfExists = true; } ]
6629-
6628+
[ LOOKAHEAD(2) <K_IF> <K_EXISTS> { alter.setUseTableIfExists(true); } ]
66306629
table=Table() { alter.setTable(table); }
66316630

6632-
alterExp=AlterExpression() { if (usingIfExists)
6633-
alter.addAlterExpression( alterExp.withUsingIfExists(true) );
6634-
else
6635-
alter.addAlterExpression(alterExp); }
6631+
alterExp=AlterExpression() { alter.addAlterExpression(alterExp); }
66366632

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

src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -988,4 +988,11 @@ public void testIssue1890() throws JSQLParserException {
988988
"ALTER TABLE xdmiddle.ft_mid_sop_sms_send_list_daily TRUNCATE PARTITION sum_date";
989989
assertSqlCanBeParsedAndDeparsed(stmt);
990990
}
991+
992+
@Test
993+
public void testIssue1875() throws JSQLParserException {
994+
String stmt =
995+
"ALTER TABLE IF EXISTS usercenter.dict_surgeries ADD COLUMN IF NOT EXISTS operation_grade_id int8 NULL";
996+
assertSqlCanBeParsedAndDeparsed(stmt);
997+
}
991998
}

0 commit comments

Comments
 (0)