use of io.vertx.sqlclient.Tuple in project raml-module-builder by folio-org.
the class PostgresClientIT method executeList.
@Test
public void executeList(TestContext context) {
Async async = context.async();
JsonArray ids = new JsonArray().add(randomUuid()).add(randomUuid());
List<Tuple> list = new ArrayList<>(2);
list.add(Tuple.of(UUID.fromString(ids.getString(0))));
list.add(Tuple.of(UUID.fromString(ids.getString(1))));
insertXAndSingleQuotePojo(context, ids).execute("DELETE FROM tenant_raml_module_builder.foo WHERE id=$1", list, res -> {
assertSuccess(context, res);
List<RowSet<Row>> result = res.result();
context.assertEquals(2, result.size());
context.assertEquals(1, result.get(0).rowCount());
context.assertEquals(1, result.get(1).rowCount());
async.complete();
});
}
use of io.vertx.sqlclient.Tuple in project raml-module-builder by folio-org.
the class PostgresClient method getById.
/**
* Get jsonb by id for a list of ids.
* <p>
* The result is a map of all found records where the key is the id
* and the value is the jsonb.
*
* @param table the table to search in
* @param ids the values of the id field
* @param function how to convert the (String encoded) JSON
* @param replyHandler the result after applying function
*/
private <R> void getById(String table, JsonArray ids, FunctionWithException<String, R, Exception> function, Handler<AsyncResult<Map<String, R>>> replyHandler) {
if (ids == null || ids.isEmpty()) {
replyHandler.handle(Future.succeededFuture(Collections.emptyMap()));
return;
}
getConnection(res -> {
if (res.failed()) {
replyHandler.handle(Future.failedFuture(res.cause()));
return;
}
Tuple list = Tuple.tuple();
for (int i = 0; i < ids.size(); i++) {
list.addUUID(UUID.fromString(ids.getString(i)));
}
PgConnection connection = res.result();
StringBuilder sql = new StringBuilder().append(SELECT).append(ID_FIELD).append(", ").append(DEFAULT_JSONB_FIELD_NAME).append(FROM).append(schemaName).append(DOT).append(table).append(WHERE).append(ID_FIELD).append(" IN ($1");
for (int i = 2; i <= ids.size(); i++) {
sql.append(", $" + i);
}
sql.append(")");
connection.preparedQuery(sql.toString()).execute(list, query -> {
connection.close();
if (query.failed()) {
replyHandler.handle(Future.failedFuture(query.cause()));
return;
}
try {
Map<String, R> result = new HashMap<>();
Iterator<Row> iterator = query.result().iterator();
while (iterator.hasNext()) {
Row row = iterator.next();
result.put(row.getValue(0).toString(), function.apply(row.getValue(1).toString()));
}
replyHandler.handle(Future.succeededFuture(result));
} catch (Exception e) {
replyHandler.handle(Future.failedFuture(e));
}
});
});
}
use of io.vertx.sqlclient.Tuple in project raml-module-builder by folio-org.
the class Conn method saveBatch.
/**
* Insert or upsert the entities into table.
*
* <p>A transaction must be open on this {@link #Conn} so that SELECT ... FOR UPDATE works.
*
* @param upsert true for upsert, false for insert with fail on duplicate id
* @param table destination table to insert into
* @param entities each array element is a String with the content for the JSONB field of table; if id is missing a random id is generated
* @return one result row per inserted row, containing the id field
*/
Future<RowSet<Row>> saveBatch(boolean upsert, String table, JsonArray entities) {
try {
List<Tuple> list = new ArrayList<>();
if (entities != null) {
for (int i = 0; i < entities.size(); i++) {
String json = entities.getString(i);
JsonObject jsonObject = new JsonObject(json);
String id = jsonObject.getString("id");
list.add(Tuple.of(id == null ? UUID.randomUUID() : UUID.fromString(id), jsonObject));
}
}
return saveBatchInternal(upsert, table, list);
} catch (Exception e) {
log.error(e.getMessage(), e);
return Future.failedFuture(e);
}
}
use of io.vertx.sqlclient.Tuple in project raml-module-builder by folio-org.
the class Conn method updateBatch.
/**
* Update the entities in the table , match using the id property.
*
* <p>A transaction must be open on this {@link #Conn} so that SELECT ... FOR UPDATE works.
*
* @param entities each array element is a String with the content for the JSONB field of table
* @return one {@link RowSet} per array element with {@link RowSet#rowCount()} information
*/
public Future<RowSet<Row>> updateBatch(String table, JsonArray entities) {
try {
if (entities == null || entities.size() == 0) {
return Future.succeededFuture();
}
List<Tuple> list = new ArrayList<>(entities.size());
for (int i = 0; i < entities.size(); i++) {
Object o = entities.getValue(i);
list.add(Tuple.of(o instanceof JsonObject ? o : new JsonObject(o.toString())));
}
return updateBatchInternal(table, list);
} catch (Throwable t) {
log.error("updateBatch error " + t.getMessage(), t);
return Future.failedFuture(t);
}
}
use of io.vertx.sqlclient.Tuple in project raml-module-builder by folio-org.
the class Conn method saveBatch.
/**
* A transaction must be open on this {@link #Conn} so that SELECT ... FOR UPDATE works.
*/
<T> Future<RowSet<Row>> saveBatch(boolean upsert, String table, List<T> entities) {
try {
if (entities == null || entities.isEmpty()) {
return Future.succeededFuture(emptyRowSetOfId());
}
List<Tuple> batch = new ArrayList<>(entities.size());
// We must use reflection, the POJOs don't have an interface/superclass in common.
Method getIdMethod = entities.get(0).getClass().getDeclaredMethod("getId");
for (Object entity : entities) {
Object obj = getIdMethod.invoke(entity);
UUID id = obj == null ? UUID.randomUUID() : UUID.fromString((String) obj);
batch.add(Tuple.of(id, PostgresClient.pojo2JsonObject(entity)));
}
return saveBatchInternal(upsert, table, batch);
} catch (Exception e) {
log.error("saveBatch error " + e.getMessage(), e);
return Future.failedFuture(e);
}
}
Aggregations