Hello folks!
First of all, let me thank you for this wonderful library. I've been using it for a few weeks now and It's a delightful experience.
I have run into an issue which I believe is more about me misusing the library than anything else. But I thought it was worth sharing and ask for pointers.
Suppose I have a DAO that looks like this:
@SqlQuery("SELECT name FROM players p where p.id = :playerId")
String queryA(@Bind("playerId") UUID playerId);
@SqlQuery("SELECT count(*) FROM players p <query>")
@AllowUnusedBindings
int queryB(@Define("query") String query, @Bind("playerId") UUID playerIds);
@SqlQuery("SELECT count(*) FROM players p <query>")
@AllowUnusedBindings
int queryC(@Define("query") String query, @Bind("playerIds") List<UUID> playerIds);
And then I try to use the queries this way:
Jdbi jdbi = Jdbi.create("jdbc:postgresql://localhost/jdbi_test");
jdbi.installPlugin(new SqlObjectPlugin());
jdbi.installPlugin(new PostgresPlugin());
PlayerDAO playerDAO = jdbi.onDemand(PlayerDAO.class);
playerDAO.queryA(UUID.randomUUID());
playerDAO.queryB("where p.id = :playerId", UUID.randomUUID());
playerDAO.queryC("where p.id in (<playerIds>)", List.of(UUID.randomUUID()));
The third query will fail with the following error:
org.jdbi.v3.core.statement.UnableToExecuteStatementException: org.postgresql.util.PSQLException: ERROR: syntax error at or near "<"
Position: 47 [statement:"SELECT count(*) FROM players p where p.id in (<playerIds>)", arguments:{positional:{1:[1f876165-7df8-435a-ae4d-1ccd5b3a6053]}, named:{playerIds:[1f876165-7df8-435a-ae4d-1ccd5b3a6053]}, finder:[]}]
at org.jdbi.v3.core.statement.SqlStatement.internalExecute(SqlStatement.java:1676
So my understanding is that I can't bind a list this way. Am I just misusing the API here? Or should this be possible?
For context, in the project I'm using jdbi I have mostly very simple queries and jdbi is really shining.
But I've got a couple of endpoints that require some fancy search and I'd want to construct the query
on the fly (so that I'm in full control of the query that will get executed, after all that's the number one reason why I chose jdbi).
The idea pattern (from my perspective, please bear in mind I'm new to the library and I may be missing some obviously solution) for such cases would be a DAO with a signature like:
@SqlQuery("SELECT p.id, p.name FROM players p <query>")
@AllowUnusedBindings
PlayerStatsDTO search(@Define("query") String query, @Bind PlayerSearchDTO playerSearchDTO);
That would allow one to build a simple query builder and be in full control of the query with a neat api (and all the safeness of parameter binding). but it's not working with lists and I'm not sure how to solve it.
Any pointer would be greatly appreciated.
In case you want to play around with the code, I published a test repo here: https://github.com/lucapette/jdbi-uuid-test
Thank you for your time!