3030import net .sf .jsqlparser .statement .create .table .NamedConstraint ;
3131import net .sf .jsqlparser .statement .create .table .KeyConstraint ;
3232import net .sf .jsqlparser .statement .create .table .TableElement ;
33+ import net .sf .jsqlparser .statement .create .table .TableElementList ;
3334import net .sf .jsqlparser .statement .create .table .IndexOptionSource ;
3435import net .sf .jsqlparser .statement .create .table .ConstraintKind ;
3536import net .sf .jsqlparser .statement .create .table .PartitionDefinition ;
@@ -50,6 +51,7 @@ public enum TableRenameKeyword {
5051 // private ColDataType dataType;
5152 private String columnOldName ;
5253 private List <ColumnDataType > colDataTypeList ;
54+ private List <TableElement > addGroupElements ;
5355 private List <ColumnDropNotNull > columnDropNotNullList ;
5456 private List <ColumnSetNotNull > columnSetNotNullList ;
5557 private List <ColumnDropDefault > columnDropDefaultList ;
@@ -434,15 +436,73 @@ private void setForeignKeyTablePart(String value, int partIndex) {
434436 table .setNameParts (parts );
435437 }
436438
439+ /**
440+ * Returns the mutable column list, filtering out the other declarations of a mixed ADD group.
441+ */
437442 public List <ColumnDataType > getColDataTypeList () {
443+ if (hasAddGroupElements ()) {
444+ return new TableElementList <>(addGroupElements , ColumnDataType .class );
445+ }
438446 return colDataTypeList ;
439447 }
440448
449+ /**
450+ * Returns the mutable declarations of a parenthesized mixed ADD group in source order. Parsed
451+ * columns are {@link ColumnDataType} nodes shared with {@link #getColDataTypeList()}.
452+ */
453+ public List <TableElement > getAddGroupElements () {
454+ return addGroupElements ;
455+ }
456+
457+ /** Copies the group membership, retaining the mutable column, index and constraint nodes. */
458+ public void setAddGroupElements (List <TableElement > elements ) {
459+ addGroupElements = elements == null ? null : new ArrayList <>(elements );
460+ if (elements != null ) {
461+ colDataTypeList = null ;
462+ constraint = null ;
463+ index = null ;
464+ useBrackets = true ;
465+ }
466+ }
467+
468+ public boolean hasAddGroupElements () {
469+ return operation == AlterOperation .ADD && addGroupElements != null ;
470+ }
471+
472+ private <E extends TableElement > E firstAddGroupElement (Class <E > type ) {
473+ for (TableElement element : addGroupElements ) {
474+ if (type .isInstance (element )) {
475+ return type .cast (element );
476+ }
477+ }
478+ return null ;
479+ }
480+
481+ private <E extends TableElement > void replaceFirstAddGroupElement (Class <E > type , E element ) {
482+ for (int i = 0 ; i < addGroupElements .size (); i ++) {
483+ if (type .isInstance (addGroupElements .get (i ))) {
484+ if (element == null ) {
485+ addGroupElements .remove (i );
486+ } else {
487+ addGroupElements .set (i , element );
488+ }
489+ return ;
490+ }
491+ }
492+ if (element != null ) {
493+ addGroupElements .add (element );
494+ }
495+ }
496+
441497 public void addColDataType (String columnName , ColDataType colDataType ) {
442498 addColDataType (new ColumnDataType (columnName , false , colDataType , null ));
443499 }
444500
445501 public void addColDataType (ColumnDataType columnDataType ) {
502+ if (hasAddGroupElements ()) {
503+ addGroupElements .add (columnDataType );
504+ return ;
505+ }
446506 if (colDataTypeList == null ) {
447507 colDataTypeList = new ArrayList <>();
448508 }
@@ -782,7 +842,11 @@ public void setUkName(String ukName) {
782842 }
783843 }
784844
845+ /** Returns the ordinary index, or the first ordinary index in a mixed ADD group. */
785846 public Index getIndex () {
847+ if (hasAddGroupElements ()) {
848+ return firstAddGroupElement (Index .class );
849+ }
786850 return index ;
787851 }
788852
@@ -878,22 +942,39 @@ public boolean hasActiveConstraintHeader() {
878942 return hasExplicitConstraintHeader ();
879943 }
880944
881- /** Sets an index definition or target; a non-null value replaces any constraint definition. */
945+ /**
946+ * Sets an index definition or target; a non-null value replaces any constraint definition. In a
947+ * mixed ADD group, replaces or removes only its first ordinary index.
948+ */
882949 public void setIndex (Index index ) {
950+ if (hasAddGroupElements ()) {
951+ replaceFirstAddGroupElement (Index .class , index );
952+ return ;
953+ }
883954 this .index = index ;
884955 if (index != null ) {
885956 clearReplacedConstraintAliases (null );
886957 constraint = null ;
887958 }
888959 }
889960
890- /** Returns the constraint definition, distinct from an ordinary index or a named target . */
961+ /** Returns the constraint definition, or the first constraint in a mixed ADD group . */
891962 public NamedConstraint getConstraint () {
963+ if (hasAddGroupElements ()) {
964+ return firstAddGroupElement (NamedConstraint .class );
965+ }
892966 return constraint ;
893967 }
894968
895- /** Sets a constraint definition; a non-null value replaces any ordinary index definition. */
969+ /**
970+ * Sets a constraint definition; a non-null value replaces any ordinary index definition. In a
971+ * mixed ADD group, replaces or removes only its first constraint.
972+ */
896973 public void setConstraint (NamedConstraint constraint ) {
974+ if (hasAddGroupElements ()) {
975+ replaceFirstAddGroupElement (NamedConstraint .class , constraint );
976+ return ;
977+ }
897978 clearReplacedConstraintAliases (constraint );
898979 this .constraint = constraint ;
899980 if (constraint != null ) {
@@ -1208,7 +1289,9 @@ public final String toString() {
12081289 @ SuppressWarnings ({"PMD.CyclomaticComplexity" , "PMD.NPathComplexity" ,
12091290 "PMD.ExcessiveMethodLength" , "PMD.SwitchStmtsShouldHaveDefault" })
12101291 protected void appendBody (StringBuilder b ) {
1211- if (operation == AlterOperation .UNSPECIFIC ) {
1292+ if (hasAddGroupElements ()) {
1293+ appendAddGroupBody (b , b ::append );
1294+ } else if (operation == AlterOperation .UNSPECIFIC ) {
12121295 b .append (optionalSpecifier );
12131296 } else if (hasActiveConstraintHeader ()) {
12141297 toStringConstraintAlter (b );
@@ -1250,7 +1333,28 @@ protected boolean usesColumnActionBody() {
12501333 * deparsing. Subclasses with their own body dispatch override this method accordingly.
12511334 */
12521335 public boolean hasActiveColumnDefinitions () {
1253- return hasColumnDefinitionPayload () && usesGeneralBody ();
1336+ return hasAddGroupElements () || hasColumnDefinitionPayload () && usesGeneralBody ();
1337+ }
1338+
1339+ /** Appends a mixed ADD group using the supplied renderer for every declaration. */
1340+ public void appendAddGroupTo (StringBuilder b , Consumer <TableElement > elementPrinter ) {
1341+ appendAddGroupBody (b , elementPrinter );
1342+ appendCommonTail (b );
1343+ }
1344+
1345+ private void appendAddGroupBody (StringBuilder b , Consumer <TableElement > elementPrinter ) {
1346+ b .append ("ADD " );
1347+ if (hasColumn ) {
1348+ b .append ("COLUMN " );
1349+ }
1350+ b .append ('(' );
1351+ for (int i = 0 ; i < addGroupElements .size (); i ++) {
1352+ if (i > 0 ) {
1353+ b .append (", " );
1354+ }
1355+ elementPrinter .accept (addGroupElements .get (i ));
1356+ }
1357+ b .append (')' );
12541358 }
12551359
12561360 /** Whether this action selects the general body, before choosing its active payload. */
0 commit comments