Search in sources :

Example 1 with UpdateResult

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

the class PostgresTable method delete.

public void delete(String id, Handler<ExtendedAsyncResult<Void>> fut) {
    PostgresQuery q = pg.getQuery();
    String sql = "DELETE FROM " + table + " WHERE " + idSelect;
    JsonArray jsa = new JsonArray();
    jsa.add(id);
    q.updateWithParams(sql, jsa, res -> {
        if (res.failed()) {
            fut.handle(new Failure<>(INTERNAL, res.cause()));
        } else {
            UpdateResult result = res.result();
            if (result.getUpdated() > 0) {
                fut.handle(new Success<>());
            } else {
                fut.handle(new Failure<>(NOT_FOUND, id));
            }
            q.close();
        }
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) UpdateResult(io.vertx.ext.sql.UpdateResult)

Example 2 with UpdateResult

use of io.vertx.ext.sql.UpdateResult 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 UpdateResult

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

the class Client method start.

@Override
@Suspendable
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);
    try (SQLConnection conn = awaitResult(jdbc::getConnection)) {
        // Create a table
        Void v = awaitResult(h -> conn.execute("CREATE TABLE test(col VARCHAR(20))", h));
        // Insert some stuff
        for (int i = 0; i < 10; i++) {
            int ii = i;
            UpdateResult res = awaitResult(h -> conn.update("INSERT INTO test (col) VALUES ('val" + ii + "')", h));
            System.out.println("Rows updated: " + res.getUpdated());
        }
        // Select the results
        ResultSet res = awaitResult(h -> conn.query("SELECT * FROM test", h));
        System.out.println("Selected " + res.getNumRows() + " results");
        res.getResults().forEach(System.out::println);
    }
}
Also used : SQLConnection(io.vertx.ext.sql.SQLConnection) ResultSet(io.vertx.ext.sql.ResultSet) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.ext.jdbc.JDBCClient) UpdateResult(io.vertx.ext.sql.UpdateResult) Suspendable(co.paralleluniverse.fibers.Suspendable)

Example 4 with UpdateResult

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

the class UpdateWithParamsTest method handle.

@Override
public void handle(RoutingContext rc) {
    jdbcClient.getConnection(ar -> {
        if (ar.failed()) {
            TestUtil.fail(rc, ar.cause());
            return;
        }
        SQLConnection connection = ar.result();
        rc.response().bodyEndHandler(v -> {
            connection.close();
        });
        connection.updateWithParams("update car set name = ? where name = ?", new JsonArray().add("chick hicks").add("martin"), ures -> {
            if (ures.failed()) {
                TestUtil.fail(rc, ures.cause());
                return;
            }
            UpdateResult updateResult = ures.result();
            if (updateResult.getUpdated() != 1) {
                TestUtil.fail(rc, String.valueOf(updateResult.getUpdated()));
            } else {
                rc.response().setStatusCode(200).end();
            }
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) SQLConnection(io.vertx.ext.sql.SQLConnection) UpdateResult(io.vertx.ext.sql.UpdateResult)

Aggregations

UpdateResult (io.vertx.ext.sql.UpdateResult)4 JsonArray (io.vertx.core.json.JsonArray)3 SQLConnection (io.vertx.ext.sql.SQLConnection)3 ResultSet (io.vertx.ext.sql.ResultSet)2 Suspendable (co.paralleluniverse.fibers.Suspendable)1 JsonObject (io.vertx.core.json.JsonObject)1 JDBCClient (io.vertx.ext.jdbc.JDBCClient)1