use of io.vertx.sqlclient.SqlConnection in project raml-module-builder by folio-org.
the class PostgresClientTransactionsIT method postgresClientMonitor.
private PostgresClient postgresClientMonitor(AtomicInteger open, AtomicInteger active) {
try {
PostgresClient postgresClient = new PostgresClient(vertx, tenant);
PgPool ePool = postgresClient.getClient();
PgPool client = new PgPoolBase() {
@Override
public Future<SqlConnection> getConnection() {
return ePool.getConnection().map(conn -> new MonitorPgConnection((PgConnection) conn, open, active));
}
@Override
public Query<RowSet<Row>> query(String s) {
return ePool.query(s);
}
@Override
public PreparedQuery<RowSet<Row>> preparedQuery(String s) {
return ePool.preparedQuery(s);
}
@Override
public Future<Void> close() {
return ePool.close();
}
};
postgresClient.setClient(client);
return postgresClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of io.vertx.sqlclient.SqlConnection in project vertx-examples by vert-x3.
the class SqlClientExample method start.
@Override
public void start() throws Exception {
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.query("select * from test").execute(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 deleteByCqlWrapperThatThrowsException.
@Test
public void deleteByCqlWrapperThatThrowsException(TestContext context) {
CQLWrapper cqlWrapper = new CQLWrapper() {
@Override
public String getWhereClause() {
throw new RuntimeException("ping pong");
}
};
createFoo(context).getSQLConnection(asyncAssertTx(context, sqlConnection -> {
postgresClient.delete(sqlConnection, FOO, cqlWrapper, context.asyncAssertFailure(fail -> {
context.assertTrue(fail.getMessage().contains("ping pong"));
}));
}));
}
use of io.vertx.sqlclient.SqlConnection in project raml-module-builder by folio-org.
the class PostgresClientIT method postgresClientQueryFails.
/**
* @return a PostgresClient where invoking SQLConnection::update, SQLConnection::updateWithParams or
* SQLConnection::queryWithParams will report a failure via the resultHandler.
*/
private PostgresClient postgresClientQueryFails() {
PgConnection pgConnection = new PgConnection() {
@Override
public PgConnection notificationHandler(Handler<PgNotification> handler) {
return this;
}
@Override
public PgConnection cancelRequest(Handler<AsyncResult<Void>> handler) {
handler.handle(Future.failedFuture("cancelRequestFails"));
return this;
}
@Override
public int processId() {
return 0;
}
@Override
public int secretKey() {
return 0;
}
@Override
public PgConnection prepare(String s, Handler<AsyncResult<PreparedStatement>> handler) {
prepare(s).onComplete(handler);
return null;
}
@Override
public Future<PreparedStatement> prepare(String s) {
return Future.failedFuture("preparedFails");
}
@Override
public SqlConnection prepare(String sql, PrepareOptions options, Handler<AsyncResult<PreparedStatement>> handler) {
prepare(sql, options).onComplete(handler);
return null;
}
@Override
public Future<PreparedStatement> prepare(String sql, PrepareOptions options) {
return prepare(sql);
}
@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 new Query<RowSet<Row>>() {
@Override
public void execute(Handler<AsyncResult<RowSet<Row>>> handler) {
handler.handle(execute());
}
@Override
public Future<RowSet<Row>> execute() {
return Future.failedFuture("queryFails");
}
@Override
public <R> Query<SqlResult<R>> collecting(Collector<Row, ?, R> collector) {
return null;
}
@Override
public <U> Query<RowSet<U>> mapping(Function<Row, U> function) {
return null;
}
};
}
@Override
public PreparedQuery<RowSet<Row>> preparedQuery(String sql, PrepareOptions options) {
return preparedQuery(sql);
}
@Override
public PreparedQuery<RowSet<Row>> preparedQuery(String s) {
throw new RuntimeException("queryFails");
}
@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);
}
}
use of io.vertx.sqlclient.SqlConnection in project raml-module-builder by folio-org.
the class PostgresClient method getSQLConnection.
/**
* Don't forget to close the connection!
*
* <p>Use closeAndHandleResult as replyHandler, for example:
*
* <pre>getSQLConnection(timeout, conn -> execute(conn, sql, params, closeAndHandleResult(conn, replyHandler)))</pre>
*
* <p>Or avoid this method and use the preferred {@link #withConn(int, Function)}.
*
* @see #withConn(Function)
* @see #withConnection(Function)
* @see #withTrans(Function)
* @see #withTransaction(Function)
*/
void getSQLConnection(int queryTimeout, Handler<AsyncResult<SQLConnection>> handler) {
getConnection(res -> {
if (res.failed()) {
handler.handle(Future.failedFuture(res.cause()));
return;
}
PgConnection pgConnection = res.result();
if (queryTimeout == 0) {
handler.handle(Future.succeededFuture(new SQLConnection(pgConnection, null, null)));
return;
}
long timerId = vertx.setTimer(queryTimeout, id -> pgConnection.cancelRequest(ar -> {
if (ar.succeeded()) {
log.warn(String.format("Cancelling request due to timeout after : %d ms", queryTimeout));
} else {
log.warn("Failed to send cancelling request", ar.cause());
}
}));
SQLConnection sqlConnection = new SQLConnection(pgConnection, null, timerId);
handler.handle(Future.succeededFuture(sqlConnection));
});
}
Aggregations