Repository navigation
Expand file tree
/
Copy pathCreateIndex.java
More file actions
406 lines (352 loc) · 13.3 KB
/
Copy pathCreateIndex.java
File metadata and controls
406 lines (352 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.create.index;
import net.sf.jsqlparser.statement.create.table.IndexOption;
import net.sf.jsqlparser.statement.create.table.KeyElement;
import java.util.*;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.*;
import net.sf.jsqlparser.statement.*;
import net.sf.jsqlparser.statement.create.table.*;
public class CreateIndex implements Statement {
private Table table;
private Index index;
private IndexOptions detachedOptions;
private final List<AlgorithmOrLockOption> algorithmOrLockOptions = new ArrayList<>();
private boolean indexTypeBeforeOn = false;
private boolean usingIfNotExists = false;
private boolean concurrently;
private boolean only;
private boolean nullFiltered;
private Expression where;
public boolean isIndexTypeBeforeOn() {
return indexTypeBeforeOn;
}
public void setIndexTypeBeforeOn(boolean indexTypeBeforeOn) {
this.indexTypeBeforeOn = indexTypeBeforeOn;
}
public boolean isUsingIfNotExists() {
return usingIfNotExists;
}
public CreateIndex setUsingIfNotExists(boolean usingIfNotExists) {
this.usingIfNotExists = usingIfNotExists;
return this;
}
public boolean isConcurrently() {
return concurrently;
}
public void setConcurrently(boolean concurrently) {
this.concurrently = concurrently;
}
public boolean isOnly() {
return only;
}
public void setOnly(boolean only) {
this.only = only;
}
/** Whether this Spanner index omits rows with null key values. */
public boolean isNullFiltered() {
return nullFiltered;
}
public void setNullFiltered(boolean nullFiltered) {
this.nullFiltered = nullFiltered;
}
public CreateIndex withNullFiltered(boolean nullFiltered) {
setNullFiltered(nullFiltered);
return this;
}
public List<String> getIncludeColumns() {
IndexOptions options = getOptions();
return options == null ? null : options.getIncludeColumns();
}
/**
* Copies the supplied list as {@link IndexOptions#setIncludeColumns(List)} does; null clears
* it. {@link #getIncludeColumns()} returns the live, mutable list held by the index options.
*/
public void setIncludeColumns(List<String> includeColumns) {
getOrCreateOptions().setIncludeColumns(includeColumns);
}
public Boolean getNullsDistinct() {
IndexOptions options = getOptions();
return options == null ? null : options.getNullsDistinct();
}
public void setNullsDistinct(Boolean nullsDistinct) {
getOrCreateOptions().setNullsDistinct(nullsDistinct);
}
public List<IndexOption> getStorageParameters() {
IndexOptions options = getOptions();
return options == null ? null : options.getStorageParameters();
}
/**
* Copies the list container as {@link IndexOptions#setStorageParameters(List)} does, retaining
* the option objects; null clears it. {@link #getStorageParameters()} returns the live, mutable
* list held by the index options.
*/
public void setStorageParameters(List<IndexOption> storageParameters) {
getOrCreateOptions().setStorageParameters(storageParameters);
}
public String getTableSpace() {
IndexOptions options = getOptions();
return options == null ? null : options.getTableSpace();
}
public void setTableSpace(String tableSpace) {
getOrCreateOptions().setTableSpace(tableSpace);
}
private IndexOptions getOptions() {
return index == null ? detachedOptions : index.getIndexOptions();
}
private IndexOptions getOrCreateOptions() {
if (index != null) {
return index.getIndexOptions();
}
if (detachedOptions == null) {
detachedOptions = new IndexOptions();
}
return detachedOptions;
}
public Expression getWhere() {
return where;
}
public void setWhere(Expression where) {
this.where = where;
}
@Override
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statementVisitor.visit(this, context);
}
public Index getIndex() {
return index;
}
/**
* Replaces the index definition. Options supplied by the new index take precedence; omitted
* options inherit the current statement options, including those set before an index was
* attached. Passing null detaches the definition without discarding its options. The detached
* option lists have independent containers, with their elements retained. Use the option
* setters with null to clear individual options.
*/
public void setIndex(Index index) {
IndexOptions previousOptions = getOptions();
if (index == null) {
if (this.index != null) {
detachedOptions = new IndexOptions();
detachedOptions.setIncludeColumns(previousOptions.getIncludeColumns());
detachedOptions.setNullsDistinct(previousOptions.getNullsDistinct());
detachedOptions.setStorageParameters(previousOptions.getStorageParameters());
detachedOptions.setTableSpace(previousOptions.getTableSpace());
detachedOptions.setSpecOptions(previousOptions.getSpecOptions());
}
} else {
if (previousOptions != null && previousOptions != index.getIndexOptions()) {
if (index.getIncludeColumns() == null) {
index.setIncludeColumns(previousOptions.getIncludeColumns());
}
if (index.getNullsDistinct() == null) {
index.setNullsDistinct(previousOptions.getNullsDistinct());
}
if (index.getStorageParameters() == null) {
index.setStorageParameters(previousOptions.getStorageParameters());
}
if (index.getTableSpace() == null) {
index.setTableSpace(previousOptions.getTableSpace());
}
if (index.getSpecOptions() == null) {
index.setSpecOptions(previousOptions.getSpecOptions());
}
}
detachedOptions = null;
}
this.index = index;
}
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
}
/**
* Returns the mutable MySQL ALGORITHM and LOCK clauses in source order. They are rendered after
* the index options, as MySQL requires.
*/
public List<AlgorithmOrLockOption> getAlgorithmOrLockOptions() {
return algorithmOrLockOptions;
}
/** Copies the list membership and retains the option nodes; null clears the clauses. */
public void setAlgorithmOrLockOptions(List<AlgorithmOrLockOption> options) {
algorithmOrLockOptions.clear();
if (options != null) {
algorithmOrLockOptions.addAll(options);
}
}
public CreateIndex withAlgorithmOrLockOptions(List<AlgorithmOrLockOption> options) {
setAlgorithmOrLockOptions(options);
return this;
}
public CreateIndex addAlgorithmOrLockOptions(AlgorithmOrLockOption... options) {
Collections.addAll(algorithmOrLockOptions, options);
return this;
}
/**
* Returns an unmodifiable token snapshot of the trailing clauses: the index options after the
* key list ({@link Index#getSpecOptions()}), followed by {@link #getAlgorithmOrLockOptions()}.
* Edit those nodes instead of the snapshot.
*/
public List<String> getTailParameters() {
List<String> tokens = new ArrayList<>();
IndexOptions options = getOptions();
if (options != null && options.getIndexSpec() != null) {
tokens.addAll(options.getIndexSpec());
}
for (AlgorithmOrLockOption option : algorithmOrLockOptions) {
tokens.addAll(option.getTokens());
}
return Collections.unmodifiableList(tokens);
}
/**
* Replaces the index options after the key list and the ALGORITHM and LOCK clauses with one
* opaque option holding a copy of the tokens, without parsing them. Null clears them.
*/
public void setTailParameters(List<String> tailParameters) {
algorithmOrLockOptions.clear();
getOrCreateOptions().setIndexSpec(tailParameters);
}
@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
/** Shared rendering for the statement model and CreateIndexDeParser. */
public StringBuilder appendTo(StringBuilder buffer) {
return appendTo(buffer, expression -> buffer.append(expression));
}
/** Shares rendering while allowing visitors to transform key and option expressions. */
public StringBuilder appendTo(StringBuilder buffer, Consumer<Expression> expressionPrinter) {
appendIndexHeader(buffer);
appendIndexTarget(buffer);
appendIndexColumns(buffer, expressionPrinter);
appendPostgreSqlTail(buffer, expressionPrinter);
if (index.getSpecOptions() != null) {
for (IndexSpecOption option : index.getSpecOptions()) {
buffer.append(" ").append(option);
}
}
for (AlgorithmOrLockOption option : algorithmOrLockOptions) {
buffer.append(" ").append(option);
}
return buffer;
}
private void appendIndexHeader(StringBuilder buffer) {
buffer.append("CREATE ");
if (index.getType() != null) {
buffer.append(index.getType()).append(" ");
}
if (index.getClustering() != null) {
buffer.append(index.getClustering()).append(" ");
}
if (nullFiltered) {
buffer.append("NULL_FILTERED ");
}
buffer.append("INDEX ");
if (concurrently) {
buffer.append("CONCURRENTLY ");
}
if (usingIfNotExists) {
buffer.append("IF NOT EXISTS ");
}
if (index.getName() != null) {
buffer.append(index.getName()).append(" ");
}
}
private void appendIndexTarget(StringBuilder buffer) {
if (index.getUsing() != null && isIndexTypeBeforeOn()) {
buffer.append("USING ").append(index.getUsing()).append(" ");
}
buffer.append("ON ");
if (only) {
buffer.append("ONLY ");
}
buffer.append(table.getFullyQualifiedName());
if (index.getUsing() != null && !isIndexTypeBeforeOn()) {
buffer.append(" USING ").append(index.getUsing());
}
}
private void appendIndexColumns(StringBuilder buffer, Consumer<Expression> expressionPrinter) {
if (index.getColumns() != null) {
buffer.append(" (");
for (Iterator<KeyElement> columns = index.getColumns().iterator(); columns
.hasNext();) {
columns.next().appendTo(buffer, expressionPrinter);
if (columns.hasNext()) {
buffer.append(", ");
}
}
buffer.append(")");
}
}
private void appendPostgreSqlTail(StringBuilder buffer,
Consumer<Expression> expressionPrinter) {
if (getIncludeColumns() != null) {
buffer.append(" INCLUDE (").append(String.join(", ", getIncludeColumns())).append(")");
}
if (getNullsDistinct() != null) {
buffer.append(" NULLS ").append(getNullsDistinct() ? "DISTINCT" : "NOT DISTINCT");
}
if (getStorageParameters() != null) {
buffer.append(" WITH ");
IndexOption.appendListTo(buffer, getStorageParameters(), expressionPrinter);
}
if (getTableSpace() != null) {
buffer.append(" TABLESPACE ").append(getTableSpace());
}
if (where != null) {
buffer.append(" WHERE ");
expressionPrinter.accept(where);
}
}
public CreateIndex withTable(Table table) {
this.setTable(table);
return this;
}
public CreateIndex withIndex(Index index) {
this.setIndex(index);
return this;
}
public CreateIndex withTailParameters(List<String> tailParameters) {
this.setTailParameters(tailParameters);
return this;
}
public CreateIndex withConcurrently(boolean concurrently) {
setConcurrently(concurrently);
return this;
}
public CreateIndex withOnly(boolean only) {
setOnly(only);
return this;
}
public CreateIndex withIncludeColumns(List<String> includeColumns) {
setIncludeColumns(includeColumns);
return this;
}
public CreateIndex withNullsDistinct(Boolean nullsDistinct) {
setNullsDistinct(nullsDistinct);
return this;
}
public CreateIndex withStorageParameters(List<IndexOption> storageParameters) {
setStorageParameters(storageParameters);
return this;
}
public CreateIndex withTableSpace(String tableSpace) {
setTableSpace(tableSpace);
return this;
}
public CreateIndex withWhere(Expression where) {
setWhere(where);
return this;
}
}