-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathCreateIndex.java
More file actions
353 lines (303 loc) · 10.9 KB
/
Copy pathCreateIndex.java
File metadata and controls
353 lines (303 loc) · 10.9 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
/*-
* #%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 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 Index detachedOptions;
private List<String> tailParameters;
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() {
Index options = getOptions();
return options == null ? null : options.getIncludeColumns();
}
/**
* Copies the supplied list as {@link Index#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() {
Index options = getOptions();
return options == null ? null : options.getNullsDistinct();
}
public void setNullsDistinct(Boolean nullsDistinct) {
getOrCreateOptions().setNullsDistinct(nullsDistinct);
}
public List<Index.Option> getStorageParameters() {
Index options = getOptions();
return options == null ? null : options.getStorageParameters();
}
/**
* Copies the list container as {@link Index#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<Index.Option> storageParameters) {
getOrCreateOptions().setStorageParameters(storageParameters);
}
public String getTableSpace() {
Index options = getOptions();
return options == null ? null : options.getTableSpace();
}
public void setTableSpace(String tableSpace) {
getOrCreateOptions().setTableSpace(tableSpace);
}
private Index getOptions() {
return index == null ? detachedOptions : index;
}
private Index getOrCreateOptions() {
if (index != null) {
return index;
}
if (detachedOptions == null) {
detachedOptions = new Index();
}
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) {
Index previousOptions = getOptions();
if (index == null) {
if (this.index != null) {
detachedOptions = new Index();
detachedOptions.setIncludeColumns(previousOptions.getIncludeColumns());
detachedOptions.setNullsDistinct(previousOptions.getNullsDistinct());
detachedOptions.setStorageParameters(previousOptions.getStorageParameters());
detachedOptions.setTableSpace(previousOptions.getTableSpace());
}
} else {
if (previousOptions != null && previousOptions != index) {
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());
}
}
detachedOptions = null;
}
this.index = index;
}
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
}
public List<String> getTailParameters() {
return tailParameters;
}
public void setTailParameters(List<String> tailParameters) {
this.tailParameters = 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 (tailParameters != null) {
for (String param : tailParameters) {
buffer.append(" ").append(param);
}
}
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<Index.ColumnParams> 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 ");
Index.Option.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<Index.Option> storageParameters) {
setStorageParameters(storageParameters);
return this;
}
public CreateIndex withTableSpace(String tableSpace) {
setTableSpace(tableSpace);
return this;
}
public CreateIndex withWhere(Expression where) {
setWhere(where);
return this;
}
}