use of io.vertx.sqlclient.SqlConnection in project vertx-examples by vert-x3.
the class SqlClientExample method start.
@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(res1 -> {
if (res1.failed()) {
System.err.println(res1.cause().getMessage());
return;
}
SqlConnection connection = res1.result();
// create a test table
connection.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
if (res2.failed()) {
connection.close();
System.err.println("Cannot create the table");
res2.cause().printStackTrace();
return;
}
// insert some test data
connection.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
// query some data with arguments
connection.preparedQuery("select * from test where id = ?").execute(Tuple.of(2), rs -> {
if (rs.failed()) {
System.err.println("Cannot retrieve the data from the database");
rs.cause().printStackTrace();
return;
}
for (Row line : rs.result()) {
System.out.println("" + line);
}
// and close the connection
connection.close();
});
});
});
});
}
use of io.vertx.sqlclient.SqlConnection in project raml-module-builder by folio-org.
the class PostgresClientIT method saveBatchJsonConnection.
@Test
public void saveBatchJsonConnection(TestContext context) {
String id = randomUuid();
JsonArray update = new JsonArray().add(new JsonObject().put("id", id).put("key", "y").encode());
createFoo(context).save(FOO, id, new StringPojo("x"), context.asyncAssertSuccess(x -> {
postgresClient.getSQLConnection(sqlConnection -> {
postgresClient.saveBatch(sqlConnection, FOO, update, context.asyncAssertFailure(e -> {
postgresClient.upsertBatch(sqlConnection, FOO, update, context.asyncAssertSuccess(rowSet -> {
postgresClient.getById(FOO, id, context.asyncAssertSuccess(json -> {
assertThat(rowSet.iterator().next().getUUID(0).toString(), is(id));
assertThat(json.getString("key"), is("y"));
}));
}));
}));
});
}));
}
use of io.vertx.sqlclient.SqlConnection in project raml-module-builder by folio-org.
the class PostgresClientIT method postgresClientConnectionThrowsException.
/**
* @return a PostgresClient where invoking SQLConnection::update(...) or SQLConnection::updateWithParams
* throws an RuntimeException.
*/
private PostgresClient postgresClientConnectionThrowsException() {
PgConnection pgConnection = new PgConnection() {
@Override
public PgConnection notificationHandler(Handler<PgNotification> handler) {
throw new RuntimeException();
}
@Override
public PgConnection cancelRequest(Handler<AsyncResult<Void>> handler) {
throw new RuntimeException();
}
@Override
public int processId() {
throw new RuntimeException();
}
@Override
public int secretKey() {
throw new RuntimeException();
}
@Override
public PgConnection prepare(String s, Handler<AsyncResult<PreparedStatement>> handler) {
throw new RuntimeException();
}
@Override
public Future<PreparedStatement> prepare(String s) {
return null;
}
@Override
public SqlConnection prepare(String sql, PrepareOptions options, Handler<AsyncResult<PreparedStatement>> handler) {
return null;
}
@Override
public Future<PreparedStatement> prepare(String s, PrepareOptions prepareOptions) {
return null;
}
@Override
public PgConnection exceptionHandler(Handler<Throwable> handler) {
return null;
}
@Override
public PgConnection closeHandler(Handler<Void> handler) {
return null;
}
@Override
public void begin(Handler<AsyncResult<Transaction>> handler) {
}
@Override
public Future<Transaction> begin() {
return null;
}
@Override
public boolean isSSL() {
return false;
}
@Override
public void close(Handler<AsyncResult<Void>> handler) {
close().onComplete(handler);
}
@Override
public Future<Void> close() {
return Future.succeededFuture();
}
@Override
public Query<RowSet<Row>> query(String s) {
throw new RuntimeException();
}
@Override
public PreparedQuery<RowSet<Row>> preparedQuery(String s, PrepareOptions options) {
throw new RuntimeException();
}
@Override
public PreparedQuery<RowSet<Row>> preparedQuery(String s) {
throw new RuntimeException();
}
@Override
public DatabaseMetadata databaseMetadata() {
throw new RuntimeException();
}
};
PgPool client = new PgPoolBase() {
@Override
public Future<SqlConnection> getConnection() {
return Future.succeededFuture(pgConnection);
}
};
try {
PostgresClient postgresClient = new PostgresClient(vertx, TENANT);
postgresClient.setClient(client);
return postgresClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of io.vertx.sqlclient.SqlConnection in project raml-module-builder by folio-org.
the class PostgresClientIT method postgresClientGetConnectionFails.
/**
* @return a PostgresClient where getConnection(handler) invokes the handler with a failure.
*/
private PostgresClient postgresClientGetConnectionFails() {
PgPool client = new PgPoolBase() {
@Override
public Future<SqlConnection> getConnection() {
return Future.failedFuture("postgresClientGetConnectionFails");
}
};
try {
PostgresClient postgresClient = new PostgresClient(vertx, TENANT);
postgresClient.setClient(client);
return postgresClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of io.vertx.sqlclient.SqlConnection in project raml-module-builder by folio-org.
the class PostgresClientIT method postgresClientQueryReturnBadResults.
/**
* @return a PostgresClient where invoking SQLConnection::queryWithParams will return null ResultSet
*/
private PostgresClient postgresClientQueryReturnBadResults() {
PgConnection pgConnection = new PgConnection() {
@Override
public PgConnection notificationHandler(Handler<PgNotification> handler) {
return null;
}
@Override
public PgConnection cancelRequest(Handler<AsyncResult<Void>> handler) {
return this;
}
@Override
public int processId() {
return 0;
}
@Override
public int secretKey() {
return 0;
}
@Override
public PgConnection prepare(String s, Handler<AsyncResult<PreparedStatement>> handler) {
return null;
}
@Override
public Future<PreparedStatement> prepare(String s) {
return null;
}
@Override
public SqlConnection prepare(String sql, PrepareOptions options, Handler<AsyncResult<PreparedStatement>> handler) {
return null;
}
@Override
public Future<PreparedStatement> prepare(String sql, PrepareOptions options) {
return null;
}
@Override
public PgConnection exceptionHandler(Handler<Throwable> handler) {
return null;
}
@Override
public PgConnection closeHandler(Handler<Void> handler) {
return null;
}
@Override
public void begin(Handler<AsyncResult<Transaction>> handler) {
}
@Override
public Future<Transaction> begin() {
return null;
}
@Override
public boolean isSSL() {
return false;
}
@Override
public void close(Handler<AsyncResult<Void>> handler) {
}
@Override
public Query<RowSet<Row>> query(String s) {
return null;
}
@Override
public PreparedQuery<RowSet<Row>> preparedQuery(String s) {
return null;
}
@Override
public PreparedQuery<RowSet<Row>> preparedQuery(String sql, PrepareOptions options) {
return preparedQuery(sql);
}
@Override
public Future<Void> close() {
return null;
}
@Override
public DatabaseMetadata databaseMetadata() {
return null;
}
};
PgPool client = new PgPoolBase() {
@Override
public Future<SqlConnection> getConnection() {
return Future.succeededFuture(pgConnection);
}
};
try {
PostgresClient postgresClient = new PostgresClient(vertx, TENANT);
postgresClient.setClient(client);
return postgresClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations