Fork me on GitHub
#hugsql
<
2020-06-18
>
rgm20:06:10

hoping someone can point me in the right place: I’m trying a jsonb existence query select * from users u where id = :id and permissions::jsonb ? :permission-string where permissions is an array of strings. Hugsql flips out a bit with thinking there are 3 parameters, per this sqlvec: ["SELECT\n u.id\nFROM USERS u\nWHERE id = ?\n AND permissions::jsonb ? ?" "an-id" "write"]. Is there a way to escape that ? … the JSONB existence operator in postgres?

rgm20:06:32

Note that select * from users u, jsonb_array_elements(u.permissions) permission where id = 'an-id' and permission.value::TEXT = '"write"' can work too, but that last double-quoting is a pain.

curtis.summers20:06:00

@rgm Try escaping the ? with a ??. “In JDBC, the question mark (`?`) is the placeholder for the positional parameters of a `PreparedStatement`. There are, however, a number of PostgreSQL operators that contain a question mark. To keep such question marks in a SQL statement from being interpreted as positional parameters, use two question marks (`??`) as escape sequence.” Per this page: https://jdbc.postgresql.org/documentation/head/statement.html

rgm20:06:06

oh wow, thanks. I’ll give that a go.

rgm20:06:58

(think psql might need that “hard to google” explanation page).

rgm20:06:17

(or I should actually read the JSON docs cover to cover some day… also a idea).

adam23:06:47

Is it possible to pass the field name from Clojure? WHERE XXX = 1. I have too many select by email, select by id, select by username funcs now... it would be simpler to be able to have WHERE dynamic somehow