use of io.vertx.sqlclient.RowStream in project raml-module-builder by folio-org.
the class PostgresClientIT method streamGetResultException.
@Test
public void streamGetResultException(TestContext context) {
createTableWithPoLines(context);
ResultInfo resultInfo = new ResultInfo();
context.assertNotNull(vertx);
RowStream<Row> sqlRowStream = new MySQLRowStream();
StringBuilder events = new StringBuilder();
Async async = context.async();
PostgresClientStreamResult<Object> streamResult = new PostgresClientStreamResult(resultInfo);
Transaction transaction = null;
postgresClient.doStreamRowResults(sqlRowStream, Object.class, transaction, new QueryHelper("table_name"), streamResult, context.asyncAssertSuccess(sr -> {
sr.handler(streamHandler -> {
events.append("[handler]");
});
sr.endHandler(x -> {
events.append("[endHandler]");
throw new NullPointerException("null");
});
sr.exceptionHandler(x -> {
events.append("[exception]");
context.assertEquals("SQLRowStream exception", x.getMessage());
async.complete();
});
}));
async.await(1000);
context.assertEquals("[exception]", events.toString());
}
use of io.vertx.sqlclient.RowStream in project raml-module-builder by folio-org.
the class PostgresClient method doStreamRowResults.
/**
* @param transaction the transaction to close, null if not to close
*/
<T> void doStreamRowResults(RowStream<Row> rowStream, Class<T> clazz, Transaction transaction, QueryHelper queryHelper, PostgresClientStreamResult<T> streamResult, Handler<AsyncResult<PostgresClientStreamResult<T>>> replyHandler) {
ResultInfo resultInfo = streamResult.resultInfo();
Promise<PostgresClientStreamResult<T>> promise = Promise.promise();
ResultsHelper<T> resultsHelper = new ResultsHelper<>(clazz);
boolean isAuditFlavored = isAuditFlavored(resultsHelper.clazz);
Map<String, Method> externalColumnSetters = new HashMap<>();
AtomicInteger resultCount = new AtomicInteger();
rowStream.handler(r -> {
try {
// for first row, get column names
if (resultsHelper.offset == 0) {
List<String> columnNames = getColumnNames(r);
collectExternalColumnSetters(columnNames, resultsHelper.clazz, isAuditFlavored, externalColumnSetters);
}
@SuppressWarnings("unchecked") T objRow = (T) deserializeRow(resultsHelper, externalColumnSetters, isAuditFlavored, r);
if (!resultsHelper.facet) {
resultCount.incrementAndGet();
if (!promise.future().isComplete()) {
// end of facets (if any) .. produce result
resultsHelper.facets.forEach((k, v) -> resultInfo.getFacets().add(v));
promise.complete(streamResult);
replyHandler.handle(promise.future());
}
streamResult.fireHandler(objRow);
}
resultsHelper.offset++;
} catch (Exception e) {
streamResult.handler(null);
log.error(e.getMessage(), e);
if (!promise.future().isComplete()) {
promise.complete(streamResult);
replyHandler.handle(promise.future());
}
rowStream.close();
closeIfNonNull(transaction).onComplete((AsyncResult<Void> voidRes) -> streamResult.fireExceptionHandler(e));
}
}).endHandler(v2 -> {
rowStream.close();
closeIfNonNull(transaction).onComplete(ignore -> {
resultInfo.setTotalRecords(getTotalRecords(resultCount.get(), resultInfo.getTotalRecords(), queryHelper.offset, queryHelper.limit));
try {
if (!promise.future().isComplete()) {
promise.complete(streamResult);
replyHandler.handle(promise.future());
}
streamResult.fireEndHandler();
} catch (Exception ex) {
streamResult.fireExceptionHandler(ex);
}
});
}).exceptionHandler(e -> {
rowStream.close();
closeIfNonNull(transaction).onComplete(ignore -> {
if (!promise.future().isComplete()) {
promise.complete(streamResult);
replyHandler.handle(promise.future());
}
streamResult.fireExceptionHandler(e);
});
});
}
Aggregations