Skip to content

Commit 4a9208c

Browse files
authored
Share CREATE INDEX options with its index definition (#2741)
* refactor: share CREATE INDEX options with its index definition Signed-off-by: minleejae <mmj9808@gmail.com> * Preserve detached CREATE INDEX option isolation and traversal Signed-off-by: minleejae <mmj9808@gmail.com> --------- Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent a0fb6f2 commit 4a9208c

3 files changed

Lines changed: 318 additions & 21 deletions

File tree

‎src/main/java/net/sf/jsqlparser/statement/create/index/CreateIndex.java‎

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ public class CreateIndex implements Statement {
2020

2121
private Table table;
2222
private Index index;
23+
private Index detachedOptions;
2324
private List<String> tailParameters;
2425
private boolean indexTypeBeforeOn = false;
2526
private boolean usingIfNotExists = false;
2627
private boolean concurrently;
2728
private boolean only;
2829
private boolean nullFiltered;
29-
private List<String> includeColumns;
30-
private Boolean nullsDistinct;
31-
private List<Index.Option> storageParameters;
32-
private String tableSpace;
3330
private Expression where;
3431

3532
public boolean isIndexTypeBeforeOn() {
@@ -80,35 +77,62 @@ public CreateIndex withNullFiltered(boolean nullFiltered) {
8077
}
8178

8279
public List<String> getIncludeColumns() {
83-
return includeColumns;
80+
Index options = getOptions();
81+
return options == null ? null : options.getIncludeColumns();
8482
}
8583

84+
/**
85+
* Copies the supplied list as {@link Index#setIncludeColumns(List)} does; null clears it.
86+
* {@link #getIncludeColumns()} returns the live, mutable list held by the index options.
87+
*/
8688
public void setIncludeColumns(List<String> includeColumns) {
87-
this.includeColumns = includeColumns;
89+
getOrCreateOptions().setIncludeColumns(includeColumns);
8890
}
8991

9092
public Boolean getNullsDistinct() {
91-
return nullsDistinct;
93+
Index options = getOptions();
94+
return options == null ? null : options.getNullsDistinct();
9295
}
9396

9497
public void setNullsDistinct(Boolean nullsDistinct) {
95-
this.nullsDistinct = nullsDistinct;
98+
getOrCreateOptions().setNullsDistinct(nullsDistinct);
9699
}
97100

98101
public List<Index.Option> getStorageParameters() {
99-
return storageParameters;
102+
Index options = getOptions();
103+
return options == null ? null : options.getStorageParameters();
100104
}
101105

106+
/**
107+
* Copies the list container as {@link Index#setStorageParameters(List)} does, retaining the
108+
* option objects; null clears it. {@link #getStorageParameters()} returns the live, mutable
109+
* list held by the index options.
110+
*/
102111
public void setStorageParameters(List<Index.Option> storageParameters) {
103-
this.storageParameters = storageParameters;
112+
getOrCreateOptions().setStorageParameters(storageParameters);
104113
}
105114

106115
public String getTableSpace() {
107-
return tableSpace;
116+
Index options = getOptions();
117+
return options == null ? null : options.getTableSpace();
108118
}
109119

110120
public void setTableSpace(String tableSpace) {
111-
this.tableSpace = tableSpace;
121+
getOrCreateOptions().setTableSpace(tableSpace);
122+
}
123+
124+
private Index getOptions() {
125+
return index == null ? detachedOptions : index;
126+
}
127+
128+
private Index getOrCreateOptions() {
129+
if (index != null) {
130+
return index;
131+
}
132+
if (detachedOptions == null) {
133+
detachedOptions = new Index();
134+
}
135+
return detachedOptions;
112136
}
113137

114138
public Expression getWhere() {
@@ -128,7 +152,40 @@ public Index getIndex() {
128152
return index;
129153
}
130154

155+
/**
156+
* Replaces the index definition. Options supplied by the new index take precedence; omitted
157+
* options inherit the current statement options, including those set before an index was
158+
* attached. Passing null detaches the definition without discarding its options. The detached
159+
* option lists have independent containers, with their elements retained. Use the option
160+
* setters with null to clear individual options.
161+
*/
131162
public void setIndex(Index index) {
163+
Index previousOptions = getOptions();
164+
if (index == null) {
165+
if (this.index != null) {
166+
detachedOptions = new Index();
167+
detachedOptions.setIncludeColumns(previousOptions.getIncludeColumns());
168+
detachedOptions.setNullsDistinct(previousOptions.getNullsDistinct());
169+
detachedOptions.setStorageParameters(previousOptions.getStorageParameters());
170+
detachedOptions.setTableSpace(previousOptions.getTableSpace());
171+
}
172+
} else {
173+
if (previousOptions != null && previousOptions != index) {
174+
if (index.getIncludeColumns() == null) {
175+
index.setIncludeColumns(previousOptions.getIncludeColumns());
176+
}
177+
if (index.getNullsDistinct() == null) {
178+
index.setNullsDistinct(previousOptions.getNullsDistinct());
179+
}
180+
if (index.getStorageParameters() == null) {
181+
index.setStorageParameters(previousOptions.getStorageParameters());
182+
}
183+
if (index.getTableSpace() == null) {
184+
index.setTableSpace(previousOptions.getTableSpace());
185+
}
186+
}
187+
detachedOptions = null;
188+
}
132189
this.index = index;
133190
}
134191

@@ -225,18 +282,18 @@ private void appendIndexColumns(StringBuilder buffer, Consumer<Expression> expre
225282

226283
private void appendPostgreSqlTail(StringBuilder buffer,
227284
Consumer<Expression> expressionPrinter) {
228-
if (includeColumns != null) {
229-
buffer.append(" INCLUDE (").append(String.join(", ", includeColumns)).append(")");
285+
if (getIncludeColumns() != null) {
286+
buffer.append(" INCLUDE (").append(String.join(", ", getIncludeColumns())).append(")");
230287
}
231-
if (nullsDistinct != null) {
232-
buffer.append(" NULLS ").append(nullsDistinct ? "DISTINCT" : "NOT DISTINCT");
288+
if (getNullsDistinct() != null) {
289+
buffer.append(" NULLS ").append(getNullsDistinct() ? "DISTINCT" : "NOT DISTINCT");
233290
}
234-
if (storageParameters != null) {
291+
if (getStorageParameters() != null) {
235292
buffer.append(" WITH ");
236-
Index.Option.appendListTo(buffer, storageParameters, expressionPrinter);
293+
Index.Option.appendListTo(buffer, getStorageParameters(), expressionPrinter);
237294
}
238-
if (tableSpace != null) {
239-
buffer.append(" TABLESPACE ").append(tableSpace);
295+
if (getTableSpace() != null) {
296+
buffer.append(" TABLESPACE ").append(getTableSpace());
240297
}
241298
if (where != null) {
242299
buffer.append(" WHERE ");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public static void visit(CreateIndex createIndex, Consumer<Expression> expressio
4343
accept(createIndex.getTable(), tables);
4444
if (createIndex.getIndex() != null) {
4545
visit(createIndex.getIndex(), expressions, tables);
46+
} else {
47+
visitOptions(createIndex.getStorageParameters(), expressions);
4648
}
47-
visitOptions(createIndex.getStorageParameters(), expressions);
4849
accept(createIndex.getWhere(), expressions);
4950
}
5051

0 commit comments

Comments
 (0)