use of io.vertx.ext.sql.ResultSet in project vertx-examples by vert-x3.
the class Client method start.
@Override
@Suspendable
public void start() throws Exception {
JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
JDBCClient jdbc = JDBCClient.createShared(vertx, config);
try (SQLConnection conn = awaitResult(jdbc::getConnection)) {
// Create a table
Void v = awaitResult(h -> conn.execute("CREATE TABLE test(col VARCHAR(20))", h));
// Insert some stuff
for (int i = 0; i < 10; i++) {
int ii = i;
UpdateResult res = awaitResult(h -> conn.update("INSERT INTO test (col) VALUES ('val" + ii + "')", h));
System.out.println("Rows updated: " + res.getUpdated());
}
// Select the results
ResultSet res = awaitResult(h -> conn.query("SELECT * FROM test", h));
System.out.println("Selected " + res.getNumRows() + " results");
res.getResults().forEach(System.out::println);
}
}
use of io.vertx.ext.sql.ResultSet in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
JDBCClient jdbc = JDBCClient.createShared(vertx, config);
// Connect to the database
jdbc.rxGetConnection().flatMap(conn -> {
// Now chain some statements using flatmap composition
Single<ResultSet> resa = conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))").flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")).flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")).flatMap(result -> conn.rxQuery("SELECT * FROM test"));
return resa.doAfterTerminate(conn::close);
}).subscribe(resultSet -> {
// Subscribe to the final result
System.out.println("Results : " + resultSet.getRows());
}, err -> {
System.out.println("Database problem");
err.printStackTrace();
});
}
use of io.vertx.ext.sql.ResultSet in project vertx-openshift-it by cescoffier.
the class TextQueryTest method handle.
@Override
public void handle(RoutingContext rc) {
jdbcClient.getConnection(ar -> {
if (ar.failed()) {
fail(rc, ar.cause());
return;
}
SQLConnection connection = ar.result();
rc.response().bodyEndHandler(v -> {
connection.close();
});
connection.query("select 24/2 as test_column", res -> {
if (res.failed()) {
fail(rc, res.cause());
return;
}
ResultSet resultSet = res.result();
if (!resultSet.getColumnNames().equals(Collections.singletonList("test_column"))) {
fail(rc, String.join(",", resultSet.getColumnNames()));
} else if (resultSet.getResults().size() != 1) {
fail(rc, String.valueOf(resultSet.getResults().size()));
} else if (!resultSet.getResults().get(0).getInteger(0).equals(12)) {
fail(rc, String.valueOf(resultSet.getResults().get(0).getInteger(0)));
} else if (resultSet.getNumRows() != 1) {
fail(rc, String.valueOf(resultSet.getNumRows()));
} else {
rc.response().setStatusCode(200).end();
}
});
});
}
use of io.vertx.ext.sql.ResultSet in project vertx-openshift-it by cescoffier.
the class BinaryTest method handle.
@Override
public void handle(RoutingContext rc) {
jdbcClient.getConnection(ar -> {
if (ar.failed()) {
fail(rc, ar.cause());
return;
}
SQLConnection connection = ar.result();
rc.response().bodyEndHandler(v -> {
connection.close();
});
String imageName = "random image";
byte[] imageContent = new byte[1024];
new Random().nextBytes(imageContent);
String sqlInsert = "insert into image (name,content) values (?, decode(?, 'base64'))";
JsonArray paramsInsert = new JsonArray().add(imageName).add(imageContent);
connection.updateWithParams(sqlInsert, paramsInsert, ires -> {
if (ires.failed()) {
fail(rc, ires.cause());
return;
}
Long imageId = ires.result().getKeys().getLong(0);
String sqlSelect = "select name, content from image where id = ?";
connection.queryWithParams(sqlSelect, new JsonArray().add(imageId), sres -> {
if (sres.failed()) {
fail(rc, sres.cause());
return;
}
ResultSet resultSet = sres.result();
JsonArray row = resultSet.getResults().get(0);
if (!row.getString(0).equals(imageName)) {
fail(rc, String.valueOf(row.getString(0)));
} else if (!Arrays.equals(imageContent, row.getBinary(1))) {
fail(rc, "Binary content differs");
} else {
rc.response().setStatusCode(200).end();
}
});
});
});
}
use of io.vertx.ext.sql.ResultSet in project vertx-openshift-it by cescoffier.
the class SpecialDatatypesTest method handle.
@Override
public void handle(RoutingContext rc) {
jdbcClient.getConnection(ar -> {
if (ar.failed()) {
fail(rc, ar.cause());
return;
}
SQLConnection connection = ar.result();
rc.response().bodyEndHandler(v -> {
connection.close();
});
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zonedDateTime = ZonedDateTime.of(1980, 10, 16, 5, 28, 33, 0, zoneId);
Instant instant = zonedDateTime.toInstant();
String uuid = UUID.randomUUID().toString();
String sqlInsert = "insert into item (cid,name,created_on,created_at,created) values (?,?,?,?,?)";
JsonArray paramsInsert = new JsonArray().add(uuid).add("My special item").add(zonedDateTime.toLocalDate().toString()).add(zonedDateTime.toLocalTime().toString()).add(instant);
connection.updateWithParams(sqlInsert, paramsInsert, ires -> {
if (ires.failed()) {
fail(rc, ires.cause());
return;
}
String sqlSelect = "select cid,created_on,created_at,created from item where cid = ?";
connection.queryWithParams(sqlSelect, new JsonArray().add(uuid), sres -> {
if (sres.failed()) {
fail(rc, sres.cause());
return;
}
ResultSet resultSet = sres.result();
JsonArray row = resultSet.getResults().get(0);
if (!row.getString(0).equals(uuid)) {
fail(rc, String.valueOf(row.getString(0)));
} else if (!row.getString(1).equals("1980-10-16")) {
fail(rc, String.valueOf(row.getString(1)));
} else if (!row.getString(2).equals("05:28:33")) {
fail(rc, String.valueOf(row.getString(1)));
} else if (!row.getInstant(3).equals(instant)) {
fail(rc, String.valueOf(row.getInstant(3)));
} else {
rc.response().setStatusCode(200).end();
}
});
});
});
}
Aggregations