Merge pull request #2073 from hgschmie/examples · jdbi/jdbi@453d072 (original) (raw)
``
1
`+
/*
`
``
2
`+
- Licensed under the Apache License, Version 2.0 (the "License");
`
``
3
`+
- you may not use this file except in compliance with the License.
`
``
4
`+
- You may obtain a copy of the License at
`
``
5
`+
`
``
6
`+
`
``
7
`+
`
``
8
`+
- Unless required by applicable law or agreed to in writing, software
`
``
9
`+
- distributed under the License is distributed on an "AS IS" BASIS,
`
``
10
`+
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
`
``
11
`+
- See the License for the specific language governing permissions and
`
``
12
`+
- limitations under the License.
`
``
13
`+
*/
`
``
14
`+
package org.jdbi.v3.examples.order;
`
``
15
+
``
16
`+
import java.security.SecureRandom;
`
``
17
`+
import java.util.UUID;
`
``
18
`+
import java.util.function.Consumer;
`
``
19
+
``
20
`+
import de.softwareforge.testing.postgres.embedded.DatabaseInfo;
`
``
21
`+
import de.softwareforge.testing.postgres.embedded.DatabaseManager;
`
``
22
`+
import de.softwareforge.testing.postgres.embedded.EmbeddedPostgres;
`
``
23
`+
import org.jdbi.v3.core.Jdbi;
`
``
24
+
``
25
`+
public final class OrderSupport {
`
``
26
`+
private static final SecureRandom RANDOM = new SecureRandom();
`
``
27
+
``
28
`+
private OrderSupport() {
`
``
29
`+
throw new AssertionError("OrderSupport can not be instantiated");
`
``
30
`+
}
`
``
31
+
``
32
`+
public static void withOrders(Consumer jdbiConsumer) throws Exception {
`
``
33
`+
try (DatabaseManager manager = DatabaseManager.singleDatabase()
`
``
34
`+
// same as EmbeddedPostgres.defaultInstance()
`
``
35
`+
.withInstancePreparer(EmbeddedPostgres.Builder::withDefaults)
`
``
36
`+
.build()
`
``
37
`+
.start()) {
`
``
38
`+
DatabaseInfo databaseInfo = manager.getDatabaseInfo();
`
``
39
`+
Jdbi jdbi = Jdbi.create(databaseInfo.asDataSource());
`
``
40
+
``
41
`+
createTables(jdbi);
`
``
42
`+
populateOrders(jdbi, 3, 20);
`
``
43
+
``
44
`+
jdbi.registerRowMapper(new OrderMapper());
`
``
45
+
``
46
`+
jdbiConsumer.accept(jdbi);
`
``
47
`+
}
`
``
48
`+
}
`
``
49
+
``
50
`+
public static void createTables(Jdbi jdbi) {
`
``
51
`+
jdbi.withHandle(
`
``
52
`+
handle -> handle.execute("CREATE TABLE orders (id INT, user_id INT, comment VARCHAR, address VARCHAR)"));
`
``
53
`+
}
`
``
54
+
``
55
`+
public static void populateOrders(Jdbi jdbi, int orderCount, int userIdCount) {
`
``
56
+
``
57
`+
jdbi.withHandle(
`
``
58
`+
handle -> {
`
``
59
`+
for (int j = 0; j < userIdCount; j++) {
`
``
60
`+
int userId = RANDOM.nextInt(10_000);
`
``
61
`+
for (int i = 0; i < orderCount; i++) {
`
``
62
`+
handle.createUpdate("INSERT INTO orders (id, user_id, comment, address) VALUES (:id, :user_id, :comment, :address)")
`
``
63
`+
.bind("id", RANDOM.nextInt(1_000_000))
`
``
64
`+
.bind("user_id", userId)
`
``
65
`+
.bind("comment", UUID.randomUUID().toString().substring(0, 5))
`
``
66
`+
.bind("address", UUID.randomUUID().toString().substring(0, 5))
`
``
67
`+
.execute();
`
``
68
`+
}
`
``
69
`+
}
`
``
70
`+
return null;
`
``
71
`+
});
`
``
72
`+
}
`
``
73
`+
}
`