use of org.folio.rest.persist.PostgresClient.QueryHelper 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 org.folio.rest.persist.PostgresClient.QueryHelper in project raml-module-builder by folio-org.
the class PostgresClientIT method processQueryWithCountBelowOffset.
// offset >= estimated total https://issues.folio.org/browse/RMB-684
@Test
public void processQueryWithCountBelowOffset(TestContext context) {
postgresClient = createNumbers(context, 1, 2, 3, 4, 5);
postgresClient.startTx(context.asyncAssertSuccess(conn -> {
QueryHelper queryHelper = new QueryHelper("numbers");
queryHelper.selectQuery = "SELECT i FROM numbers ORDER BY i OFFSET 2";
queryHelper.offset = 2;
// estimation=1 is below offset=2
queryHelper.countQuery = "SELECT 1";
Function<TotaledResults, Results<Integer>> resultSetMapper = totaledResults -> {
context.verify(verify -> {
assertThat(totaledResults.estimatedTotal, is(1));
assertThat(totaledResults.set.size(), is(3));
});
return null;
};
postgresClient.processQueryWithCount(conn.conn, queryHelper, "statMethod", resultSetMapper).onComplete(context.asyncAssertSuccess());
}));
}
use of org.folio.rest.persist.PostgresClient.QueryHelper 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 org.folio.rest.persist.PostgresClient.QueryHelper in project raml-module-builder by folio-org.
the class PostgresClientIT method processQueryWithCountSqlFailure.
@Test
public void processQueryWithCountSqlFailure(TestContext context) {
postgresClient = postgresClient();
postgresClient.startTx(context.asyncAssertSuccess(conn -> {
QueryHelper queryHelper = new QueryHelper("table");
queryHelper.selectQuery = "'";
queryHelper.countQuery = "'";
postgresClient.processQueryWithCount(conn.conn, queryHelper, "statMethod", null).onComplete(context.asyncAssertFailure(fail -> {
assertThat(fail.getMessage(), containsString("unterminated quoted string"));
}));
}));
}
use of org.folio.rest.persist.PostgresClient.QueryHelper 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