forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.java
More file actions
253 lines (206 loc) · 6.71 KB
/
Index.java
File metadata and controls
253 lines (206 loc) · 6.71 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
/*-
* #%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.table;
import static java.util.stream.Collectors.toList;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.statement.select.PlainSelect;
public class Index implements Serializable {
private final List<String> name = new ArrayList<>();
private String type;
private String using;
private List<ColumnParams> columns;
private List<String> idxSpec;
private String commentText;
private String indexKeyword;
public List<String> getColumnsNames() {
return columns.stream()
.map(ColumnParams::getColumnName)
.collect(toList());
}
public void setColumnsNames(List<String> list) {
if (list == null) {
this.columns = Collections.emptyList();
} else {
this.columns = list.stream().map(ColumnParams::new).collect(toList());
}
}
@Deprecated
public List<ColumnParams> getColumnWithParams() {
return getColumns();
}
@Deprecated
public void setColumnNamesWithParams(List<ColumnParams> list) {
setColumns(list);
}
public List<ColumnParams> getColumns() {
return columns;
}
public void setColumns(List<ColumnParams> columns) {
this.columns = columns;
}
public Index withColumns(List<ColumnParams> columns) {
setColumns(columns);
return this;
}
public Index addColumns(ColumnParams... functionDeclarationParts) {
List<ColumnParams> collection = Optional.ofNullable(getColumns()).orElseGet(ArrayList::new);
Collections.addAll(collection, functionDeclarationParts);
return this.withColumns(collection);
}
public Index addColumns(Collection<? extends ColumnParams> functionDeclarationParts) {
List<ColumnParams> collection = Optional.ofNullable(getColumns()).orElseGet(ArrayList::new);
collection.addAll(functionDeclarationParts);
return this.withColumns(collection);
}
public String getName() {
return name.isEmpty() ? null : String.join(".", name);
}
public void setName(String name) {
this.name.clear();
if (name != null) {
this.name.add(name);
}
}
public void setName(List<String> name) {
this.name.clear();
this.name.addAll(name);
}
public List<String> getNameParts() {
return Collections.unmodifiableList(name);
}
public String getType() {
return type;
}
public void setType(String string) {
type = string;
}
public Index withColumnsNames(List<String> list) {
setColumnsNames(list);
return this;
}
public String getUsing() {
return using;
}
/**
* In postgresql, the index type (Btree, GIST, etc.) is indicated with a USING clause. Please
* note that: Oracle - the type might be BITMAP, indicating a bitmap kind of index MySQL - the
* type might be FULLTEXT or SPATIAL
*
* @param using
*/
public void setUsing(String using) {
this.using = using;
}
public List<String> getIndexSpec() {
return idxSpec;
}
public void setIndexSpec(List<String> idxSpec) {
this.idxSpec = idxSpec;
}
public Index withIndexSpec(List<String> idxSpec) {
setIndexSpec(idxSpec);
return this;
}
public void setIndexKeyword(String indexKeyword) {
this.indexKeyword = indexKeyword;
}
public String getIndexKeyword() {
return indexKeyword;
}
public Index withIndexKeyword(String indexKeyword) {
this.setIndexKeyword(indexKeyword);
return this;
}
@Override
public String toString() {
String idxSpecText = PlainSelect.getStringList(idxSpec, false, false);
String keyword = (indexKeyword != null) ? " " + indexKeyword : "";
String head =
(type != null ? type : "") +
keyword +
(!name.isEmpty() ? " " + getName() : "") +
(using != null ? " USING " + using : "");
String tail = (columns != null && !columns.isEmpty()
? PlainSelect.getStringList(columns, true, true)
: "")
+ (!idxSpecText.isEmpty() ? " " + idxSpecText : "");
return tail.isEmpty() ? head : head + " " + tail;
}
public Index withType(String type) {
this.setType(type);
return this;
}
public Index withUsing(String using) {
this.setUsing(using);
return this;
}
public Index withName(List<String> name) {
this.setName(name);
return this;
}
public Index withName(String name) {
this.setName(name);
return this;
}
public String getCommentText() {
return commentText;
}
public void setCommentText(String commentText) {
this.commentText = commentText;
}
public static class ColumnParams implements Serializable {
public final String columnName;
public final List<String> params;
private final Expression expression;
public ColumnParams(String columnName) {
this.columnName = columnName;
this.params = null;
this.expression = null;
}
public ColumnParams(String columnName, List<String> params) {
this.columnName = columnName;
this.params = params;
this.expression = null;
}
public ColumnParams(Expression expression) {
this.columnName = null;
this.params = null;
this.expression = expression;
}
public ColumnParams(Expression expression, List<String> params) {
this.columnName = null;
this.params = params;
this.expression = expression;
}
public String getColumnName() {
return expression != null ? expression.toString() : columnName;
}
public List<String> getParams() {
return params;
}
public Expression getExpression() {
return expression;
}
public boolean isExpression() {
return expression != null;
}
@Override
public String toString() {
String head = expression != null ? "(" + expression + ")" : columnName;
return head + (params != null ? " " + String.join(" ", params) : "");
}
}
}