我正在尝试使用准备好的查询和使用一组 Long 作为参数的“where xxx in ()”查询:
public static Multi<Item> findAll(PgPool client, Set<Long> ids) {
Tuple parameter = Tuple.of(ids.toArray(new Long[]{}));
// Tuple parameter = Tuple.wrap(new ArrayList(ids));
return client.preparedQuery("SELECT id, name, properties FROM items where id in ($1)").execute(parameter)
.onItem().transformToMulti(set -> Multi.createFrom().iterable(set))
.onItem().transform(Item::from);
}
但是,虽然“in”SQL 查询应该处理多个值,但它在传递数组时确实有效,并抛出以下内容:
io.vertx.core.impl.NoStackTraceThrowable: Parameter at position[0] with class = [[Ljava.lang.Long;] and value = [[Ljava.lang.Long;@1a074753] can not be coerced to the expected class = [java.lang.Number] for encoding.
传递单个值有效,但这不是该方法的目的:
Tuple parameter = Tuple.of(1206756351360216067L);
处理一组 id 以返回多行的正确方法是什么?
编辑
我最终这样做了:
Tuple parameter = Tuple.of(ids.toArray(new Long[]{}));
String query = "with values as (\n" +
"select unnest(($1)::bigint[]) as id\n" +
")\n" +
"select v.* from values vl " +
"join items v on v.id = vl.id";