如何使用列表作为 Vertx JDBC 客户端的 SQL 查询的参数源?

How to use a list as a parameter source for SQL queries with Vertx JDBC Client?

我有一个 Vert.x Web 应用程序需要查询 AWS RDS 实例 运行 Postgres 10.7。 Vert.x JDBC 客户是 io.vertx:vertx-jdbc-client:3.8.4。我想查询一个 table,其约束条件是某个列的值包含在一组值中:

select from table where column in/any (?)

我遵循了 Vertx 文档,它说创建一个 JsonArray 并用要注入查询的值填充它。该列的类型为 text,我要匹配的列表是 Java ArrayList<String>。我的查询代码如下所示:

String sql = "SELECT a FROM table WHERE col IN (?)";
List<String> values = someObject.someField();

            sqlClient.getConnection(connectionResult -> {
                if (connectionResult.failed()) {
                    // handle
                } else {
                    SQLConnection connection = connectionResult.result();

                    JsonArray params = new JsonArray()
                            .add(values);
                    connection.queryWithParams(sql, params, queryResult -> {
                       if (queryResult.failed()) {
                           // handle
                       } else {
                           // parse
                       }
                    });
                }
            });

查询失败并出现错误: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.

我知道在最坏的情况下,我可以创建一个文字 SQL 字符串 where col in (?, ?, ?, ..., ?) 并将列表中的每个字符串添加到 JsonArray,但必须有一种方法只需添加 ArrayList<String> 作为参数并保持查询简单。如何指定要在查询中匹配的值列表?

Vert.xJDBC客户端不支持查询中的数组参数。

但是 Vert.x Pg Client 是可能的,它不依赖于 JDBC。您需要先修改查询:

SELECT a FROM table WHERE col = ANY(?)

然后:

pgClient.preparedQuery(query, Tuple.of(possibleValues), collector, handler);