Skip to content

Commit 2bdcea0

Browse files
committed
Traverse and validate active DDL payloads
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent a7ea55d commit 2bdcea0

15 files changed

Lines changed: 531 additions & 136 deletions

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterConstraintAttributes.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ protected boolean usesColumnActionBody() {
4444
}
4545

4646
@Override
47-
public boolean hasActiveColumnDefinitions() {
47+
public boolean hasActiveConstraintHeader() {
4848
return false;
4949
}
5050

5151
@Override
52-
public boolean hasActiveTableDefinition() {
52+
protected boolean usesGeneralBody() {
5353
return false;
5454
}
5555

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.sf.jsqlparser.statement.create.table.Index;
3030
import net.sf.jsqlparser.statement.create.table.NamedConstraint;
3131
import net.sf.jsqlparser.statement.create.table.KeyConstraint;
32+
import net.sf.jsqlparser.statement.create.table.TableElement;
3233
import net.sf.jsqlparser.statement.create.table.IndexOptionSource;
3334
import net.sf.jsqlparser.statement.create.table.ConstraintKind;
3435
import 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();

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterExpressionCharset.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ protected boolean usesColumnActionBody() {
3131
}
3232

3333
@Override
34-
public boolean hasActiveColumnDefinitions() {
34+
public boolean hasActiveConstraintHeader() {
3535
return false;
3636
}
3737

3838
@Override
39-
public boolean hasActiveTableDefinition() {
39+
protected boolean usesGeneralBody() {
4040
return false;
4141
}
4242

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterExpressionDrop.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ protected boolean usesColumnActionBody() {
3030
}
3131

3232
@Override
33-
public boolean hasActiveColumnDefinitions() {
34-
return hasColumnDefinitionPayload() && !isDropSpecialOperation()
33+
public boolean hasActiveConstraintHeader() {
34+
return false;
35+
}
36+
37+
@Override
38+
protected boolean usesGeneralBody() {
39+
return !isDropSpecialOperation()
3540
&& getOperation() != AlterOperation.DROP_PARTITION;
3641
}
3742

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterExpressionOrderBy.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ protected boolean usesColumnActionBody() {
4141
}
4242

4343
@Override
44-
public boolean hasActiveColumnDefinitions() {
44+
public boolean hasActiveConstraintHeader() {
4545
return false;
4646
}
4747

4848
@Override
49-
public boolean hasActiveTableDefinition() {
49+
protected boolean usesGeneralBody() {
5050
return false;
5151
}
5252

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterExpressionPartition.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,12 @@ protected boolean usesColumnActionBody() {
276276
}
277277

278278
@Override
279-
public boolean hasActiveColumnDefinitions() {
279+
public boolean hasActiveConstraintHeader() {
280280
return false;
281281
}
282282

283283
@Override
284-
public boolean hasActiveTableDefinition() {
284+
protected boolean usesGeneralBody() {
285285
return false;
286286
}
287287

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterExpressionPrimaryKey.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,20 @@ protected boolean usesColumnActionBody() {
4545
}
4646

4747
@Override
48-
public boolean hasActiveColumnDefinitions() {
48+
public boolean hasActiveConstraintHeader() {
4949
return false;
5050
}
5151

5252
@Override
53-
public boolean hasActiveTableDefinition() {
53+
protected boolean usesGeneralBody() {
5454
return false;
5555
}
5656

57+
@Override
58+
public KeyConstraint getActiveTableDefinition() {
59+
return getConstraint();
60+
}
61+
5762
@Override
5863
public KeyConstraint getConstraint() {
5964
return (KeyConstraint) super.getConstraint();

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterExpressionRename.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ protected boolean usesColumnActionBody() {
5656
}
5757

5858
@Override
59-
public boolean hasActiveColumnDefinitions() {
60-
return getOperation() == AlterOperation.RENAME && hasColumnDefinitionPayload();
59+
public boolean hasActiveConstraintHeader() {
60+
return false;
61+
}
62+
63+
@Override
64+
protected boolean usesGeneralBody() {
65+
return getOperation() == AlterOperation.RENAME;
6166
}
6267

6368
@Override

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterExpressionTableOption.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ protected boolean usesColumnActionBody() {
4747
}
4848

4949
@Override
50-
public boolean hasActiveColumnDefinitions() {
50+
public boolean hasActiveConstraintHeader() {
5151
return false;
5252
}
5353

5454
@Override
55-
public boolean hasActiveTableDefinition() {
55+
protected boolean usesGeneralBody() {
5656
return false;
5757
}
5858

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterForeignDataOptions.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ protected boolean usesColumnActionBody() {
5353
}
5454

5555
@Override
56-
public boolean hasActiveColumnDefinitions() {
56+
public boolean hasActiveConstraintHeader() {
5757
return false;
5858
}
5959

6060
@Override
61-
public boolean hasActiveTableDefinition() {
61+
protected boolean usesGeneralBody() {
6262
return false;
6363
}
6464

0 commit comments

Comments
 (0)