forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlClientExample.java
89 lines (81 loc) · 2.94 KB
/
SqlClientExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package io.vertx.example.sqlclient.streaming;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.sqlclient.*;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.PostgreSQLContainer;
/*
* @author <a href="mailto:[email protected]">Paulo Lopes</a>
*/
public class SqlClientExample extends AbstractVerticle {
// Convenience method so you can run it in your IDE
public static void main(String[] args) {
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>();
postgres.start();
PgConnectOptions options = new PgConnectOptions()
.setPort(postgres.getMappedPort(5432))
.setHost(postgres.getContainerIpAddress())
.setDatabase(postgres.getDatabaseName())
.setUser(postgres.getUsername())
.setPassword(postgres.getPassword());
// Uncomment for MySQL
// MySQLContainer<?> mysql = new MySQLContainer<>();
// mysql.start();
// MySQLConnectOptions options = new MySQLConnectOptions()
// .setPort(mysql.getMappedPort(3306))
// .setHost(mysql.getContainerIpAddress())
// .setDatabase(mysql.getDatabaseName())
// .setUser(mysql.getUsername())
// .setPassword(mysql.getPassword());
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new SqlClientExample(options)); }
private final SqlConnectOptions options;
public SqlClientExample(SqlConnectOptions options) {
this.options = options;
}
@Override
public void start() {
Pool pool = Pool.pool(vertx, options, new PoolOptions().setMaxSize(4));
pool.getConnection().compose(connection -> {
Promise<Void> promise = Promise.promise();
// create a test table
connection.query("create table test(id int primary key, name varchar(255))").execute()
.compose(v -> {
// insert some test data
return connection.query("insert into test values (1, 'Hello'), (2, 'World')").execute();
})
.compose(v -> {
// prepare the query
return connection.prepare("select * from test");
})
.map(preparedStatement -> {
// create a stream
return preparedStatement.createStream(50, Tuple.tuple());
})
.onComplete(ar -> {
if (ar.succeeded()) {
RowStream<Row> stream = ar.result();
stream
.exceptionHandler(promise::fail)
.endHandler(promise::complete)
.handler(row -> System.out.println("row = " + row.toJson()));
} else {
promise.fail(ar.cause());
}
});
return promise.future().onComplete(v -> {
// close the connection
connection.close();
});
}).onComplete(ar -> {
if (ar.succeeded()) {
System.out.println("done");
} else {
ar.cause().printStackTrace();
}
});
}
}