如何通过 postgresql 查询在字符串列表中搜索枚举?

How to search an enum in list of strings by postgresql query?

考虑一个SQL语句:

select * from table where status in <statuses>

其中 status 是一个枚举:

CREATE TYPE statusType AS ENUM (
  'enum1',
  'enum2';

在 Java 中,我有一个字符串表示形式的枚举列表:

List<String> list = new ArrayList<>();
list.add("enum1");
list.add("enum2");

然后我尝试使用 SqlStatement 构建和执行查询:

handle.createQuery("select * from table where status in <statuses>")
                    .bindList("statuses", statuses)
                    .map(Mapper.instance)
                    .list());

我不断收到以下错误:

org.jdbi.v3.core.statement.UnableToExecuteStatementException: org.postgresql.util.PSQLException: ERROR: operator does not exist: status = character varying
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

我试过用 CAST(enum1 as statusType) 或 enum1::statusType 包装每个列表项,但没有成功。

感谢任何帮助。

您可以将枚举转换为文本

handle.createQuery("select * from table where status::text in <statuses>")
                .bindList("statuses", statuses)
                .map(Mapper.instance)
                .list());