@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(); }); } }
public static JDBCClient newInstance(io.vertx.ext.jdbc.JDBCClient arg) { return arg != null ? new JDBCClient(arg) : null; } }
public void handle(AsyncResult<io.vertx.ext.jdbc.JDBCClient> ar) { if (ar.succeeded()) { resultHandler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.jdbc.JDBCClient.newInstance(ar.result()))); } else { resultHandler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } });
public CartEventDataSourceImpl(io.vertx.core.Vertx vertx, JsonObject json) { this.client = JDBCClient.createNonShared(Vertx.newInstance(vertx), json); // TODO: Should not init the table here. client.rxGetConnection() .flatMap(connection -> connection.rxExecute(INIT_STATEMENT) .doAfterTerminate(connection::close) ) .subscribe(); }
/** * Execute a one shot SQL statement that returns a single SQL row. This method will reduce the boilerplate code by * getting a connection from the pool (this object) and return it back after the execution. Only the first result * from the result set is returned. * @param sql the statement to execute * @return self */ public Single<JsonArray> rxQuerySingle(String sql) { return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> { querySingle(sql, fut); })); }
/** * Execute a one shot SQL statement with arguments that returns a single SQL row. This method will reduce the * boilerplate code by getting a connection from the pool (this object) and return it back after the execution. * Only the first result from the result set is returned. * @param sql the statement to execute * @param arguments the arguments * @return self */ public Single<JsonArray> rxQuerySingleWithParams(String sql, JsonArray arguments) { return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> { querySingleWithParams(sql, arguments, fut); })); }
/** * Create a JDBC auth provider implementation * @param vertx * @param client the JDBC client instance * @return the auth provider */ public static io.vertx.rxjava.ext.auth.jdbc.JDBCAuth create(io.vertx.rxjava.core.Vertx vertx, io.vertx.rxjava.ext.jdbc.JDBCClient client) { io.vertx.rxjava.ext.auth.jdbc.JDBCAuth ret = io.vertx.rxjava.ext.auth.jdbc.JDBCAuth.newInstance(io.vertx.ext.auth.jdbc.JDBCAuth.create(vertx.getDelegate(), client.getDelegate())); return ret; }
@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() // Connect to the database .flatMapObservable(conn -> { // With the connection... return conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))") // ...create test table .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")) // ...insert a row .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")) // ...another one .flatMap(result -> conn.rxQueryStream("SELECT * FROM test")) // ...get values stream .flatMapObservable(sqlRowStream -> { return sqlRowStream.toObservable() // Transform the stream into an Observable... .doOnTerminate(conn::close); // ...and close the connection when the stream is fully read or an error occurs }); }).subscribe(row -> System.out.println("Row : " + row.encode())); } }
public void handle(AsyncResult<io.vertx.ext.jdbc.JDBCClient> ar) { if (ar.succeeded()) { resultHandler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.jdbc.JDBCClient.newInstance(ar.result()))); } else { resultHandler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } });
public static JDBCClient newInstance(io.vertx.ext.jdbc.JDBCClient arg) { return arg != null ? new JDBCClient(arg) : null; } }
/** * Execute a one shot SQL statement that returns a single SQL row. This method will reduce the boilerplate code by * getting a connection from the pool (this object) and return it back after the execution. Only the first result * from the result set is returned. * @param sql the statement to execute * @return self */ public Single<JsonArray> rxQuerySingle(String sql) { return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> { querySingle(sql, fut); })); }
/** * Execute a one shot SQL statement with arguments that returns a single SQL row. This method will reduce the * boilerplate code by getting a connection from the pool (this object) and return it back after the execution. * Only the first result from the result set is returned. * @param sql the statement to execute * @param arguments the arguments * @return self */ public Single<JsonArray> rxQuerySingleWithParams(String sql, JsonArray arguments) { return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> { querySingleWithParams(sql, arguments, fut); })); }
"datetime TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL)"; JDBCClient client = JDBCClient.createShared(vertx, config); .rxGetConnection() .flatMap(conn -> conn
public void handle(AsyncResult<io.vertx.ext.jdbc.JDBCClient> ar) { if (ar.succeeded()) { resultHandler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.jdbc.JDBCClient.newInstance(ar.result()))); } else { resultHandler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } });
/** * Create a JDBC client which shares its data source with any other JDBC clients created with the same * data source name * @param vertx the Vert.x instance * @param config the configuration * @param dataSourceName the data source name * @return the client */ public static io.vertx.rxjava.ext.jdbc.JDBCClient createShared(io.vertx.rxjava.core.Vertx vertx, JsonObject config, String dataSourceName) { io.vertx.rxjava.ext.jdbc.JDBCClient ret = io.vertx.rxjava.ext.jdbc.JDBCClient.newInstance(io.vertx.ext.jdbc.JDBCClient.createShared(vertx.getDelegate(), config, dataSourceName)); return ret; }
/** * Like {@link io.vertx.rxjava.ext.jdbc.JDBCClient#createShared} but with the default data source name * @param vertx the Vert.x instance * @param config the configuration * @return the client */ public static io.vertx.rxjava.ext.jdbc.JDBCClient createShared(io.vertx.rxjava.core.Vertx vertx, JsonObject config) { io.vertx.rxjava.ext.jdbc.JDBCClient ret = io.vertx.rxjava.ext.jdbc.JDBCClient.newInstance(io.vertx.ext.jdbc.JDBCClient.createShared(vertx.getDelegate(), config)); return ret; }
/** * Create a JDBC client which maintains its own data source. * @param vertx the Vert.x instance * @param config the configuration * @return the client */ public static io.vertx.rxjava.ext.jdbc.JDBCClient createNonShared(io.vertx.rxjava.core.Vertx vertx, JsonObject config) { io.vertx.rxjava.ext.jdbc.JDBCClient ret = io.vertx.rxjava.ext.jdbc.JDBCClient.newInstance(io.vertx.ext.jdbc.JDBCClient.createNonShared(vertx.getDelegate(), config)); return ret; }
/** * Like {@link io.vertx.rxjava.ext.jdbc.JDBCClient#createShared} but with the default data source name * @param vertx the Vert.x instance * @param config the configuration * @return the client */ public static io.vertx.rxjava.ext.jdbc.JDBCClient createShared(io.vertx.rxjava.core.Vertx vertx, JsonObject config) { io.vertx.rxjava.ext.jdbc.JDBCClient ret = io.vertx.rxjava.ext.jdbc.JDBCClient.newInstance(io.vertx.ext.jdbc.JDBCClient.createShared(vertx.getDelegate(), config)); return ret; }
/** * Create a JDBC client which maintains its own data source. * @param vertx the Vert.x instance * @param config the configuration * @return the client */ public static io.vertx.rxjava.ext.jdbc.JDBCClient createNonShared(io.vertx.rxjava.core.Vertx vertx, JsonObject config) { io.vertx.rxjava.ext.jdbc.JDBCClient ret = io.vertx.rxjava.ext.jdbc.JDBCClient.newInstance(io.vertx.ext.jdbc.JDBCClient.createNonShared(vertx.getDelegate(), config)); return ret; }
/** * Create a JDBC client which shares its data source with any other JDBC clients created with the same * data source name * @param vertx the Vert.x instance * @param config the configuration * @param dataSourceName the data source name * @return the client */ public static io.vertx.rxjava.ext.jdbc.JDBCClient createShared(io.vertx.rxjava.core.Vertx vertx, JsonObject config, String dataSourceName) { io.vertx.rxjava.ext.jdbc.JDBCClient ret = io.vertx.rxjava.ext.jdbc.JDBCClient.newInstance(io.vertx.ext.jdbc.JDBCClient.createShared(vertx.getDelegate(), config, dataSourceName)); return ret; }