Search in sources :

Example 1 with JDBCClient

use of io.vertx.reactivex.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.

the class Streaming 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);
    jdbc.rxGetConnection().flatMapPublisher(conn -> {
        // With the connection...
        return // ...create test table
        conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))").flatMap(// ...insert a row
        result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")).flatMap(// ...another one
        result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")).flatMap(// ...get values stream
        result -> conn.rxQueryStream("SELECT * FROM test")).flatMapPublisher(sqlRowStream -> {
            return // Transform the stream into a Flowable...
            sqlRowStream.toFlowable().doOnTerminate(() -> {
                // ...and close the connection when the stream is fully read or an
                // error occurs
                conn.close();
                System.out.println("Connection closed");
            });
        });
    }).subscribe(row -> System.out.println("Row : " + row.encode()));
}
Also used : JDBCClient(io.vertx.reactivex.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.reactivex.ext.jdbc.JDBCClient)

Example 2 with JDBCClient

use of io.vertx.reactivex.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.

the class Transaction 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");
    String sql = "CREATE TABLE colors (" + "id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY, " + "name VARCHAR(255), " + "datetime TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL)";
    JDBCClient client = JDBCClient.createShared(vertx, config);
    // Connect to the database
    client.rxGetConnection().flatMap(conn -> conn.rxSetAutoCommit(false).toSingleDefault(false).flatMap(autoCommit -> conn.rxExecute(sql).toSingleDefault(true)).flatMap(executed -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("BLACK"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("WHITE"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("PURPLE"))).flatMap(updateResult -> conn.rxCommit().toSingleDefault(true).map(commit -> updateResult)).onErrorResumeNext(ex -> conn.rxRollback().toSingleDefault(true).onErrorResumeNext(ex2 -> Single.error(new CompositeException(ex, ex2))).flatMap(ignore -> Single.error(ex))).flatMap(updateResult -> conn.rxQuery("SELECT * FROM colors")).doAfterTerminate(conn::close)).subscribe(resultSet -> {
        // Subscribe to get the final result
        System.out.println("Results : " + resultSet.getRows());
    }, Throwable::printStackTrace);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) CompositeException(io.reactivex.exceptions.CompositeException) JDBCClient(io.vertx.reactivex.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Single(io.reactivex.Single) Runner(io.vertx.example.util.Runner) JsonArray(io.vertx.core.json.JsonArray) CompositeException(io.reactivex.exceptions.CompositeException) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.reactivex.ext.jdbc.JDBCClient)

Example 3 with JDBCClient

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

Aggregations

JsonObject (io.vertx.core.json.JsonObject)3 Runner (io.vertx.example.util.Runner)3 AbstractVerticle (io.vertx.reactivex.core.AbstractVerticle)3 JDBCClient (io.vertx.reactivex.ext.jdbc.JDBCClient)3 Single (io.reactivex.Single)2 CompositeException (io.reactivex.exceptions.CompositeException)1 JsonArray (io.vertx.core.json.JsonArray)1 ResultSet (io.vertx.ext.sql.ResultSet)1