private void execute(SQLConnection conn, String sql, Handler<Void> done) { conn.execute(sql, res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(null); }); }
private void execute(SQLConnection conn, String sql, Handler<Void> done) { conn.execute(sql, res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(null); }); }
private void setUpInitialData(Handler<Void> done) { client.getConnection(res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } final SQLConnection conn = res.result(); conn.execute("CREATE TABLE IF NOT EXISTS products(id INT IDENTITY, name VARCHAR(255), price FLOAT, weight INT)", ddl -> { if (ddl.failed()) { throw new RuntimeException(ddl.cause()); } conn.execute("INSERT INTO products (name, price, weight) VALUES ('Egg Whisk', 3.99, 150), ('Tea Cosy', 5.99, 100), ('Spatula', 1.00, 80)", fixtures -> { if (fixtures.failed()) { throw new RuntimeException(fixtures.cause()); } done.handle(null); }); }); }); } }
@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); // Get a connection 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); } }
/** * Executes the given SQL statement * @param sql the SQL to execute. For example <code>CREATE TABLE IF EXISTS table ...</code> * @param resultHandler the handler which is called once this operation completes. * @return */ public io.vertx.rxjava.ext.sql.SQLConnection execute(String sql, Handler<AsyncResult<Void>> resultHandler) { delegate.execute(sql, resultHandler); return this; }
private void setupAutoIncrementTable(SQLConnection conn, Handler<AsyncResult<Void>> handler) { conn.execute("BEGIN", ar -> conn.execute("DROP TABLE IF EXISTS test_table", ar2 -> conn.execute("CREATE TABLE test_table (id BIGSERIAL, name VARCHAR(255), PRIMARY KEY(id))", ar3 -> conn.execute("COMMIT", handler::handle)))); }
@Override public SQLConnection execute(String sql, Handler<AsyncResult<Void>> resultHandler) { delegate.execute(sql, resultHandler); return this; }
private void setupTableWithUUIDs(SQLConnection conn, Handler<AsyncResult<Void>> handler) { conn.execute("BEGIN", ar -> conn.execute("DROP TABLE IF EXISTS test_table", ar2 -> conn.execute("CREATE TABLE test_table (some_uuid UUID, name VARCHAR(255))", ar3 -> conn.execute("COMMIT", handler::handle)))); } }
private void setupAutoIncrementTable(SQLConnection conn, Handler<AsyncResult<Void>> handler) { conn.execute("BEGIN", ar -> conn.execute("DROP TABLE IF EXISTS test_table", ar2 -> conn.execute("CREATE TABLE test_table (id BIGINT AUTO_INCREMENT, name VARCHAR(255), PRIMARY KEY(id))", ar3 -> conn.execute("COMMIT", handler::handle)))); } }
private void setupTestTable(SQLConnection conn, Supplier<String> idNameValuesSupplier, Handler<AsyncResult<Void>> handler) { conn.execute("BEGIN", ar -> conn.execute("DROP TABLE IF EXISTS test_table", ar2 -> conn.execute(CREATE_TABLE_STATEMENT, ar3 -> conn.update("INSERT INTO test_table (id, name) VALUES " + idNameValuesSupplier.get(), ar4 -> conn.execute("COMMIT", handler))))); }
private void setupTestTable(SQLConnection conn, Supplier<String> idNameValuesSupplier, Handler<AsyncResult<Void>> handler) { conn.execute("BEGIN", ar -> conn.execute("DROP TABLE IF EXISTS test_table", ar2 -> conn.execute(CREATE_TABLE_STATEMENT, ar3 -> conn.update("INSERT INTO test_table (id, name) VALUES " + idNameValuesSupplier.get(), ar4 -> conn.execute("COMMIT", handler))))); }
@Override protected void setSqlModeIfPossible(Handler<Void> handler) { conn.execute("set SQL_MODE = 'STRICT_ALL_TABLES'", ar1 -> { // INFO: we ignore the result of this call because it is a mysql specific feature and not all versions support it // what is means is that we want the sql parser to be strict even if the engine e.g.: myisam does not implement // all constraints such as is the date Feb 31 a valid date. By specifying this we will tell for example that the // previous date is invalid. handler.handle(null); }); }
@Override public void initializePersistence(Handler<AsyncResult<Void>> resultHandler) { jdbc.getConnection(connHandler(resultHandler, connection -> { connection.execute(CREATE_STATEMENT, r -> { resultHandler.handle(r); connection.close(); }); })); }
@Override public ProductService initializePersistence(Handler<AsyncResult<Void>> resultHandler) { client.getConnection(connHandler(resultHandler, connection -> { connection.execute(CREATE_STATEMENT, r -> { resultHandler.handle(r); connection.close(); }); })); return this; }
@Override public AccountService initializePersistence(Handler<AsyncResult<Void>> resultHandler) { client.getConnection(connHandler(resultHandler, connection -> { connection.execute(CREATE_STATEMENT, r -> { resultHandler.handle(r); connection.close(); }); })); return this; }
@Override public OrderService initializePersistence(Handler<AsyncResult<Void>> resultHandler) { client.getConnection(connHandler(resultHandler, connection -> { connection.execute(CREATE_STATEMENT, r -> { resultHandler.handle(r); connection.close(); }); })); return this; }