Search in sources :

Example 1 with ResultSet

use of io.vertx.ext.sql.ResultSet in project raml-module-builder by folio-org.

the class PostgresClientIT method parallel.

@Test
public void parallel(TestContext context) {
    /**
     * number of parallel queries
     */
    int n = 20;
    /**
     * sleep time in milliseconds
     */
    double sleep = 150;
    String selectSleep = "select pg_sleep(" + sleep / 1000 + ")";
    /**
     * maximum duration in milliseconds for the completion of all parallel queries
     */
    long maxDuration = (long) (n * sleep / 2);
    /* create n queries in parallel, each sleeping for some time.
     * If vert.x properly processes them in parallel it finishes
     * in less than half of the time needed for sequential processing.
     */
    Async async = context.async();
    PostgresClient client = PostgresClient.getInstance(vertx);
    List<Future> futures = new ArrayList<>(n);
    for (int i = 0; i < n; i++) {
        Future<ResultSet> future = Future.future();
        client.select(selectSleep, future.completer());
        futures.add(future);
    }
    long start = System.currentTimeMillis();
    CompositeFuture.all(futures).setHandler(handler -> {
        long duration = System.currentTimeMillis() - start;
        client.closeClient(whenDone -> {
        });
        context.assertTrue(handler.succeeded());
        context.assertTrue(duration < maxDuration, "duration must be less than " + maxDuration + " ms, it is " + duration + " ms");
        async.complete();
    });
}
Also used : Async(io.vertx.ext.unit.Async) ArrayList(java.util.ArrayList) ResultSet(io.vertx.ext.sql.ResultSet) Future(io.vertx.core.Future) CompositeFuture(io.vertx.core.CompositeFuture) Test(org.junit.Test)

Example 2 with ResultSet

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

the class CRUDTest 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 calypso = "Calypso";
        connection.updateWithParams("insert into ship (name) values (?)", new JsonArray().add(calypso), ires -> {
            if (ires.failed()) {
                fail(rc, ires.cause());
                return;
            }
            UpdateResult insertResult = ires.result();
            if (insertResult.getUpdated() != 1) {
                fail(rc, String.valueOf(insertResult.getUpdated()));
                return;
            }
            Long calypsoId = insertResult.getKeys().getLong(0);
            connection.queryWithParams("select name from ship where id = ?", new JsonArray().add(calypsoId), sres -> {
                if (sres.failed()) {
                    fail(rc, sres.cause());
                    return;
                }
                ResultSet resultSet = sres.result();
                if (!resultSet.getResults().get(0).getString(0).equals(calypso)) {
                    fail(rc, resultSet.getResults().get(0).getString(0));
                    return;
                }
                connection.updateWithParams("update ship set name = ? where id = ?", new JsonArray().add("Alcyone").add(calypsoId), ures -> {
                    if (ures.failed()) {
                        fail(rc, ures.cause());
                        return;
                    }
                    UpdateResult updateResult = ures.result();
                    if (updateResult.getUpdated() != 1) {
                        fail(rc, String.valueOf(updateResult.getUpdated()));
                        return;
                    }
                    connection.updateWithParams("delete from ship where id = ?", new JsonArray().add(calypsoId), dres -> {
                        if (dres.failed()) {
                            fail(rc, dres.cause());
                            return;
                        }
                        UpdateResult deleteResult = dres.result();
                        if (deleteResult.getUpdated() != 1) {
                            fail(rc, String.valueOf(deleteResult.getUpdated()));
                        } 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) UpdateResult(io.vertx.ext.sql.UpdateResult)

Example 3 with ResultSet

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

the class StorageTestSuite method checkForMismatchedIDs.

static void checkForMismatchedIDs(String table) {
    try {
        ResultSet results = getRecordsWithUnmatchedIds(TENANT_ID, table);
        Integer mismatchedRowCount = results.getNumRows();
        assertThat(mismatchedRowCount, is(0));
    } catch (Exception e) {
        System.out.println("WARNING!!!!! Unable to determine mismatched ID rows");
    }
}
Also used : ResultSet(io.vertx.ext.sql.ResultSet) MalformedURLException(java.net.MalformedURLException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ResultSet

use of io.vertx.ext.sql.ResultSet in project vertx-auth by vert-x3.

the class JDBCAuthImpl method executeQuery.

<T> void executeQuery(String query, JsonArray params, Handler<AsyncResult<T>> resultHandler, Consumer<ResultSet> resultSetConsumer) {
    client.getConnection(res -> {
        if (res.succeeded()) {
            SQLConnection conn = res.result();
            conn.queryWithParams(query, params, queryRes -> {
                if (queryRes.succeeded()) {
                    ResultSet rs = queryRes.result();
                    resultSetConsumer.accept(rs);
                } else {
                    resultHandler.handle(Future.failedFuture(queryRes.cause()));
                }
                conn.close(closeRes -> {
                });
            });
        } else {
            resultHandler.handle(Future.failedFuture(res.cause()));
        }
    });
}
Also used : SQLConnection(io.vertx.ext.sql.SQLConnection) ResultSet(io.vertx.ext.sql.ResultSet)

Example 5 with ResultSet

use of io.vertx.ext.sql.ResultSet in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
    JDBCClient jdbc = JDBCClient.createShared(vertx, config);
    // Connect to the database
    jdbc.rxGetConnection().flatMap(conn -> {
        // Now chain some statements using flatmap composition
        Single<ResultSet> resa = conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))").flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")).flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")).flatMap(result -> conn.rxQuery("SELECT * FROM test"));
        return resa.doAfterTerminate(conn::close);
    }).subscribe(resultSet -> {
        // Subscribe to the final result
        System.out.println("Results : " + resultSet.getRows());
    }, err -> {
        System.out.println("Database problem");
        err.printStackTrace();
    });
}
Also used : Single(rx.Single) ResultSet(io.vertx.ext.sql.ResultSet) JsonObject(io.vertx.core.json.JsonObject) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) Single(rx.Single) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

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