package io.vertx.example.sqlclient.transaction_rollback;

import io.vertx.core.AbstractVerticle;
import io.vertx.example.util.Runner;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.Row;

import java.util.ArrayList;
import java.util.List;

/*
 * @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
 */
public class SqlClientExample extends AbstractVerticle {

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

  @Override
  public void start() {

    Pool pool = PgPool.pool(vertx, new PgConnectOptions()
      .setPort(5432)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret"), new PoolOptions().setMaxSize(4));

    // Uncomment for MySQL
//    Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
//      .setPort(5432)
//      .setHost("the-host")
//      .setDatabase("the-db")
//      .setUser("user")
//      .setPassword("secret"), new PoolOptions().setMaxSize(4));

    pool.getConnection().compose(connection -> {
      return connection.begin()
        .compose(tx -> {
          // create a test table
          return connection.query("create table test(id int primary key, name varchar(255))").execute()
            .compose(v -> {
              // insert some test data
              return connection.query("insert into test values (1, 'Hello'), (2, 'World')").execute();
            })
            .compose(v -> {
              // rollback transaction
              return tx.rollback();
            })
            .compose(v -> {
              // query some data
              return connection.query("select * from test").execute();
            })
            .onSuccess(rows -> {
              for (Row row : rows) {
                System.out.println("row = " + row.toString());
              }
            });
        })
        .onComplete(v -> {
          // and close the connection
          connection.close();
        });
    }).onComplete(ar -> {
      if (ar.succeeded()) {
        System.out.println("done");
      } else {
        ar.cause().printStackTrace();
      }
    });
  }
}

@@ -90,16 +90,45 @@
 rticle;%0A
+import io.vertx.core.Future;%0A
 import i
@@ -339,60 +339,8 @@
 w;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /*%0A 
@@ -1152,109 +1152,40 @@
 ool.
-getConnection().compose(connection -%3E %7B%0A      return connection.begin()%0A        .compose(tx -%3E %7B%0A    
+withTransaction(connection -%3E %7B%0A
     
@@ -1209,20 +1209,16 @@
 t table%0A
-    
       re
@@ -1309,36 +1309,32 @@
 ecute()%0A        
-    
 .compose(v -%3E %7B%0A
@@ -1343,20 +1343,16 @@
         
- 
-   
 // inser
@@ -1364,28 +1364,24 @@
 e test data%0A
-    
           re
@@ -1471,35 +1471,27 @@
 ();%0A        
-   
- 
 %7D)%0A
-    
         .com
@@ -1516,23 +1516,19 @@
     
-    // rollback
+// Triggers
  tra
@@ -1531,29 +1531,35 @@
  transaction
-%0A    
+ roll back%0A
           re
@@ -1567,20 +1567,36 @@
 urn 
-tx.rollback(
+Future.failedFuture(%22Failed%22
 );%0A 
@@ -1594,36 +1594,32 @@
 iled%22);%0A        
-    
 %7D)%0A            .
@@ -1609,28 +1609,24 @@
  %7D)%0A        
- 
-   
 .compose(v -
@@ -1625,28 +1625,24 @@
 pose(v -%3E %7B%0A
-    
           //
@@ -1664,28 +1664,24 @@
 a%0A          
-   
- 
 return conne
@@ -1729,35 +1729,27 @@
 ();%0A        
-    %7D)%0A    
+%7D)%0A
         .onS
@@ -1775,20 +1775,16 @@
         
- 
-   
 for (Row
@@ -1794,28 +1794,24 @@
 w : rows) %7B%0A
-    
             
@@ -1871,135 +1871,9 @@
     
-   
- %7D%0A            %7D);%0A        %7D)%0A        .onComplete(v -%3E %7B%0A          // and close the connection%0A          connection.close();
+%7D
 %0A   

Update to use withTransaction for example