package io.vertx.example.reactivex.database.jdbc;

import io.vertx.core.json.JsonObject;
import io.vertx.example.util.Runner;
import io.vertx.ext.sql.ResultSet;
import io.vertx.reactivex.core.AbstractVerticle;
import io.vertx.reactivex.ext.jdbc.JDBCClient;
import io.reactivex.Single;

/*
 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
 */
public class Client extends AbstractVerticle {

  // Convenience method so you can run it in your IDE
  public static void main(String[] args) {
    Runner.runExample(Client.class);
  }

  @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();
    });
  }
}

@@ -44,16 +44,56 @@
 .jdbc;%0A%0A
+import io.reactivex.functions.Function;%0A
 import i
@@ -179,25 +179,37 @@
 rtx.
-ext.sql.ResultSet
+reactivex.jdbcclient.JDBCPool
 ;%0Aim
@@ -269,42 +269,108 @@
  io.
-vertx.reactivex.ext.jdbc.JDBCClien
+reactivex.Single;%0Aimport io.vertx.reactivex.sqlclient.Row;%0Aimport io.vertx.reactivex.sqlclient.RowSe
 t;%0Ai
@@ -370,32 +370,38 @@
 wSet;%0Aimport io.
+vertx.
 reactivex.Single
@@ -394,22 +394,81 @@
 activex.
-Single
+sqlclient.SqlConnection;%0Aimport org.jetbrains.annotations.NotNull
 ;%0A%0A/*%0A *
@@ -932,245 +932,176 @@
 JDBC
-Client jdbc = JDBCClient.createShared(vertx, config);%0A%0A    // Connect to the database%0A    jdbc.rxGetConnection().flatMap(conn -%3E %7B%0A%0A      // Now chain some statements using flatmap composition%0A      Single%3CResultSet%3E resa = conn.rxUpdate
+Pool pool = JDBCPool.pool(vertx, config);%0A%0A    Single%3CRowSet%3CRow%3E%3E resa = pool.rxWithConnection((Function%3CSqlConnection, Single%3CRowSet%3CRow%3E%3E%3E) conn -%3E conn%0A      .query
 (%22CR
@@ -1133,24 +1133,41 @@
 AR(20))%22)%0A  
+    .rxExecute()%0A
       .flatM
@@ -1164,35 +1164,32 @@
     .flatMap(res
-ult
  -%3E conn.rxUpdat
@@ -1177,32 +1177,29 @@
 res -%3E conn.
-rxUpdate
+query
 (%22INSERT INT
@@ -1224,28 +1224,38 @@
 S ('val1')%22)
+.rxExecute()
 )%0A
-  
       .flatM
@@ -1252,35 +1252,32 @@
     .flatMap(res
-ult
  -%3E conn.rxUpdat
@@ -1273,16 +1273,13 @@
 onn.
-rxUpdate
+query
 (%22IN
@@ -1316,20 +1316,30 @@
 val2')%22)
+.rxExecute()
 )%0A
-  
       .f
@@ -1348,19 +1348,16 @@
 tMap(res
-ult
  -%3E conn
@@ -1361,11 +1361,9 @@
 onn.
-rxQ
+q
 uery
@@ -1384,24 +1384,37 @@
 M test%22)
+.rxExecute())
 );%0A%0A    
   return
@@ -1409,60 +1409,43 @@
     
- 
- return resa.doAfterTerminate(conn::close);%0A
+// Connect to the database
 %0A    
-%7D)
+resa
 .sub
@@ -1558,18 +1558,8 @@
 tSet
-.getRows()
 );%0A 

Use SQL client for some examples