Skip to content

Commit 6c00a12

Browse files
committed
Traverse and validate active DDL payloads
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 166f0a2 commit 6c00a12

15 files changed

Lines changed: 474 additions & 116 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;
@@ -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();

‎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)