hugsql

emboss 2017-03-09T04:17:05.000011Z

Hi, stupid question from a Clojure beginner: I have an INSERT with an optional parameter like INSERT INTO table (name, color) VALUES (:name, :color). Let's say :color is optional and the name is create-table!. Now if I do (create-table! { :name "Test"}), it will fail with ExceptionInfo Parameter Mismatch: :color parameter data not found. I would imagine this is a frequent thing but I couldn't find an easy solution to this problem. Could you help me please? Can I somehow mark :color as being optional?

2017-03-09T04:45:21.000013Z

@emboss if you're doing simple inserts you can use jdbc's insert! function instead of trying to template the insert statement

emboss 2017-03-09T04:57:35.000016Z

@rymndhng I managed to use --~ (if (:color params) ":color" "NULL") in the template. It actually works but it feels wrong... So you think I'm better off using a JDBC insert directly?

2017-03-09T04:58:33.000017Z

yeah, I prefer using HugSQL for building complex queries (for reading), but inserts are generally straight forward enough using raw jdbc

2017-03-09T04:59:13.000018Z

(jdbc/insert! db "table-name" {:name "Test"})
(jdbc/insert! db "table-name" {:name "Test" :color "some-color"})

emboss 2017-03-09T05:00:22.000020Z

OK, makes perfect sense. I was just wondering if I am missing something obvious because it wasn't mentioned anywhere I looked and it was literally the second thing that I stumbled upon when starting to play with it 🙂

👌 1
emboss 2017-03-09T05:01:16.000022Z

Thank you so much for your help and time, much appreciated!

2017-03-09T05:01:42.000023Z

no worries 🙂 enjoy clojuring!

emboss 2017-03-09T05:02:23.000024Z

I do and I will, thanks 😉