-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSelect.java
More file actions
191 lines (157 loc) · 4.73 KB
/
Copy pathSelect.java
File metadata and controls
191 lines (157 loc) · 4.73 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
package jaskell.sql;
import jaskell.script.Directive;
import javax.naming.SizeLimitExceededException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Select extends Query implements CouldFrom, CouldAlias {
private final List<Directive> _fields = new ArrayList<>();
private Boolean distinct = false;
Select(){
}
public Select distinct() {
this.distinct = true;
return this;
}
Select(String names){
_fields.addAll(Arrays.stream(names.split(","))
.map(String::trim)
.map(Name::new)
.collect(Collectors.toList()));
}
Select(String... names){
_fields.addAll(Arrays.stream(names).map(Name::new).collect(Collectors.toList()));
}
Select(List<Directive> names){
_fields.addAll(names);
}
Select(Directive... directives){
_fields.addAll(Arrays.asList(directives));
}
public Select select(String names){
_fields.addAll(Arrays.stream(names.split(","))
.map(String::trim)
.map(Name::new)
.collect(Collectors.toList()));
return this;
}
public Select select(List<Directive> names){
_fields.addAll(names);
return this;
}
public Select select(String ... names){
_fields.addAll(Arrays.stream(names).map(Name::new).collect(Collectors.toList()));
return this;
}
public Select select(Directive ... directives){
_fields.addAll(Arrays.asList(directives));
return this;
}
public Select all(){
_fields.add(new Literal("*"));
return this;
}
public From from(String name){
From re = new From();
re._from = new Name(name);
re._select = this;
return re;
}
public From from(Directive f) {
From re = new From();
re._select = this;
re._from = f;
return re;
}
public Where where(Predicate predicate){
Where re = new Where(predicate);
re._prefix = this;
re._predicate = predicate;
return re;
}
@Override
public String script() {
if (distinct) {
return String.format("SELECT DISTINCT %s",
_fields.stream().map(Directive::script).collect(Collectors.joining(", ")));
} else {
return String.format("SELECT %s",
_fields.stream().map(Directive::script).collect(Collectors.joining(", ")));
}
}
@Override
public List<jaskell.script.Parameter<?>> parameters() {
List<jaskell.script.Parameter<?>> re = new ArrayList<>();
for (Directive field : _fields) {
re.addAll(field.parameters());
}
setOrder(re);
return re;
}
public static class From extends Query implements jaskell.sql.From, CouldWhere, CouldGroup, CouldOrder, CouldAlias {
Select _select;
Directive _from;
@Override
public String script() {
return String.format("%s FROM %s", _select.script(), _from.script());
}
@Override
public List<jaskell.script.Parameter<?>> parameters() {
ArrayList<jaskell.script.Parameter<?>> re = new ArrayList<>();
re.addAll(_select.parameters());
re.addAll(_from.parameters());
for (int i = 0; i < re.size(); i++) {
re.get(i).order(i);
}
return re;
}
public Join join(Directive other){
Join re = new Join();
re._prefix = this;
re._join = other;
return re;
}
public Left left(){
Left re = new Left();
re._prefix = this;
return re;
}
public Right right(){
Right re = new Right();
re._prefix = this;
return re;
}
public Full full(){
Full re = new Full();
re._prefix = this;
return re;
}
public Inner inner(){
Inner re = new Inner();
re._prefix = this;
return re;
}
public Cross cross(){
Cross re = new Cross();
re._prefix = this;
return re;
}
public Where where(Predicate predicate){
Where re = new Where(predicate);
re._prefix = this;
re._predicate = predicate;
return re;
}
public Group group() {
Group re = new Group();
re._prefix = this;
return re;
}
public Order order() {
Order re = new Order();
re._prefix = this;
return re;
}
}
}