Skip to content

Commit 9a36a48

Browse files
authored
feat: add support for parsing MySQL ALTER TABLE statements with new options (#2223)
1 parent ec053b3 commit 9a36a48

File tree

4 files changed

+436
-92
lines changed

4 files changed

+436
-92
lines changed

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

+135
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public class AlterExpression implements Serializable {
3636
private List<ColumnDataType> colDataTypeList;
3737
private List<ColumnDropNotNull> columnDropNotNullList;
3838
private List<ColumnDropDefault> columnDropDefaultList;
39+
private List<ColumnSetDefault> columnSetDefaultList;
40+
private List<ColumnSetVisibility> columnSetVisibilityList;
41+
3942
private List<String> pkColumns;
4043
private List<String> ukColumns;
4144
private String ukName;
@@ -88,6 +91,10 @@ public class AlterExpression implements Serializable {
8891

8992
private int keyBlockSize;
9093

94+
private String constraintSymbol;
95+
private boolean enforced;
96+
private String constraintType;
97+
9198
public Index getOldIndex() {
9299
return oldIndex;
93100
}
@@ -302,13 +309,39 @@ public void addColDropNotNull(ColumnDropNotNull columnDropNotNull) {
302309
columnDropNotNullList.add(columnDropNotNull);
303310
}
304311

312+
public List<ColumnDropDefault> getColumnDropDefaultList() {
313+
return columnDropDefaultList;
314+
}
315+
305316
public void addColDropDefault(ColumnDropDefault columnDropDefault) {
306317
if (columnDropDefaultList == null) {
307318
columnDropDefaultList = new ArrayList<>();
308319
}
309320
columnDropDefaultList.add(columnDropDefault);
310321
}
311322

323+
public void addColSetDefault(ColumnSetDefault columnSetDefault) {
324+
if (columnSetDefaultList == null) {
325+
columnSetDefaultList = new ArrayList<>();
326+
}
327+
columnSetDefaultList.add(columnSetDefault);
328+
}
329+
330+
public List<ColumnSetDefault> getColumnSetDefaultList() {
331+
return columnSetDefaultList;
332+
}
333+
334+
public void addColSetVisibility(ColumnSetVisibility columnSetVisibility) {
335+
if (columnSetVisibilityList == null) {
336+
columnSetVisibilityList = new ArrayList<>();
337+
}
338+
columnSetVisibilityList.add(columnSetVisibility);
339+
}
340+
341+
public List<ColumnSetVisibility> getColumnSetVisibilityList() {
342+
return columnSetVisibilityList;
343+
}
344+
312345
public List<String> getFkSourceColumns() {
313346
return fkSourceColumns;
314347
}
@@ -571,6 +604,30 @@ public int getKeyBlockSize() {
571604
return keyBlockSize;
572605
}
573606

607+
public String getConstraintSymbol() {
608+
return constraintSymbol;
609+
}
610+
611+
public void setConstraintSymbol(String constraintSymbol) {
612+
this.constraintSymbol = constraintSymbol;
613+
}
614+
615+
public boolean isEnforced() {
616+
return enforced;
617+
}
618+
619+
public void setEnforced(boolean enforced) {
620+
this.enforced = enforced;
621+
}
622+
623+
public String getConstraintType() {
624+
return constraintType;
625+
}
626+
627+
public void setConstraintType(String constraintType) {
628+
this.constraintType = constraintType;
629+
}
630+
574631
@Override
575632
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity",
576633
"PMD.ExcessiveMethodLength", "PMD.SwitchStmtsShouldHaveDefault"})
@@ -580,6 +637,38 @@ public String toString() {
580637

581638
if (operation == AlterOperation.UNSPECIFIC) {
582639
b.append(optionalSpecifier);
640+
} else if (operation == AlterOperation.ALTER && constraintType != null) {
641+
b.append("ALTER");
642+
b.append(" ").append(constraintType);
643+
644+
if (constraintSymbol != null) {
645+
b.append(" ").append(constraintSymbol);
646+
}
647+
if (!isEnforced()) {
648+
b.append(" NOT ");
649+
}
650+
b.append(" ENFORCED");
651+
} else if (operation == AlterOperation.ALTER
652+
&& columnDropDefaultList != null && !columnDropDefaultList.isEmpty()) {
653+
b.append("ALTER ");
654+
if (hasColumn) {
655+
b.append("COLUMN ");
656+
}
657+
b.append(PlainSelect.getStringList(columnDropDefaultList));
658+
} else if (operation == AlterOperation.ALTER
659+
&& columnSetDefaultList != null && !columnSetDefaultList.isEmpty()) {
660+
b.append("ALTER ");
661+
if (hasColumn) {
662+
b.append("COLUMN ");
663+
}
664+
b.append(PlainSelect.getStringList(columnSetDefaultList));
665+
} else if (operation == AlterOperation.ALTER
666+
&& columnSetVisibilityList != null && !columnSetVisibilityList.isEmpty()) {
667+
b.append("ALTER ");
668+
if (hasColumn) {
669+
b.append("COLUMN ");
670+
}
671+
b.append(PlainSelect.getStringList(columnSetVisibilityList));
583672
} else if (operation == AlterOperation.SET_TABLE_OPTION) {
584673
b.append(tableOption);
585674
} else if (operation == AlterOperation.DISCARD_TABLESPACE) {
@@ -1139,6 +1228,52 @@ public String toString() {
11391228
}
11401229
}
11411230

1231+
public static final class ColumnSetDefault implements Serializable {
1232+
private final String columnName;
1233+
private final String defaultValue;
1234+
1235+
public ColumnSetDefault(String columnName, String defaultValue) {
1236+
this.columnName = columnName;
1237+
this.defaultValue = defaultValue;
1238+
}
1239+
1240+
public String getColumnName() {
1241+
return columnName;
1242+
}
1243+
1244+
public String getDefaultValue() {
1245+
return defaultValue;
1246+
}
1247+
1248+
@Override
1249+
public String toString() {
1250+
return columnName + " SET DEFAULT " + defaultValue;
1251+
}
1252+
}
1253+
1254+
public static final class ColumnSetVisibility implements Serializable {
1255+
private final String columnName;
1256+
private final boolean visible;
1257+
1258+
public ColumnSetVisibility(String columnName, boolean visible) {
1259+
this.columnName = columnName;
1260+
this.visible = visible;
1261+
}
1262+
1263+
public String getColumnName() {
1264+
return columnName;
1265+
}
1266+
1267+
public boolean isVisible() {
1268+
return visible;
1269+
}
1270+
1271+
@Override
1272+
public String toString() {
1273+
return columnName + " SET " + (visible ? " VISIBLE" : " INVISIBLE");
1274+
}
1275+
}
1276+
11421277
public enum ConvertType {
11431278
CONVERT_TO, DEFAULT_CHARACTER_SET, CHARACTER_SET
11441279
}

src/main/java/net/sf/jsqlparser/statement/create/table/Index.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ public List<String> getColumnsNames() {
3737
}
3838

3939
public void setColumnsNames(List<String> list) {
40-
columns = list.stream().map(ColumnParams::new).collect(toList());
40+
if (list == null) {
41+
this.columns = Collections.emptyList();
42+
} else {
43+
this.columns = list.stream().map(ColumnParams::new).collect(toList());
44+
}
4145
}
4246

4347
@Deprecated
@@ -159,7 +163,9 @@ public String toString() {
159163
(!name.isEmpty() ? " " + getName() : "") +
160164
(using != null ? " USING " + using : "");
161165

162-
String tail = PlainSelect.getStringList(columns, true, true)
166+
String tail = (columns != null && !columns.isEmpty()
167+
? PlainSelect.getStringList(columns, true, true)
168+
: "")
163169
+ (!idxSpecText.isEmpty() ? " " + idxSpecText : "");
164170

165171
return tail.isEmpty() ? head : head + " " + tail;

0 commit comments

Comments
 (0)