hugsql

rgm 2020-06-18T20:41:10.018800Z

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?

rgm 2020-06-18T20:44:32.019600Z

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.summers 2020-06-18T20:50:00.020700Z

@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

rgm 2020-06-18T20:53:06.021100Z

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

rgm 2020-06-18T20:53:58.021800Z

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

rgm 2020-06-18T20:54:17.022300Z

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

adam 2020-06-18T23:59:47.024300Z

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