use of org.folio.cql2pgjson.exception.FieldException in project raml-module-builder by folio-org.
the class BooksDemoAPI method getRmbtestsTest.
@Validate
@Override
public void getRmbtestsTest(String query, RoutingContext routingContext, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
switch(query == null ? "null" : query) {
case "badclass=true":
PgUtil.streamGet(TABLE, /* can not be deserialized */
StringBuilder.class, null, 0, 10, new LinkedList<>(), "books", routingContext, okapiHeaders, vertxContext);
break;
case "nullpointer=true":
PgUtil.streamGet(TABLE, Book.class, null, 0, 10, null, "books", routingContext, /* okapiHeaders is null which results in exception */
null, vertxContext);
break;
case "slim=true":
PgUtil.streamGet(TABLE, SlimBook.class, null, 0, 10, new LinkedList<>(), "books", routingContext, okapiHeaders, vertxContext);
break;
case "wrapper=true":
try {
CQLWrapper wrapper = new CQLWrapper(new CQL2PgJSON("jsonb"), "cql.allRecords=true");
PgUtil.streamGet(TABLE, Book.class, wrapper, null, "books", routingContext, okapiHeaders, vertxContext);
} catch (FieldException e) {
GetRmbtestsTestResponse.respond500WithTextPlain(e.getMessage());
}
break;
default:
PgUtil.streamGet(TABLE, Book.class, query, 0, 10, new LinkedList<>(), "books", routingContext, okapiHeaders, vertxContext);
}
}
use of org.folio.cql2pgjson.exception.FieldException in project raml-module-builder by folio-org.
the class PgUtil method getWithOptimizedSql.
/**
* Run the cql query using optimized SQL (if possible) or standard SQL.
* <p>
* PostgreSQL has no statistics about a field within a JSONB resulting in bad performance.
* <p>
* This method requires that the sortField has a b-tree index (non-unique) and caseSensitive=false
* and removeAccents=true, and that the cql query is supported by a full text index.
* <p>
* This method starts a full table scan until getOptimizedSqlSize() records have been scanned.
* Then it assumes that there are only a few result records and uses the full text match.
* If the requested number of records have been found it stops immediately.
* @param table
* @param clazz
* @param cql
* @param queryTimeout query timeout in milliseconds, or 0 for no timeout
* @param okapiHeaders
* @param vertxContext
* @param responseDelegateClass
* @return
*/
public static <T, C> Future<Response> getWithOptimizedSql(String table, Class<T> clazz, Class<C> collectionClazz, String sortField, String cql, int offset, int limit, int queryTimeout, Map<String, String> okapiHeaders, Context vertxContext, Class<? extends ResponseDelegate> responseDelegateClass) {
final Method respond500;
try {
respond500 = responseDelegateClass.getMethod(RESPOND_500_WITH_TEXT_PLAIN, Object.class);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return response(e.getMessage(), null, null);
}
final Method respond200;
final Method respond400;
try {
respond200 = responseDelegateClass.getMethod(RESPOND_200_WITH_APPLICATION_JSON, collectionClazz);
respond400 = responseDelegateClass.getMethod(RESPOND_400_WITH_TEXT_PLAIN, Object.class);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return response(e.getMessage(), respond500, respond500);
}
try {
CQL2PgJSON cql2pgJson = new CQL2PgJSON(table + "." + JSON_COLUMN);
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, cql, limit, offset);
PreparedCQL preparedCql = new PreparedCQL(table, cqlWrapper, okapiHeaders);
String sql = generateOptimizedSql(sortField, preparedCql, offset, limit);
if (sql == null) {
// the cql is not suitable for optimization, generate simple sql
return get(preparedCql, clazz, collectionClazz, okapiHeaders, vertxContext, responseDelegateClass);
}
logger.info("Optimized SQL generated. Source CQL: " + cql);
Promise<Response> promise = Promise.promise();
PostgresClient postgresClient = postgresClient(vertxContext, okapiHeaders);
postgresClient.select(sql, queryTimeout, reply -> {
try {
if (reply.failed()) {
Throwable cause = reply.cause();
logger.error("Optimized SQL failed: " + cause.getMessage() + ": " + sql, cause);
response(cause.getMessage(), respond500, respond500).onComplete(promise);
return;
}
C collection = collection(clazz, collectionClazz, reply.result(), offset, limit);
response(collection, respond200, respond500).onComplete(promise);
} catch (Exception e) {
logger.error(e.getMessage(), e);
response(e.getMessage(), respond500, respond500).onComplete(promise);
}
});
return promise.future();
} catch (FieldException | QueryValidationException e) {
logger.error(e.getMessage(), e);
return response(e.getMessage(), respond400, respond500);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return response(e.getMessage(), respond500, respond500);
}
}
use of org.folio.cql2pgjson.exception.FieldException in project raml-module-builder by folio-org.
the class PostgresClientTransactionsIT method updateTransaction.
private void updateTransaction(TestContext context) {
PostgresClient c1 = PostgresClient.getInstance(vertx, tenant);
Async async = context.async();
// create connection
c1.startTx(handler -> {
if (handler.succeeded()) {
SimplePojo z = new SimplePojo();
z.setId("1");
z.setName("me");
// update record
CQL2PgJSON cql2pgJson = null;
try {
cql2pgJson = new CQL2PgJSON(table + ".jsonb");
} catch (FieldException e1) {
e1.printStackTrace();
context.fail(e1);
}
CQLWrapper cql = new CQLWrapper(cql2pgJson, "name==d");
c1.update(handler, "z", z, cql, true, reply -> {
if (reply.succeeded()) {
// make sure record is not updated since not committed yet
c1.select("SELECT jsonb->>'name' FROM " + fullTable, reply2 -> {
if (!reply2.succeeded()) {
context.fail(reply2.cause());
}
try {
String name = reply2.result().iterator().next().getString(0);
context.assertEquals("d", name, "Name property should not have been changed");
} catch (Exception e) {
e.printStackTrace();
context.fail(e.getMessage());
}
// end transaction / commit
c1.endTx(handler, done -> {
if (done.succeeded()) {
// record should have been updated
c1.select("SELECT jsonb->>'name' FROM " + fullTable, selectReply -> {
if (!selectReply.succeeded()) {
context.fail(selectReply.cause());
} else {
try {
String name = selectReply.result().iterator().next().getString(0);
context.assertEquals("me", name, "Name property should have been changed");
} catch (Exception e) {
e.printStackTrace();
context.fail(e.getMessage());
}
async.complete();
}
});
} else {
context.fail(done.cause());
}
});
});
} else {
context.fail(reply.cause());
}
});
} else {
context.fail(handler.cause());
}
});
async.await(5000);
c1.closeClient(context.asyncAssertSuccess());
}
use of org.folio.cql2pgjson.exception.FieldException in project raml-module-builder by folio-org.
the class PostgresClientIT method getCQLWrapperFailure.
@Test
public void getCQLWrapperFailure(TestContext context) throws IOException, FieldException {
final String tableDefiniton = "id UUID PRIMARY KEY , jsonb JSONB NOT NULL, distinct_test_field TEXT";
createTableWithPoLines(context, MOCK_POLINES_TABLE, tableDefiniton);
CQL2PgJSON cql2pgJson = new CQL2PgJSON("jsonb");
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, // syntax error
"cql.allRecords=");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, null, null, /*facets*/
handler -> {
context.assertTrue(handler.failed());
async.complete();
});
async.awaitSuccess();
}
}
use of org.folio.cql2pgjson.exception.FieldException in project raml-module-builder by folio-org.
the class PostgresClientIT method getCQLWrapperWithFacets.
@Test
public void getCQLWrapperWithFacets(TestContext context) throws FieldException {
final String tableDefiniton = "id UUID PRIMARY KEY , jsonb JSONB NOT NULL, distinct_test_field TEXT";
createTableWithPoLines(context, MOCK_POLINES_TABLE, tableDefiniton);
CQL2PgJSON cql2pgJson = new CQL2PgJSON("jsonb");
List<FacetField> facets = List.of(new FacetField("jsonb->>'edition'"));
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, null, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(6, resultInfo.getTotalRecords());
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(1, retFacets.size());
async.complete();
});
async.awaitSuccess();
}
String distinctOn = "jsonb->>'order_format'";
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "cql.allRecords=1");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, distinctOn, handler -> {
context.assertTrue(handler.succeeded());
ResultInfo resultInfo = handler.result().getResultInfo();
context.assertEquals(4, resultInfo.getTotalRecords());
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(1, retFacets.size());
async.complete();
});
async.awaitSuccess();
}
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "order_format==Other");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Object.class, "*", cqlWrapper, true, true, facets, distinctOn, context.asyncAssertSuccess(res -> {
ResultInfo resultInfo = res.getResultInfo();
context.assertEquals(1, resultInfo.getTotalRecords());
List<Object> objs = res.getResults();
ObjectMapper mapper = new ObjectMapper();
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(1, retFacets.size());
context.assertEquals("edition", retFacets.get(0).getType());
context.assertEquals(1, retFacets.get(0).getFacetValues().get(0).getCount());
context.assertEquals("First edition", retFacets.get(0).getFacetValues().get(0).getValue().toString());
context.assertEquals(1, objs.size());
try {
context.assertEquals("70fb4e66-cdf1-11e8-a8d5-f2801f1b9fd1", new JsonObject(mapper.writeValueAsString(objs.get(0))).getString("id"));
} catch (JsonProcessingException e) {
context.fail(e);
}
async.complete();
}));
async.awaitSuccess();
}
{
CQLWrapper cqlWrapper = new CQLWrapper(cql2pgJson, "order_format==Other");
Async async = context.async();
postgresClient.get(MOCK_POLINES_TABLE, Poline.class, "*", cqlWrapper, true, true, facets, distinctOn, context.asyncAssertSuccess(res -> {
ResultInfo resultInfo = res.getResultInfo();
context.assertEquals(1, resultInfo.getTotalRecords());
List<Poline> objs = res.getResults();
List<Facet> retFacets = resultInfo.getFacets();
context.assertEquals(1, retFacets.size());
context.assertEquals("edition", retFacets.get(0).getType());
context.assertEquals(1, retFacets.get(0).getFacetValues().get(0).getCount());
context.assertEquals("First edition", retFacets.get(0).getFacetValues().get(0).getValue().toString());
context.assertEquals(1, objs.size());
context.assertEquals("70fb4e66-cdf1-11e8-a8d5-f2801f1b9fd1", objs.get(0).getId());
async.complete();
}));
async.awaitSuccess();
}
}
Aggregations