use of io.vertx.pgclient.PgConnection in project raml-module-builder by folio-org.
the class SQLConnectionTest method closeConnectionAfterTimerException.
@Test
void closeConnectionAfterTimerException() {
PgConnection pgConnection = mock(PgConnection.class);
assertThrows(NullPointerException.class, () -> new SQLConnection(pgConnection, null, 5L).close(null));
verify(pgConnection).close();
}
use of io.vertx.pgclient.PgConnection in project raml-module-builder by folio-org.
the class Conn method get.
/**
* Return records selected by {@link CQLWrapper} filter.
*
* @param table - table to query
* @param clazz - class of objects to be returned
* @param fieldName - database column to return, for example @link {@link PostgresClient#DEFAULT_JSONB_FIELD_NAME}
* @param wrapper - filter to select records
* @param returnCount - whether to return totalRecords, the number of matching records when disabling OFFSET and LIMIT
* @param returnIdField - if the id field should also be returned, must be true for facets
* @param facets - fields to calculate counts for
* @param distinctOn - database column to calculate the number of distinct values for, null or empty string for none
*/
public <T> Future<Results<T>> get(String table, Class<T> clazz, String fieldName, CQLWrapper wrapper, boolean returnCount, boolean returnIdField, List<FacetField> facets, String distinctOn) {
try {
QueryHelper queryHelper = postgresClient.buildQueryHelper(table, fieldName, wrapper, returnIdField, facets, distinctOn);
Function<TotaledResults, Results<T>> resultSetMapper = totaledResults -> postgresClient.processResults(totaledResults.set, totaledResults.estimatedTotal, queryHelper.offset, queryHelper.limit, clazz);
if (returnCount) {
return postgresClient.processQueryWithCount(pgConnection, queryHelper, "get", resultSetMapper);
} else {
return Future.future(promise -> postgresClient.processQuery(pgConnection, queryHelper, null, "get", resultSetMapper, promise));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
return Future.failedFuture(e);
}
}
use of io.vertx.pgclient.PgConnection in project raml-module-builder by folio-org.
the class ConnIT method getPgConnection.
@Test
void getPgConnection() {
PgConnection pgConnection = mock(PgConnection.class);
assertThat(new Conn(null, pgConnection).getPgConnection()).isEqualTo(pgConnection);
}
use of io.vertx.pgclient.PgConnection 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.pgclient.PgConnection in project raml-module-builder by folio-org.
the class PostgresClientTest method testProcessQueryWithCount.
@Test
public void testProcessQueryWithCount() {
PostgresClient testClient = PostgresClient.testClient();
QueryHelper queryHelper = new QueryHelper("test_pojo");
queryHelper.selectQuery = "SELECT * FROM test_pojo";
queryHelper.countQuery = "COUNT (*) FROM test_pojo";
int total = 10;
PgConnection connection = new FakeSqlConnection(Future.succeededFuture(getMockTestJsonbPojoResultSet(total)), false);
testClient.processQueryWithCount(connection, queryHelper, "get", totaledResults -> {
assertThat(totaledResults.estimatedTotal, is(total));
return testClient.processResults(totaledResults.set, totaledResults.estimatedTotal, DEFAULT_OFFSET, DEFAULT_LIMIT, TestPojo.class);
}).onSuccess(reply -> assertTestPojoResults(reply.getResults(), total));
}
Aggregations