honeysql

quan xing 2024-05-06T11:53:32.366139Z

Does honey.sql provide options like jdbc/snake-kebab-opts in next.jdbc for column style conversion when inserting data?

p-himik 2024-05-07T08:19:05.794609Z

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.

igrishaev 2024-05-07T08:25:59.057409Z

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"

p-himik 2024-05-07T08:28:26.119669Z

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.

p-himik 2024-05-06T11:58:38.717159Z

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.

quan xing 2024-05-06T12:09:22.930079Z

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 (?,?)

p-himik 2024-05-06T12:14:58.539479Z

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".

quan xing 2024-05-06T12:18:47.872129Z

Oh, honey.sql doesn't support this kind of column style conversion. It seems I can only use jdbc/insert! then.

p-himik 2024-05-06T12:19:53.211309Z

Oh, wait - you can use register-dialect! to register your own dialect that provides the right :quote function.

quan xing 2024-05-06T12:21:18.264079Z

Alright, I'll check the documentation and do some research.

👍 1
igrishaev 2024-05-06T15:56:12.435399Z

You may create a function or an operator that produces something like this:

select email as "user-email" from users;
(pay attention to quotes)

igrishaev 2024-05-06T15:59:15.452599Z

Ah, it's even simpler:

(format {:select [[:email "user-email"]]})

["SELECT email AS \"user-email\""]

igrishaev 2024-05-06T16:03:18.629879Z

(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\""]

quan xing 2024-05-07T00:21:50.815849Z

👍oh, self-defined conversion function.