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:

  1. String query = sqlQueries.get(SqlQuery.GET_PAGE_BY_ID);
  2. JsonArray params = new JsonArray().add(id);
  3. 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:

  1. SQLClientHelper.usingConnectionSingle(this.dbClient, conn -> conn (1)
  2. .rxExecute(sqlQueries.get(SqlQuery.CREATE_PAGES_TABLE)) (2)
  3. .andThen(Single.just(this)))
  1. borrows a connection from the pool and provides it in a callback.

  2. 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.