Skip to content

Commit 365ff33

Browse files
refactor: Introduce AlterExpression subclass hierarchy (internal, API-compatible)
The grammar now instantiates operation-specific subclasses instead of the monolithic AlterExpression for all ALTER TABLE operations. All existing API contracts are preserved — getAlterExpressions() still returns List<AlterExpression>, all getters/setters remain on the base class, instanceof AlterExpression still works for every instance. New public subclasses in net.sf.jsqlparser.statement.alter: - AlterExpressionDrop: DROP column/constraint/index/PK/UK/FK/PARTITION - AlterExpressionPartition: all 12 partition maintenance operations - AlterExpressionRename: RENAME column/table/index/key/constraint - AlterExpressionCharset: CONVERT/DEFAULT CHARACTER SET/COLLATE - AlterExpressionTableOption: ENGINE/ALGORITHM/LOCK/COMMENT/ENCRYPTION/ AUTO_INCREMENT/KEY_BLOCK_SIZE/TABLESPACE/KEYS Each subclass overrides appendBody() with focused rendering logic, replacing the 330-line if/else dispatch chain in the base class. ADD/ALTER/MODIFY/CHANGE remain on the base class for now. AlterExpression changes: - toString() is now final, delegates to appendBody() + appendCommonTail() - appendBody() and all toString helpers promoted from private to protected - Base class appendBody() retained as fallback for non-subclassed operations Grammar changes: - AlterExpressionDrop(), AlterExpressionPartitionOp(), AlterExpressionDiscardOrImport() changed from void to returning AlterExpression, instantiating the correct subclass internally - Each branch in AlterExpression() creates the appropriate subclass This lays the groundwork for a future major version where fields can migrate to subclasses and a visitor pattern can be introduced, while keeping the current release fully backward-compatible. Signed-off-by: Andreas Reichel <andreas@manticore-projects.com> Signed-off-by: manticore-projects <andreas@manticore-projects.com>
1 parent 24ebaec commit 365ff33

File tree

8 files changed

+329
-132
lines changed

8 files changed

+329
-132
lines changed

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

Lines changed: 88 additions & 112 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.alter;
11+
12+
/**
13+
* Internal subclass for character set and collation operations within ALTER TABLE.
14+
* Handles CONVERT TO CHARACTER SET, DEFAULT CHARACTER SET, CHARACTER SET, and COLLATE.
15+
*/
16+
public class AlterExpressionCharset extends AlterExpression {
17+
18+
@Override
19+
protected void appendBody(StringBuilder b) {
20+
toStringConvert(b);
21+
}
22+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.alter;
11+
12+
import net.sf.jsqlparser.statement.select.PlainSelect;
13+
14+
/**
15+
* Internal subclass for DROP operations within ALTER TABLE.
16+
* Handles DROP column, DROP CONSTRAINT, DROP INDEX/KEY, DROP PRIMARY KEY,
17+
* DROP UNIQUE, DROP FOREIGN KEY, and DROP PARTITION.
18+
*/
19+
public class AlterExpressionDrop extends AlterExpression {
20+
21+
@Override
22+
protected void appendBody(StringBuilder b) {
23+
switch (getOperation()) {
24+
case DROP_PRIMARY_KEY:
25+
b.append("DROP PRIMARY KEY ");
26+
break;
27+
case DROP_UNIQUE:
28+
b.append("DROP UNIQUE (")
29+
.append(PlainSelect.getStringList(getPkColumns())).append(')');
30+
break;
31+
case DROP_FOREIGN_KEY:
32+
b.append("DROP FOREIGN KEY (")
33+
.append(PlainSelect.getStringList(getPkColumns())).append(')');
34+
break;
35+
case DROP_PARTITION:
36+
b.append("DROP PARTITION ")
37+
.append(PlainSelect.getStringList(getPartitions()));
38+
break;
39+
default:
40+
toStringDropDefault(b);
41+
break;
42+
}
43+
}
44+
45+
private void toStringDropDefault(StringBuilder b) {
46+
b.append("DROP ");
47+
if (getColumnName() == null && getPkColumns() != null && !getPkColumns().isEmpty()) {
48+
// Oracle Multi Column Drop
49+
b.append("(").append(PlainSelect.getStringList(getPkColumns())).append(')');
50+
} else if (getConstraintName() != null) {
51+
b.append("CONSTRAINT ");
52+
if (isUsingIfExists()) {
53+
b.append("IF EXISTS ");
54+
}
55+
b.append(getConstraintName());
56+
} else if (getColumnName() != null) {
57+
if (hasColumn()) {
58+
b.append("COLUMN ");
59+
}
60+
if (isUsingIfExists()) {
61+
b.append("IF EXISTS ");
62+
}
63+
b.append(getColumnName());
64+
} else if (getIndex() != null) {
65+
b.append(getIndex());
66+
}
67+
}
68+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.alter;
11+
12+
import java.util.stream.Collectors;
13+
14+
import net.sf.jsqlparser.statement.create.table.PartitionDefinition;
15+
import net.sf.jsqlparser.statement.select.PlainSelect;
16+
17+
/**
18+
* Internal subclass for partition maintenance operations within ALTER TABLE.
19+
* Handles TRUNCATE, COALESCE, REORGANIZE, EXCHANGE, ANALYZE, CHECK, OPTIMIZE,
20+
* REBUILD, REPAIR PARTITION, PARTITION BY, and REMOVE PARTITIONING.
21+
*/
22+
public class AlterExpressionPartition extends AlterExpression {
23+
24+
@Override
25+
protected void appendBody(StringBuilder b) {
26+
toStringPartition(b);
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.alter;
11+
12+
/**
13+
* Internal subclass for RENAME operations within ALTER TABLE.
14+
* Handles RENAME COLUMN, RENAME TO (table), RENAME INDEX/KEY/CONSTRAINT.
15+
*/
16+
public class AlterExpressionRename extends AlterExpression {
17+
18+
@Override
19+
protected void appendBody(StringBuilder b) {
20+
switch (getOperation()) {
21+
case RENAME:
22+
b.append("RENAME ");
23+
if (hasColumn()) {
24+
b.append("COLUMN ");
25+
}
26+
b.append(getColumnOldName()).append(" TO ").append(getColumnName());
27+
break;
28+
case RENAME_TABLE:
29+
b.append("RENAME TO ").append(getNewTableName());
30+
break;
31+
case RENAME_INDEX:
32+
case RENAME_KEY:
33+
case RENAME_CONSTRAINT:
34+
toStringRename(b);
35+
break;
36+
}
37+
}
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.alter;
11+
12+
/**
13+
* Internal subclass for table-level option operations within ALTER TABLE.
14+
* Handles ENGINE, ALGORITHM, LOCK, KEY_BLOCK_SIZE, COMMENT, ENCRYPTION,
15+
* AUTO_INCREMENT (SET_TABLE_OPTION), DISCARD/IMPORT TABLESPACE, DISABLE/ENABLE KEYS.
16+
*/
17+
public class AlterExpressionTableOption extends AlterExpression {
18+
19+
@Override
20+
protected void appendBody(StringBuilder b) {
21+
switch (getOperation()) {
22+
case COMMENT:
23+
b.append("COMMENT ");
24+
b.append(getCommentText());
25+
break;
26+
case COMMENT_WITH_EQUAL_SIGN:
27+
b.append("COMMENT = ");
28+
b.append(getCommentText());
29+
break;
30+
default:
31+
toStringSimpleKeyword(b);
32+
break;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)