forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchCriteria.java
More file actions
executable file
·358 lines (307 loc) · 11.9 KB
/
Copy pathSearchCriteria.java
File metadata and controls
executable file
·358 lines (307 loc) · 11.9 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.utils.db;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.cloud.utils.Pair;
import com.cloud.utils.db.GenericSearchBuilder.Condition;
import com.cloud.utils.db.GenericSearchBuilder.Select;
/**
* big joins or high performance searches, it is much better to
*/
public class SearchCriteria<K> {
public enum Op {
GT(" > ? ", 1),
GTEQ(" >= ? ", 1),
LT(" < ? ", 1),
LTEQ(" <= ? ", 1),
EQ(" = ? ", 1),
NEQ(" != ? ", 1),
BETWEEN(" BETWEEN ? AND ? ", 2),
NBETWEEN(" NOT BETWEEN ? AND ? ", 2),
IN(" IN () ", -1),
NOTIN(" NOT IN () ", -1),
LIKE(" LIKE ? ", 1),
NLIKE(" NOT LIKE ? ", 1),
NIN(" NOT IN () ", -1),
NULL(" IS NULL ", 0),
NNULL(" IS NOT NULL ", 0),
SC(" () ", 1),
TEXT(" () ", 1),
RP("", 0),
AND(" AND ", 0),
OR(" OR ", 0),
NOT(" NOT ", 0);
private final String op;
int params;
Op(String op, int params) {
this.op = op;
this.params = params;
}
@Override
public String toString() {
return op;
}
public int getParams() {
return params;
}
}
public enum Func {
NATIVE("@", 1),
MAX("MAX(@)", 1),
MIN("MIN(@)", 1),
FIRST("FIRST(@)", 1),
LAST("LAST(@)", 1),
SUM("SUM(@)", 1),
COUNT("COUNT(@)", 1),
DISTINCT("DISTINCT(@)", 1);
private String func;
private int count;
Func(String func, int params) {
this.func = func;
this.count = params;
}
@Override
public String toString() {
return func;
}
public int getCount() {
return count;
}
}
public enum SelectType {
Fields,
Entity,
Single,
Result
}
private final Map<String, Attribute> _attrs;
private final ArrayList<Condition> _conditions;
private ArrayList<Condition> _additionals = null;
private HashMap<String, Object[]> _params = new HashMap<String, Object[]>();
private int _counter;
private HashMap<String, JoinBuilder<SearchCriteria<?>>> _joins;
private final ArrayList<Select> _selects;
private final GroupBy<?, K> _groupBy;
private final List<Object> _groupByValues;
private final Class<K> _resultType;
private final SelectType _selectType;
protected SearchCriteria(final Map<String, Attribute> attrs, ArrayList<GenericSearchBuilder.Condition> conditions, ArrayList<Select> selects, SelectType selectType, Class<K> resultType, HashMap<String, Object[]> params) {
this._attrs = attrs;
this._conditions = conditions;
this._selects = selects;
this._selectType = selectType;
this._resultType = resultType;
this._params = params;
this._additionals = new ArrayList<Condition>();
this._counter = 0;
this._joins = null;
this._groupBy = null;
this._groupByValues = null;
}
protected SearchCriteria(GenericSearchBuilder<?, K> sb) {
this._attrs = sb._attrs;
this._conditions = sb._conditions;
this._additionals = new ArrayList<Condition>();
this._counter = 0;
this._joins = null;
if (sb._joins != null) {
_joins = new HashMap<String, JoinBuilder<SearchCriteria<?>>>(sb._joins.size());
for (Map.Entry<String, JoinBuilder<GenericSearchBuilder<?, ?>>> entry : sb._joins.entrySet()) {
JoinBuilder<GenericSearchBuilder<?, ?>> value = entry.getValue();
_joins.put(entry.getKey(), new JoinBuilder<SearchCriteria<?>>(value.getT().create(),value.getFirstAttribute(), value.getSecondAttribute(), value.getType()));
}
}
_selects = sb._selects;
_groupBy = sb._groupBy;
if (_groupBy != null) {
_groupByValues = new ArrayList<Object>();
} else {
_groupByValues = null;
}
_resultType = sb._resultType;
_selectType = sb._selectType;
}
public SelectType getSelectType() {
return _selectType;
}
public void getSelect(StringBuilder str, int insertAt) {
if (_selects == null || _selects.size() == 0) {
return;
}
for (Select select : _selects) {
String func = select.func.toString() + ",";
if (select.attr == null) {
func = func.replace("@", "*");
} else {
func = func.replace("@", select.attr.table + "." + select.attr.columnName);
}
str.insert(insertAt, func);
insertAt += func.length();
if (select.field == null) {
break;
}
}
str.delete(insertAt - 1, insertAt);
}
public List<Field> getSelectFields() {
List<Field> fields = new ArrayList<Field>(_selects.size());
for (Select select : _selects) {
fields.add(select.field);
}
return fields;
}
public void setParameters(String conditionName, Object... params) {
assert _conditions.contains(new Condition(conditionName)) || _additionals.contains(new Condition(conditionName)) : "Couldn't find " + conditionName;
_params.put(conditionName, params);
}
public boolean isSelectAll() {
return _selects == null || _selects.size() == 0;
}
protected JoinBuilder<SearchCriteria<?>> findJoin(Map<String, JoinBuilder<SearchCriteria<?>>> jbmap, String joinName) {
JoinBuilder<SearchCriteria<?>> jb = jbmap.get(joinName);
if (jb != null) {
return jb;
}
for (JoinBuilder<SearchCriteria<?>> j2 : jbmap.values()) {
SearchCriteria<?> sc = j2.getT();
if(sc._joins != null)
jb = findJoin(sc._joins, joinName);
if (jb != null) {
return jb;
}
}
assert (false) : "Unable to find a join by the name " + joinName;
return null;
}
public void setJoinParameters(String joinName, String conditionName, Object... params) {
JoinBuilder<SearchCriteria<?>> join = findJoin(_joins, joinName);
assert (join != null) : "Incorrect join name specified: " + joinName;
join.getT().setParameters(conditionName, params);
}
public void addJoinAnd(String joinName, String field, Op op, Object... values) {
JoinBuilder<SearchCriteria<?>> join = _joins.get(joinName);
assert (join != null) : "Incorrect join name specified: " + joinName;
join.getT().addAnd(field, op, values);
}
public void addJoinOr(String joinName, String field, Op op, Object... values) {
JoinBuilder<SearchCriteria<?>> join = _joins.get(joinName);
assert (join != null) : "Incorrect join name specified: " + joinName;
join.getT().addOr(field, op, values);
}
public SearchCriteria<?> getJoin(String joinName) {
return _joins.get(joinName).getT();
}
public Pair<GroupBy<?, ?>, List<Object>> getGroupBy() {
return _groupBy == null ? null : new Pair<GroupBy<?, ?>, List<Object>>(_groupBy, _groupByValues);
}
public void setGroupByValues(Object... values) {
for (Object value : values) {
_groupByValues.add(value);
}
}
public Class<K> getResultType() {
return _resultType;
}
public void addAnd(String field, Op op, Object... values) {
String name = Integer.toString(_counter++);
addCondition(name, " AND ", field, op);
setParameters(name, values);
}
public void addAnd(Attribute attr, Op op, Object... values) {
String name = Integer.toString(_counter++);
addCondition(name, " AND ", attr, op);
setParameters(name, values);
}
public void addOr(String field, Op op, Object... values) {
String name = Integer.toString(_counter++);
addCondition(name, " OR ", field, op);
setParameters(name, values);
}
public void addOr(Attribute attr, Op op, Object... values) {
String name = Integer.toString(_counter++);
addCondition(name, " OR ", attr, op);
setParameters(name, values);
}
protected void addCondition(String conditionName, String cond, String fieldName, Op op) {
Attribute attr = _attrs.get(fieldName);
assert attr != null : "Unable to find field: " + fieldName;
addCondition(conditionName, cond, attr, op);
}
protected void addCondition(String conditionName, String cond, Attribute attr, Op op) {
Condition condition = new Condition(conditionName, /*(_conditions.size() + _additionals.size()) == 0 ? "" : */cond, attr, op);
_additionals.add(condition);
}
public String getWhereClause() {
StringBuilder sql = new StringBuilder();
int i = 0;
for (Condition condition : _conditions) {
Object[] params = _params.get(condition.name);
if ((condition.op == null || condition.op.params == 0) || (params != null)) {
condition.toSql(sql, params, i++);
}
}
for (Condition condition : _additionals) {
Object[] params = _params.get(condition.name);
if ((condition.op.params == 0) || (params != null)) {
condition.toSql(sql, params, i++);
}
}
return sql.toString();
}
public List<Pair<Attribute, Object>> getValues() {
ArrayList<Pair<Attribute, Object>> params = new ArrayList<Pair<Attribute, Object>>(_params.size());
for (Condition condition : _conditions) {
Object[] objs = _params.get(condition.name);
if (condition.op != null && condition.op.params != 0 && objs != null) {
getParams(params, condition, objs);
}
}
for (Condition condition : _additionals) {
Object[] objs = _params.get(condition.name);
if ((condition.op.params == 0) || (objs != null)) {
getParams(params, condition, objs);
}
}
return params;
}
public Collection<JoinBuilder<SearchCriteria<?>>> getJoins() {
return _joins != null ? _joins.values() : null;
}
private void getParams(ArrayList<Pair<Attribute, Object>> params, Condition condition, Object[] objs) {
if (condition.op == Op.SC) {
assert (objs != null && objs.length > 0) : " Where's your search criteria object? " + condition.name;
params.addAll(((SearchCriteria<?>)objs[0]).getValues());
return;
}
if (objs != null && objs.length > 0) {
for (Object obj : objs) {
if ((condition.op != Op.EQ && condition.op != Op.NEQ) || (obj != null)) {
params.add(new Pair<Attribute, Object>(condition.attr, obj));
}
}
}
}
public Pair<String, ArrayList<Object>> toSql() {
StringBuilder sql = new StringBuilder();
return new Pair<String, ArrayList<Object>>(sql.toString(), null);
}
}