Vertx JDBC客户端queryWithParams-如何添加列表? [英] Vertx JDBC client queryWithParams - how to add a list?

查看:160
本文介绍了Vertx JDBC客户端queryWithParams-如何添加列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有条件为currency in ?的SQL查询 我正在使用vertx JDBC客户端queryWithparams方法,该方法在JsonArray中接收查询参数.

I have SQL query with condition currency in ? and I'm using vertx JDBC client queryWithparams method, which receives query parameters in JsonArray.

如何将可能的currency值列表传递给查询? 我尝试了new JsonArray().add(new JsonArray(currencies)但出现了异常

How can I pass my list of possible currency values to the query? I tried new JsonArray().add(new JsonArray(currencies) but got exception

org.postgresql.util.PSQLException:无法推断用于io.vertx.core.json.JsonArray实例的SQL类型.将setObject()与显式的Types值一起使用以指定要使用的类型.

org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of io.vertx.core.json.JsonArray. Use setObject() with an explicit Types value to specify the type to use.

推荐答案

简短的答案是,您无法使用通用的Vertx JDBC客户端添加列表作为查询参数,但是由于您使用的是Postgres,您可以使用特定于Postgres的称为vertx-pg-client的库.我实现了与您对此代码大致相同的查询:

The short answer is that you can't add a List as a query parameter with the generic Vertx JDBC client, but since you're using Postgres there is a Postgres-specific library called vertx-pg-client that you can use. I implemented roughly the same query as you did with this code:

List<String> currencies = whatever();
String uri = "your-uri";
String query = "select from table where currency = any($1)";
PgConnection.connect(vertx, uri, connectionResult -> {
    if (connectionResult.failed()) {
        // handle
    } else {
        PgConnection connection = connectionResult.result();
        Tuple params = Tuple.of(currencies);

        doQuery(query, connection, params).setHandler(queryResult -> {
            connection.close();
            msg.reply(queryResult.result());
        });
    }
});

    private Future<String> doQuery(String sql, PgConnection connection, Tuple params) {
        Promise<String> promise = Promise.promise();
        connection.preparedQuery(sql, params, res -> {
            if (res.failed()) {
                // log
                promise.fail(res.cause());
            } else {
                RowSet<Row> rowSet = res.result();
                // do something with the rows and construct a return object (here, a string)
                String result = something;
                promise.complete(result);
            }
        });
        return promise.future();
    }

所有荣誉归功于@tsegismont,他曾帮助我解决相同的问题

All credit goes to @tsegismont who helped me with the same question here.

这篇关于Vertx JDBC客户端queryWithParams-如何添加列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆