use of org.folio.cql2pgjson.CQL2PgJSON 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.CQL2PgJSON in project raml-module-builder by folio-org.
the class CQL2PGCLIMain method handleOptions.
static String handleOptions(String[] args) throws FieldException, IOException, QueryValidationException, ParseException {
Options options = new Options();
Option database = Option.builder("t").hasArg().required(true).desc("Postgres table name").build();
Option field = Option.builder("f").hasArg().required(false).desc("Postgres field name").build();
Option dbschema = Option.builder("b").hasArg().required(false).desc("Path to RMB-style schema.json to describe database").build();
options.addOption(database);
options.addOption(field);
options.addOption(dbschema);
CommandLineParser parser = new DefaultParser();
CommandLine line = parser.parse(options, args);
CQL2PgJSON cql2pgJson = null;
String fullFieldName = line.getOptionValue("t") + "." + line.getOptionValue("f", "jsonb");
cql2pgJson = new CQL2PgJSON(fullFieldName);
if (line.hasOption("b")) {
cql2pgJson.setDbSchemaPath(line.getOptionValue("b"));
}
List<String> cliArgs = line.getArgList();
String cql = cliArgs.get(0);
return parseCQL(cql2pgJson, line.getOptionValue("t"), cql);
}
use of org.folio.cql2pgjson.CQL2PgJSON in project raml-module-builder by folio-org.
the class TestCLI method testCLIWithDBSchema.
@Test
public void testCLIWithDBSchema() throws FieldException, IOException, ParseException, QueryValidationException {
String cql = "hrid=\"fcd64ce1-6995-48f0-840e-89ffa2\"";
String[] args = new String[] { "-t", "instance", "-f", "jsonb", "-b", dbSchemaPath, cql };
String fullFieldName = "instance.jsonb";
CQL2PgJSON cql2pgjson = new CQL2PgJSON(fullFieldName);
cql2pgjson.setDbSchemaPath(dbSchemaPath);
String output = CQL2PGCLIMain.parseCQL(cql2pgjson, "instance", cql);
String cli_output = CQL2PGCLIMain.handleOptions(args);
assertNotNull(output);
logger.info(output);
logger.info(cli_output);
assertEquals(output, cli_output);
}
use of org.folio.cql2pgjson.CQL2PgJSON in project raml-module-builder by folio-org.
the class ConnIT method streamGet.
@ParameterizedTest
@CsvSource({ "key=*, 2", "key=b, 1", "key=x, 0" })
void streamGet(String cql, int total, VertxTestContext vtc) throws CQL2PgJSONException {
AtomicInteger count = new AtomicInteger();
AtomicBoolean end = new AtomicBoolean();
CQLWrapper cqlWrapper = new CQLWrapper(new CQL2PgJSON("jsonb"), cql);
with(randomUuid(), "a", randomUuid(), "b", trans -> {
return trans.streamGet("t", Pojo.class, cqlWrapper, as -> {
PostgresClientStreamResult<Pojo> r = as.result();
assertThat(r.resultInfo().getTotalRecords()).isEqualTo(total);
r.handler(x -> count.incrementAndGet());
r.exceptionHandler(t -> vtc.failNow(t));
r.endHandler(x -> end.set(true));
});
}).onComplete(succeedingThenComplete(vtc, x -> {
assertThat(count.get()).isEqualTo(total);
assertThat(end.get()).isTrue();
}));
}
use of org.folio.cql2pgjson.CQL2PgJSON 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();
}
}
Aggregations