2929import net .sf .jsqlparser .statement .create .table .Index ;
3030import net .sf .jsqlparser .statement .create .table .NamedConstraint ;
3131import net .sf .jsqlparser .statement .create .table .KeyConstraint ;
32+ import net .sf .jsqlparser .statement .create .table .TableElement ;
3233import net .sf .jsqlparser .statement .create .table .IndexOptionSource ;
3334import net .sf .jsqlparser .statement .create .table .ConstraintKind ;
3435import net .sf .jsqlparser .statement .create .table .PartitionDefinition ;
@@ -822,8 +823,12 @@ private ConstraintKind getAddedConstraintKind() {
822823 }
823824
824825 private boolean hasColumnOrNamedTarget () {
826+ return oldIndex != null || hasGeneralBodyTarget ();
827+ }
828+
829+ private boolean hasGeneralBodyTarget () {
825830 return columnName != null || colDataTypeList != null || constraintName != null
826- || oldIndex != null || commentText != null || columnSetNotNullList != null
831+ || commentText != null || columnSetNotNullList != null
827832 || columnDropNotNullList != null
828833 || columnDropDefaultList != null && !columnDropDefaultList .isEmpty ();
829834 }
@@ -835,12 +840,30 @@ private boolean hasColumnOrNamedTarget() {
835840 */
836841 public boolean hasActiveTableDefinition () {
837842 return operation == AlterOperation .ADD && (constraint != null || index != null )
838- && !(constraintType != null && constraintSymbol != null )
839- && !isRenameOperation () && commentText == null && columnName == null
840- && colDataTypeList == null && columnSetNotNullList == null
841- && columnDropNotNullList == null
842- && (columnDropDefaultList == null || columnDropDefaultList .isEmpty ())
843- && constraintName == null ;
843+ && usesGeneralBody () && !hasGeneralBodyTarget ();
844+ }
845+
846+ /**
847+ * Returns the original definition contributing to the current body, or null when another
848+ * payload takes precedence. A legacy constraint header renders only its key elements, not every
849+ * option of the returned definition. Stored definitions remain available through
850+ * {@link #getConstraint()} and {@link #getIndex()}.
851+ */
852+ public TableElement getActiveTableDefinition () {
853+ if (hasActiveConstraintHeader ()) {
854+ return operation == AlterOperation .ADD && constraint instanceof KeyConstraint
855+ ? constraint
856+ : null ;
857+ }
858+ if (!usesGeneralBody () || hasGeneralBodyTarget ()) {
859+ return null ;
860+ }
861+ return constraint != null ? constraint : index ;
862+ }
863+
864+ /** Whether the selected body renders the legacy constraint header. */
865+ public boolean hasActiveConstraintHeader () {
866+ return hasExplicitConstraintHeader ();
844867 }
845868
846869 /** Sets an index definition or target; a non-null value replaces any constraint definition. */
@@ -1166,7 +1189,7 @@ public final String toString() {
11661189 protected void appendBody (StringBuilder b ) {
11671190 if (operation == AlterOperation .UNSPECIFIC ) {
11681191 b .append (optionalSpecifier );
1169- } else if (hasExplicitConstraintHeader ()) {
1192+ } else if (hasActiveConstraintHeader ()) {
11701193 toStringConstraintAlter (b );
11711194 } else if (usesColumnActionBody ()) {
11721195 toStringAlterColumn (b );
@@ -1197,7 +1220,7 @@ private boolean hasAlterColumnValueActions() {
11971220 * overriding {@link #appendBody(StringBuilder)} must reflect their dispatch here as well.
11981221 */
11991222 protected boolean usesColumnActionBody () {
1200- return hasAlterColumnValueActions () && !hasExplicitConstraintHeader ();
1223+ return hasAlterColumnValueActions () && !hasActiveConstraintHeader ();
12011224 }
12021225
12031226 /**
@@ -1206,8 +1229,13 @@ protected boolean usesColumnActionBody() {
12061229 * deparsing. Subclasses with their own body dispatch override this method accordingly.
12071230 */
12081231 public boolean hasActiveColumnDefinitions () {
1209- return hasColumnDefinitionPayload () && operation != AlterOperation .UNSPECIFIC
1210- && !hasExplicitConstraintHeader () && !usesColumnActionBody ()
1232+ return hasColumnDefinitionPayload () && usesGeneralBody ();
1233+ }
1234+
1235+ /** Whether this action selects the general body, before choosing its active payload. */
1236+ protected boolean usesGeneralBody () {
1237+ return operation != AlterOperation .UNSPECIFIC
1238+ && !hasActiveConstraintHeader () && !usesColumnActionBody ()
12111239 && !isSimpleKeywordOperation () && !isRenameOperation () && !isDropSpecialOperation ()
12121240 && operation != AlterOperation .CONVERT && operation != AlterOperation .COLLATE
12131241 && !isPartitionOperation ();
0 commit comments