This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-05-06
Channels
- # announcements (37)
- # babashka (17)
- # beginners (20)
- # calva (2)
- # clj-kondo (15)
- # cljs-dev (1)
- # clojure (6)
- # clojure-austin (2)
- # clojure-china (3)
- # clojure-europe (27)
- # clojure-korea (1)
- # clojure-nl (1)
- # clojure-norway (18)
- # clojurescript (13)
- # conjure (3)
- # datomic (31)
- # events (3)
- # honeysql (14)
- # jobs-discuss (33)
- # leiningen (4)
- # lsp (57)
- # membrane (3)
- # missionary (5)
- # off-topic (1)
- # releases (3)
- # shadow-cljs (18)
- # slack-help (2)
- # specter (2)
- # yamlscript (10)
Does honey.sql provide options like jdbc/snake-kebab-opts
in next.jdbc
for column style conversion when inserting data?
You don't need to specify anything for it to work by default - :a-b
will become a_b
.
But if you use :quoted
or :dialect
that turns :a-b
into "a-b"
, then you can use :quoted-snake
to override that.
I want to generate sql with snake style
(-> {:insert-into [:t_test]
:values [{:unitId "1001" :userName "Jack"}]}
(honey.sql/format ))
hope: insert into t_test (unit_id,user_name) values (?,?)
Ah, HoneySQL has built-in facilities only to convert -
to _
.
Next.jdbc uses camel-snake-kebab
- you can use it as well.
I don't think there's a way for HoneySQL to convert things like :unitId
into "unit_id"
.
Oh, honey.sql doesn't support this kind of column style conversion. It seems I can only use jdbc/insert! then.
Oh, wait - you can use register-dialect!
to register your own dialect that provides the right :quote
function.
You may create a function or an operator that produces something like this:
select email as "user-email" from users;
(pay attention to quotes)Ah, it's even simpler:
(format {:select [[:email "user-email"]]})
["SELECT email AS \"user-email\""]
(defn make-columns [prefix cols]
(for [col cols]
[col (clojure.core/format "%s-%s" (name prefix) (name col))]))
(honey.sql/format {:select (make-columns :user [:id :name :email])})
["SELECT id AS \"user-id\", name AS \"user-name\", email AS \"user-email\""]
The problem with the approach, given that the original question is about inserting data, is that if you feed the data in the form of maps then there will be much more work than necessary to convert all of them to use the suitable keys. If you use explicit columns and vectors of vectors, then you only need to convert the columns so it's not a big deal. But I myself would simply use a custom dialect - then there's no extra work anywhere.
Right, I didn't notice that the question is about insertion. Well, the simplest way would be to turn maps into arrays of values. You take the first map as reference, and then:
• make a juxter like (apply juxt (-> maps first keys)
• make arrays like (map juxter maps)
• then you convert the keys of the first map into colum_names like this: :`the-map-key` -> "the_map_key"
Why isn't the custom dialect the simplest way? It's literally calling a function on a map that has another 1-line function. And the rest HoneySQL does for you. The only downside is that the quoting will be applied everywhere. So either you have to use the same kind of keywords everywhere, or the quoting function has to be flexible. If that's a deal breaker, then yeah, pre-formatting the data would be the only way.