Skip to content

Commit 2f88e48

Browse files
author
Gerald Unterrainer
committed
refactoring
update README.md
1 parent 9d727df commit 2f88e48

File tree

3 files changed

+59
-49
lines changed

3 files changed

+59
-49
lines changed

README.md

+58-14
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ configuration = MyProgramConfiguration.read();
2929
EntityManagerFactory emf =
3030
dbUtils.createAutoclosingEntityManagerFactory(MyProgram.class, "my-server");
3131

32-
// Create a JpqlTransactionManager which will be used to maintain transactions
33-
// throughout the server.
34-
JpqlTransactionManager jpqlTransactionManager = new JpqlTransactionManager(emf);
35-
3632
// Create the server.
3733
HttpServer server = HttpServer.builder()
3834
.applicationName("my-rest-server")
@@ -115,9 +111,8 @@ And lastly, update the server-code so that we expose the endpoint.
115111
// Last line here is the creation of the server ending with ".build()"
116112

117113
// Register a custom handler for the resource 'user'.
118-
server.handlerGroupFor(UserJpa.class, UserJson.class, jpqlTransactionManager)
114+
server.handlerGroupFor(UserJpa.class, UserJson.class, new JpqlDao<UserJpa>(emf, UserJpa.class))
119115
.path("users")
120-
.dao(new JpqlDao<UserJpa>(emf, UserJpa.class))
121116
.endpoints(Endpoint.ALL)
122117
.addRoleFor(Endpoint.ALL, RoleBuilder.open())
123118
.getListInterceptor()
@@ -182,9 +177,8 @@ If you have long-running operations, you better choose an async-extension point.
182177
#### Example
183178

184179
```java
185-
server.handlerGroupFor(SomeSingletonJpa.class, SomeSingletonJson.class, jpqlTransactionManager)
180+
server.handlerGroupFor(SomeSingletonJpa.class, SomeSingletonJson.class, new JpqlAsyncDao<SomeSingletonJpa>(emf, SomeSingletonJpa.class))
186181
.path("cmd/somesingleton")
187-
.dao(new JpqlAsyncDao<SomeSingletonJpa>(emf, SomeSingletonJpa.class))
188182
.endpoints(Endpoint.ALL)
189183
.addRoleFor(Endpoint.ALL, RoleBuilder.open())
190184
.extension()
@@ -212,9 +206,8 @@ Run in their own context, detached from the request-response-process and therefo
212206
#### Example
213207

214208
```java
215-
server.handlerGroupFor(SubscriptionJpa.class, SubscriptionJson.class, jpqlTransactionManager)
209+
server.handlerGroupFor(SubscriptionJpa.class, SubscriptionJson.class, subscriptionDao)
216210
.path("/subscriptions")
217-
.dao(subscriptionDao)
218211
.endpoints(Endpoint.ALL)
219212
.addRoleFor(Endpoint.ALL, RoleBuilder.open())
220213
.extension()
@@ -253,9 +246,8 @@ Be cautious when using those and be sure to have the right indexes on your datab
253246
##### Example 1
254247

255248
```java
256-
server.handlerGroupFor(SubscriptionJpa.class, SubscriptionJson.class, jpqlTransactionManager)
249+
server.handlerGroupFor(SubscriptionJpa.class, SubscriptionJson.class, subscriptionDao)
257250
.path("/subscriptions")
258-
.dao(subscriptionDao)
259251
.endpoints(Endpoint.ALL)
260252
.addRoleFor(Endpoint.ALL, RoleBuilder.open())
261253
.getListInterceptor()
@@ -353,9 +345,8 @@ public class InterceptorData {
353345
##### Example
354346

355347
```java
356-
server.handlerGroupFor(SubscriptionJpa.class, SubscriptionJson.class, jpqlTransactionManager)
348+
server.handlerGroupFor(SubscriptionJpa.class, SubscriptionJson.class, subscriptionDao)
357349
.path("/subscriptions")
358-
.dao(subscriptionDao)
359350
.endpoints(Endpoint.ALL)
360351
.addRoleFor(Endpoint.ALL, RoleBuilder.open())
361352
.getListInterceptor(subscriptionInterceptor::select)
@@ -375,5 +366,58 @@ public InterceptorData select(final Context ctx, final HandlerUtils hu) {
375366
...
376367
```
377368

369+
#### Business-Logic SQL Queries
370+
371+
In order to keep SQL-queries somewhat consistent and because of my deep aversion of Criteria queries, I've used the following 'query language' you can get calling every `JpqlDao<JpaType>`.
372+
373+
##### Examples
374+
375+
```java
376+
// Insert...
377+
userDao.insert(entity).execute();
378+
userDao.insert(entity).entityManager(em).execute();
379+
380+
// Full-Update...
381+
userDao.update(entity).execute();
382+
userDao.update(entity).entityManager(em).execute();
383+
384+
// Single-Result Query...
385+
SingleQueryBuilder<NexusUserJpa, NexusUserJpa> single = userDao.select(32L);
386+
single.delete();
387+
NexusUserJpa en = single.get();
388+
389+
// List-Result Query...
390+
JpaListQuery<NexusUserJpa> query;
391+
query = userDao.select().build();
392+
query = userDao.select().entityManager(em).build();
393+
query = userDao.select().where("o.id = :id").addParam("id", 32L).build();
394+
query = userDao.select()
395+
.where("o.priority > :priority AND o.enabled = :enabled AND userId IS NOT NULL")
396+
.addParam("priority", 10L)
397+
.addParam("enabled", true)
398+
.desc("o.priority")
399+
.lockPessimistic()
400+
.build();
401+
query = userDao.select("o")
402+
.join("LEFT JOIN GroupJpa g ON g.id = o.groupId")
403+
.where("g.enabled = :enabled")
404+
.addParam("enabled", true)
405+
.build();
378406
407+
// Delete all list-results.
408+
query.delete();
409+
410+
// Upsert first element of list-results.
411+
query.upsert(entity);
412+
413+
// Various ways to get some or all entities from the list-results.
414+
NexusUserJpa a = query.getFirst();
415+
List<NexusUserJpa> b = query.getList();
416+
List<NexusUserJpa> c = query.getList(0, 10);
417+
ListJson<NexusUserJpa> d = query.getListJson();
418+
ListJson<NexusUserJpa> e = query.getListJson(10, 10);
419+
List<NexusUserJpa> f = query.getListReversed();
420+
List<NexusUserJpa> g = query.getN(22);
421+
NexusUserJpa h = query.getSingle();
422+
```
379423

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<modelVersion>4.0.0</modelVersion>
1919
<artifactId>http-server</artifactId>
20-
<version>0.2.10</version>
20+
<version>0.2.11</version>
2121
<name>HttpServer</name>
2222
<packaging>jar</packaging>
2323

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

-34
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,6 @@ public R where(final String whereClause) {
5050
return (R) this;
5151
}
5252

53-
/**
54-
* Adds an 'AND' part to the where-clause.
55-
* <p>
56-
* For example: .and("o.loggedIn = :loggedIn");
57-
*
58-
* @param andWhereClause the clause to add
59-
* @return an instance of this builder to provide a fluent interface
60-
*/
61-
@SuppressWarnings("unchecked")
62-
public R and(final String andWhereClause) {
63-
if (whereClause == null || whereClause.isBlank())
64-
whereClause = andWhereClause;
65-
else
66-
whereClause += " AND " + andWhereClause;
67-
return (R) this;
68-
}
69-
70-
/**
71-
* Adds an 'OR' part to the where-clause.
72-
* <p>
73-
* For example: .or("o.loggedIn = :loggedIn");
74-
*
75-
* @param orWhereClause the clause to add
76-
* @return an instance of this builder to provide a fluent interface
77-
*/
78-
@SuppressWarnings("unchecked")
79-
public R or(final String orWhereClause) {
80-
if (whereClause == null || whereClause.isBlank())
81-
whereClause = orWhereClause;
82-
else
83-
whereClause += " OR " + orWhereClause;
84-
return (R) this;
85-
}
86-
8753
/**
8854
* Clears the parameters and resets them to default.
8955
* <p>

0 commit comments

Comments
 (0)