-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuery.java
More file actions
467 lines (382 loc) · 13.3 KB
/
Copy pathQuery.java
File metadata and controls
467 lines (382 loc) · 13.3 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package org.javawebstack.orm.query;
import org.javawebstack.orm.Model;
import org.javawebstack.orm.Repo;
import org.javawebstack.orm.SQLMapper;
import org.javawebstack.orm.Session;
import org.javawebstack.orm.exception.ORMQueryException;
import org.javawebstack.orm.wrapper.SQL;
import org.javawebstack.orm.wrapper.builder.SQLQueryString;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
public class Query<T extends Model> {
private final Repo<T> repo;
private final Class<T> model;
private SQL connection;
private List<String> select = new ArrayList<>();
private final QueryGroup<T> where = new QueryGroup<>();
private Integer offset;
private Integer limit;
private final List<QueryOrderBy> order = new ArrayList<>();
private boolean withDeleted = false;
private final List<QueryColumn> groupBy = new ArrayList<>();
private QueryGroup<T> having;
private boolean applyAccessible = false;
private Object accessor;
public Query(Class<T> model) {
this(Repo.get(model), model);
}
public Query(Repo<T> repo, Class<T> model) {
this.repo = repo;
this.model = model;
Session session = Session.current();
this.connection = repo.getConnection();
if(session != null && session.getConnection() != null)
this.connection = session.getConnection();
}
public Query<T> via(SQL connection) {
this.connection = connection;
return this;
}
public boolean isWithDeleted() {
return withDeleted;
}
public boolean shouldApplyAccessible() {
return applyAccessible;
}
public Object getAccessor() {
return accessor;
}
public QueryGroup<T> getWhereGroup() {
return where;
}
public List<String> getSelect() {
return select;
}
public List<QueryColumn> getGroupBy() {
return groupBy;
}
public QueryGroup<T> getHaving() {
return having;
}
public Integer getLimit() {
return limit;
}
public Integer getOffset() {
return offset;
}
public List<QueryOrderBy> getOrder() {
return order;
}
public Repo<T> getRepo() {
return repo;
}
public Class<T> getModel() {
return model;
}
public Query<T> select(String... columns) {
this.select = Arrays.asList(columns);
return this;
}
public Query<T> and(Function<QueryGroup<T>, QueryGroup<T>> group) {
where.and(group);
return this;
}
public Query<T> or(Function<QueryGroup<T>, QueryGroup<T>> group) {
where.or(group);
return this;
}
public Query<T> where(Object left, String condition, Object right) {
where.where(left, condition, right);
return this;
}
public Query<T> where(Object left, Object right) {
where.where(left, right);
return this;
}
public Query<T> where(Class<? extends Model> leftTable, String left, String operator, Class<? extends Model> rightTable, String right) {
if (rightTable != null)
right = Repo.get(rightTable).getInfo().getTableName() + "." + Repo.get(rightTable).getInfo().getColumnName(right);
return where(leftTable, left, operator, new QueryColumn(right));
}
public Query<T> where(Class<? extends Model> leftTable, String left, String operator, Object right) {
if (leftTable != null)
left = Repo.get(leftTable).getInfo().getTableName() + "." + Repo.get(leftTable).getInfo().getColumnName(left);
return where(left, operator, right);
}
public Query<T> like(String left, Object right) {
return where(left, "LIKE", right);
}
public Query<T> orLike(String left, Object right) {
return orWhere(left, "LIKE", right);
}
public Query<T> orWhere(Class<? extends Model> leftTable, String left, String operator, Class<? extends Model> rightTable, String right) {
if (rightTable != null)
right = Repo.get(rightTable).getInfo().getTableName() + "." + Repo.get(rightTable).getInfo().getColumnName(right);
return orWhere(leftTable, left, operator, new QueryColumn(right));
}
public Query<T> orWhere(Class<? extends Model> leftTable, String left, String operator, Object right) {
if (leftTable != null)
left = Repo.get(leftTable).getInfo().getTableName() + "." + Repo.get(leftTable).getInfo().getColumnName(left);
return orWhere(left, operator, right);
}
public QueryGroup<T> orWhereMorph(String name, Class<? extends Model> type) {
return where.orWhereMorph(name, type);
}
public QueryGroup<T> orWhereMorph(String name, Class<? extends Model> type, Object id) {
return where.orWhereMorph(name, type, id);
}
public QueryGroup<T> orWhereMorph(String name, Model entity) {
return where.orWhereMorph(name, entity);
}
public Query<T> whereId(String operator, Object right) {
return where(repo.getInfo().getIdField(), operator, right);
}
public Query<T> whereId(Object right) {
return whereId("=", right);
}
public Query<T> whereId(Class<? extends Model> other, String field) {
return whereId("=", other, field);
}
public Query<T> whereId(String operator, Class<? extends Model> other, String field) {
return whereId(operator, new QueryColumn(Repo.get(other).getInfo().getTableName() + "." + Repo.get(other).getInfo().getColumnName(field)));
}
public Query<T> orWhereId(String operator, Object right) {
return orWhere(repo.getInfo().getIdField(), operator, right);
}
public Query<T> orWhereId(Object right) {
return orWhereId("=", right);
}
public Query<T> orWhereId(Class<? extends Model> other, String field) {
return orWhereId("=", other, field);
}
public Query<T> orWhereId(String operator, Class<? extends Model> other, String field) {
return orWhereId(operator, new QueryColumn(Repo.get(other).getInfo().getTableName() + "." + Repo.get(other).getInfo().getColumnName(field)));
}
@Deprecated
public Query<T> isNull(Object left) {
where.isNull(left);
return this;
}
@Deprecated
public Query<T> notNull(Object left) {
where.notNull(left);
return this;
}
public Query<T> whereNull(Object left) {
where.whereNull(left);
return this;
}
public Query<T> whereNotNull(Object left) {
where.whereNotNull(left);
return this;
}
public Query<T> orWhere(Object left, String condition, Object right) {
where.orWhere(left, condition, right);
return this;
}
public Query<T> orWhere(Object left, Object right) {
where.orWhere(left, right);
return this;
}
@Deprecated
public Query<T> orIsNull(Object left) {
where.orIsNull(left);
return this;
}
@Deprecated
public Query<T> orNotNull(Object left) {
where.orNotNull(left);
return this;
}
public Query<T> orWhereNull(Object left) {
where.orWhereNull(left);
return this;
}
public Query<T> orWhereNotNull(Object left) {
where.orWhereNotNull(left);
return this;
}
public <M extends Model> Query<T> whereExists(Class<M> model, Function<Query<M>, Query<M>> consumer) {
where.whereExists(model, consumer);
return this;
}
public <M extends Model> Query<T> orWhereExists(Class<M> model, Function<Query<M>, Query<M>> consumer) {
where.orWhereExists(model, consumer);
return this;
}
public Query<T> whereIn(Object left, Object... values) {
where.whereIn(left, values);
return this;
}
public Query<T> whereNotIn(Object left, Object... values) {
where.whereNotIn(left, values);
return this;
}
public Query<T> orWhereIn(Object left, Object... values) {
where.orWhereIn(left, values);
return this;
}
public Query<T> orWhereNotIn(Object left, Object... values) {
where.orWhereNotIn(left, values);
return this;
}
public Query<T> groupBy(String column) {
groupBy.add(new QueryColumn(column));
return this;
}
public Query<T> groupBy(QueryColumn column) {
groupBy.add(column);
return this;
}
public Query<T> having(Consumer<QueryGroup<T>> consumer) {
having = new QueryGroup<>();
consumer.accept(having);
return this;
}
public Query<T> has(Query<?> relation, String operator, int count) {
where.has(relation, operator, count);
return this;
}
public Query<T> has(Query<?> relation) {
return has(relation, ">=", 1);
}
public Query<T> accessible(Object accessor) {
this.applyAccessible = true;
this.accessor = accessor;
return this;
}
public Query<T> filter(Map<String, String> filter) {
if (filter == null)
return this;
repo.getFilter().filter(this, filter);
return this;
}
public Query<T> search(String search) {
if (search == null || search.length() == 0)
return this;
repo.getFilter().search(this, search);
return this;
}
public Query<T> order(String columnName) throws ORMQueryException {
return order(columnName, false);
}
public Query<T> order(String columnName, boolean desc) throws ORMQueryException {
return order(new QueryColumn(columnName), desc);
}
public Query<T> order(QueryColumn column, boolean desc) throws ORMQueryException{
this.order.add(new QueryOrderBy(column, desc));
return this;
}
public Query<T> limit(int offset, int limit) {
return offset(offset).limit(limit);
}
public Query<T> limit(int limit) {
this.limit = limit;
return this;
}
public Query<T> offset(int offset) {
this.offset = offset;
return this;
}
public Query<T> withDeleted() {
withDeleted = true;
return this;
}
public void finalDelete() {
SQLQueryString qs = connection.builder().buildDelete(this);
try {
connection.write(qs.getQuery(), qs.getParameters().toArray());
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
}
}
public Timestamp delete() {
if (!repo.getInfo().isSoftDelete()) {
finalDelete();
return null;
}
Timestamp now = Timestamp.from(Instant.now());
Map<String, Object> values = new HashMap<>();
values.put(repo.getInfo().getColumnName(repo.getInfo().getSoftDeleteField()), now);
update(values);
return now;
}
public void restore() {
if (!repo.getInfo().isSoftDelete())
return;
Map<String, Object> values = new HashMap<>();
values.put(repo.getInfo().getColumnName(repo.getInfo().getSoftDeleteField()), null);
withDeleted().update(values);
}
public T refresh(T entity) {
SQLQueryString qs = connection.builder().buildQuery(this);
try {
ResultSet rs = connection.read(qs.getQuery(), qs.getParameters().toArray());
if(rs.next()) {
SQLMapper.mapBack(repo, rs, entity);
connection.close(rs);
return entity;
} else {
return null;
}
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
}
}
public void update(T entity) {
update(SQLMapper.map(repo, entity));
}
public void update(Map<String, Object> values) {
SQLQueryString queryString = connection.builder().buildUpdate(this, values);
try {
connection.write(queryString.getQuery(), queryString.getParameters().toArray());
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
}
}
public List<T> all() {
SQLQueryString qs = connection.builder().buildQuery(this);
try {
ResultSet rs = connection.read(qs.getQuery(), qs.getParameters().toArray());
List<T> list = SQLMapper.map(repo, rs, new ArrayList<>());
connection.close(rs);
return list;
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
}
}
public List<T> get() {
return all();
}
public T first() {
List<T> list = limit(1).all();
if (list.size() == 0)
return null;
return list.get(0);
}
public Stream<T> stream() {
return all().stream();
}
public int count() {
SQLQueryString qs = connection.builder().buildQuery(this.select("count(*)"));
try {
ResultSet rs = connection.read(qs.getQuery(), qs.getParameters().toArray());
int c = 0;
if (rs.next())
c = rs.getInt(1);
connection.close(rs);
return c;
} catch (SQLException throwables) {
throw new ORMQueryException(throwables);
}
}
public boolean hasRecords() {
return count() > 0;
}
}