Skip to content

Commit 3759644

Browse files
committed
fix: reuse MySQL table option parsing across CREATE and ALTER TABLE
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 78d3de8 commit 3759644

5 files changed

Lines changed: 355 additions & 90 deletions

File tree

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,76 @@ public void setStructuredTableOption(TableOption option) {
3232
super.setUseEqual(false);
3333
}
3434

35+
@Override
36+
public AlterOperation getOperation() {
37+
if (structuredTableOption != null) {
38+
switch (structuredTableOption.getKind()) {
39+
case ENGINE:
40+
return AlterOperation.ENGINE;
41+
case KEY_BLOCK_SIZE:
42+
return AlterOperation.KEY_BLOCK_SIZE;
43+
case COMMENT:
44+
return structuredTableOption.isUseEquals()
45+
? AlterOperation.COMMENT_WITH_EQUAL_SIGN
46+
: AlterOperation.COMMENT;
47+
default:
48+
return AlterOperation.SET_TABLE_OPTION;
49+
}
50+
}
51+
return super.getOperation();
52+
}
53+
54+
@Override
55+
public String getEngineOption() {
56+
return structuredTableOption != null
57+
&& structuredTableOption.getKind() == TableOption.Kind.ENGINE
58+
? structuredTableOption.getValue()
59+
: super.getEngineOption();
60+
}
61+
62+
@Override
63+
public void setEngineOption(String value) {
64+
if (structuredTableOption != null
65+
&& structuredTableOption.getKind() == TableOption.Kind.ENGINE) {
66+
structuredTableOption.setValue(value);
67+
}
68+
super.setEngineOption(value);
69+
}
70+
71+
@Override
72+
public int getKeyBlockSize() {
73+
return structuredTableOption != null
74+
&& structuredTableOption.getKind() == TableOption.Kind.KEY_BLOCK_SIZE
75+
? Integer.parseInt(structuredTableOption.getValue())
76+
: super.getKeyBlockSize();
77+
}
78+
79+
@Override
80+
public void setKeyBlockSize(int value) {
81+
if (structuredTableOption != null
82+
&& structuredTableOption.getKind() == TableOption.Kind.KEY_BLOCK_SIZE) {
83+
structuredTableOption.setValue(Integer.toString(value));
84+
}
85+
super.setKeyBlockSize(value);
86+
}
87+
88+
@Override
89+
public String getCommentText() {
90+
return structuredTableOption != null
91+
&& structuredTableOption.getKind() == TableOption.Kind.COMMENT
92+
? structuredTableOption.getValue()
93+
: super.getCommentText();
94+
}
95+
96+
@Override
97+
public void setCommentText(String value) {
98+
if (structuredTableOption != null
99+
&& structuredTableOption.getKind() == TableOption.Kind.COMMENT) {
100+
structuredTableOption.setValue(value);
101+
}
102+
super.setCommentText(value);
103+
}
104+
35105
@Override
36106
public String getTableOption() {
37107
return structuredTableOption == null ? super.getTableOption()
@@ -60,6 +130,10 @@ public void setUseEqual(boolean useEqual) {
60130

61131
@Override
62132
protected void appendBody(StringBuilder b) {
133+
if (structuredTableOption != null) {
134+
b.append(structuredTableOption);
135+
return;
136+
}
63137
switch (getOperation()) {
64138
case SET_TABLE_OPTION:
65139
b.append(getTableOption());

‎src/main/java/net/sf/jsqlparser/statement/create/table/TableOption.java‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class TableOption implements Serializable {
2222

2323
public enum Kind {
24-
ENGINE, CHARACTER_SET, COLLATE, COMMENT, AUTO_INCREMENT, STATS_AUTO_RECALC, STATS_PERSISTENT, STATS_SAMPLE_PAGES, UNION, ENCRYPTION, PASSWORD, DATA_DIRECTORY, INDEX_DIRECTORY, SECONDARY_ENGINE, AUTOEXTEND_SIZE, INSERT_METHOD, PACK_KEYS, DELAY_KEY_WRITE, CHECKSUM, CONNECTION, COMPRESSION, STORAGE_PARAMETERS, WITHOUT_OIDS, ENGINE_ATTRIBUTE, SECONDARY_ENGINE_ATTRIBUTE, ROW_FORMAT, FOREIGN_SERVER, OTHER
24+
ENGINE, CHARACTER_SET, COLLATE, COMMENT, AUTO_INCREMENT, STATS_AUTO_RECALC, STATS_PERSISTENT, STATS_SAMPLE_PAGES, UNION, ENCRYPTION, PASSWORD, DATA_DIRECTORY, INDEX_DIRECTORY, SECONDARY_ENGINE, AUTOEXTEND_SIZE, INSERT_METHOD, PACK_KEYS, DELAY_KEY_WRITE, CHECKSUM, CONNECTION, COMPRESSION, STORAGE_PARAMETERS, WITHOUT_OIDS, ENGINE_ATTRIBUTE, SECONDARY_ENGINE_ATTRIBUTE, ROW_FORMAT, AVG_ROW_LENGTH, MAX_ROWS, MIN_ROWS, KEY_BLOCK_SIZE, TABLESPACE, FOREIGN_SERVER, OTHER
2525
}
2626

2727
private ForeignTableOptions foreignTableOptions;
@@ -43,6 +43,19 @@ public static TableOption foreignServer(ForeignTableOptions options) {
4343
private List<String> tokens;
4444
private List<Table> unionTables;
4545
private List<Index.Option> storageParameters;
46+
private ColumnOption.Storage tablespaceStorage;
47+
48+
public ColumnOption.Storage getTablespaceStorage() {
49+
return tablespaceStorage;
50+
}
51+
52+
public void setTablespaceStorage(ColumnOption.Storage storage) {
53+
if (storage != null && storage != ColumnOption.Storage.DISK
54+
&& storage != ColumnOption.Storage.MEMORY) {
55+
throw new IllegalArgumentException("TABLESPACE storage must be DISK or MEMORY");
56+
}
57+
tablespaceStorage = storage;
58+
}
4659

4760
public List<Index.Option> getStorageParameters() {
4861
return storageParameters;
@@ -51,6 +64,7 @@ public List<Index.Option> getStorageParameters() {
5164
public void setStorageParameters(List<Index.Option> storageParameters) {
5265
this.storageParameters = storageParameters;
5366
foreignTableOptions = null;
67+
tablespaceStorage = null;
5468
kind = Kind.STORAGE_PARAMETERS;
5569
name = "WITH";
5670
value = null;
@@ -96,6 +110,9 @@ public Kind getKind() {
96110

97111
public void setKind(Kind kind) {
98112
this.kind = kind;
113+
if (kind != Kind.TABLESPACE) {
114+
tablespaceStorage = null;
115+
}
99116
if (kind != Kind.FOREIGN_SERVER) {
100117
foreignTableOptions = null;
101118
}
@@ -141,6 +158,7 @@ public List<Table> getUnionTables() {
141158
public void setUnionTables(List<Table> unionTables) {
142159
this.unionTables = unionTables;
143160
foreignTableOptions = null;
161+
tablespaceStorage = null;
144162
kind = Kind.UNION;
145163
name = "UNION";
146164
value = null;
@@ -177,12 +195,17 @@ public List<String> getTokens() {
177195
if (renderedValue != null) {
178196
result.add(renderedValue);
179197
}
198+
if (kind == Kind.TABLESPACE && tablespaceStorage != null) {
199+
result.add("STORAGE");
200+
result.add(tablespaceStorage.name());
201+
}
180202
return Collections.unmodifiableList(result);
181203
}
182204

183205
public void setTokens(List<String> tokens) {
184206
this.tokens = tokens;
185207
if (tokens != null) {
208+
tablespaceStorage = null;
186209
foreignTableOptions = null;
187210
unionTables = null;
188211
storageParameters = null;
@@ -218,6 +241,9 @@ public String toString() {
218241
return PlainSelect.getStringList(tokens, false, false);
219242
}
220243
String renderedValue = getValue();
221-
return name + (renderedValue != null ? (useEquals ? " = " : " ") + renderedValue : "");
244+
return name + (renderedValue != null ? (useEquals ? " = " : " ") + renderedValue : "")
245+
+ (kind == Kind.TABLESPACE && tablespaceStorage != null
246+
? " STORAGE " + tablespaceStorage
247+
: "");
222248
}
223249
}

‎src/main/java/net/sf/jsqlparser/util/TableDefinitionTraversal.java‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public static void visit(AlterExpression action, Consumer<Expression> expression
6161
relation.visitExpressions(expressions);
6262
relation.visitTables(tables);
6363
}
64+
if (action instanceof net.sf.jsqlparser.statement.alter.AlterExpressionTableOption) {
65+
visit(((net.sf.jsqlparser.statement.alter.AlterExpressionTableOption) action)
66+
.getStructuredTableOption(), expressions, tables);
67+
}
6468
if (action.getOperation() == AlterOperation.RENAME_TABLE) {
6569
accept(action.getNewTable(), tables);
6670
}
@@ -123,12 +127,7 @@ public static void visit(CreateTable table, Consumer<Expression> expressions,
123127
expressions);
124128
}
125129
if (table.getTableOptions() != null) {
126-
table.getTableOptions().forEach(option -> {
127-
visitOptions(option.getStorageParameters(), expressions);
128-
if (option.getUnionTables() != null) {
129-
option.getUnionTables().forEach(source -> accept(source, tables));
130-
}
131-
});
130+
table.getTableOptions().forEach(option -> visit(option, expressions, tables));
132131
}
133132
if (table.getInherits() != null) {
134133
table.getInherits().forEach(parent -> accept(parent, tables));
@@ -142,6 +141,17 @@ public static void visit(CreateTable table, Consumer<Expression> expressions,
142141
visit(table.getPartitionBound(), expressions);
143142
}
144143

144+
/** Shared CREATE/ALTER table option traversal. */
145+
public static void visit(net.sf.jsqlparser.statement.create.table.TableOption option,
146+
Consumer<Expression> expressions, Consumer<Table> tables) {
147+
if (option != null) {
148+
visitOptions(option.getStorageParameters(), expressions);
149+
if (option.getUnionTables() != null) {
150+
option.getUnionTables().forEach(source -> accept(source, tables));
151+
}
152+
}
153+
}
154+
145155
/** Visits the active partition key and any subpartition key. Raw bounds remain opaque. */
146156
public static void visit(TablePartitioning partitioning, Consumer<Expression> expressions) {
147157
if (partitioning == null) {

0 commit comments

Comments
 (0)