Skip to content

Commit 787f33e

Browse files
committed
fix: keep cleared index and constraint keys editable
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 066f581 commit 787f33e

2 files changed

Lines changed: 93 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package net.sf.jsqlparser.statement.create.table;
1111

1212
import java.util.ArrayList;
13-
import java.util.Collections;
1413
import java.util.List;
1514
import java.util.function.Consumer;
1615
import java.util.stream.Collectors;
@@ -29,9 +28,12 @@ default List<String> getColumnsNames() {
2928
.map(KeyElement::toString).collect(Collectors.toList());
3029
}
3130

32-
/** Replaces keys with opaque names, without parsing or retaining expression options. */
31+
/**
32+
* Replaces keys with opaque names, without parsing or retaining expression options. A null or
33+
* empty list clears the keys and leaves the structured list editable.
34+
*/
3335
default void setColumnsNames(List<String> names) {
34-
setColumns(names == null ? Collections.emptyList()
36+
setColumns(names == null ? new ArrayList<>()
3537
: names.stream()
3638
.map(KeyElement::new).collect(Collectors.toList()));
3739
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.*;
13+
14+
import java.util.List;
15+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
16+
import net.sf.jsqlparser.statement.create.table.*;
17+
import net.sf.jsqlparser.util.deparser.StatementDeParser;
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
import org.junit.jupiter.params.provider.ValueSource;
20+
21+
class KeyColumnSourceMutationTest {
22+
@ParameterizedTest
23+
@ValueSource(strings = {"INDEX ix (id DESC)", "PRIMARY KEY (id)", "UNIQUE (id DESC)",
24+
"FOREIGN KEY (id) REFERENCES parent (id)", "EXCLUDE USING btree (id WITH =)"})
25+
void clearedNamesCanBeRepopulatedThroughBothAppendApisAndTheLiveList(String declaration)
26+
throws Exception {
27+
for (boolean nullClear : new boolean[] {true, false}) {
28+
for (boolean collectionAppend : new boolean[] {false, true}) {
29+
CreateTable table = (CreateTable) CCJSqlParserUtil.parse(
30+
"CREATE TABLE t (id INT, " + declaration + ")");
31+
KeyColumnSource key = (KeyColumnSource) table.getTableElements().get(1);
32+
KeyElement element = key.getColumns().get(0);
33+
String expected = table.toString();
34+
35+
key.setColumnsNames(nullClear ? null : List.of());
36+
assertNotNull(key.getColumns());
37+
assertTrue(key.getColumns().isEmpty());
38+
assertTrue(key.getColumnsNames().isEmpty());
39+
append(key, element, collectionAppend);
40+
assertSame(element, key.getColumns().get(0));
41+
assertSql(table, expected);
42+
43+
// Clearing and editing through the shared structured API must also work.
44+
key.setColumnsNames(null);
45+
key.getColumns().add(element);
46+
assertSql(table, expected);
47+
}
48+
}
49+
}
50+
51+
private static void append(KeyColumnSource key, KeyElement element, boolean collection) {
52+
if (key instanceof Index) {
53+
if (collection) {
54+
((Index) key).addColumns(List.of(element));
55+
} else {
56+
((Index) key).addColumns(element);
57+
}
58+
} else if (key instanceof KeyConstraint) {
59+
if (collection) {
60+
((KeyConstraint) key).addColumns(List.of(element));
61+
} else {
62+
((KeyConstraint) key).addColumns(element);
63+
}
64+
} else if (key instanceof ForeignKeyConstraint) {
65+
if (collection) {
66+
((ForeignKeyConstraint) key).addColumns(List.of(element));
67+
} else {
68+
((ForeignKeyConstraint) key).addColumns(element);
69+
}
70+
} else if (key instanceof ExcludeConstraint) {
71+
if (collection) {
72+
((ExcludeConstraint) key).addColumns(List.of(element));
73+
} else {
74+
((ExcludeConstraint) key).addColumns(element);
75+
}
76+
} else {
77+
fail("Unexpected key owner: " + key.getClass());
78+
}
79+
}
80+
81+
private static void assertSql(CreateTable table, String expected) throws Exception {
82+
assertEquals(expected, table.toString());
83+
StringBuilder output = new StringBuilder();
84+
table.accept(new StatementDeParser(output), null);
85+
assertEquals(expected, output.toString());
86+
assertEquals(expected, CCJSqlParserUtil.parse(output.toString()).toString());
87+
}
88+
}

0 commit comments

Comments
 (0)