Skip to content

Commit 3efd3d6

Browse files
committed
Fix ALTER deparser dispatch for inactive column metadata
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent cdbaf1a commit 3efd3d6

14 files changed

Lines changed: 403 additions & 8 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
@@ -30,6 +30,16 @@ public void setAttributes(ConstraintAttributes attributes) {
3030
this.attributes = Objects.requireNonNull(attributes, "attributes");
3131
}
3232

33+
@Override
34+
protected boolean usesColumnActionBody() {
35+
return false;
36+
}
37+
38+
@Override
39+
public boolean hasActiveColumnDefinitions() {
40+
return false;
41+
}
42+
3343
@Override
3444
public boolean hasActiveTableDefinition() {
3545
return false;

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,7 @@ public List<ColumnSetDefault> getColumnSetDefaultList() {
436436
* existing action-specific API to add or remove entries.
437437
*/
438438
public List<ColumnDefaultAction> getColumnDefaults() {
439-
if (operation != AlterOperation.ALTER || columnSetDefaultList == null
440-
|| constraintType != null && constraintSymbol != null
439+
if (!usesColumnActionBody() || columnSetDefaultList == null
441440
|| columnDropDefaultList != null && !columnDropDefaultList.isEmpty()) {
442441
return Collections.emptyList();
443442
}
@@ -994,10 +993,7 @@ protected void appendBody(StringBuilder b) {
994993
b.append(optionalSpecifier);
995994
} else if (hasExplicitConstraintHeader()) {
996995
toStringConstraintAlter(b);
997-
} else if (operation == AlterOperation.ALTER
998-
&& (columnDropDefaultList != null && !columnDropDefaultList.isEmpty()
999-
|| columnSetDefaultList != null && !columnSetDefaultList.isEmpty()
1000-
|| columnSetVisibilityList != null && !columnSetVisibilityList.isEmpty())) {
996+
} else if (usesColumnActionBody()) {
1001997
toStringAlterColumn(b);
1002998
} else if (isSimpleKeywordOperation()) {
1003999
toStringSimpleKeyword(b);
@@ -1014,6 +1010,35 @@ protected void appendBody(StringBuilder b) {
10141010
}
10151011
}
10161012

1013+
/**
1014+
* Whether the selected body renders ALTER COLUMN default or visibility actions. Subclasses
1015+
* overriding {@link #appendBody(StringBuilder)} must reflect their dispatch here as well.
1016+
*/
1017+
protected boolean usesColumnActionBody() {
1018+
return operation == AlterOperation.ALTER && !hasExplicitConstraintHeader()
1019+
&& (columnDropDefaultList != null && !columnDropDefaultList.isEmpty()
1020+
|| columnSetDefaultList != null && !columnSetDefaultList.isEmpty()
1021+
|| columnSetVisibilityList != null && !columnSetVisibilityList.isEmpty());
1022+
}
1023+
1024+
/**
1025+
* Whether this action currently renders its column definitions through the general body.
1026+
* Inactive column metadata is retained in the AST but must not select a different body when
1027+
* deparsing. Subclasses with their own body dispatch override this method accordingly.
1028+
*/
1029+
public boolean hasActiveColumnDefinitions() {
1030+
return hasColumnDefinitionPayload() && operation != AlterOperation.UNSPECIFIC
1031+
&& !hasExplicitConstraintHeader() && !usesColumnActionBody()
1032+
&& !isSimpleKeywordOperation() && !isRenameOperation() && !isDropSpecialOperation()
1033+
&& operation != AlterOperation.CONVERT && operation != AlterOperation.COLLATE
1034+
&& !isPartitionOperation();
1035+
}
1036+
1037+
/** Whether column definitions take precedence within the general body. */
1038+
protected boolean hasColumnDefinitionPayload() {
1039+
return commentText == null && columnName == null && colDataTypeList != null;
1040+
}
1041+
10171042
/**
10181043
* Appends the common tail (parameters, index comment) shared by all ALTER expressions.
10191044
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
*/
1818
public class AlterExpressionCharset extends AlterExpression {
1919

20+
@Override
21+
protected boolean usesColumnActionBody() {
22+
return false;
23+
}
24+
25+
@Override
26+
public boolean hasActiveColumnDefinitions() {
27+
return false;
28+
}
29+
2030
@Override
2131
public boolean hasActiveTableDefinition() {
2232
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
@@ -18,6 +18,17 @@
1818
* DROP INDEX/KEY, DROP PRIMARY KEY, DROP UNIQUE, DROP FOREIGN KEY, and DROP PARTITION.
1919
*/
2020
public class AlterExpressionDrop extends AlterExpression {
21+
@Override
22+
protected boolean usesColumnActionBody() {
23+
return false;
24+
}
25+
26+
@Override
27+
public boolean hasActiveColumnDefinitions() {
28+
return hasColumnDefinitionPayload() && !isDropSpecialOperation()
29+
&& getOperation() != AlterOperation.DROP_PARTITION;
30+
}
31+
2132
@Override
2233
public boolean hasActiveTableDefinition() {
2334
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
@@ -28,6 +28,16 @@ public List<OrderByElement> getOrderByElements() {
2828
return orderByElements;
2929
}
3030

31+
@Override
32+
protected boolean usesColumnActionBody() {
33+
return false;
34+
}
35+
36+
@Override
37+
public boolean hasActiveColumnDefinitions() {
38+
return false;
39+
}
40+
3141
@Override
3242
public boolean hasActiveTableDefinition() {
3343
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
@@ -264,6 +264,16 @@ public void setCoalescePartitionCount(Integer coalescePartitionCount) {
264264
}
265265
}
266266

267+
@Override
268+
protected boolean usesColumnActionBody() {
269+
return false;
270+
}
271+
272+
@Override
273+
public boolean hasActiveColumnDefinitions() {
274+
return false;
275+
}
276+
267277
@Override
268278
public boolean hasActiveTableDefinition() {
269279
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
@@ -31,6 +31,16 @@ public AlterExpressionPrimaryKey() {
3131
setConstraint(new KeyConstraint().withType("PRIMARY KEY"));
3232
}
3333

34+
@Override
35+
protected boolean usesColumnActionBody() {
36+
return false;
37+
}
38+
39+
@Override
40+
public boolean hasActiveColumnDefinitions() {
41+
return false;
42+
}
43+
3444
@Override
3545
public boolean hasActiveTableDefinition() {
3646
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
@@ -42,6 +42,16 @@ public void setTargetName(String name) {
4242
setColumnName(name);
4343
}
4444

45+
@Override
46+
protected boolean usesColumnActionBody() {
47+
return false;
48+
}
49+
50+
@Override
51+
public boolean hasActiveColumnDefinitions() {
52+
return getOperation() == AlterOperation.RENAME && hasColumnDefinitionPayload();
53+
}
54+
4555
@Override
4656
public boolean hasActiveTableDefinition() {
4757
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
@@ -33,6 +33,16 @@ public void setStructuredTableOption(TableOption option) {
3333
super.setUseEqual(false);
3434
}
3535

36+
@Override
37+
protected boolean usesColumnActionBody() {
38+
return false;
39+
}
40+
41+
@Override
42+
public boolean hasActiveColumnDefinitions() {
43+
return false;
44+
}
45+
3646
@Override
3747
public boolean hasActiveTableDefinition() {
3848
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
@@ -40,6 +40,16 @@ public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expres
4040
return builder;
4141
}
4242

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

0 commit comments

Comments
 (0)