Search in sources :

Example 1 with SQLRowStream

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();
                            }
                        });
                    });
                });
            });
        });
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) OpenOptions(io.vertx.core.file.OpenOptions) SQLRowStream(io.vertx.ext.sql.SQLRowStream) RoutingContext(io.vertx.ext.web.RoutingContext) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TestUtil(io.vertx.openshift.jdbc.TestUtil) List(java.util.List) JDBCClient(io.vertx.ext.jdbc.JDBCClient) FileSystem(io.vertx.core.file.FileSystem) SQLConnection(io.vertx.ext.sql.SQLConnection) RecordParser(io.vertx.core.parsetools.RecordParser) Handler(io.vertx.core.Handler) SQLConnection(io.vertx.ext.sql.SQLConnection) FileSystem(io.vertx.core.file.FileSystem) ArrayList(java.util.ArrayList) RecordParser(io.vertx.core.parsetools.RecordParser) AsyncFile(io.vertx.core.file.AsyncFile) SQLRowStream(io.vertx.ext.sql.SQLRowStream)

Example 2 with SQLRowStream

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());
                                }
                            });
                        });
                    }
                });
            });
        });
    });
}
Also used : JDBCClient(io.vertx.ext.jdbc.JDBCClient) SQLRowStream(io.vertx.ext.sql.SQLRowStream) AbstractVerticle(io.vertx.core.AbstractVerticle) SQLConnection(io.vertx.ext.sql.SQLConnection) JsonObject(io.vertx.core.json.JsonObject) Runner(io.vertx.example.util.Runner) SQLConnection(io.vertx.ext.sql.SQLConnection) JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject) SQLRowStream(io.vertx.ext.sql.SQLRowStream)

Aggregations

JDBCClient (io.vertx.ext.jdbc.JDBCClient)2 SQLConnection (io.vertx.ext.sql.SQLConnection)2 SQLRowStream (io.vertx.ext.sql.SQLRowStream)2 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Handler (io.vertx.core.Handler)1 AsyncFile (io.vertx.core.file.AsyncFile)1 FileSystem (io.vertx.core.file.FileSystem)1 OpenOptions (io.vertx.core.file.OpenOptions)1 JsonObject (io.vertx.core.json.JsonObject)1 RecordParser (io.vertx.core.parsetools.RecordParser)1 Runner (io.vertx.example.util.Runner)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 TestUtil (io.vertx.openshift.jdbc.TestUtil)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1