Search in sources :

Example 1 with PreparedStatement

use of io.vertx.sqlclient.PreparedStatement 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);
    }
}
Also used : PgPool(io.vertx.pgclient.PgPool) PgConnection(io.vertx.pgclient.PgConnection) RowSet(io.vertx.sqlclient.RowSet) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) PreparedStatement(io.vertx.sqlclient.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) FieldException(org.folio.cql2pgjson.exception.FieldException) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Transaction(io.vertx.sqlclient.Transaction) PrepareOptions(io.vertx.sqlclient.PrepareOptions) SqlConnection(io.vertx.sqlclient.SqlConnection)

Example 2 with PreparedStatement

use of io.vertx.sqlclient.PreparedStatement 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);
    }
}
Also used : PgPool(io.vertx.pgclient.PgPool) PgConnection(io.vertx.pgclient.PgConnection) RowSet(io.vertx.sqlclient.RowSet) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) PreparedStatement(io.vertx.sqlclient.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) FieldException(org.folio.cql2pgjson.exception.FieldException) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Transaction(io.vertx.sqlclient.Transaction) PrepareOptions(io.vertx.sqlclient.PrepareOptions) SqlConnection(io.vertx.sqlclient.SqlConnection)

Example 3 with PreparedStatement

use of io.vertx.sqlclient.PreparedStatement 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);
    }
}
Also used : SqlResult(io.vertx.sqlclient.SqlResult) Query(io.vertx.sqlclient.Query) PreparedQuery(io.vertx.sqlclient.PreparedQuery) PgPool(io.vertx.pgclient.PgPool) PgConnection(io.vertx.pgclient.PgConnection) RowSet(io.vertx.sqlclient.RowSet) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) PreparedStatement(io.vertx.sqlclient.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) FieldException(org.folio.cql2pgjson.exception.FieldException) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Function(java.util.function.Function) Transaction(io.vertx.sqlclient.Transaction) PrepareOptions(io.vertx.sqlclient.PrepareOptions) Collector(java.util.stream.Collector) SqlConnection(io.vertx.sqlclient.SqlConnection) Row(io.vertx.sqlclient.Row)

Example 4 with PreparedStatement

use of io.vertx.sqlclient.PreparedStatement in project raml-module-builder by folio-org.

the class PostgresClient method executeGetQuery.

private <T> void executeGetQuery(PgConnection connection, QueryHelper queryHelper, ResultInfo resultInfo, Class<T> clazz, Handler<AsyncResult<PostgresClientStreamResult<T>>> replyHandler, Transaction transaction) {
    connection.prepare(queryHelper.selectQuery, prepareRes -> {
        if (prepareRes.failed()) {
            closeIfNonNull(transaction).onComplete(ignore -> {
                log.error(prepareRes.cause().getMessage(), prepareRes.cause());
                replyHandler.handle(Future.failedFuture(prepareRes.cause()));
            });
            return;
        }
        PreparedStatement preparedStatement = prepareRes.result();
        RowStream<Row> rowStream = new PreparedRowStream(preparedStatement, STREAM_GET_DEFAULT_CHUNK_SIZE, Tuple.tuple());
        PostgresClientStreamResult<T> streamResult = new PostgresClientStreamResult<>(resultInfo);
        doStreamRowResults(rowStream, clazz, transaction, queryHelper, streamResult, replyHandler);
    });
}
Also used : PreparedStatement(io.vertx.sqlclient.PreparedStatement) Row(io.vertx.sqlclient.Row)

Aggregations

PreparedStatement (io.vertx.sqlclient.PreparedStatement)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 Handler (io.vertx.core.Handler)3 PgConnection (io.vertx.pgclient.PgConnection)3 PgPool (io.vertx.pgclient.PgPool)3 PrepareOptions (io.vertx.sqlclient.PrepareOptions)3 RowSet (io.vertx.sqlclient.RowSet)3 SqlConnection (io.vertx.sqlclient.SqlConnection)3 Transaction (io.vertx.sqlclient.Transaction)3 TransactionRollbackException (io.vertx.sqlclient.TransactionRollbackException)3 IOException (java.io.IOException)3 UncheckedIOException (java.io.UncheckedIOException)3 FieldException (org.folio.cql2pgjson.exception.FieldException)3 LocalRowSet (org.folio.rest.persist.helpers.LocalRowSet)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Row (io.vertx.sqlclient.Row)2 PreparedQuery (io.vertx.sqlclient.PreparedQuery)1 Query (io.vertx.sqlclient.Query)1 SqlResult (io.vertx.sqlclient.SqlResult)1 Function (java.util.function.Function)1