Spring JDBCClient with IN clause query for PostgreSQL type field - Stack Overflow

I am using the new Spring boot JDBCClient, where I am trying to use a List into query IN clauses. I fou

I am using the new Spring boot JDBCClient, where I am trying to use a List into query IN clauses. I found a solution that does not seem to be a very elegant/handy way of using any() operator, so I am wondering if there is some other approach.

Note: My field is PostgreSQL's Enum type

private final JdbcClient jdbcClient;

String sql = """
                SELECT * 
                FROM configuration 
                WHERE status = ANY(?::configuration_status[])
                """;

String statusArrayLiteral = "{" + request.configurationStatus().stream()
                .map(Enum::name)
                .collect(Collectors.joining(",")) + "}";

        return jdbcClient.sql(sql)
                .param(statusArrayLiteral)
                .query((rs, rowNum) -> parseConfiguration(rs))
                .list();

I am using the new Spring boot JDBCClient, where I am trying to use a List into query IN clauses. I found a solution that does not seem to be a very elegant/handy way of using any() operator, so I am wondering if there is some other approach.

Note: My field is PostgreSQL's Enum type

private final JdbcClient jdbcClient;

String sql = """
                SELECT * 
                FROM configuration 
                WHERE status = ANY(?::configuration_status[])
                """;

String statusArrayLiteral = "{" + request.configurationStatus().stream()
                .map(Enum::name)
                .collect(Collectors.joining(",")) + "}";

        return jdbcClient.sql(sql)
                .param(statusArrayLiteral)
                .query((rs, rowNum) -> parseConfiguration(rs))
                .list();
Share Improve this question edited Mar 31 at 14:02 Tsvetoslav Tsvetkov asked Mar 21 at 9:07 Tsvetoslav TsvetkovTsvetoslav Tsvetkov 1,1761 gold badge11 silver badges30 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can tidy it up but not by much because the parameter wants a list of strings. Expanding all the way to a single comma separated string is not necessary.

Note that I introduced the named parameter configuration_status because .param requires it if a List is passed in. Also your question referred to the IN clause but your code uses ANY, so I changed that too.

String sql = """
                SELECT * 
                FROM configuration 
                WHERE status IN(:configuration_status)
                """;

        return jdbcClient.sql(sql)
                    .param("configuration_status", request.configurationStatus().stream().map(Enum::name).collect(Collectors.toList()))
                .query((rs, rowNum) -> parseConfiguration(rs))
                .list();

I found a solution, how to make proper IN clauses in case somebody needs to search based on multiple values in a field that is from a PosgreSQL type as the John Williams's solution works BUT on Varchar field

 return jdbcClient.sql("""
                                SELECT * 
                                FROM configuration 
                                WHERE status IN (:status)
                        """)
                .param("status", request.configurationStatus().stream().map(Enum::name).collect(Collectors.toList()), OTHER)
                .query((rs, rowNum) -> parseConfiguration(rs))
                .list();

The key thing is that the third parameter should be used, which defines the SQL type

In my case I used OTHER (The type can be seen from java.sql.Types)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744364522a4570634.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信