Skip to content

Commit 94f413d

Browse files
committed
Fix ALTER deparser dispatch for inactive column metadata
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 066f581 commit 94f413d

14 files changed

Lines changed: 400 additions & 5 deletions

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public List<ColumnIdentityAction> getColumnIdentityActions() {
3838
return Collections.emptyList();
3939
}
4040

41+
@Override
42+
protected boolean usesColumnActionBody() {
43+
return false;
44+
}
45+
46+
@Override
47+
public boolean hasActiveColumnDefinitions() {
48+
return false;
49+
}
50+
4151
@Override
4252
public boolean hasActiveTableDefinition() {
4353
return false;

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,7 @@ public List<ColumnSetDefault> getColumnSetDefaultList() {
482482
* existing action-specific API to add or remove entries.
483483
*/
484484
public List<ColumnDefaultAction> getColumnDefaults() {
485-
if (operation != AlterOperation.ALTER || columnSetDefaultList == null
486-
|| constraintType != null && constraintSymbol != null
485+
if (!usesColumnActionBody() || columnSetDefaultList == null
487486
|| columnDropDefaultList != null && !columnDropDefaultList.isEmpty()) {
488487
return Collections.emptyList();
489488
}
@@ -1077,7 +1076,7 @@ protected void appendBody(StringBuilder b) {
10771076
b.append(optionalSpecifier);
10781077
} else if (hasExplicitConstraintHeader()) {
10791078
toStringConstraintAlter(b);
1080-
} else if (hasAlterColumnValueActions()) {
1079+
} else if (usesColumnActionBody()) {
10811080
toStringAlterColumn(b);
10821081
} else if (isSimpleKeywordOperation()) {
10831082
toStringSimpleKeyword(b);
@@ -1101,6 +1100,32 @@ private boolean hasAlterColumnValueActions() {
11011100
|| columnSetVisibilityList != null && !columnSetVisibilityList.isEmpty());
11021101
}
11031102

1103+
/**
1104+
* Whether the selected body renders ALTER COLUMN default or visibility actions. Subclasses
1105+
* overriding {@link #appendBody(StringBuilder)} must reflect their dispatch here as well.
1106+
*/
1107+
protected boolean usesColumnActionBody() {
1108+
return hasAlterColumnValueActions() && !hasExplicitConstraintHeader();
1109+
}
1110+
1111+
/**
1112+
* Whether this action currently renders its column definitions through the general body.
1113+
* Inactive column metadata is retained in the AST but must not select a different body when
1114+
* deparsing. Subclasses with their own body dispatch override this method accordingly.
1115+
*/
1116+
public boolean hasActiveColumnDefinitions() {
1117+
return hasColumnDefinitionPayload() && operation != AlterOperation.UNSPECIFIC
1118+
&& !hasExplicitConstraintHeader() && !usesColumnActionBody()
1119+
&& !isSimpleKeywordOperation() && !isRenameOperation() && !isDropSpecialOperation()
1120+
&& operation != AlterOperation.CONVERT && operation != AlterOperation.COLLATE
1121+
&& !isPartitionOperation();
1122+
}
1123+
1124+
/** Whether column definitions take precedence within the general body. */
1125+
protected boolean hasColumnDefinitionPayload() {
1126+
return commentText == null && columnName == null && colDataTypeList != null;
1127+
}
1128+
11041129
/**
11051130
* Appends the common tail (parameters, index comment) shared by all ALTER expressions.
11061131
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public List<ColumnIdentityAction> getColumnIdentityActions() {
2525
return Collections.emptyList();
2626
}
2727

28+
@Override
29+
protected boolean usesColumnActionBody() {
30+
return false;
31+
}
32+
33+
@Override
34+
public boolean hasActiveColumnDefinitions() {
35+
return false;
36+
}
37+
2838
@Override
2939
public boolean hasActiveTableDefinition() {
3040
return false;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public List<ColumnIdentityAction> getColumnIdentityActions() {
2424
return getColumnIdentityActionsFromDefinitions();
2525
}
2626

27+
@Override
28+
protected boolean usesColumnActionBody() {
29+
return false;
30+
}
31+
32+
@Override
33+
public boolean hasActiveColumnDefinitions() {
34+
return hasColumnDefinitionPayload() && !isDropSpecialOperation()
35+
&& getOperation() != AlterOperation.DROP_PARTITION;
36+
}
37+
2738
@Override
2839
public boolean hasActiveTableDefinition() {
2940
return false;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public List<ColumnIdentityAction> getColumnIdentityActions() {
3535
return Collections.emptyList();
3636
}
3737

38+
@Override
39+
protected boolean usesColumnActionBody() {
40+
return false;
41+
}
42+
43+
@Override
44+
public boolean hasActiveColumnDefinitions() {
45+
return false;
46+
}
47+
3848
@Override
3949
public boolean hasActiveTableDefinition() {
4050
return false;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ public List<ColumnIdentityAction> getColumnIdentityActions() {
270270
return Collections.emptyList();
271271
}
272272

273+
@Override
274+
protected boolean usesColumnActionBody() {
275+
return false;
276+
}
277+
278+
@Override
279+
public boolean hasActiveColumnDefinitions() {
280+
return false;
281+
}
282+
273283
@Override
274284
public boolean hasActiveTableDefinition() {
275285
return false;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public List<ColumnIdentityAction> getColumnIdentityActions() {
3939
return Collections.emptyList();
4040
}
4141

42+
@Override
43+
protected boolean usesColumnActionBody() {
44+
return false;
45+
}
46+
47+
@Override
48+
public boolean hasActiveColumnDefinitions() {
49+
return false;
50+
}
51+
4252
@Override
4353
public boolean hasActiveTableDefinition() {
4454
return false;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ public List<ColumnIdentityAction> getColumnIdentityActions() {
5050
return Collections.emptyList();
5151
}
5252

53+
@Override
54+
protected boolean usesColumnActionBody() {
55+
return false;
56+
}
57+
58+
@Override
59+
public boolean hasActiveColumnDefinitions() {
60+
return getOperation() == AlterOperation.RENAME && hasColumnDefinitionPayload();
61+
}
62+
5363
@Override
5464
public boolean hasActiveTableDefinition() {
5565
return false;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public List<ColumnIdentityAction> getColumnIdentityActions() {
4141
return Collections.emptyList();
4242
}
4343

44+
@Override
45+
protected boolean usesColumnActionBody() {
46+
return false;
47+
}
48+
49+
@Override
50+
public boolean hasActiveColumnDefinitions() {
51+
return false;
52+
}
53+
4454
@Override
4555
public boolean hasActiveTableDefinition() {
4656
return false;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public List<ColumnIdentityAction> getColumnIdentityActions() {
4747
return Collections.emptyList();
4848
}
4949

50+
@Override
51+
protected boolean usesColumnActionBody() {
52+
return false;
53+
}
54+
55+
@Override
56+
public boolean hasActiveColumnDefinitions() {
57+
return false;
58+
}
59+
5060
@Override
5161
public boolean hasActiveTableDefinition() {
5262
return false;

0 commit comments

Comments
 (0)