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 ;
@@ -763,8 +764,12 @@ private ConstraintKind getAddedConstraintKind() {
763764 }
764765
765766 private boolean hasColumnOrNamedTarget () {
767+ return oldIndex != null || hasGeneralBodyTarget ();
768+ }
769+
770+ private boolean hasGeneralBodyTarget () {
766771 return columnName != null || colDataTypeList != null || constraintName != null
767- || oldIndex != null || commentText != null || columnSetNotNullList != null
772+ || commentText != null || columnSetNotNullList != null
768773 || columnDropNotNullList != null
769774 || columnDropDefaultList != null && !columnDropDefaultList .isEmpty ();
770775 }
@@ -776,12 +781,30 @@ private boolean hasColumnOrNamedTarget() {
776781 */
777782 public boolean hasActiveTableDefinition () {
778783 return operation == AlterOperation .ADD && (constraint != null || index != null )
779- && !(constraintType != null && constraintSymbol != null )
780- && !isRenameOperation () && commentText == null && columnName == null
781- && colDataTypeList == null && columnSetNotNullList == null
782- && columnDropNotNullList == null
783- && (columnDropDefaultList == null || columnDropDefaultList .isEmpty ())
784- && constraintName == null ;
784+ && usesGeneralBody () && !hasGeneralBodyTarget ();
785+ }
786+
787+ /**
788+ * Returns the original definition contributing to the current body, or null when another
789+ * payload takes precedence. A legacy constraint header renders only its key elements, not every
790+ * option of the returned definition. Stored definitions remain available through
791+ * {@link #getConstraint()} and {@link #getIndex()}.
792+ */
793+ public TableElement getActiveTableDefinition () {
794+ if (hasActiveConstraintHeader ()) {
795+ return operation == AlterOperation .ADD && constraint instanceof KeyConstraint
796+ ? constraint
797+ : null ;
798+ }
799+ if (!usesGeneralBody () || hasGeneralBodyTarget ()) {
800+ return null ;
801+ }
802+ return constraint != null ? constraint : index ;
803+ }
804+
805+ /** Whether the selected body renders the legacy constraint header. */
806+ public boolean hasActiveConstraintHeader () {
807+ return hasExplicitConstraintHeader ();
785808 }
786809
787810 /** Sets an index definition or target; a non-null value replaces any constraint definition. */
@@ -1074,7 +1097,7 @@ public final String toString() {
10741097 protected void appendBody (StringBuilder b ) {
10751098 if (operation == AlterOperation .UNSPECIFIC ) {
10761099 b .append (optionalSpecifier );
1077- } else if (hasExplicitConstraintHeader ()) {
1100+ } else if (hasActiveConstraintHeader ()) {
10781101 toStringConstraintAlter (b );
10791102 } else if (usesColumnActionBody ()) {
10801103 toStringAlterColumn (b );
@@ -1105,7 +1128,7 @@ private boolean hasAlterColumnValueActions() {
11051128 * overriding {@link #appendBody(StringBuilder)} must reflect their dispatch here as well.
11061129 */
11071130 protected boolean usesColumnActionBody () {
1108- return hasAlterColumnValueActions () && !hasExplicitConstraintHeader ();
1131+ return hasAlterColumnValueActions () && !hasActiveConstraintHeader ();
11091132 }
11101133
11111134 /**
@@ -1114,8 +1137,13 @@ protected boolean usesColumnActionBody() {
11141137 * deparsing. Subclasses with their own body dispatch override this method accordingly.
11151138 */
11161139 public boolean hasActiveColumnDefinitions () {
1117- return hasColumnDefinitionPayload () && operation != AlterOperation .UNSPECIFIC
1118- && !hasExplicitConstraintHeader () && !usesColumnActionBody ()
1140+ return hasColumnDefinitionPayload () && usesGeneralBody ();
1141+ }
1142+
1143+ /** Whether this action selects the general body, before choosing its active payload. */
1144+ protected boolean usesGeneralBody () {
1145+ return operation != AlterOperation .UNSPECIFIC
1146+ && !hasActiveConstraintHeader () && !usesColumnActionBody ()
11191147 && !isSimpleKeywordOperation () && !isRenameOperation () && !isDropSpecialOperation ()
11201148 && operation != AlterOperation .CONVERT && operation != AlterOperation .COLLATE
11211149 && !isPartitionOperation ();
0 commit comments