honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
bartuka 2021-03-10T18:46:42.106900Z

Hi, I need an extra help with insert-into. I have a data like:

(def my-data [{:value1 "1", :mymap {:map2 "42"}}])
this can potentially be several maps and I want to insert-many.
(jdbc/execute! datasource (-> {:insert-into :Table :values my-data} (sql/format)))
but I got an error
Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2553).
ERROR: syntax error at or near ")"
The sql query generate will be
(-> {:insert-into :Table
     :values [{:value1 "1" :mymap {:map2 "42"}}]}
     (sql/format))
;; => ["INSERT INTO Table (value1, mymap) VALUES (?, ())" "1"]

bartuka 2021-03-10T18:47:13.107400Z

I feel I am missing something here. Not sure if I need to add something specific into sql/format

seancorfield 2021-03-10T19:15:45.108300Z

@iagwanderson Which version of HoneySQL are you using? And what does it mean to have a column whose value is a hash map?

bartuka 2021-03-10T19:16:19.108600Z

version "1.0.461", the hashmap is coerced to jsonb fields

seancorfield 2021-03-10T19:17:59.109300Z

With 2.0, you can do this:

user=> (-> {:insert-into :Table
  #_=>      :values [{:value1 "1" :mymap [:lift {:map2 "42"}]}]}
  #_=>      (sql/format))
["INSERT INTO Table (value1, mymap) VALUES (?, ?)" "1" {:map2 "42"}]
I'm not sure what the solution is in 1.0, probably wrapping it in (sql/param ..)?

seancorfield 2021-03-10T19:19:36.109500Z

Nope, not sql/param

bartuka 2021-03-10T19:19:49.109800Z

I tried, sql/param too.

seancorfield 2021-03-10T19:20:13.110100Z

I'm not sure if it's possible with 1.0.

seancorfield 2021-03-10T19:20:47.110500Z

(well, I'm sure it is possible but I don't know how, off the top of my head)

bartuka 2021-03-10T19:22:28.112300Z

I will investigate this further later on and fix my situation now using next.jdbc directly. Would be good idea to create an issue as enhancement to support this? At least for tracking and generate docs if current facilities allow

seancorfield 2021-03-10T19:23:51.112800Z

It works in 2.0 as shown above. If it isn't possible in 1.0, it won't get fixed at this point.

👍 1
seancorfield 2021-03-10T19:25:56.114600Z

If you figure out how to do it in 1.0, I'd accept a PR for the docs for 1.0, but I'd encourage you you start upgrading to 2.0 at this point: you can depend on both versions at the same time -- different group IDs and different namespaces -- so you could use 2.0 for just that one insert if you wanted, and stay with 1.0 for everything else.

bartuka 2021-03-10T19:27:16.115300Z

> If you figure out how to do it in 1.0, I'd accept a PR for the docs for 1.0, but I'd encourage you you start upgrading to 2.0 at this point: you can depend on both versions at the same time -- different group IDs and different namespaces -- so you could use 2.0 for just that one insert if you wanted, and stay with 1.0 for everything else. nice, it makes sense. thank you!