Repository navigation
Expand file tree
/
Copy pathKeyElement.java
More file actions
305 lines (253 loc) · 8.79 KB
/
Copy pathKeyElement.java
File metadata and controls
305 lines (253 loc) · 8.79 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
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.create.table;
import java.io.Serializable;
import java.util.List;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;
/** A column or expression key shared by index and constraint declarations. */
public class KeyElement implements Serializable {
public enum SortOrder {
ASC, DESC
}
public enum NullOrdering {
FIRST, LAST
}
public final String columnName;
public final List<String> params;
private final Expression expression;
private Integer prefixLength;
private boolean expressionParenthesized = true;
private String collation;
private String operatorClass;
private List<IndexOption> operatorClassParameters;
private SortOrder sortOrder;
private NullOrdering nullOrdering;
private ExclusionOperator exclusionOperator;
private boolean withoutOverlaps;
private boolean period;
public String getExclusionOperator() {
return exclusionOperator == null ? null : exclusionOperator.toString();
}
/** Replaces the complete operator text; retained for source compatibility. */
public void setExclusionOperator(String exclusionOperator) {
this.exclusionOperator = exclusionOperator == null ? null
: new ExclusionOperator().withName(exclusionOperator);
}
/** Returns the editable operator reference, or null when this is not an exclusion key. */
public ExclusionOperator getExclusionOperatorReference() {
return exclusionOperator;
}
public void setExclusionOperatorReference(ExclusionOperator exclusionOperator) {
this.exclusionOperator = exclusionOperator;
}
public KeyElement withExclusionOperatorReference(ExclusionOperator exclusionOperator) {
setExclusionOperatorReference(exclusionOperator);
return this;
}
public KeyElement(String columnName) {
this.columnName = columnName;
this.params = null;
this.expression = null;
}
public KeyElement(String columnName, List<String> params) {
this.columnName = columnName;
this.params = params;
this.expression = null;
}
public KeyElement(Expression expression) {
this.columnName = null;
this.params = null;
this.expression = expression;
}
public KeyElement(Expression expression, List<String> params) {
this.columnName = null;
this.params = params;
this.expression = expression;
}
/** Marks the final key of a PostgreSQL temporal PRIMARY KEY or UNIQUE constraint. */
public boolean isWithoutOverlaps() {
return withoutOverlaps;
}
public void setWithoutOverlaps(boolean withoutOverlaps) {
this.withoutOverlaps = withoutOverlaps;
}
public KeyElement withWithoutOverlaps(boolean withoutOverlaps) {
setWithoutOverlaps(withoutOverlaps);
return this;
}
/** Marks the final referencing column of a temporal foreign key. */
public boolean isPeriod() {
return period;
}
public void setPeriod(boolean period) {
this.period = period;
}
public KeyElement withPeriod(boolean period) {
setPeriod(period);
return this;
}
public String getColumnName() {
return expression != null ? expression.toString() : columnName;
}
public List<String> getParams() {
return params;
}
/**
* The MySQL key-part prefix length, as in {@code KEY k (name(10))}, or null when the whole
* column is indexed. It is not repeated in {@link #getParams()}.
*/
public Integer getPrefixLength() {
return prefixLength;
}
public void setPrefixLength(Integer prefixLength) {
this.prefixLength = prefixLength;
}
public KeyElement withPrefixLength(Integer prefixLength) {
setPrefixLength(prefixLength);
return this;
}
public Expression getExpression() {
return expression;
}
public boolean isExpression() {
return expression != null;
}
public boolean isExpressionParenthesized() {
return expressionParenthesized;
}
public void setExpressionParenthesized(boolean expressionParenthesized) {
this.expressionParenthesized = expressionParenthesized;
}
public KeyElement withExpressionParenthesized(boolean expressionParenthesized) {
setExpressionParenthesized(expressionParenthesized);
return this;
}
public String getCollation() {
return collation;
}
public void setCollation(String collation) {
this.collation = collation;
}
public String getOperatorClass() {
return operatorClass;
}
public void setOperatorClass(String operatorClass) {
this.operatorClass = operatorClass;
}
public List<IndexOption> getOperatorClassParameters() {
return operatorClassParameters;
}
public void setOperatorClassParameters(List<IndexOption> operatorClassParameters) {
this.operatorClassParameters = operatorClassParameters;
}
public SortOrder getSortOrder() {
return sortOrder;
}
public void setSortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
}
public NullOrdering getNullOrdering() {
return nullOrdering;
}
public void setNullOrdering(NullOrdering nullOrdering) {
this.nullOrdering = nullOrdering;
}
public KeyElement withCollation(String collation) {
setCollation(collation);
return this;
}
public KeyElement withOperatorClass(String operatorClass) {
setOperatorClass(operatorClass);
return this;
}
public KeyElement withOperatorClassParameters(List<IndexOption> operatorClassParameters) {
setOperatorClassParameters(operatorClassParameters);
return this;
}
public KeyElement withSortOrder(SortOrder sortOrder) {
setSortOrder(sortOrder);
return this;
}
public KeyElement withNullOrdering(NullOrdering nullOrdering) {
setNullOrdering(nullOrdering);
return this;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
appendTo(builder, value -> builder.append(value));
return builder.toString();
}
/** Renders expression keys through the caller's expression printer. */
public void appendTo(StringBuilder builder, Consumer<Expression> expressionPrinter) {
if (period) {
builder.append("PERIOD ");
}
if (expression != null) {
if (expressionParenthesized) {
builder.append('(');
}
expressionPrinter.accept(expression);
if (expressionParenthesized) {
builder.append(')');
}
} else {
builder.append(columnName);
}
if (prefixLength != null) {
builder.append('(').append(prefixLength).append(')');
}
appendParams(builder);
appendCollation(builder);
appendOperatorClass(builder, expressionPrinter);
appendSortOrder(builder);
appendNullOrdering(builder);
if (exclusionOperator != null) {
builder.append(" WITH ").append(exclusionOperator);
}
if (withoutOverlaps) {
builder.append(" WITHOUT OVERLAPS");
}
}
private void appendParams(StringBuilder builder) {
if (params != null) {
builder.append(" ").append(String.join(" ", params));
}
}
private void appendCollation(StringBuilder builder) {
if (collation != null && !hasParam("COLLATE")) {
builder.append(" COLLATE ").append(collation);
}
}
private void appendOperatorClass(StringBuilder builder,
Consumer<Expression> expressionPrinter) {
if (operatorClass != null && !hasParam(operatorClass)) {
builder.append(" ").append(operatorClass);
if (operatorClassParameters != null && !operatorClassParameters.isEmpty()) {
builder.append(" ");
IndexOption.appendListTo(builder, operatorClassParameters, expressionPrinter);
}
}
}
private void appendSortOrder(StringBuilder builder) {
if (sortOrder != null && !hasParam(sortOrder.name())) {
builder.append(" ").append(sortOrder);
}
}
private void appendNullOrdering(StringBuilder builder) {
if (nullOrdering != null && !hasParam("NULLS")) {
builder.append(" NULLS ").append(nullOrdering);
}
}
private boolean hasParam(String expected) {
return params != null && params.stream().anyMatch(expected::equalsIgnoreCase);
}
}