forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithItem.java
More file actions
229 lines (189 loc) · 6.39 KB
/
WithItem.java
File metadata and controls
229 lines (189 loc) · 6.39 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
/*-
* #%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.select;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.statement.ParenthesedStatement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.delete.ParenthesedDelete;
import net.sf.jsqlparser.statement.insert.ParenthesedInsert;
import net.sf.jsqlparser.statement.update.ParenthesedUpdate;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class WithItem<K extends ParenthesedStatement> implements Serializable {
private K statement;
private Alias alias;
private List<SelectItem<?>> withItemList;
private WithFunctionDeclaration withFunctionDeclaration;
private boolean recursive = false;
private boolean usingNot = false;
private boolean materialized = false;
public WithItem(K statement, Alias alias) {
this.statement = statement;
this.alias = alias;
}
public WithItem() {
this(null, (Alias) null);
}
public K getParenthesedStatement() {
return statement;
}
public void setParenthesedStatement(K statement) {
this.statement = statement;
}
public WithItem<K> withParenthesedStatement(K statement) {
this.setParenthesedStatement(statement);
return this;
}
public Alias getAlias() {
return alias;
}
public String getAliasName() {
return alias != null ? alias.getName() : null;
}
public String getUnquotedAliasName() {
return alias != null ? alias.getUnquotedName() : null;
}
public void setAlias(Alias alias) {
this.alias = alias;
}
public WithItem<?> withAlias(Alias alias) {
this.setAlias(alias);
return this;
}
public boolean isRecursive() {
return recursive;
}
public void setRecursive(boolean recursive) {
this.recursive = recursive;
}
public boolean isMaterialized() {
return materialized;
}
public void setMaterialized(boolean materialized) {
this.materialized = materialized;
}
public K getStatement() {
return statement;
}
public WithItem<K> setStatement(K statement) {
this.statement = statement;
return this;
}
public boolean isUsingNot() {
return usingNot;
}
public WithItem<K> setUsingNot(boolean usingNot) {
this.usingNot = usingNot;
return this;
}
/**
* The {@link SelectItem}s in this WITH (for example the A,B,C in "WITH mywith (A,B,C) AS ...")
*
* @return a list of {@link SelectItem}s
*/
public List<SelectItem<?>> getWithItemList() {
return withItemList;
}
public void setWithItemList(List<SelectItem<?>> withItemList) {
this.withItemList = withItemList;
}
public WithFunctionDeclaration getWithFunctionDeclaration() {
return withFunctionDeclaration;
}
public void setWithFunctionDeclaration(WithFunctionDeclaration withFunctionDeclaration) {
this.withFunctionDeclaration = withFunctionDeclaration;
}
public WithItem<K> withWithFunctionDeclaration(
WithFunctionDeclaration withFunctionDeclaration) {
this.setWithFunctionDeclaration(withFunctionDeclaration);
return this;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (withFunctionDeclaration != null) {
builder.append(withFunctionDeclaration);
} else {
builder.append(recursive ? "RECURSIVE " : "");
if (alias != null) {
builder.append(alias.getName());
}
if (withItemList != null) {
builder.append("(");
int size = withItemList.size();
for (int i = 0; i < size; i++) {
builder.append(withItemList.get(i)).append(i < size - 1 ? "," : "");
}
builder.append(")");
}
builder.append(" AS ");
if (materialized) {
builder.append(usingNot
? "NOT MATERIALIZED "
: "MATERIALIZED ");
}
builder.append(statement);
}
return builder.toString();
}
@Deprecated
public <T, S> T accept(SelectVisitor<T> selectVisitor, S context) {
return selectVisitor.visit(this, context);
}
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statement.accept(statementVisitor, context);
}
public WithItem<?> withWithItemList(List<SelectItem<?>> withItemList) {
this.setWithItemList(withItemList);
return this;
}
public WithItem<?> withRecursive(boolean recursive, boolean materialized) {
this.setRecursive(recursive);
this.setMaterialized(materialized);
return this;
}
public WithItem<?> withRecursive(boolean recursive, boolean usingNot, boolean materialized) {
this.setRecursive(recursive);
this.setUsingNot(usingNot);
this.setMaterialized(materialized);
return this;
}
public WithItem<?> addWithItemList(SelectItem<?>... withItemList) {
List<SelectItem<?>> collection =
Optional.ofNullable(getWithItemList()).orElseGet(ArrayList::new);
Collections.addAll(collection, withItemList);
return this.withWithItemList(collection);
}
public WithItem<?> addWithItemList(Collection<? extends SelectItem<?>> withItemList) {
List<SelectItem<?>> collection =
Optional.ofNullable(getWithItemList()).orElseGet(ArrayList::new);
collection.addAll(withItemList);
return this.withWithItemList(collection);
}
public ParenthesedSelect getSelect() {
return (ParenthesedSelect) statement;
}
public ParenthesedInsert getInsert() {
return (ParenthesedInsert) statement;
}
public ParenthesedUpdate getUpdate() {
return (ParenthesedUpdate) statement;
}
public ParenthesedDelete getDelete() {
return (ParenthesedDelete) statement;
}
public void setSelect(ParenthesedSelect select) {
this.statement = (K) select;
}
}