Querying the database
Direct queries
Very often, a single database query is needed to prepare a response to the user. For such simple cases, the JDBCClient
provides rxQueryXXX
and rxUpdateXXX
methods:
String query = sqlQueries.get(SqlQuery.GET_PAGE_BY_ID);
JsonArray params = new JsonArray().add(id);
Single<ResultSet> resultSet = dbClient.rxQueryWithParams(query, params);
Working with a database connection
When direct queries do not fit (e.g. when creating then querying a temporary table), you can generate a reactive flow from a single SQLConnection
:
SQLClientHelper.usingConnectionSingle(this.dbClient, conn -> conn (1)
.rxExecute(sqlQueries.get(SqlQuery.CREATE_PAGES_TABLE)) (2)
.andThen(Single.just(this)))
borrows a connection from the pool and provides it in a callback.
returns a
Single
generated from execution of queries and transformation of results.
You may execute any number of queries and transform results with RxJava operators.
Note | io.vertx.reactivex.ext.sql.SQLClientHelper can also generate Flowable , Observable , Maybe and Completable flows. |