Skip to content

Commit 4c36ab7

Browse files
author
Gerald Unterrainer
committed
working SingleQueryBuilder
1 parent 780f17c commit 4c36ab7

18 files changed

+202
-292
lines changed

src/main/java/info/unterrainer/commons/httpserver/HandlerUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public Long checkAndGetId(final Context ctx) {
2121

2222
public <P extends BasicJpa, E> P getJpaById(final Context ctx, final E entityManager, final BasicDao<P, E> dao) {
2323
Long id = checkAndGetId(ctx);
24-
P jpa = dao.getById(entityManager, id);
24+
P jpa = dao._getById(entityManager, id);
2525
if (jpa == null)
2626
throw new NotFoundException();
2727
return jpa;
2828
}
2929

3030
public <P extends BasicJpa, E> P getJpaById(final Context ctx, final BasicDao<P, E> dao) {
3131
Long id = checkAndGetId(ctx);
32-
P jpa = dao.getById(id);
32+
P jpa = dao._getById(id);
3333
if (jpa == null)
3434
throw new NotFoundException();
3535
return jpa;

src/main/java/info/unterrainer/commons/httpserver/daos/JpqlAsyncDao.java renamed to src/main/java/info/unterrainer/commons/httpserver/daos/AsyncJpqlDao.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
import info.unterrainer.commons.rdbutils.entities.BasicAsyncJpa;
66

7-
public class JpqlAsyncDao<P extends BasicAsyncJpa> extends BasicJpqlDao<P> {
7+
public class AsyncJpqlDao<P extends BasicAsyncJpa> extends BasicJpqlDao<P> {
88

99
/**
1010
* Generates a DAO that lets you build and execute queries.
1111
*
1212
* @param emf the {@link EntityManagerFactory} to use
1313
* @param type the return-type of the query (the underlying JPA)
1414
*/
15-
public JpqlAsyncDao(final EntityManagerFactory emf, final Class<P> type) {
15+
public AsyncJpqlDao(final EntityManagerFactory emf, final Class<P> type) {
1616
super(emf, type);
1717
}
1818

@@ -22,8 +22,8 @@ public JpqlAsyncDao(final EntityManagerFactory emf, final Class<P> type) {
2222
*
2323
* @return a query-builder
2424
*/
25-
public SelectAsyncQueryBuilder<P, P> select() {
26-
return new SelectAsyncQueryBuilder<>(emf, this, type);
25+
public AsyncListQueryBuilder<P, P> select() {
26+
return new AsyncListQueryBuilder<>(emf, this, type);
2727
}
2828

2929
/**
@@ -34,8 +34,8 @@ public SelectAsyncQueryBuilder<P, P> select() {
3434
* @param resultType the type the result will be
3535
* @return a query-builder
3636
*/
37-
public <T> SelectAsyncQueryBuilder<P, T> select(final Class<T> resultType) {
38-
return new SelectAsyncQueryBuilder<>(emf, this, resultType);
37+
public <T> AsyncListQueryBuilder<P, T> select(final Class<T> resultType) {
38+
return new AsyncListQueryBuilder<>(emf, this, resultType);
3939
}
4040

4141
/**
@@ -49,8 +49,8 @@ public <T> SelectAsyncQueryBuilder<P, T> select(final Class<T> resultType) {
4949
* @param resultType the type the result will be
5050
* @return a query-builder
5151
*/
52-
public <T> SelectAsyncQueryBuilder<P, T> select(final String selectClause, final Class<T> resultType) {
53-
SelectAsyncQueryBuilder<P, T> b = new SelectAsyncQueryBuilder<>(emf, this, resultType);
52+
public <T> AsyncListQueryBuilder<P, T> select(final String selectClause, final Class<T> resultType) {
53+
AsyncListQueryBuilder<P, T> b = new AsyncListQueryBuilder<>(emf, this, resultType);
5454
b.setSelect(selectClause);
5555
return b;
5656
}
@@ -64,9 +64,19 @@ public <T> SelectAsyncQueryBuilder<P, T> select(final String selectClause, final
6464
* a "SELECT o")
6565
* @return a query-builder
6666
*/
67-
public <T> SelectAsyncQueryBuilder<P, P> select(final String selectClause) {
68-
SelectAsyncQueryBuilder<P, P> b = new SelectAsyncQueryBuilder<>(emf, this, type);
67+
public AsyncListQueryBuilder<P, P> select(final String selectClause) {
68+
AsyncListQueryBuilder<P, P> b = new AsyncListQueryBuilder<>(emf, this, type);
6969
b.setSelect(selectClause);
7070
return b;
7171
}
72+
73+
/**
74+
* Get an element by ID.
75+
*
76+
* @param id the ID to fetch.
77+
* @return the element with the given ID or null, if there was no such thing
78+
*/
79+
public AsyncSingleQueryBuilder<P, P> select(final Long id) {
80+
return new AsyncSingleQueryBuilder<>(this, id);
81+
}
7282
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package info.unterrainer.commons.httpserver.daos;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
import javax.persistence.EntityManagerFactory;
8+
9+
import info.unterrainer.commons.rdbutils.entities.BasicAsyncJpa;
10+
import info.unterrainer.commons.rdbutils.enums.AsyncState;
11+
12+
public class AsyncListQueryBuilder<P extends BasicAsyncJpa, T>
13+
extends BasicListQueryBuilder<P, T, AsyncListQueryBuilder<P, T>> implements QueryInterface<P, T> {
14+
15+
private Set<AsyncState> asyncStates = new HashSet<>();
16+
17+
AsyncListQueryBuilder(final EntityManagerFactory emf, final AsyncJpqlDao<P> dao, final Class<T> resultType) {
18+
super(emf, dao, resultType);
19+
}
20+
21+
public ListQuery<P, T> build() {
22+
return new ListQuery<>(emf, this);
23+
}
24+
25+
/**
26+
* Adds a single or multiple OR-clauses containing the specified
27+
* {@link AsyncState}s.
28+
* <p>
29+
* For example calling
30+
* {@code .whereStateOf(AsyncState.NEW, AsyncState.PROCESSING)} will result in
31+
* the where-clause
32+
* {@code (..rest of where clause..) AND (state = 'NEW' OR state = 'PROCESSING').}
33+
* <p>
34+
* Repetitive calling of this method just adds all the AsyncState values
35+
* (doesn't clear the collection).
36+
*
37+
* @param asyncStates a single or number of AsyncState values
38+
* @return an instance of this builder to provide a fluent interface
39+
*/
40+
public AsyncListQueryBuilder<P, T> asyncStateOf(final AsyncState... asyncStates) {
41+
this.asyncStates.addAll(Arrays.asList(asyncStates));
42+
return this;
43+
}
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package info.unterrainer.commons.httpserver.daos;
2+
3+
import info.unterrainer.commons.rdbutils.entities.BasicAsyncJpa;
4+
5+
public class AsyncSingleQueryBuilder<P extends BasicAsyncJpa, T>
6+
extends BasicSingleQueryBuilder<P, T, AsyncSingleQueryBuilder<P, T>> {
7+
8+
AsyncSingleQueryBuilder(final AsyncJpqlDao<P> dao, final Long id) {
9+
super(dao, id);
10+
}
11+
12+
public P get() {
13+
if (entityManager == null)
14+
return dao._getById(id);
15+
return dao._getById(entityManager, id);
16+
}
17+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public interface BasicDao<P extends BasicJpa, E> {
99

10-
P getById(Long id);
10+
P _getById(Long id);
1111

1212
P create(P entity);
1313

@@ -22,7 +22,7 @@ ListJson<P> getList(E em, long offset, long size, String selectClause, String jo
2222

2323
void delete(Long id);
2424

25-
P getById(E em, Long id);
25+
P _getById(E em, Long id);
2626

2727
P create(E em, P entity);
2828

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public class BasicJpqlDao<P extends BasicJpa> implements BasicDao<P, EntityManag
2929
protected final Class<P> type;
3030

3131
@Override
32-
public P getById(final Long id) {
33-
return Transactions.withNewTransactionReturning(emf, em -> getById(em, id));
32+
public P _getById(final Long id) {
33+
return Transactions.withNewTransactionReturning(emf, em -> _getById(em, id));
3434
}
3535

3636
@Override
@@ -144,7 +144,7 @@ public void delete(final EntityManager em, final Long id) {
144144
}
145145

146146
@Override
147-
public P getById(final EntityManager em, final Long id) {
147+
public P _getById(final EntityManager em, final Long id) {
148148
try {
149149
return getQuery(em, "o", null, "o.id = :id", Map.of("id", id), type, null, false, null).getSingleResult();
150150
} catch (NoResultException e) {
@@ -262,7 +262,7 @@ else if (!orderBy.isBlank())
262262
return q;
263263
}
264264

265-
private boolean isAllowed(final info.unterrainer.commons.httpserver.daos.SelectQueryBuilder query,
265+
private boolean isAllowed(final info.unterrainer.commons.httpserver.daos.ListQueryBuilder query,
266266
final EntityManager em) {
267267
String tenantReferenceField = "testId";
268268
String tenantIdField = "tenantId";

src/main/java/info/unterrainer/commons/httpserver/daos/BasicSelectQueryBuilder.java renamed to src/main/java/info/unterrainer/commons/httpserver/daos/BasicListQueryBuilder.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@
55

66
import javax.persistence.EntityManager;
77
import javax.persistence.EntityManagerFactory;
8+
import javax.persistence.TypedQuery;
89

910
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
1011
import lombok.AccessLevel;
12+
import lombok.Getter;
1113
import lombok.RequiredArgsConstructor;
1214

1315
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
14-
public class BasicSelectQueryBuilder<P extends BasicJpa, T, R extends BasicSelectQueryBuilder<P, T, R>> {
16+
public class BasicListQueryBuilder<P extends BasicJpa, T, R extends BasicListQueryBuilder<P, T, R>>
17+
extends BasicQueryEntityManagerBuilder<P, T, R> {
1518

1619
protected final EntityManagerFactory emf;
20+
@Getter
1721
protected final BasicJpqlDao<P> dao;
1822
protected final Class<T> resultType;
1923

20-
protected EntityManager entityManager;
2124
protected String selectClause = "o";
2225
protected String joinClause;
2326
protected String whereClause;
@@ -26,27 +29,21 @@ public class BasicSelectQueryBuilder<P extends BasicJpa, T, R extends BasicSelec
2629

2730
protected Map<String, Object> parameters = new HashMap<>();
2831

29-
/**
30-
* Sets a custom {@link EntityManager}.
31-
* <p>
32-
* Default is to create one when creating the query.<br>
33-
* To reset it to default, set it to null.
34-
*
35-
* @param em an {@link EntityManager}
36-
* @return an instance of this builder to provide a fluent interface
37-
*/
38-
@SuppressWarnings("unchecked")
39-
public R entityManager(final EntityManager em) {
40-
entityManager = em;
41-
return (R) this;
42-
}
43-
4432
void setSelect(final String selectClause) {
4533
this.selectClause = selectClause;
4634
if (this.selectClause == null || this.selectClause.isBlank())
4735
this.selectClause = "o";
4836
}
4937

38+
public TypedQuery<T> getTypedQuery(final EntityManager em) {
39+
return dao.getQuery(em, selectClause, joinClause, whereClause, parameters, resultType, orderByClause,
40+
lockPessimistic, null);
41+
}
42+
43+
public javax.persistence.Query getCountQuery(final EntityManager em) {
44+
return dao.getCountQuery(em, selectClause, joinClause, whereClause, parameters, null);
45+
}
46+
5047
/**
5148
* Sets a custom join-clause.
5249
* <p>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package info.unterrainer.commons.httpserver.daos;
2+
3+
import javax.persistence.EntityManager;
4+
5+
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
6+
import lombok.AccessLevel;
7+
import lombok.Getter;
8+
import lombok.RequiredArgsConstructor;
9+
10+
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
11+
public class BasicQueryEntityManagerBuilder<P extends BasicJpa, T, R extends BasicQueryEntityManagerBuilder<P, T, R>> {
12+
13+
@Getter
14+
protected EntityManager entityManager;
15+
16+
/**
17+
* Sets a custom {@link EntityManager}.
18+
* <p>
19+
* Default is to create one when creating the query.<br>
20+
* To reset it to default, set it to null.
21+
*
22+
* @param em an {@link EntityManager}
23+
* @return an instance of this builder to provide a fluent interface
24+
*/
25+
@SuppressWarnings("unchecked")
26+
public R entityManager(final EntityManager em) {
27+
entityManager = em;
28+
return (R) this;
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package info.unterrainer.commons.httpserver.daos;
2+
3+
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
4+
import lombok.AccessLevel;
5+
import lombok.Getter;
6+
import lombok.RequiredArgsConstructor;
7+
8+
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
9+
public class BasicSingleQueryBuilder<P extends BasicJpa, T, R extends BasicSingleQueryBuilder<P, T, R>>
10+
extends BasicQueryEntityManagerBuilder<P, T, R> {
11+
12+
@Getter
13+
protected final BasicJpqlDao<P> dao;
14+
protected final Long id;
15+
}

src/main/java/info/unterrainer/commons/httpserver/daos/SelectCountQueryBuilder.java renamed to src/main/java/info/unterrainer/commons/httpserver/daos/CountQueryBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import info.unterrainer.commons.rdbutils.Transactions;
66
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
77

8-
public class SelectCountQueryBuilder<P extends BasicJpa> extends BasicSelectQueryBuilder<P, Long, SelectCountQueryBuilder<P>> {
8+
public class CountQueryBuilder<P extends BasicJpa> extends BasicListQueryBuilder<P, Long, CountQueryBuilder<P>> {
99

10-
SelectCountQueryBuilder(final EntityManagerFactory emf, final JpqlDao<P> dao) {
10+
CountQueryBuilder(final EntityManagerFactory emf, final JpqlDao<P> dao) {
1111
super(emf, dao, Long.class);
1212
}
1313

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public JpqlDao(final EntityManagerFactory emf, final Class<P> type) {
2323
*
2424
* @return a query-builder
2525
*/
26-
public SelectQueryBuilder<P, P> select() {
27-
return new SelectQueryBuilder<>(emf, this, type);
26+
public ListQueryBuilder<P, P> select() {
27+
return new ListQueryBuilder<>(emf, this, type);
2828
}
2929

3030
/**
@@ -35,8 +35,8 @@ public SelectQueryBuilder<P, P> select() {
3535
* @param resultType the type the result will be
3636
* @return a query-builder
3737
*/
38-
public <T> SelectQueryBuilder<P, T> select(final Class<T> resultType) {
39-
return new SelectQueryBuilder<>(emf, this, resultType);
38+
public <T> ListQueryBuilder<P, T> select(final Class<T> resultType) {
39+
return new ListQueryBuilder<>(emf, this, resultType);
4040
}
4141

4242
/**
@@ -50,8 +50,8 @@ public <T> SelectQueryBuilder<P, T> select(final Class<T> resultType) {
5050
* @param resultType the type the result will be
5151
* @return a query-builder
5252
*/
53-
public <T> SelectQueryBuilder<P, T> select(final String selectClause, final Class<T> resultType) {
54-
SelectQueryBuilder<P, T> b = new SelectQueryBuilder<>(emf, this, resultType);
53+
public <T> ListQueryBuilder<P, T> select(final String selectClause, final Class<T> resultType) {
54+
ListQueryBuilder<P, T> b = new ListQueryBuilder<>(emf, this, resultType);
5555
b.setSelect(selectClause);
5656
return b;
5757
}
@@ -65,9 +65,19 @@ public <T> SelectQueryBuilder<P, T> select(final String selectClause, final Clas
6565
* a "SELECT o")
6666
* @return a query-builder
6767
*/
68-
public <T> SelectQueryBuilder<P, P> select(final String selectClause) {
69-
SelectQueryBuilder<P, P> b = new SelectQueryBuilder<>(emf, this, type);
68+
public ListQueryBuilder<P, P> select(final String selectClause) {
69+
ListQueryBuilder<P, P> b = new ListQueryBuilder<>(emf, this, type);
7070
b.setSelect(selectClause);
7171
return b;
7272
}
73+
74+
/**
75+
* Get an element by ID.
76+
*
77+
* @param id the ID to fetch.
78+
* @return the element with the given ID or null, if there was no such thing
79+
*/
80+
public SingleQueryBuilder<P, P> select(final Long id) {
81+
return new SingleQueryBuilder<>(this, id);
82+
}
7383
}

src/main/java/info/unterrainer/commons/httpserver/daos/Dml.java renamed to src/main/java/info/unterrainer/commons/httpserver/daos/ListQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import lombok.RequiredArgsConstructor;
1818

1919
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
20-
public class Dml<P extends BasicJpa, T> {
20+
public class ListQuery<P extends BasicJpa, T> {
2121

2222
protected final EntityManagerFactory emf;
2323
protected final QueryInterface<P, T> builder;

0 commit comments

Comments
 (0)