Java Code Examples for io.vertx.sqlclient.SqlConnection

The following examples show how to use io.vertx.sqlclient.SqlConnection. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void queries10(SqlConnection sqlConnection) {
  sqlConnection
    .prepare("SELECT * FROM users WHERE id = $1", ar -> {
      if (ar.succeeded()) {
        PreparedStatement preparedStatement = ar.result();
        preparedStatement.query()
          .execute(Tuple.of("julien"), ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();
              System.out.println("Got " + rows.size() + " rows ");
              preparedStatement.close();
            } else {
              System.out.println("Failure: " + ar2.cause().getMessage());
            }
          });
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
}
 
Example 2
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingConnections01(Vertx vertx, Pool pool) {

    pool.getConnection(ar1 -> {
      if (ar1.succeeded()) {
        SqlConnection connection = ar1.result();

        connection
          .query("SELECT * FROM users WHERE id='julien'")
          .execute(ar2 -> {
          if (ar1.succeeded()) {
            connection
              .query("SELECT * FROM users WHERE id='paulo'")
              .execute(ar3 -> {
              // Do something with rows and return the connection to the pool
              connection.close();
            });
          } else {
            // Return the connection to the pool
            connection.close();
          }
        });
      }
    });
  }
 
Example 3
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingConnections03(SqlConnection connection) {
  connection.prepare("INSERT INTO USERS (id, name) VALUES ($1, $2)", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement prepared = ar1.result();

      // Create a query : bind parameters
      List<Tuple> batch = new ArrayList();

      // Add commands to the createBatch
      batch.add(Tuple.of("julien", "Julien Viet"));
      batch.add(Tuple.of("emad", "Emad Alblueshi"));

      prepared.query().executeBatch(batch, res -> {
        if (res.succeeded()) {

          // Process rows
          RowSet<Row> rows = res.result();
        } else {
          System.out.println("Batch failed " + res.cause());
        }
      });
    }
  });
}
 
Example 4
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void queries10(SqlConnection sqlConnection) {
  sqlConnection
    .prepare("SELECT * FROM users WHERE id= ?", ar -> {
      if (ar.succeeded()) {
        PreparedStatement preparedStatement = ar.result();
        preparedStatement.query()
          .execute(Tuple.of("julien"), ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();
              System.out.println("Got " + rows.size() + " rows ");
              preparedStatement.close();
            } else {
              System.out.println("Failure: " + ar2.cause().getMessage());
            }
          });
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
}
 
Example 5
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingConnections01(Vertx vertx, Pool pool) {

    pool.getConnection(ar1 -> {
      if (ar1.succeeded()) {
        SqlConnection connection = ar1.result();

        connection
          .query("SELECT * FROM users WHERE id='andy'")
          .execute(ar2 -> {
          if (ar1.succeeded()) {
            connection
              .query("SELECT * FROM users WHERE id='julien'")
              .execute(ar3 -> {
              // Do something with rows and return the connection to the pool
              connection.close();
            });
          } else {
            // Return the connection to the pool
            connection.close();
          }
        });
      }
    });
  }
 
Example 6
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingConnections03(SqlConnection connection) {
  connection.prepare("INSERT INTO USERS (id, name) VALUES ($1, $2)", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement prepared = ar1.result();

      // Create a query : bind parameters
      List<Tuple> batch = new ArrayList();

      // Add commands to the createBatch
      batch.add(Tuple.of("julien", "Julien Viet"));
      batch.add(Tuple.of("emad", "Emad Alblueshi"));
      batch.add(Tuple.of("andy", "Andy Guibert"));

      prepared.query().executeBatch(batch, res -> {
        if (res.succeeded()) {

          // Process rows
          RowSet<Row> rows = res.result();
        } else {
          System.out.println("Batch failed " + res.cause());
        }
      });
    }
  });
}
 
Example 7
Source Project: vertx-sql-client   Source File: DB2SimpleQueryUriTest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void initConnector() {
  String uri = "db2://"+rule.options().getUser()+":"
                       +rule.options().getPassword()+"@"
                       +rule.options().getHost()+":"
                       +rule.options().getPort()+"/"
                       +rule.options().getDatabase();

  connector = new Connector<SqlConnection>() {
    @Override
    public void connect(Handler<AsyncResult<SqlConnection>> handler) {
      DB2Connection.connect(vertx, uri, ar -> {
        if (ar.succeeded()) {
          handler.handle(Future.succeededFuture(ar.result()));
        } else {
          handler.handle(Future.failedFuture(ar.cause()));
        }
      });
    }

    @Override
    public void close() {
    }
  };
}
 
Example 8
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void queries10(SqlConnection sqlConnection) {
  sqlConnection
    .prepare("SELECT * FROM users WHERE id=$1", ar -> {
      if (ar.succeeded()) {
        PreparedStatement preparedStatement = ar.result();
        preparedStatement.query()
          .execute(Tuple.of("julien"), ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();
              System.out.println("Got " + rows.size() + " rows ");
              preparedStatement.close();
            } else {
              System.out.println("Failure: " + ar2.cause().getMessage());
            }
          });
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
}
 
Example 9
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingConnections01(Vertx vertx, Pool pool) {

    pool.getConnection(ar1 -> {
      if (ar1.succeeded()) {
        SqlConnection connection = ar1.result();

        connection
          .query("SELECT * FROM users WHERE id='julien'")
          .execute(ar2 -> {
          if (ar1.succeeded()) {
            connection
              .query("SELECT * FROM users WHERE id='paulo'")
              .execute(ar3 -> {
              // Do something with rows and return the connection to the pool
              connection.close();
            });
          } else {
            // Return the connection to the pool
            connection.close();
          }
        });
      }
    });
  }
 
Example 10
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingConnections03(SqlConnection connection) {
  connection.prepare("INSERT INTO USERS (id, name) VALUES ($1, $2)", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement prepared = ar1.result();

      // Create a query : bind parameters
      List<Tuple> batch = new ArrayList();

      // Add commands to the createBatch
      batch.add(Tuple.of("julien", "Julien Viet"));
      batch.add(Tuple.of("emad", "Emad Alblueshi"));

      prepared.query().executeBatch(batch, res -> {
        if (res.succeeded()) {

          // Process rows
          RowSet<Row> rows = res.result();
        } else {
          System.out.println("Batch failed " + res.cause());
        }
      });
    }
  });
}
 
Example 11
Source Project: vertx-sql-client   Source File: PoolBase.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Future<SqlConnection> getConnection() {
  ContextInternal current = vertx.getOrCreateContext();
  Object metric;
  if (metrics != null) {
    metric = metrics.enqueueRequest();
  } else {
    metric = null;
  }
  Promise<Connection> promise = current.promise();
  acquire(promise);
  if (metrics != null) {
    promise.future().onComplete(ar -> {
      metrics.dequeueRequest(metric);
    });
  }
  return promise.future().map(conn -> {
    SqlConnectionImpl wrapper = wrap(current, conn);
    conn.init(wrapper);
    return wrapper;
  });
}
 
Example 12
Source Project: vertx-sql-client   Source File: TransactionTestBase.java    License: Apache License 2.0 6 votes vote down vote up
protected void initConnector() {
  connector = handler -> {
    Pool pool = getPool();
    pool.getConnection(ar1 -> {
      if (ar1.succeeded()) {
        SqlConnection conn = ar1.result();
        conn.begin(ar2 -> {
          if (ar2.succeeded()) {
            Transaction tx = ar2.result();
            tx.completion().onComplete(ar3 -> {
              conn.close();
            });
            handler.handle(Future.succeededFuture(new Result(conn, tx)));
          } else {
            conn.close();
          }
        });
      } else {
        handler.handle(ar1.mapEmpty());
      }
    });
  };
}
 
Example 13
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void queries10(SqlConnection sqlConnection) {
  sqlConnection
    .prepare("SELECT * FROM users WHERE id = ?", ar -> {
      if (ar.succeeded()) {
        PreparedStatement preparedStatement = ar.result();
        preparedStatement.query()
          .execute(Tuple.of("julien"), ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();
              System.out.println("Got " + rows.size() + " rows ");
              preparedStatement.close();
            } else {
              System.out.println("Failure: " + ar2.cause().getMessage());
            }
          });
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }
    });
}
 
Example 14
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingConnections01(Vertx vertx, Pool pool) {

    pool.getConnection(ar1 -> {
      if (ar1.succeeded()) {
        SqlConnection connection = ar1.result();

        connection
          .query("SELECT * FROM users WHERE id='julien'")
          .execute(ar2 -> {
          if (ar1.succeeded()) {
            connection
              .query("SELECT * FROM users WHERE id='paulo'")
              .execute(ar3 -> {
              // Do something with rows and return the connection to the pool
              connection.close();
            });
          } else {
            // Return the connection to the pool
            connection.close();
          }
        });
      }
    });
  }
 
Example 15
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingConnections03(SqlConnection connection) {
  connection.prepare("INSERT INTO USERS (id, name) VALUES (?, ?)", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement prepared = ar1.result();

      // Create a query : bind parameters
      List<Tuple> batch = new ArrayList();

      // Add commands to the createBatch
      batch.add(Tuple.of("julien", "Julien Viet"));
      batch.add(Tuple.of("emad", "Emad Alblueshi"));

      prepared.query().executeBatch(batch, res -> {
        if (res.succeeded()) {

          // Process rows
          RowSet<Row> rows = res.result();
        } else {
          System.out.println("Batch failed " + res.cause());
        }
      });
    }
  });
}
 
Example 16
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE age > ?", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();

      // Create a cursor
      Cursor cursor = pq.cursor(Tuple.of(18));

      // Read 50 rows
      cursor.read(50, ar2 -> {
        if (ar2.succeeded()) {
          RowSet<Row> rows = ar2.result();

          // Check for more ?
          if (cursor.hasMore()) {
            // Repeat the process...
          } else {
            // No more rows - close the cursor
            cursor.close();
          }
        }
      });
    }
  });
}
 
Example 17
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE age > ?", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();

      // Fetch 50 rows at a time
      RowStream<Row> stream = pq.createStream(50, Tuple.of(18));

      // Use the stream
      stream.exceptionHandler(err -> {
        System.out.println("Error: " + err.getMessage());
      });
      stream.endHandler(v -> {
        System.out.println("End of stream");
      });
      stream.handler(row -> {
        System.out.println("User: " + row.getString("last_name"));
      });
    }
  });
}
 
Example 18
SqlClientConnection(SqlConnection connection,
					boolean showSQL, boolean formatSQL,
					boolean usePostgresStyleParameters) {
	this.showSQL = showSQL;
	this.connection = connection;
	this.formatSQL = formatSQL;
	this.usePostgresStyleParameters = usePostgresStyleParameters;
}
 
Example 19
public SqlConnection createVertxConnection() {
	String url = getVertxUrl();
	CountDownLatch countDown = new CountDownLatch( 1 );
	AtomicReference<AsyncResult<SqlConnection>> result = new AtomicReference<>();
	MySQLPool.pool( url ).getConnection( ar -> {
		result.set( ar );
		countDown.countDown();
	});
	await(countDown);
	SqlConnection con = result.get().result();
	if (con != null)
		return con;
	else
		throw new ContainerLaunchException( "Failed to obtain a connection", result.get().cause() );
}
 
Example 20
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void usingConnections02(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();
      pq.query().execute(Tuple.of("julien"), ar2 -> {
        if (ar2.succeeded()) {
          // All rows
          RowSet<Row> rows = ar2.result();
        }
      });
    }
  });
}
 
Example 21
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Cursors require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Create a cursor
          Cursor cursor = pq.cursor(Tuple.of("julien"));

          // Read 50 rows
          cursor.read(50, ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();

              // Check for more ?
              if (cursor.hasMore()) {
                // Repeat the process...
              } else {
                // No more rows - commit the transaction
                tx.commit();
              }
            }
          });
        }
      });
    }
  });
}
 
Example 22
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Streams require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Fetch 50 rows at a time
          RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

          // Use the stream
          stream.exceptionHandler(err -> {
            System.out.println("Error: " + err.getMessage());
          });
          stream.endHandler(v -> {
            tx.commit();
            System.out.println("End of stream");
          });
          stream.handler(row -> {
            System.out.println("User: " + row.getString("last_name"));
          });
        }
      });
    }
  });
}
 
Example 23
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void usingConnections02(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();
      pq.query().execute(Tuple.of("andy"), ar2 -> {
        if (ar2.succeeded()) {
          // All rows
          RowSet<Row> rows = ar2.result();
        }
      });
    }
  });
}
 
Example 24
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Cursors require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Create a cursor
          Cursor cursor = pq.cursor(Tuple.of("julien"));

          // Read 50 rows
          cursor.read(50, ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();

              // Check for more ?
              if (cursor.hasMore()) {
                // Repeat the process...
              } else {
                // No more rows - commit the transaction
                tx.commit();
              }
            }
          });
        }
      });
    }
  });
}
 
Example 25
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Streams require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Fetch 50 rows at a time
          RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

          // Use the stream
          stream.exceptionHandler(err -> {
            System.out.println("Error: " + err.getMessage());
          });
          stream.endHandler(v -> {
            tx.commit();
            System.out.println("End of stream");
          });
          stream.handler(row -> {
            System.out.println("User: " + row.getString("last_name"));
          });
        }
      });
    }
  });
}
 
Example 26
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void usingConnections02(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
    if (ar1.succeeded()) {
      PreparedStatement pq = ar1.result();
      pq.query().execute(Tuple.of("julien"), ar2 -> {
        if (ar2.succeeded()) {
          // All rows
          RowSet<Row> rows = ar2.result();
        }
      });
    }
  });
}
 
Example 27
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Cursors require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Create a cursor
          Cursor cursor = pq.cursor(Tuple.of("julien"));

          // Read 50 rows
          cursor.read(50, ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();

              // Check for more ?
              if (cursor.hasMore()) {
                // Repeat the process...
              } else {
                // No more rows - commit the transaction
                tx.commit();
              }
            }
          });
        }
      });
    }
  });
}
 
Example 28
Source Project: vertx-sql-client   Source File: SqlClientExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Streams require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Fetch 50 rows at a time
          RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

          // Use the stream
          stream.exceptionHandler(err -> {
            System.out.println("Error: " + err.getMessage());
          });
          stream.endHandler(v -> {
            tx.commit();
            System.out.println("End of stream");
          });
          stream.handler(row -> {
            System.out.println("User: " + row.getString("last_name"));
          });
        }
      });
    }
  });
}
 
Example 29
Source Project: vertx-sql-client   Source File: PoolBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void getConnection(Handler<AsyncResult<SqlConnection>> handler) {
  Future<SqlConnection> fut = getConnection();
  if (handler != null) {
    fut.onComplete(handler);
  }
}
 
Example 30
Source Project: vertx-sql-client   Source File: MetricsTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPrepareAndQuery(TestContext ctx) {
  Function<SqlConnection, Future<?>> fn = conn -> conn
    .prepare("SELECT * FROM immutable WHERE id=1")
    .compose(ps -> ps.query().execute());
  testMetrics(ctx, false, fn);
}