honeysql

igrishaev 2024-05-30T08:55:01.073459Z

This is not a question but rather an observation of an interesting fact. When HoneySQL renders a query, it prepends each ? with an extra ? so the JDBC driver detects the parameters properly. For example, the @? operator for JSONpath becomes @?? when rendered. Internally, JDBC turns it back to @? again. Last days, I extended HonySQL with a custom clause which renders Postgres-specific COPY ... TO ... command. This command accepts either a table name or a select query. In my case, I pass a nested query with JSONpath expressions and the @? operator. Since I pre-render the nested query with honey.sql/format , it produces the @?? operator instead of @? . At the end, I have a SQL string like

COPY (SELECT id AS _id, entity AS _source FROM foo.entities WHERE ((entity @@ '$."attrs"."status" == ":bad"') OR (entity @@ '$."attrs"."status" == ":good"') OR (entity @@ '$."attrs"."status" == ":test"') OR (entity @@ '$."attrs"."status" == ":hello"')) OR (NOT (entity @? '$."attrs"."status"') AND (entity @@ '$."attrs"."bar" == ":hello"'))) TO STDOUT WITH ( HEADER FALSE , FORMAT CSV )
Suddenly, when I pass this SQL into the standard CopyManager class, it blames the @?? expression. Turns out, the CopyManager class does not pre-process the ?? characters like the .executeStatement method does. After all, I've managed to replace these manually as follows:
(-> query honey.sql/format first (.replace "??" "?"))
Sharing this because for me, it looks quite unobvious. Hope it'll save one's time one day.