Fork me on GitHub
#honeysql
<
2024-02-08
>
cddr10:02:08

I want to generate SQL like insert into foo (a, b) values (?, ? format json), and then provide a string as the 2nd parameter. Can I do this using honeysql? I tried to do this by registering a function.

(hsql/register-fn! :h2-json
                   (fn [_ arg]
                     ["? format json" arg]))
and then using this in an expression in a value. e.g.
{:insert-into [:collection.event-v1],
 :columns [:event-id :type :game-id :attributes],
 :values
 [["5d600f99-ac53-5c44-b9e5-a0cd54a5a0b7"
   "starting_xi"
   1147541
   [:h2-json
    "{\"video_timestamp\":0,\"game_id\":1147541,\"team_directions\":{\"1277\":\"right_to_left\",\"1285\":\"left_to_right\"},\"formation\":\"4-2-3-1\",\"index\":1,\"name\":\"starting_xi\",\"event_id\":\"5d600f99-ac53-5c44-b9e5-a0cd54a5a0b7\",\"team_id\":1285,\"period\":1,\"lineup\":{\"rw\":1013520,\"gk\":81405,\"rdm\":1012881,\"ldm\":134650,\"rb\":139914,\"cam\":1012550,\"lb\":75078,\"lw\":1018595,\"cf\":86937,\"rcb\":83675,\"lcb\":87310},\"arqam_event_id\":305226079,\"related_events\":[],\"opposition_team_id\":1277}"]]]}
Unfortunately, when this is formatted, the [:h2-json expression comes in a list
["INSERT INTO collection.event_v1 (event_id, type, game_id, attributes) VALUES (?, ?, ?, ? format json)"
 "5d600f99-ac53-5c44-b9e5-a0cd54a5a0b7"
 "starting_xi"
 1147541
 ("{\"video_timestamp\":0,\"game_id\":1147541,\"team_directions\":{\"1277\":\"right_to_left\",\"1285\":\"left_to_right\"},\"formation\":\"4-2-3-1\",\"index\":1,\"name\":\"starting_xi\",\"event_id\":\"5d600f99-ac53-5c44-b9e5-a0cd54a5a0b7\",\"team_id\":1285,\"period\":1,\"lineup\":{\"rw\":1013520,\"gk\":81405,\"rdm\":1012881,\"ldm\":134650,\"rb\":139914,\"cam\":1012550,\"lb\":75078,\"lw\":1018595,\"cf\":86937,\"rcb\":83675,\"lcb\":87310},\"arqam_event_id\":305226079,\"related_events\":[],\"opposition_team_id\":1277}")]

p-himik10:02:43

Use (into ["? format json"] arg) instead (because arg is actually args).

cddr10:02:13

ah perfect. thanks