btbam91
btbam91

Reputation: 608

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

Consider a SQL Statement:

select * from table where status in <statuses>

Where status is an enum:

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

In Java I have a list of the enums in string representation:

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

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());

I keep getting the following error:

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.

I've tried wrapping each list item with CAST(enum1 as statusType) or enum1::statusType but no luck.

Any help is appreciated.

Upvotes: 1

Views: 1478

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40048

You can cast the enum to text

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

Upvotes: 3

Related Questions