2
2
3
3
import java .util .Collections ;
4
4
import java .util .List ;
5
+ import java .util .function .Function ;
5
6
7
+ import javax .persistence .EntityManager ;
8
+ import javax .persistence .EntityManagerFactory ;
6
9
import javax .persistence .NoResultException ;
10
+ import javax .persistence .NonUniqueResultException ;
7
11
import javax .persistence .TypedQuery ;
8
12
9
13
import info .unterrainer .commons .httpserver .jsons .ListJson ;
14
+ import info .unterrainer .commons .rdbutils .Transactions ;
10
15
import info .unterrainer .commons .rdbutils .entities .BasicJpa ;
11
16
import lombok .AccessLevel ;
12
- import lombok .Getter ;
13
17
import lombok .RequiredArgsConstructor ;
14
18
15
19
@ RequiredArgsConstructor (access = AccessLevel .PACKAGE )
16
20
public class Query <P extends BasicJpa , T > {
17
21
18
- protected final JpqlDao <P > dao ;
19
- @ Getter
20
- protected final TypedQuery <T > typedQuery ;
21
- @ Getter
22
- protected final javax .persistence .Query countQuery ;
22
+ protected final EntityManagerFactory emf ;
23
+ protected final QueryBuilder <P , T > builder ;
24
+
25
+ private <V > V wrap (final Function <EntityManager , V > func ) {
26
+ if (builder .entityManager == null )
27
+ return Transactions .withNewTransactionReturning (emf , em -> func .apply (em ));
28
+ return func .apply (builder .entityManager );
29
+ }
23
30
24
31
/**
25
- * Gets all the rows specified by offset and size as a list.
32
+ * Gets all the rows as a {@link ListJson} as returned by a web-service.
33
+ * <p>
34
+ * The list contains additional information about the data.
35
+ *
36
+ * @return a {@link ListJson} containing the rows as specified
37
+ */
38
+ public ListJson <T > getListJson () {
39
+ return wrap (em -> {
40
+ ListJson <T > r = new ListJson <>();
41
+ r .setEntries (getList (em ));
42
+ r .setCount ((Long ) builder .getCountQuery (em ).getSingleResult ());
43
+ return r ;
44
+ });
45
+ }
46
+
47
+ /**
48
+ * Gets all the rows specified by offset and size as a {@link ListJson} as
49
+ * returned by a web-service.
50
+ * <p>
51
+ * The list contains additional information about the data.
26
52
*
27
53
* @param offset the row-number to start at
28
54
* @param size the number-of-rows to return
29
55
* @return a {@link ListJson} containing the rows as specified
30
56
*/
31
- public ListJson <T > getList (final long offset , final long size ) {
57
+ public ListJson <T > getListJson (final long offset , final long size ) {
32
58
ListJson <T > r = new ListJson <>();
33
- int s = Integer .MAX_VALUE ;
34
- if (size < s )
35
- s = (int ) size ;
36
- int o = Integer .MAX_VALUE ;
37
- if (offset < o )
38
- o = (int ) offset ;
39
- List <T > qResult = typedQuery .getResultList ();
40
- Long cqResult = (Long ) countQuery .getSingleResult ();
41
- r .setEntries (qResult );
42
- r .setCount (cqResult );
59
+ r .setEntries (getList (offset , size ));
60
+ r .setCount ((Long ) countQuery .getSingleResult ());
43
61
return r ;
44
62
}
45
63
@@ -49,7 +67,7 @@ public ListJson<T> getList(final long offset, final long size) {
49
67
* @return the result-row as a JPA
50
68
*/
51
69
public T getFirst () {
52
- List <T > r = typedQuery . setMaxResults ( 1 ). getResultList ( );
70
+ List <T > r = getList ( 0 , 1 );
53
71
if (r .size () == 1 ) {
54
72
T jpa = r .get (0 );
55
73
return jpa ;
@@ -59,16 +77,12 @@ public T getFirst() {
59
77
60
78
/**
61
79
* Get the first N rows from this queries' results.
62
- *
80
+ *
63
81
* @param count the number of rows to get
64
82
* @return the list of the first N result-rows as JPAs
65
83
*/
66
84
public List <T > getN (final long count ) {
67
- int s = Integer .MAX_VALUE ;
68
- if (count < s )
69
- s = (int ) count ;
70
- typedQuery .setMaxResults (s );
71
- return typedQuery .getResultList ();
85
+ return getList (0 , count );
72
86
}
73
87
74
88
/**
@@ -89,12 +103,44 @@ public T getSingle() {
89
103
* @return the list of result-rows as JPAs
90
104
*/
91
105
public List <T > getList () {
92
- return typedQuery .getResultList ();
106
+ return wrap (em -> getList (em ));
107
+ }
108
+
109
+ private List <T > getList (final EntityManager em ) {
110
+ return getList (0 , Long .MAX_VALUE );
111
+ }
112
+
113
+ /**
114
+ * Gets all the rows specified by offset and size as a list.
115
+ *
116
+ * @param offset the row-number to start at
117
+ * @param size the number-of-rows to return
118
+ * @return a list containing the rows as specified
119
+ */
120
+ public List <T > getList (final long offset , final long size ) {
121
+ return wrap (em -> getList (em , offset , size ));
122
+ }
123
+
124
+ private List <T > getList (final EntityManager em , final long offset , final long size ) {
125
+ int s = Integer .MAX_VALUE ;
126
+ if (size < s )
127
+ s = (int ) size ;
128
+ int o = Integer .MAX_VALUE ;
129
+ if (offset < o )
130
+ o = (int ) offset ;
131
+ TypedQuery <T > query = builder .getTypedQuery (em );
132
+ query .setFirstResult (o );
133
+ query .setMaxResults (s );
134
+ return query .getResultList ();
93
135
}
94
136
95
137
/**
96
138
* Execute a SELECT query that returns multiple result-rows and reverse the
97
139
* order of results.
140
+ * <p>
141
+ * The reversing-process happens after retrieval of the result, since the whole
142
+ * list is going to be transferred anyway.<br>
143
+ * The list is reversed using {@code Collections.reverse(list)};
98
144
*
99
145
* @return the reversed list of result-rows as JPAs
100
146
*/
0 commit comments