Search in sources :

Example 6 with JDBCClient

use of io.vertx.ext.jdbc.JDBCClient in project vertx-auth by vert-x3.

the class JDBCAuthOptions method createProvider.

@Override
public JDBCAuth createProvider(Vertx vertx) {
    JDBCClient client;
    if (shared) {
        if (datasourceName != null) {
            client = JDBCClient.createShared(vertx, config, datasourceName);
        } else {
            client = JDBCClient.createShared(vertx, config);
        }
    } else {
        client = JDBCClient.createNonShared(vertx, config);
    }
    JDBCAuth auth = JDBCAuth.create(vertx, client);
    if (authenticationQuery != null) {
        auth.setAuthenticationQuery(authenticationQuery);
    }
    if (rolesQuery != null) {
        auth.setRolesQuery(rolesQuery);
    }
    if (permissionsQuery != null) {
        auth.setPermissionsQuery(permissionsQuery);
    }
    if (rolesPrefix != null) {
        auth.setRolePrefix(rolesPrefix);
    }
    return auth;
}
Also used : JDBCClient(io.vertx.ext.jdbc.JDBCClient)

Example 7 with JDBCClient

use of io.vertx.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.

the class JDBCExample method start.

@Override
public void start() throws Exception {
    final JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver").put("max_pool_size", 30).put("user", "SA").put("password", ""));
    client.getConnection(conn -> {
        if (conn.failed()) {
            System.err.println(conn.cause().getMessage());
            return;
        }
        final SQLConnection connection = conn.result();
        // create a test table
        connection.execute("create table test(id int primary key, name varchar(255))", create -> {
            if (create.failed()) {
                System.err.println("Cannot create the table");
                create.cause().printStackTrace();
                return;
            }
            // insert some test data
            connection.execute("insert into test values (1, 'Hello'), (2, 'World')", insert -> {
                // query some data with arguments
                connection.queryWithParams("select * from test where id = ?", new JsonArray().add(2), rs -> {
                    if (rs.failed()) {
                        System.err.println("Cannot retrieve the data from the database");
                        rs.cause().printStackTrace();
                        return;
                    }
                    for (JsonArray line : rs.result().getResults()) {
                        System.out.println(line.encode());
                    }
                    // and close the connection
                    connection.close(done -> {
                        if (done.failed()) {
                            throw new RuntimeException(done.cause());
                        }
                    });
                });
            });
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) SQLConnection(io.vertx.ext.sql.SQLConnection) JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject)

Example 8 with JDBCClient

use of io.vertx.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.

the class JDBCExample method start.

@Override
public void start() throws Exception {
    final JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver").put("max_pool_size", 30).put("user", "SA").put("password", ""));
    client.getConnection(conn -> {
        if (conn.failed()) {
            System.err.println(conn.cause().getMessage());
            return;
        }
        final SQLConnection connection = conn.result();
        connection.execute("create table test(id int primary key, name varchar(255))", res -> {
            if (res.failed()) {
                throw new RuntimeException(res.cause());
            }
            // insert some test data
            connection.execute("insert into test values (1, 'Hello'), (2, 'Goodbye'), (3, 'Cya Later')", insert -> {
                // query some data
                connection.queryStream("select * from test", stream -> {
                    if (stream.succeeded()) {
                        SQLRowStream sqlRowStream = stream.result();
                        sqlRowStream.handler(row -> {
                            // do something with the row...
                            System.out.println(row.encode());
                        }).endHandler(v -> {
                            // no more data available, close the connection
                            connection.close(done -> {
                                if (done.failed()) {
                                    throw new RuntimeException(done.cause());
                                }
                            });
                        });
                    }
                });
            });
        });
    });
}
Also used : JDBCClient(io.vertx.ext.jdbc.JDBCClient) SQLRowStream(io.vertx.ext.sql.SQLRowStream) AbstractVerticle(io.vertx.core.AbstractVerticle) SQLConnection(io.vertx.ext.sql.SQLConnection) JsonObject(io.vertx.core.json.JsonObject) Runner(io.vertx.example.util.Runner) SQLConnection(io.vertx.ext.sql.SQLConnection) JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject) SQLRowStream(io.vertx.ext.sql.SQLRowStream)

Example 9 with JDBCClient

use of io.vertx.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.

the class JDBCExample method start.

@Override
public void start() throws Exception {
    final JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver").put("max_pool_size", 30).put("user", "SA").put("password", ""));
    client.getConnection(conn -> {
        if (conn.failed()) {
            System.err.println(conn.cause().getMessage());
            return;
        }
        // create a test table
        execute(conn.result(), "create table test(id int primary key, name varchar(255))", create -> {
            // start a transaction
            startTx(conn.result(), beginTrans -> {
                // insert some test data
                execute(conn.result(), "insert into test values(1, 'Hello')", insert -> {
                    // commit data
                    endTx(conn.result(), commitTrans -> {
                        // query some data
                        query(conn.result(), "select count(*) from test", rs -> {
                            for (JsonArray line : rs.getResults()) {
                                System.out.println(line.encode());
                            }
                            // and close the connection
                            conn.result().close(done -> {
                                if (done.failed()) {
                                    throw new RuntimeException(done.cause());
                                }
                            });
                        });
                    });
                });
            });
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject)

Example 10 with JDBCClient

use of io.vertx.ext.jdbc.JDBCClient 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)

Aggregations

JDBCClient (io.vertx.ext.jdbc.JDBCClient)11 JsonObject (io.vertx.core.json.JsonObject)9 JsonArray (io.vertx.core.json.JsonArray)5 SQLConnection (io.vertx.ext.sql.SQLConnection)5 Suspendable (co.paralleluniverse.fibers.Suspendable)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 Runner (io.vertx.example.util.Runner)1 AuthProvider (io.vertx.ext.auth.AuthProvider)1 JDBCAuth (io.vertx.ext.auth.jdbc.JDBCAuth)1 ResultSet (io.vertx.ext.sql.ResultSet)1 SQLRowStream (io.vertx.ext.sql.SQLRowStream)1 UpdateResult (io.vertx.ext.sql.UpdateResult)1 Router (io.vertx.ext.web.Router)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 TestUtil (io.vertx.openshift.jdbc.TestUtil)1 SQLException (java.sql.SQLException)1 PGPoolingDataSource (org.postgresql.ds.PGPoolingDataSource)1