use of io.vertx.ext.sql.SQLRowStream in project vertx-openshift-it by cescoffier.
the class StreamingResultsTest 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();
});
FileSystem fileSystem = rc.vertx().fileSystem();
fileSystem.open("db/migration/strings", new OpenOptions(), ores -> {
if (ores.failed()) {
fail(rc, ores.cause());
return;
}
AsyncFile asyncFile = ores.result();
List<String> values = new ArrayList<>();
RecordParser parser = RecordParser.newDelimited("\n", out -> values.add(out.toString()));
asyncFile.handler(parser);
asyncFile.endHandler(v -> {
asyncFile.close();
List<String> statements = values.stream().map(value -> "insert into random_string (value) values ('" + value + "')").collect(toList());
connection.batch(statements, ires -> {
if (ires.failed()) {
fail(rc, ires.cause());
return;
}
connection.queryStream("select value from random_string", sres -> {
if (sres.failed()) {
fail(rc, sres.cause());
return;
}
List<String> storedValues = new ArrayList<>(values.size());
SQLRowStream rowStream = sres.result();
rowStream.handler(row -> {
storedValues.add(row.getString(0));
}).endHandler(endRows -> {
if (!storedValues.equals(values)) {
fail(rc, storedValues.toString());
} else {
rc.response().setStatusCode(200).end();
}
});
});
});
});
});
});
}
use of io.vertx.ext.sql.SQLRowStream in project vertx-examples by vert-x3.
the class JDBCExample method start.
@Override
public void start() throws Exception {
final JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver").put("max_pool_size", 30).put("user", "SA").put("password", ""));
client.getConnection(conn -> {
if (conn.failed()) {
System.err.println(conn.cause().getMessage());
return;
}
final SQLConnection connection = conn.result();
connection.execute("create table test(id int primary key, name varchar(255))", res -> {
if (res.failed()) {
throw new RuntimeException(res.cause());
}
// insert some test data
connection.execute("insert into test values (1, 'Hello'), (2, 'Goodbye'), (3, 'Cya Later')", insert -> {
// query some data
connection.queryStream("select * from test", stream -> {
if (stream.succeeded()) {
SQLRowStream sqlRowStream = stream.result();
sqlRowStream.handler(row -> {
// do something with the row...
System.out.println(row.encode());
}).endHandler(v -> {
// no more data available, close the connection
connection.close(done -> {
if (done.failed()) {
throw new RuntimeException(done.cause());
}
});
});
}
});
});
});
});
}
Aggregations