如何通过Postgresql查询在字符串列表中搜索枚举? [英] How to search an enum in list of strings by postgresql query?
本文介绍了如何通过Postgresql查询在字符串列表中搜索枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑一条SQL语句:
select * from table where status in <statuses>
状态为枚举:
CREATE TYPE statusType AS ENUM (
'enum1',
'enum2';
在Java中,我有一个以字符串表示形式的枚举列表:
In Java I have a list of the enums in string representation:
List<String> list = new ArrayList<>();
list.add("enum1");
list.add("enum2");
然后我尝试使用SqlStatement构建并执行查询:
I then try to build and execute the query using 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作为statusType)或enum1 :: statusType包装每个列表项,但是没有运气.
I've tried wrapping each list item with CAST(enum1 as statusType) or enum1::statusType but no luck.
感谢您的帮助.
推荐答案
您可以将枚举转换为文本
You can cast the enum to text
handle.createQuery("select * from table where status::text in <statuses>")
.bindList("statuses", statuses)
.map(Mapper.instance)
.list());
这篇关于如何通过Postgresql查询在字符串列表中搜索枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文