use of io.vertx.sqlclient.Transaction in project raml-module-builder by folio-org.
the class PostgresClientIT method postgresClientQueryFails.
/**
* @return a PostgresClient where invoking SQLConnection::update, SQLConnection::updateWithParams or
* SQLConnection::queryWithParams will report a failure via the resultHandler.
*/
private PostgresClient postgresClientQueryFails() {
PgConnection pgConnection = new PgConnection() {
@Override
public PgConnection notificationHandler(Handler<PgNotification> handler) {
return this;
}
@Override
public PgConnection cancelRequest(Handler<AsyncResult<Void>> handler) {
handler.handle(Future.failedFuture("cancelRequestFails"));
return this;
}
@Override
public int processId() {
return 0;
}
@Override
public int secretKey() {
return 0;
}
@Override
public PgConnection prepare(String s, Handler<AsyncResult<PreparedStatement>> handler) {
prepare(s).onComplete(handler);
return null;
}
@Override
public Future<PreparedStatement> prepare(String s) {
return Future.failedFuture("preparedFails");
}
@Override
public SqlConnection prepare(String sql, PrepareOptions options, Handler<AsyncResult<PreparedStatement>> handler) {
prepare(sql, options).onComplete(handler);
return null;
}
@Override
public Future<PreparedStatement> prepare(String sql, PrepareOptions options) {
return prepare(sql);
}
@Override
public PgConnection exceptionHandler(Handler<Throwable> handler) {
return null;
}
@Override
public PgConnection closeHandler(Handler<Void> handler) {
return null;
}
@Override
public void begin(Handler<AsyncResult<Transaction>> handler) {
}
@Override
public Future<Transaction> begin() {
return null;
}
@Override
public boolean isSSL() {
return false;
}
@Override
public void close(Handler<AsyncResult<Void>> handler) {
}
@Override
public Query<RowSet<Row>> query(String s) {
return new Query<RowSet<Row>>() {
@Override
public void execute(Handler<AsyncResult<RowSet<Row>>> handler) {
handler.handle(execute());
}
@Override
public Future<RowSet<Row>> execute() {
return Future.failedFuture("queryFails");
}
@Override
public <R> Query<SqlResult<R>> collecting(Collector<Row, ?, R> collector) {
return null;
}
@Override
public <U> Query<RowSet<U>> mapping(Function<Row, U> function) {
return null;
}
};
}
@Override
public PreparedQuery<RowSet<Row>> preparedQuery(String sql, PrepareOptions options) {
return preparedQuery(sql);
}
@Override
public PreparedQuery<RowSet<Row>> preparedQuery(String s) {
throw new RuntimeException("queryFails");
}
@Override
public Future<Void> close() {
return null;
}
@Override
public DatabaseMetadata databaseMetadata() {
return null;
}
};
PgPool client = new PgPoolBase() {
@Override
public Future<SqlConnection> getConnection() {
return Future.succeededFuture(pgConnection);
}
};
try {
PostgresClient postgresClient = new PostgresClient(vertx, TENANT);
postgresClient.setClient(client);
return postgresClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of io.vertx.sqlclient.Transaction 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