-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPostgresSelect.java
More file actions
198 lines (155 loc) · 5.35 KB
/
PostgresSelect.java
File metadata and controls
198 lines (155 loc) · 5.35 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
package sqlancer.postgres.ast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sqlancer.Randomly;
import sqlancer.common.ast.SelectBase;
import sqlancer.common.ast.newast.Select;
import sqlancer.postgres.PostgresSchema.PostgresColumn;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
import sqlancer.postgres.PostgresSchema.PostgresTable;
import sqlancer.postgres.PostgresVisitor;
import sqlancer.postgres.ast.PostgresWindowFunction.WindowFrame;
public class PostgresSelect extends SelectBase<PostgresExpression>
implements PostgresExpression, Select<PostgresJoin, PostgresExpression, PostgresTable, PostgresColumn> {
private SelectType selectOption = SelectType.ALL;
private List<PostgresJoin> joinClauses = Collections.emptyList();
private PostgresExpression distinctOnClause;
private ForClause forClause;
private List<PostgresExpression> windowFunctions = new ArrayList<>();
private final Map<String, WindowDefinition> windowDefinitions = new HashMap<>();
public enum ForClause {
UPDATE("UPDATE"), NO_KEY_UPDATE("NO KEY UPDATE"), SHARE("SHARE"), KEY_SHARE("KEY SHARE");
private final String textRepresentation;
ForClause(String textRepresentation) {
this.textRepresentation = textRepresentation;
}
public String getTextRepresentation() {
return textRepresentation;
}
public static ForClause getRandom() {
return Randomly.fromOptions(values());
}
}
public static class WindowDefinition {
private final List<PostgresExpression> partitionBy;
private final List<PostgresOrderByTerm> orderBy;
private final WindowFrame frame;
public WindowDefinition(List<PostgresExpression> partitionBy, List<PostgresOrderByTerm> orderBy,
WindowFrame frame) {
this.partitionBy = partitionBy;
this.orderBy = orderBy;
this.frame = frame;
}
public List<PostgresExpression> getPartitionBy() {
return partitionBy;
}
public List<PostgresOrderByTerm> getOrderBy() {
return orderBy;
}
public WindowFrame getFrame() {
return frame;
}
}
// Getters setters for windowfunctions
public List<PostgresExpression> getWindowFunctions() {
return windowFunctions;
}
public void setWindowFunctions(List<PostgresExpression> windowFunctions) {
this.windowFunctions = windowFunctions;
}
// Add methods for window definitions
public void addWindowDefinition(String name, WindowDefinition definition) {
windowDefinitions.put(name, definition);
}
public WindowDefinition getWindowDefinition(String name) {
return windowDefinitions.get(name);
}
public Map<String, WindowDefinition> getWindowDefinitions() {
return windowDefinitions;
}
public static class PostgresFromTable implements PostgresExpression {
private final PostgresTable t;
private final boolean only;
public PostgresFromTable(PostgresTable t, boolean only) {
this.t = t;
this.only = only;
}
public PostgresTable getTable() {
return t;
}
public boolean isOnly() {
return only;
}
@Override
public PostgresDataType getExpressionType() {
return null;
}
}
public static class PostgresSubquery implements PostgresExpression {
private final PostgresSelect s;
private final String name;
public PostgresSubquery(PostgresSelect s, String name) {
this.s = s;
this.name = name;
}
public PostgresSelect getSelect() {
return s;
}
public String getName() {
return name;
}
@Override
public PostgresDataType getExpressionType() {
return null;
}
}
public enum SelectType {
DISTINCT, ALL;
public static SelectType getRandom() {
return Randomly.fromOptions(values());
}
}
public void setSelectType(SelectType fromOptions) {
this.setSelectOption(fromOptions);
}
public void setDistinctOnClause(PostgresExpression distinctOnClause) {
if (selectOption != SelectType.DISTINCT) {
throw new IllegalArgumentException();
}
this.distinctOnClause = distinctOnClause;
}
public SelectType getSelectOption() {
return selectOption;
}
public void setSelectOption(SelectType fromOptions) {
this.selectOption = fromOptions;
}
@Override
public PostgresDataType getExpressionType() {
return null;
}
@Override
public void setJoinClauses(List<PostgresJoin> joinStatements) {
this.joinClauses = joinStatements;
}
@Override
public List<PostgresJoin> getJoinClauses() {
return joinClauses;
}
public PostgresExpression getDistinctOnClause() {
return distinctOnClause;
}
public void setForClause(ForClause forClause) {
this.forClause = forClause;
}
public ForClause getForClause() {
return forClause;
}
@Override
public String asString() {
return PostgresVisitor.asString(this);
}
}