-
Notifications
You must be signed in to change notification settings - Fork 18
/
SoqlBuilder.cls
240 lines (206 loc) · 6.81 KB
/
SoqlBuilder.cls
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
global class SoqlBuilder implements Soqlable{
private Set<Object> selectx = null;
private Boolean selectCount = false;
private Boolean selectAverage = false;
private Boolean selectSum = false;
private Boolean selectMin = false;
private Boolean selectMax = false;
private String aggx = null;
private String fromx = null;
private Condition wherex = null;
private List<OrderBy> orderByx = null;
private String groupByx = null;
private Integer limitx = null;
public Boolean hoopla {get; set;}
public String groupByField {get; set;}
global SoqlBuilder(){
hoopla = false;
}
global SoqlBuilder(Boolean hoopla){
this.hoopla = hoopla;
}
global SoqlBuilder selectx(String field){
return addToSelect(field);
}
global SoqlBuilder selectx(Field field){
return addToSelect(field);
}
global SoqlBuilder selectx(SoqlBuilder field){
return addToSelect(field);
}
global SoqlBuilder selectx(List<Object> fields){
return selectx(ApexLangUtils.listToSet(fields));
}
global SoqlBuilder selectx(Set<Object> fields){
if(fields != null && fields.size() > 0){
for(Object field : fields){
addToSelect(field);
}
}
return this;
}
private SoqlBuilder addToSelect(Object field){
if(field == null){
throw new IllegalArgumentException('null field');
}
if(field instanceof String || field instanceof Field || field instanceof SoqlBuilder ){
if(selectx == null){
selectx = new Set<Object>();
}
selectx.add(field);
} else {
throw new IllegalArgumentException('Invalid field type. A field must be either a String, Field, or SoqlBuilder.');
}
this.selectCount = false;
return this;
}
global SoqlBuilder selectCount(){
return selectCountx();
}
global SoqlBuilder selectCountx(){
this.selectCount = true;
return this;
}
global SoqlBuilder selectCount(String aggx){
this.aggx = aggx;
return selectCountx();
}
global SoqlBuilder selectAveragex(String aggx){
this.selectAverage = true;
this.aggx = aggx;
return this;
}
global SoqlBuilder selectSumx(String aggx){
this.selectSum = true;
this.aggx = aggx;
return this;
}
global SoqlBuilder selectMinx(String aggx){
this.selectMin = true;
this.aggx = aggx;
return this;
}
global SoqlBuilder selectMaxx(String aggx){
this.selectMax = true;
this.aggx = aggx;
return this;
}
global SoqlBuilder fromx(String fromx){
this.fromx = fromx;
return this;
}
global SoqlBuilder wherex(Condition wherex){
this.wherex = wherex;
return this;
}
global SoqlBuilder orderByx(OrderBy orderByx){
if(this.orderByx == null){
this.orderByx = new List<OrderBy>();
}
this.orderByx.add(orderByx);
return this;
}
global SoqlBuilder orderByx(List<OrderBy> orderByx){
if(orderByx != null && orderByx.size() > 0){
for(OrderBy field : orderByx){
orderByx(field);
}
}
return this;
}
global SoqlBuilder groupByx(String groupByx){
this.groupByx = groupByx;
return this;
}
global SoqlBuilder limitx(Integer limitx){
this.limitx = limitx;
return this;
}
global String toSoql(){ return this.toSoql(null); }
global String toSoql(SoqlOptions options){
//Special @hooplasoftware mod
if(aggx == null && hoopla == true)
aggx = 'id';
if(options == null){
options = SoqlOptions.DEFAULT_OPTIONS;
}
if(ApexLangUtils.isBlank(fromx)){
throw new IllegalStateException('Illegal state! You must invoke fromx() with valid object name before invoking toSoql().');
}
Boolean isFirst = true;
String soql;
if(hoopla)
soql = 'SELECT ' + groupByField + ', ';
else
soql = 'SELECT ';
if(selectx == null){
selectx = new Set<Object>();
}
if(this.selectCount != null && this.selectCount && aggx == null){
soql += 'COUNT()';
} else if (this.selectCount != null && this.selectCount) {
soql += 'COUNT(' + aggx + ')';
} else if (this.selectAverage != null && this.selectAverage){
soql += 'AVG(' + aggx + ')';
} else if (this.selectSum != null && this.selectSum){
soql += 'SUM(' + aggx + ')';
} else if (this.selectMin != null && this.selectMin) {
soql += 'MIN(' + aggx + ')';
} else if (this.selectMax != null && this.selectMax) {
soql += 'MAX(' + aggx + ')';
} else {
if (selectx.size() <= 0){
selectx.add('id');
}
String distinctField = null;
Map<String,String> distinctFields = new Map<String,String>();
for(Object value : selectx){
if(value instanceof Field){
distinctField = ((Field)value).toSoql(options);
} else if(value instanceof SoqlBuilder){
distinctField = '(' + ((SoqlBuilder)value).toSoql(options) + ')';
} else {
distinctField = ''+value;
}
distinctField = ApexLangUtils.trim(distinctField);
distinctFields.put(ApexLangUtils.lowerCase(distinctField),distinctField);
}
soql += ApexLangUtils.joinStrings(distinctFields.values(),',');
}
soql += ' FROM ' + fromx;
if(wherex != null){
//@hooplasoftware MOD
if(hoopla)
{
soql += ' WHERE ' + groupByField + ' IN {!Users} ';
String wherexs = wherex.toSoql(options);
if(wherexs != null && wherexs != '')
soql += ' AND ' + wherex.toSoql(options);
}
else
soql += ' WHERE ' + wherex.toSoql(options);
}
if(groupByx != null && groupByx != ''){
soql += ' GROUP BY ' + groupByx + ' ';
}
if(orderByx != null && orderByx.size() > 0){
isFirst = true;
for(OrderBy orderBy : orderByx){
if(orderBy == null){
continue;
}
if(isFirst){
isFirst = false;
soql += ' ORDER BY ';
} else {
soql += ', ';
}
soql += orderBy.toSoql(options);
}
}
if(limitx != null){
soql += ' LIMIT ' + limitx;
}
return soql;
}
}