Search in sources :

Example 11 with ResultSet

use of io.vertx.ext.sql.ResultSet in project vertx-openshift-it by cescoffier.

the class QueryWithParamsTest method handle.

@Override
public void handle(RoutingContext rc) {
    jdbcClient.getConnection(ar -> {
        if (ar.failed()) {
            fail(rc, ar.cause());
            return;
        }
        SQLConnection connection = ar.result();
        rc.response().bodyEndHandler(v -> {
            connection.close();
        });
        connection.queryWithParams("select name from person where id=?", new JsonArray().add(2), res -> {
            if (res.failed()) {
                fail(rc, res.cause());
                return;
            }
            ResultSet resultSet = res.result();
            if (!resultSet.getResults().get(0).getString(0).equals("titi")) {
                fail(rc, resultSet.getResults().get(0).getString(0));
            } else {
                rc.response().setStatusCode(200).end();
            }
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) SQLConnection(io.vertx.ext.sql.SQLConnection) ResultSet(io.vertx.ext.sql.ResultSet)

Example 12 with ResultSet

use of io.vertx.ext.sql.ResultSet in project vertx-openshift-it by cescoffier.

the class StoredProcedureTest method handle.

@Override
public void handle(RoutingContext rc) {
    jdbcClient.getConnection(ar -> {
        if (ar.failed()) {
            fail(rc, ar.cause());
            return;
        }
        SQLConnection connection = ar.result();
        rc.response().bodyEndHandler(v -> {
            connection.close();
        });
        String statsFunc = "{ call animal_stats(?, ?, ?) }";
        connection.callWithParams(statsFunc, new JsonArray().add(false), new JsonArray().addNull().add("BIGINT").add("REAL"), sres -> {
            if (sres.failed()) {
                fail(rc, sres.cause());
                return;
            }
            ResultSet statsResult = sres.result();
            JsonArray output = statsResult.getOutput();
            if (output == null) {
                fail(rc, "output is null");
                return;
            }
            if (output.size() != 3) {
                fail(rc, output.toString());
                return;
            }
            if (output.getValue(0) != null) {
                fail(rc, output.toString());
                return;
            }
            if (output.getLong(1) != 3) {
                fail(rc, output.toString());
                return;
            }
            BigDecimal bigDecimal = new BigDecimal(output.getDouble(2)).setScale(2, RoundingMode.HALF_UP);
            if (!bigDecimal.equals(new BigDecimal("33.33"))) {
                fail(rc, output.toString());
                return;
            }
            String loadFunc = "{ call load_animals(?) }";
            connection.callWithParams(loadFunc, new JsonArray().add(true), new JsonArray(), lres -> {
                if (lres.failed()) {
                    fail(rc, lres.cause());
                    return;
                }
                ResultSet loadResult = lres.result();
                List<JsonArray> results = loadResult.getResults();
                if (results.size() != 2) {
                    fail(rc, results.toString());
                    return;
                }
                if (!loadResult.getColumnNames().equals(Arrays.asList("id", "name"))) {
                    fail(rc, results.toString());
                    return;
                }
                List<String> names = results.stream().map(array -> array.getString(1)).collect(toList());
                if (!names.equals(Arrays.asList("dog", "cat"))) {
                    fail(rc, results.toString());
                    return;
                }
                rc.response().setStatusCode(200).end();
            });
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonArray(io.vertx.core.json.JsonArray) BigDecimal(java.math.BigDecimal) TestUtil(io.vertx.openshift.jdbc.TestUtil) Arrays(java.util.Arrays) List(java.util.List) JDBCClient(io.vertx.ext.jdbc.JDBCClient) ResultSet(io.vertx.ext.sql.ResultSet) SQLConnection(io.vertx.ext.sql.SQLConnection) RoutingContext(io.vertx.ext.web.RoutingContext) Handler(io.vertx.core.Handler) Collectors(java.util.stream.Collectors) RoundingMode(java.math.RoundingMode) SQLConnection(io.vertx.ext.sql.SQLConnection) ResultSet(io.vertx.ext.sql.ResultSet) BigDecimal(java.math.BigDecimal)

Example 13 with ResultSet

use of io.vertx.ext.sql.ResultSet in project mod-inventory-storage by folio-org.

the class StorageTestSuite method getRecordsWithUnmatchedIds.

private static ResultSet getRecordsWithUnmatchedIds(String tenantId, String tableName) throws InterruptedException, ExecutionException, TimeoutException {
    PostgresClient dbClient = PostgresClient.getInstance(getVertx(), tenantId);
    CompletableFuture<ResultSet> selectCompleted = new CompletableFuture<>();
    String sql = String.format("SELECT null FROM %s_%s.%s" + " WHERE CAST(_id AS VARCHAR(50)) != jsonb->>'id'", tenantId, "mod_inventory_storage", tableName);
    dbClient.select(sql, result -> {
        if (result.succeeded()) {
            selectCompleted.complete(result.result());
        } else {
            selectCompleted.completeExceptionally(result.cause());
        }
    });
    return selectCompleted.get(5, TimeUnit.SECONDS);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ResultSet(io.vertx.ext.sql.ResultSet) PostgresClient(org.folio.rest.persist.PostgresClient)

Example 14 with ResultSet

use of io.vertx.ext.sql.ResultSet in project okapi by folio-org.

the class PostgresTable method getAll.

public void getAll(Class<T> clazz, Handler<ExtendedAsyncResult<List<T>>> fut) {
    PostgresQuery q = pg.getQuery();
    String sql = "SELECT " + jsonColumn + " FROM " + table;
    q.query(sql, res -> {
        if (res.failed()) {
            fut.handle(new Failure<>(INTERNAL, res.cause()));
        } else {
            ResultSet rs = res.result();
            List<T> ml = new ArrayList<>();
            List<JsonObject> tempList = rs.getRows();
            for (JsonObject r : tempList) {
                String tj = r.getString(jsonColumn);
                T md = Json.decodeValue(tj, clazz);
                ml.add(md);
            }
            q.close();
            fut.handle(new Success<>(ml));
        }
    });
}
Also used : ResultSet(io.vertx.ext.sql.ResultSet) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject)

Example 15 with ResultSet

use of io.vertx.ext.sql.ResultSet in project okapi by folio-org.

the class TenantStorePostgres method updateModules.

@Override
public void updateModules(String id, SortedMap<String, Boolean> enabled, Handler<ExtendedAsyncResult<Void>> fut) {
    logger.debug("updateModules " + Json.encode(enabled.keySet()));
    PostgresQuery q = pg.getQuery();
    String sql = "SELECT " + JSON_COLUMN + " FROM " + TABLE + " WHERE " + ID_SELECT;
    JsonArray jsa = new JsonArray();
    jsa.add(id);
    q.queryWithParams(sql, jsa, res -> {
        if (res.failed()) {
            logger.fatal("updateModule failed: " + res.cause().getMessage());
            fut.handle(new Failure<>(INTERNAL, res.cause()));
        } else {
            ResultSet rs = res.result();
            if (rs.getNumRows() == 0) {
                fut.handle(new Failure<>(NOT_FOUND, "Tenant " + id + " not found"));
                q.close();
            } else {
                logger.debug("update: replace");
                updateModuleR(q, id, enabled, rs.getRows().iterator(), fut);
            }
        }
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) ResultSet(io.vertx.ext.sql.ResultSet)

Aggregations

ResultSet (io.vertx.ext.sql.ResultSet)16 SQLConnection (io.vertx.ext.sql.SQLConnection)8 JsonArray (io.vertx.core.json.JsonArray)7 JsonObject (io.vertx.core.json.JsonObject)4 Runner (io.vertx.example.util.Runner)2 JDBCClient (io.vertx.ext.jdbc.JDBCClient)2 UpdateResult (io.vertx.ext.sql.UpdateResult)2 ArrayList (java.util.ArrayList)2 Suspendable (co.paralleluniverse.fibers.Suspendable)1 Single (io.reactivex.Single)1 CompositeFuture (io.vertx.core.CompositeFuture)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Async (io.vertx.ext.unit.Async)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 TestUtil (io.vertx.openshift.jdbc.TestUtil)1 AbstractVerticle (io.vertx.reactivex.core.AbstractVerticle)1 JDBCClient (io.vertx.reactivex.ext.jdbc.JDBCClient)1 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)1 JDBCClient (io.vertx.rxjava.ext.jdbc.JDBCClient)1