Skip to content

Commit d730bf0

Browse files
authored
fix: keep CREATE TABLE option tokens in sync with typed options (#2606)
1 parent c9645d7 commit d730bf0

2 files changed

Lines changed: 81 additions & 10 deletions

File tree

src/main/java/net/sf/jsqlparser/statement/create/table/CreateTable.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,19 @@ public void setColumns(List<String> columns) {
9191

9292
/**
9393
* @return a list of options (as simple strings) of this table definition, as ("TYPE", "=",
94-
* "MYISAM")
94+
* "MYISAM"). For typed options, this is a snapshot of their current tokens; use
95+
* {@link #getTableOptions()} to edit the structured options or
96+
* {@link #setTableOptionsStrings(List)} to replace them with raw options.
9597
*/
9698
public List<String> getTableOptionsStrings() {
97-
return tableOptionsStrings;
99+
if (tableOptions == null) {
100+
return tableOptionsStrings;
101+
}
102+
List<String> tokens = new ArrayList<>();
103+
for (TableOption option : tableOptions) {
104+
tokens.addAll(option.getTokens());
105+
}
106+
return tokens;
98107
}
99108

100109
public void setTableOptionsStrings(List<String> tableOptionsStrings) {
@@ -109,14 +118,7 @@ public List<TableOption> getTableOptions() {
109118

110119
public void setTableOptions(List<TableOption> tableOptions) {
111120
this.tableOptions = tableOptions;
112-
if (tableOptions == null) {
113-
tableOptionsStrings = null;
114-
return;
115-
}
116-
tableOptionsStrings = new ArrayList<>();
117-
for (TableOption option : tableOptions) {
118-
tableOptionsStrings.addAll(option.getTokens());
119-
}
121+
tableOptionsStrings = null;
120122
}
121123

122124
/** Returns the first option of the requested kind, if present. */
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2026 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.create;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertNull;
14+
15+
import java.util.ArrayList;
16+
import java.util.Arrays;
17+
import net.sf.jsqlparser.JSQLParserException;
18+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
19+
import net.sf.jsqlparser.statement.create.table.CreateTable;
20+
import net.sf.jsqlparser.statement.create.table.TableOption;
21+
import net.sf.jsqlparser.util.deparser.StatementDeParser;
22+
import org.junit.jupiter.api.Test;
23+
24+
class TableOptionMutationTest {
25+
@Test
26+
void changingTypedOptionUpdatesLegacyTokensAndBothRenderers() throws JSQLParserException {
27+
CreateTable table = parse();
28+
TableOption engine = table.getTableOption(TableOption.Kind.ENGINE).orElseThrow();
29+
engine.setValue("MyISAM");
30+
engine.setUseEquals(false);
31+
assertEquals(Arrays.asList("ENGINE", "MyISAM"), table.getTableOptionsStrings());
32+
assertSql(table, "CREATE TABLE t (id INT) ENGINE MyISAM");
33+
}
34+
35+
@Test
36+
void addingAndClearingTypedOptionsUpdatesDeparser() throws JSQLParserException {
37+
CreateTable table = parse();
38+
table.getTableOptions().add(TableOption.raw("ROW_FORMAT", "=", "DYNAMIC"));
39+
assertSql(table, "CREATE TABLE t (id INT) ENGINE = InnoDB ROW_FORMAT = DYNAMIC");
40+
table.getTableOptions().remove(0);
41+
assertSql(table, "CREATE TABLE t (id INT) ROW_FORMAT = DYNAMIC");
42+
table.getTableOptions().clear();
43+
assertSql(table, "CREATE TABLE t (id INT)");
44+
}
45+
46+
@Test
47+
void rawOptionsRemainMutableAndCanReplaceTypedOptions() throws JSQLParserException {
48+
CreateTable table = parse();
49+
table.setTableOptionsStrings(new ArrayList<>(Arrays.asList("ENGINE", "=", "CSV")));
50+
assertNull(table.getTableOptions());
51+
table.getTableOptionsStrings().set(2, "MyISAM");
52+
assertSql(table, "CREATE TABLE t (id INT) ENGINE = MyISAM");
53+
table.setTableOptions(null);
54+
assertNull(table.getTableOptionsStrings());
55+
assertSql(table, "CREATE TABLE t (id INT)");
56+
}
57+
58+
private static CreateTable parse() throws JSQLParserException {
59+
return (CreateTable) CCJSqlParserUtil.parse("CREATE TABLE t (id INT) ENGINE=InnoDB");
60+
}
61+
62+
private static void assertSql(CreateTable table, String expected) throws JSQLParserException {
63+
assertEquals(expected, table.toString());
64+
StringBuilder buffer = new StringBuilder();
65+
table.accept(new StatementDeParser(buffer), null);
66+
assertEquals(expected, buffer.toString());
67+
assertEquals(expected, CCJSqlParserUtil.parse(expected).toString());
68+
}
69+
}

0 commit comments

Comments
 (0)