Skip to content

Commit 7919531

Browse files
author
Gerald Unterrainer
committed
finished query-rewrite
1 parent 3024f7b commit 7919531

9 files changed

+192
-277
lines changed

src/main/java/info/unterrainer/commons/httpserver/daos/BasicAsyncDao.java

-42
This file was deleted.

src/main/java/info/unterrainer/commons/httpserver/daos/BasicDao.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ListJson<P> getList(E em, long offset, long size, String selectClause, String jo
2626

2727
QueryBuilder<P, P> query();
2828

29-
CountQueryBuilder<P, P> countQuery();
29+
CountQueryBuilder<P> countQuery();
3030

3131
P create(E em, P entity);
3232

src/main/java/info/unterrainer/commons/httpserver/daos/BasicQueryBuilder.java

+8-34
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ public class BasicQueryBuilder<P extends BasicJpa, T, R extends BasicQueryBuilde
1515

1616
protected final EntityManagerFactory emf;
1717
protected final JpqlDao<P> dao;
18+
protected final Class<T> resultType;
1819

1920
protected EntityManager entityManager;
2021
protected String selectClause = "o";
2122
protected String joinClause;
2223
protected String whereClause;
23-
protected String orderByClause = "o.id ASC";
24+
protected String orderByClause;
2425
protected boolean lockPessimistic = false;
2526

2627
protected Map<String, Object> parameters = new HashMap<>();
@@ -95,7 +96,7 @@ public R where(final String whereClause) {
9596
/**
9697
* Adds an 'AND' part to the where-clause.
9798
* <p>
98-
* For example: .and("o.loggedIn=:loggedIn");
99+
* For example: .and("o.loggedIn = :loggedIn");
99100
*
100101
* @param andWhereClause the clause to add
101102
* @return an instance of this builder to provide a fluent interface
@@ -112,7 +113,7 @@ public R and(final String andWhereClause) {
112113
/**
113114
* Adds an 'OR' part to the where-clause.
114115
* <p>
115-
* For example: .or("o.loggedIn=:loggedIn");
116+
* For example: .or("o.loggedIn = :loggedIn");
116117
*
117118
* @param orWhereClause the clause to add
118119
* @return an instance of this builder to provide a fluent interface
@@ -126,27 +127,6 @@ public R or(final String orWhereClause) {
126127
return (R) this;
127128
}
128129

129-
/**
130-
* Clears the where-clause and resets it to default.
131-
* <p>
132-
* Default is the empty string.
133-
*
134-
* @return an instance of this builder to provide a fluent interface
135-
*/
136-
public R clearWhere() {
137-
return where(null);
138-
}
139-
140-
/**
141-
* Clears the order-by-clause and resets it to an empty string, thus effectively
142-
* clearing the order.
143-
*
144-
* @return an instance of this builder to provide a fluent interface
145-
*/
146-
public R clearOrderBy() {
147-
return orderBy("");
148-
}
149-
150130
/**
151131
* Sets the order-by-clause to the standard-order, which is {@code "o.id ASC"}
152132
* and since this is the default as well, this effectively resets it to default.
@@ -189,6 +169,8 @@ public R clearParameters() {
189169
*/
190170
@SuppressWarnings("unchecked")
191171
public R parameters(final ParamMap params) {
172+
if (params == null)
173+
return (R) this;
192174
parameters = params.getParameters();
193175
if (this.parameters == null)
194176
parameters = new HashMap<>();
@@ -221,16 +203,11 @@ public R addParam(final String paramKey, final Object paramValue) {
221203
@SuppressWarnings("unchecked")
222204
public R orderBy(final String orderByClause) {
223205
this.orderByClause = orderByClause;
224-
if (this.orderByClause == null)
225-
this.orderByClause = "o.id ASC";
226206
return (R) this;
227207
}
228208

229209
/**
230210
* Adds an ASC-segment to the order-by-clause.
231-
* <p>
232-
* Since the standard-order is {@code "o.id ASC"} it is advised to call
233-
* {@link #clearOrderBy()} first.
234211
*
235212
* @param field the name of the field ("o.id" or "o.createdOn" for example)
236213
* @return an instance of this builder to provide a fluent interface
@@ -240,16 +217,13 @@ public R asc(final String field) {
240217
if (orderByClause == null || orderByClause.isBlank())
241218
orderByClause = field;
242219
else
243-
orderByClause = ", " + field;
220+
orderByClause += ", " + field;
244221
orderByClause += " ASC";
245222
return (R) this;
246223
}
247224

248225
/**
249226
* Adds an DESC-segment to the order-by-clause.
250-
* <p>
251-
* Since the standard-order is {@code "o.id ASC"} it is advised to call
252-
* {@link #clearOrderBy()} first.
253227
*
254228
* @param field the name of the field ("o.id" or "o.createdOn" for example)
255229
* @return an instance of this builder to provide a fluent interface
@@ -259,7 +233,7 @@ public R desc(final String field) {
259233
if (orderByClause == null || orderByClause.isBlank())
260234
orderByClause = field;
261235
else
262-
orderByClause = ", " + field;
236+
orderByClause += ", " + field;
263237
orderByClause += " DESC";
264238
return (R) this;
265239
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package info.unterrainer.commons.httpserver.daos;
22

33
import javax.persistence.EntityManagerFactory;
4-
import javax.persistence.Query;
54

65
import info.unterrainer.commons.rdbutils.Transactions;
76
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
87

9-
public class CountQueryBuilder<P extends BasicJpa, T> extends BasicQueryBuilder<P, T, CountQueryBuilder<P, T>> {
8+
public class CountQueryBuilder<P extends BasicJpa> extends BasicQueryBuilder<P, Long, CountQueryBuilder<P>> {
109

1110
CountQueryBuilder(final EntityManagerFactory emf, final JpqlDao<P> dao) {
12-
super(emf, dao);
11+
super(emf, dao, Long.class);
1312
}
1413

15-
public Query build() {
14+
public Long build() {
1615
if (entityManager != null)
17-
return dao.getCountQuery(entityManager, selectClause, joinClause, whereClause, parameters);
18-
return Transactions.withNewTransactionReturning(emf,
19-
em -> dao.getCountQuery(em, selectClause, joinClause, whereClause, parameters));
16+
return (Long) dao.getCountQuery(entityManager, selectClause, joinClause, whereClause, parameters, null)
17+
.getSingleResult();
18+
return (Long) Transactions.withNewTransactionReturning(emf,
19+
em -> dao.getCountQuery(em, selectClause, joinClause, whereClause, parameters, null).getSingleResult());
2020
}
2121
}
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,17 @@
11
package info.unterrainer.commons.httpserver.daos;
22

3-
import java.util.List;
4-
import java.util.function.Function;
5-
6-
import javax.persistence.EntityManager;
73
import javax.persistence.EntityManagerFactory;
8-
import javax.persistence.LockModeType;
9-
import javax.persistence.TypedQuery;
104

11-
import info.unterrainer.commons.rdbutils.Transactions;
125
import info.unterrainer.commons.rdbutils.entities.BasicAsyncJpa;
13-
import info.unterrainer.commons.rdbutils.enums.AsyncState;
146

15-
public class JpqlAsyncDao<P extends BasicAsyncJpa> extends JpqlDao<P> implements BasicAsyncDao<P, EntityManager> {
7+
public class JpqlAsyncDao<P extends BasicAsyncJpa> extends JpqlDao<P> {
168

179
public JpqlAsyncDao(final EntityManagerFactory emf, final Class<P> type) {
1810
super(emf, type);
1911
}
2012

2113
@Override
22-
public P getLastWith(final AsyncState... states) {
23-
return Transactions.withNewTransactionReturning(emf, em -> getLastWith(em, states));
24-
}
25-
26-
@Override
27-
public P getLastWith(final EntityManager em, final AsyncState... states) {
28-
return internalGet(em, false, false, null, states);
29-
}
30-
31-
@Override
32-
public List<P> getLastNWith(final Long count, final AsyncState... states) {
33-
return Transactions.withNewTransactionReturning(emf, em -> getLastNWith(em, count, states));
34-
}
35-
36-
@Override
37-
public List<P> getLastNWith(final EntityManager em, final Long count, final AsyncState... states) {
38-
return internalGetList(em, count, false, false, null, states);
39-
}
40-
41-
@Override
42-
public P getNextWith(final AsyncState... states) {
43-
return Transactions.withNewTransactionReturning(emf, em -> getNextWith(em, states));
44-
}
45-
46-
@Override
47-
public P getNextWith(final EntityManager em, final AsyncState... states) {
48-
return internalGet(em, false, true, null, states);
49-
}
50-
51-
@Override
52-
public List<P> getNextNWith(final Long count, final AsyncState... states) {
53-
return Transactions.withNewTransactionReturning(emf, em -> getNextNWith(em, count, states));
54-
}
55-
56-
@Override
57-
public List<P> getNextNWith(final EntityManager em, final Long count, final AsyncState... states) {
58-
return internalGetList(em, count, false, true, null, states);
59-
}
60-
61-
@Override
62-
public P lockedGetNextWith(final AsyncState stateToSetTo, final AsyncState... states) {
63-
return Transactions.withNewTransactionReturning(emf, em -> lockedGetNextWith(em, stateToSetTo, states));
64-
}
65-
66-
@Override
67-
public P lockedGetNextWith(final EntityManager em, final AsyncState stateToSetTo, final AsyncState... states) {
68-
return internalGet(em, true, true, stateToSetTo, states);
69-
}
70-
71-
@Override
72-
public List<P> lockedGetNextNWith(final Long count, final AsyncState stateToSetTo, final AsyncState... states) {
73-
return Transactions.withNewTransactionReturning(emf, em -> lockedGetNextNWith(em, count, stateToSetTo, states));
74-
}
75-
76-
@Override
77-
public List<P> lockedGetNextNWith(final EntityManager em, final Long count, final AsyncState stateToSetTo,
78-
final AsyncState... states) {
79-
return internalGetList(em, count, true, true, stateToSetTo, states);
80-
}
81-
82-
public P internalGet(final EntityManager em, final boolean lockPessimistic, final boolean ascending,
83-
final AsyncState stateToSetTo, final AsyncState... states) {
84-
List<P> list = internalGetList(em, 1, lockPessimistic, ascending, stateToSetTo, states);
85-
if (list.size() == 0)
86-
return null;
87-
return list.get(0);
88-
}
89-
90-
public List<P> internalGetList(final EntityManager em, final long size, final boolean lockPessimistic,
91-
final boolean ascending, final AsyncState stateToSetTo, final AsyncState... states) {
92-
int s = Integer.MAX_VALUE;
93-
if (size < s)
94-
s = (int) size;
95-
96-
StringBuilder sb = new StringBuilder();
97-
98-
sb.append("SELECT o FROM %s AS o ");
99-
boolean isFirst = true;
100-
for (int i = 0; i < states.length; i++) {
101-
if (isFirst) {
102-
sb.append("WHERE ");
103-
isFirst = false;
104-
} else
105-
sb.append("OR ");
106-
sb.append("state = :state");
107-
sb.append(i);
108-
sb.append(" ");
109-
}
110-
sb.append("ORDER BY o.id " + (ascending ? "ASC" : "DESC"));
111-
112-
TypedQuery<P> q = em.createQuery(String.format(sb.toString(), type.getSimpleName()), type).setMaxResults(s);
113-
if (lockPessimistic)
114-
q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
115-
116-
for (int i = 0; i < states.length; i++)
117-
q.setParameter("state" + i, states[i]);
118-
119-
List<P> r = q.getResultList();
120-
121-
if (stateToSetTo != null)
122-
for (P jpa : r) {
123-
jpa.setState(stateToSetTo);
124-
em.merge(jpa);
125-
}
126-
127-
return r;
128-
}
129-
130-
@Override
131-
public P setStateTo(final AsyncState stateToSetTo, final Long id) {
132-
return setStateTo(stateToSetTo, id, null);
133-
}
134-
135-
@Override
136-
public P setStateTo(final EntityManager em, final AsyncState stateToSetTo, final Long id) {
137-
return setStateTo(em, stateToSetTo, id, null);
138-
}
139-
140-
@Override
141-
public P setStateTo(final AsyncState stateToSetTo, final Long id, final Function<P, P> additionalTransformations) {
142-
return Transactions.withNewTransactionReturning(emf,
143-
em -> setStateTo(em, stateToSetTo, id, additionalTransformations));
144-
}
145-
146-
@Override
147-
public P setStateTo(final EntityManager em, final AsyncState stateToSetTo, final Long id,
148-
final Function<P, P> additionalTransformations) {
149-
P jpa = getById(em, id);
150-
jpa.setState(stateToSetTo);
151-
if (additionalTransformations != null)
152-
jpa = additionalTransformations.apply(jpa);
153-
update(em, jpa);
154-
return jpa;
14+
public QueryAsyncBuilder<P, P> query() {
15+
return new QueryAsyncBuilder<>(emf, this, type);
15516
}
15617
}

0 commit comments

Comments
 (0)