use of io.vertx.pgclient.PgConnection in project raml-module-builder by folio-org.
the class PostgresClientTest method testProcessQuery.
@Test
public void testProcessQuery() {
PostgresClient testClient = PostgresClient.testClient();
List<FacetField> facets = new ArrayList<FacetField>() {
{
add(new FacetField("jsonb->>'biz'"));
}
};
QueryHelper queryHelper = new QueryHelper("test_jsonb_pojo");
queryHelper.selectQuery = "SELECT id, foo, bar FROM test_jsonb_pojo LIMIT 30 OFFSET 1";
int total = 30;
PgConnection connection = new FakeSqlConnection(Future.succeededFuture(getMockTestJsonbPojoResultSet(total)), true);
testClient.processQuery(connection, queryHelper, total, "get", totaledResults -> testClient.processResults(totaledResults.set, totaledResults.estimatedTotal, DEFAULT_OFFSET, DEFAULT_LIMIT, TestJsonbPojo.class), reply -> {
List<TestJsonbPojo> results = reply.result().getResults();
assertTestJsonbPojoResults(results, total);
});
}
use of io.vertx.pgclient.PgConnection in project raml-module-builder by folio-org.
the class PostgresClientTest method testProcessQueryException.
@Test
public void testProcessQueryException() {
PostgresClient testClient = PostgresClient.testClient();
QueryHelper queryHelper = new QueryHelper("test_jsonb_pojo");
queryHelper.selectQuery = "SELECT foo";
PgConnection connection = null;
testClient.processQuery(connection, queryHelper, 30, "get", totaledResults -> testClient.processResults(totaledResults.set, totaledResults.estimatedTotal, DEFAULT_OFFSET, DEFAULT_LIMIT, TestJsonbPojo.class), reply -> {
assertThat(reply.failed(), is(true));
assertThat(reply.cause() instanceof NullPointerException, is(true));
});
}
use of io.vertx.pgclient.PgConnection in project raml-module-builder by folio-org.
the class PostgresClientTest method testProcessQueryFails.
@Test
public void testProcessQueryFails() {
PostgresClient testClient = PostgresClient.testClient();
QueryHelper queryHelper = new QueryHelper("test_jsonb_pojo");
queryHelper.selectQuery = "SELECT foo";
PgConnection connection = new FakeSqlConnection(Future.failedFuture("Bad query"), false);
testClient.processQuery(connection, queryHelper, 30, "get", totaledResults -> testClient.processResults(totaledResults.set, totaledResults.estimatedTotal, DEFAULT_OFFSET, DEFAULT_LIMIT, TestJsonbPojo.class), reply -> {
assertThat(reply.failed(), is(true));
assertThat(reply.cause().getMessage(), is("Bad query"));
});
}
use of io.vertx.pgclient.PgConnection in project raml-module-builder by folio-org.
the class PostgresClient method getSQLConnection.
/**
* Don't forget to close the connection!
*
* <p>Use closeAndHandleResult as replyHandler, for example:
*
* <pre>getSQLConnection(timeout, conn -> execute(conn, sql, params, closeAndHandleResult(conn, replyHandler)))</pre>
*
* <p>Or avoid this method and use the preferred {@link #withConn(int, Function)}.
*
* @see #withConn(Function)
* @see #withConnection(Function)
* @see #withTrans(Function)
* @see #withTransaction(Function)
*/
void getSQLConnection(int queryTimeout, Handler<AsyncResult<SQLConnection>> handler) {
getConnection(res -> {
if (res.failed()) {
handler.handle(Future.failedFuture(res.cause()));
return;
}
PgConnection pgConnection = res.result();
if (queryTimeout == 0) {
handler.handle(Future.succeededFuture(new SQLConnection(pgConnection, null, null)));
return;
}
long timerId = vertx.setTimer(queryTimeout, id -> pgConnection.cancelRequest(ar -> {
if (ar.succeeded()) {
log.warn(String.format("Cancelling request due to timeout after : %d ms", queryTimeout));
} else {
log.warn("Failed to send cancelling request", ar.cause());
}
}));
SQLConnection sqlConnection = new SQLConnection(pgConnection, null, timerId);
handler.handle(Future.succeededFuture(sqlConnection));
});
}
use of io.vertx.pgclient.PgConnection in project raml-module-builder by folio-org.
the class PostgresClient method doStreamGetCount.
/**
* private for now, might be public later (and renamed)
* @param <T>
* @param connection
* @param table
* @param clazz
* @param fieldName
* @param wrapper
* @param returnIdField
* @param distinctOn
* @param facets
* @param replyHandler
*/
// Method has >7 parameters
@SuppressWarnings({ "squid:S00107" })
<T> void doStreamGetCount(PgConnection connection, boolean startTransaction, String table, Class<T> clazz, String fieldName, CQLWrapper wrapper, boolean returnIdField, String distinctOn, List<FacetField> facets, Handler<AsyncResult<PostgresClientStreamResult<T>>> replyHandler) {
try {
QueryHelper queryHelper = buildQueryHelper(table, fieldName, wrapper, returnIdField, facets, distinctOn);
Future<Integer> countQuery;
if (wrapper == null || wrapper.hasReturnCount()) {
countQuery = connection.query(queryHelper.countQuery).execute().map(result -> result.iterator().next().getInteger(0));
} else {
countQuery = Future.succeededFuture(null);
}
countQuery.onSuccess(count -> {
ResultInfo resultInfo = new ResultInfo();
resultInfo.setTotalRecords(count);
doStreamGetQuery(connection, startTransaction, queryHelper, resultInfo, clazz, replyHandler);
}).onFailure(e -> {
log.error(e.getMessage(), e);
replyHandler.handle(Future.failedFuture(e));
});
} catch (Exception e) {
log.error(e.getMessage(), e);
replyHandler.handle(Future.failedFuture(e));
}
}
Aggregations