forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.java
More file actions
239 lines (199 loc) · 6.36 KB
/
Sequence.java
File metadata and controls
239 lines (199 loc) · 6.36 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
/*-
* #%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.schema;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
/**
* Represents the database type for a {@code SEQUENCE}
*/
public class Sequence extends ASTNodeAccessImpl implements MultiPartName {
private static final int NAME_IDX = 0;
private static final int SCHEMA_IDX = 1;
private static final int DATABASE_IDX = 2;
private static final int SERVER_IDX = 3;
private List<String> partItems = new ArrayList<>();
private List<Parameter> parameters;
private String dataType;
public Sequence() {}
public Sequence(List<String> partItems) {
this.partItems = new ArrayList<>(partItems);
Collections.reverse(this.partItems);
}
public List<Parameter> getParameters() {
return parameters;
}
public void setParameters(List<Parameter> parameters) {
this.parameters = parameters;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public Sequence withDataType(String dataType) {
this.setDataType(dataType);
return this;
}
public Database getDatabase() {
return new Database(getIndex(DATABASE_IDX));
}
public void setDatabase(Database database) {
setIndex(DATABASE_IDX, database.getDatabaseName());
if (database.getServer() != null) {
setIndex(SERVER_IDX, database.getServer().getFullyQualifiedName());
}
}
public Sequence withDatabase(Database database) {
setDatabase(database);
return this;
}
public String getSchemaName() {
return getIndex(SCHEMA_IDX);
}
public void setSchemaName(String string) {
setIndex(SCHEMA_IDX, string);
}
public Sequence withSchemaName(String string) {
setSchemaName(string);
return this;
}
public String getName() {
return getIndex(NAME_IDX);
}
public void setName(String string) {
setIndex(NAME_IDX, string);
}
public Sequence withName(String string) {
setName(string);
return this;
}
private void setIndex(int idx, String value) {
int size = partItems.size();
for (int i = 0; i < idx - size + 1; i++) {
partItems.add(null);
}
partItems.set(idx, value);
}
private String getIndex(int idx) {
if (idx < partItems.size()) {
return partItems.get(idx);
} else {
return null;
}
}
@Override
public String getFullyQualifiedName() {
StringBuilder fqn = new StringBuilder();
for (int i = partItems.size() - 1; i >= 0; i--) {
String part = partItems.get(i);
if (part == null) {
part = "";
}
fqn.append(part);
if (i != 0) {
fqn.append(".");
}
}
return fqn.toString();
}
@Override
public String getUnquotedName() {
return MultiPartName.unquote(partItems.get(NAME_IDX));
}
@Override
public String toString() {
StringBuilder sql = new StringBuilder(getFullyQualifiedName());
if (dataType != null) {
sql.append(" AS ").append(dataType);
}
if (parameters != null) {
for (Sequence.Parameter parameter : parameters) {
sql.append(" ").append(parameter.formatParameter());
}
}
return sql.toString();
}
public Sequence withParameters(List<Parameter> parameters) {
this.setParameters(parameters);
return this;
}
public Sequence addParameters(Parameter... parameters) {
List<Parameter> collection = Optional.ofNullable(getParameters()).orElseGet(ArrayList::new);
Collections.addAll(collection, parameters);
return this.withParameters(collection);
}
public Sequence addParameters(Collection<? extends Parameter> parameters) {
List<Parameter> collection = Optional.ofNullable(getParameters()).orElseGet(ArrayList::new);
collection.addAll(parameters);
return this.withParameters(collection);
}
/**
* The available parameters to a sequence
*/
public enum ParameterType {
INCREMENT_BY, INCREMENT, START_WITH, START, RESTART_WITH, MAXVALUE, NOMAXVALUE, MINVALUE, NOMINVALUE, CYCLE, NOCYCLE, CACHE, NOCACHE, ORDER, NOORDER, KEEP, NOKEEP, SESSION, GLOBAL;
public static ParameterType from(String type) {
return Enum.valueOf(ParameterType.class, type.toUpperCase());
}
}
/**
* Represents a parameter when declaring a sequence
*/
public static class Parameter {
private final ParameterType option;
private Long value;
public Parameter(ParameterType option) {
this.option = option;
}
public Long getValue() {
return value;
}
public void setValue(Long value) {
this.value = value;
}
public String formatParameter() {
switch (option) {
case INCREMENT_BY:
return prefix("INCREMENT BY");
case INCREMENT:
return prefix("INCREMENT");
case START_WITH:
return prefix("START WITH");
case START:
return prefix("START");
case RESTART_WITH:
if (value != null) {
return prefix("RESTART WITH");
} else {
return "RESTART";
}
case MAXVALUE:
case MINVALUE:
case CACHE:
return prefix(option.name());
default:
// fallthrough just return option name
return option.name();
}
}
private String prefix(String prefix) {
return prefix + " " + value;
}
public Parameter withValue(Long value) {
this.setValue(value);
return this;
}
}
}