Skip to content

Commit 1cc8b68

Browse files
committed
update
1 parent e12344e commit 1cc8b68

13 files changed

+391
-175
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,24 @@ public AsyncListQueryBuilder<P, P> select(final String selectClause) {
7979
public SingleQueryBuilder<P, P> select(final Long id) {
8080
return new SingleQueryBuilder<>(this, id);
8181
}
82+
83+
/**
84+
* Insert the given entity.
85+
*
86+
* @param entity to insert.
87+
* @return the entity after inserting
88+
*/
89+
public InsertQueryBuilder<P> insert(final P entity) {
90+
return new InsertQueryBuilder<>(this, entity);
91+
}
92+
93+
/**
94+
* Update the given entity.
95+
*
96+
* @param entity to update.
97+
* @return the entity after updating
98+
*/
99+
public UpdateQueryBuilder<P> update(final P entity) {
100+
return new UpdateQueryBuilder<>(this, entity);
101+
}
82102
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,7 @@
11
package info.unterrainer.commons.httpserver.daos;
22

3-
import javax.persistence.TypedQuery;
4-
5-
import info.unterrainer.commons.httpserver.jsons.ListJson;
63
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
74

85
public interface BasicDao<P extends BasicJpa, E> {
96

10-
P _getById(Long id);
11-
12-
P create(P entity);
13-
14-
P update(P entity);
15-
16-
ListJson<P> getList(E em, long offset, long size, String selectClause, String joinClause, String whereClause,
17-
ParamMap params, String orderByClause);
18-
19-
UpsertResult<P> upsert(String whereClause, ParamMap params, P entity);
20-
21-
UpsertResult<P> upsert(TypedQuery<P> query, P entity);
22-
23-
void _delete(Long id);
24-
25-
P _getById(E em, Long id);
26-
27-
P create(E em, P entity);
28-
29-
P update(E em, P entity);
30-
31-
UpsertResult<P> upsert(E em, String whereClause, ParamMap params, P entity);
32-
33-
UpsertResult<P> upsert(E em, TypedQuery<P> query, P entity);
34-
35-
void _delete(E em, Long id);
367
}

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

+40-57
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ public class BasicJpqlDao<P extends BasicJpa> implements BasicDao<P, EntityManag
2828
protected final EntityManagerFactory emf;
2929
protected final Class<P> type;
3030

31-
@Override
32-
public P _getById(final Long id) {
31+
P _getById(final Long id) {
3332
return Transactions.withNewTransactionReturning(emf, em -> _getById(em, id));
3433
}
3534

36-
@Override
37-
public ListJson<P> getList(final EntityManager em, final long offset, final long size, final String selectClause,
35+
ListJson<P> getList(final EntityManager em, final long offset, final long size, final String selectClause,
3836
final String joinClause, final String whereClause, final ParamMap params, final String orderByClause) {
3937
ListJson<P> r = new ListJson<>();
4038
r.setEntries(
@@ -47,33 +45,29 @@ public ListJson<P> getList(final EntityManager em, final long offset, final long
4745
return r;
4846
}
4947

50-
@Override
51-
public P create(final P entity) {
48+
P create(final P entity) {
5249
return Transactions.withNewTransactionReturning(emf, em -> create(em, entity));
5350
}
5451

55-
@Override
56-
public P create(final EntityManager em, final P entity) {
52+
P create(final EntityManager em, final P entity) {
5753
LocalDateTime time = DateUtils.nowUtc();
5854
entity.setCreatedOn(time);
5955
entity.setEditedOn(time);
6056
em.persist(entity);
6157
return entity;
6258
}
6359

64-
@Override
65-
public P update(final P entity) {
66-
return Transactions.withNewTransactionReturning(emf, em -> update(em, entity));
60+
P _update(final P entity) {
61+
return Transactions.withNewTransactionReturning(emf, em -> _update(em, entity));
6762
}
6863

69-
@Override
70-
public P update(final EntityManager em, final P entity) {
64+
P _update(final EntityManager em, final P entity) {
7165
LocalDateTime time = DateUtils.nowUtc();
7266
entity.setEditedOn(time);
7367
return em.merge(entity);
7468
}
7569

76-
public <T> List<T> getList(final EntityManager em, final TypedQuery<T> query, final long offset, final long size) {
70+
<T> List<T> getList(final EntityManager em, final TypedQuery<T> query, final long offset, final long size) {
7771
int s = Integer.MAX_VALUE;
7872
if (size < s)
7973
s = (int) size;
@@ -85,24 +79,6 @@ public <T> List<T> getList(final EntityManager em, final TypedQuery<T> query, fi
8579
return query.getResultList();
8680
}
8781

88-
@Override
89-
public UpsertResult<P> upsert(final String whereClause, final ParamMap params, final P entity) {
90-
return Transactions.withNewTransactionReturning(emf, em -> upsert(em, getQuery(em, "o", null, whereClause,
91-
params == null ? null : params.getParameters(), type, null, false, null), entity));
92-
}
93-
94-
@Override
95-
public UpsertResult<P> upsert(final EntityManager em, final String whereClause, final ParamMap params,
96-
final P entity) {
97-
return upsert(em, getQuery(em, "o", null, whereClause, params == null ? null : params.getParameters(), type,
98-
null, false, null), entity);
99-
}
100-
101-
@Override
102-
public UpsertResult<P> upsert(final TypedQuery<P> query, final P entity) {
103-
return Transactions.withNewTransactionReturning(emf, em -> upsert(em, query, entity));
104-
}
105-
10682
private <T> T getFirst(final EntityManager em, final TypedQuery<T> query) {
10783
List<T> r = getList(em, query, 0, 1);
10884
if (r.size() == 1) {
@@ -112,24 +88,7 @@ private <T> T getFirst(final EntityManager em, final TypedQuery<T> query) {
11288
return null;
11389
}
11490

115-
public <T> UpsertResult<P> _upsert(final EntityManager em, final TypedQuery<T> query, final P entity) {
116-
boolean wasInserted = false;
117-
boolean wasUpdated = false;
118-
T e = getFirst(em, query);
119-
if (e == null) {
120-
e = create(em, entity);
121-
wasInserted = true;
122-
} else {
123-
entity.setId(e.getId());
124-
entity.setCreatedOn(e.getCreatedOn());
125-
e = update(em, entity);
126-
wasUpdated = true;
127-
}
128-
return UpsertResult.<P>builder().wasInserted(wasInserted).wasUpdated(wasUpdated).jpa(e).build();
129-
}
130-
131-
@Override
132-
public UpsertResult<P> upsert(final EntityManager em, final TypedQuery<P> query, final P entity) {
91+
UpsertResult<P> _upsert(final EntityManager em, final TypedQuery<P> query, final P entity) {
13392
boolean wasInserted = false;
13493
boolean wasUpdated = false;
13594
P e = getFirst(em, query);
@@ -139,28 +98,25 @@ public UpsertResult<P> upsert(final EntityManager em, final TypedQuery<P> query,
13998
} else {
14099
entity.setId(e.getId());
141100
entity.setCreatedOn(e.getCreatedOn());
142-
e = update(em, entity);
101+
e = _update(em, entity);
143102
wasUpdated = true;
144103
}
145104
return UpsertResult.<P>builder().wasInserted(wasInserted).wasUpdated(wasUpdated).jpa(e).build();
146105
}
147106

148-
@Override
149-
public void _delete(final Long id) {
107+
void _delete(final Long id) {
150108
Transactions.withNewTransaction(emf, em -> {
151109
_delete(em, id);
152110
});
153111
}
154112

155-
@Override
156-
public void _delete(final EntityManager em, final Long id) {
113+
void _delete(final EntityManager em, final Long id) {
157114
em.createQuery(String.format("DELETE FROM %s AS o WHERE o.id = :id", type.getSimpleName()))
158115
.setParameter("id", id)
159116
.executeUpdate();
160117
}
161118

162-
@Override
163-
public P _getById(final EntityManager em, final Long id) {
119+
P _getById(final EntityManager em, final Long id) {
164120
try {
165121
return getQuery(em, "o", null, "o.id = :id", Map.of("id", id), type, null, false, null).getSingleResult();
166122
} catch (NoResultException e) {
@@ -278,6 +234,33 @@ else if (!orderBy.isBlank())
278234
return q;
279235
}
280236

237+
<T> TypedQuery<T> getDeleteQuery(final EntityManager em, final String joinClause, final String whereClause,
238+
final Map<String, Object> params) {
239+
String query = "DELETE FROM %s AS o";
240+
241+
if (joinClause != null && !joinClause.isBlank())
242+
query += " " + joinClause;
243+
244+
query += buildWhereClause(whereClause, null);
245+
246+
query = String.format(query, this.type.getSimpleName());
247+
248+
String msg = query;
249+
if (params != null)
250+
for (Entry<String, Object> p : params.entrySet())
251+
msg += "\\n " + p.getKey() + ": " + p.getValue();
252+
log.debug(msg);
253+
254+
@SuppressWarnings("unchecked")
255+
Class<T> t = (Class<T>) this.type;
256+
257+
TypedQuery<T> q = em.createQuery(query, t);
258+
if (params != null)
259+
for (Entry<String, Object> e : params.entrySet())
260+
q.setParameter(e.getKey(), e.getValue());
261+
return q;
262+
}
263+
281264
private boolean isAllowed(final info.unterrainer.commons.httpserver.daos.ListQueryBuilder query,
282265
final EntityManager em) {
283266
String tenantReferenceField = "testId";

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

+8-74
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package info.unterrainer.commons.httpserver.daos;
22

33
import java.util.HashMap;
4-
import java.util.Map;
54

65
import javax.persistence.EntityManager;
76
import javax.persistence.EntityManagerFactory;
@@ -14,21 +13,17 @@
1413

1514
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
1615
public class BasicListQueryBuilder<P extends BasicJpa, T, R extends BasicListQueryBuilder<P, T, R>>
17-
extends BasicQueryEntityManagerBuilder<P, T, R> {
16+
extends BasicQueryGeneralBuilder<P, T, R> {
1817

1918
protected final EntityManagerFactory emf;
2019
@Getter
2120
protected final BasicJpqlDao<P> dao;
2221
protected final Class<T> resultType;
2322

2423
protected String selectClause = "o";
25-
protected String joinClause;
26-
protected String whereClause;
2724
protected String orderByClause;
2825
protected boolean lockPessimistic = false;
2926

30-
protected Map<String, Object> parameters = new HashMap<>();
31-
3227
void setSelect(final String selectClause) {
3328
this.selectClause = selectClause;
3429
if (this.selectClause == null || this.selectClause.isBlank())
@@ -40,76 +35,12 @@ public TypedQuery<T> getTypedQuery(final EntityManager em) {
4035
lockPessimistic, null);
4136
}
4237

43-
public javax.persistence.Query getCountQuery(final EntityManager em) {
44-
return dao.getCountQuery(em, selectClause, joinClause, whereClause, parameters, null);
45-
}
46-
47-
/**
48-
* Sets a custom join-clause.
49-
* <p>
50-
* Default is ""<br>
51-
* To reset it to default, set it to null or directly to an empty string.
52-
*
53-
* @param joinClause the new clause
54-
* @return an instance of this builder to provide a fluent interface
55-
*/
56-
@SuppressWarnings("unchecked")
57-
public R join(final String joinClause) {
58-
this.joinClause = joinClause;
59-
if (this.joinClause == null)
60-
this.joinClause = "";
61-
return (R) this;
62-
}
63-
64-
/**
65-
* Sets a custom where-clause.
66-
* <p>
67-
* Default is ""<br>
68-
* To reset it to default, set it to null or directly to an empty string.
69-
*
70-
* @param whereClause the new clause
71-
* @return an instance of this builder to provide a fluent interface
72-
*/
73-
@SuppressWarnings("unchecked")
74-
public R where(final String whereClause) {
75-
this.whereClause = whereClause;
76-
if (this.whereClause == null)
77-
this.whereClause = "";
78-
return (R) this;
38+
public TypedQuery<T> getDeleteQuery(final EntityManager em) {
39+
return dao.getDeleteQuery(em, joinClause, whereClause, parameters);
7940
}
8041

81-
/**
82-
* Adds an 'AND' part to the where-clause.
83-
* <p>
84-
* For example: .and("o.loggedIn = :loggedIn");
85-
*
86-
* @param andWhereClause the clause to add
87-
* @return an instance of this builder to provide a fluent interface
88-
*/
89-
@SuppressWarnings("unchecked")
90-
public R and(final String andWhereClause) {
91-
if (whereClause == null || whereClause.isBlank())
92-
whereClause = andWhereClause;
93-
else
94-
whereClause += " AND " + andWhereClause;
95-
return (R) this;
96-
}
97-
98-
/**
99-
* Adds an 'OR' part to the where-clause.
100-
* <p>
101-
* For example: .or("o.loggedIn = :loggedIn");
102-
*
103-
* @param orWhereClause the clause to add
104-
* @return an instance of this builder to provide a fluent interface
105-
*/
106-
@SuppressWarnings("unchecked")
107-
public R or(final String orWhereClause) {
108-
if (whereClause == null || whereClause.isBlank())
109-
whereClause = orWhereClause;
110-
else
111-
whereClause += " OR " + orWhereClause;
112-
return (R) this;
42+
public javax.persistence.Query getCountQuery(final EntityManager em) {
43+
return dao.getCountQuery(em, selectClause, joinClause, whereClause, parameters, null);
11344
}
11445

11546
/**
@@ -139,6 +70,7 @@ public R setReversedStandardOrder() {
13970
*
14071
* @return an instance of this builder to provide a fluent interface
14172
*/
73+
@Override
14274
public R clearParameters() {
14375
return parameters(null);
14476
}
@@ -152,6 +84,7 @@ public R clearParameters() {
15284
* @param params the new {@link ParamMap}
15385
* @return an instance of this builder to provide a fluent interface
15486
*/
87+
@Override
15588
@SuppressWarnings("unchecked")
15689
public R parameters(final ParamMap params) {
15790
if (params == null)
@@ -169,6 +102,7 @@ public R parameters(final ParamMap params) {
169102
* @param paramValue the value of the parameter
170103
* @return an instance of this builder to provide a fluent interface
171104
*/
105+
@Override
172106
@SuppressWarnings("unchecked")
173107
public R addParam(final String paramKey, final Object paramValue) {
174108
parameters.put(paramKey, paramValue);

0 commit comments

Comments
 (0)