Fork me on GitHub
#honeysql
<
2021-05-04
>
Akiz04:05:23

Hi! I am using honeysql together with Babashka and I would like to produce a raw sql queries compatible with psql. Is this possible right now?

seancorfield05:05:46

If you're using V2, there is an inline option that tries to put the values inline in the string instead of using ? And putting the parameters in a vector

đź‘Ź 3
Akiz09:05:50

Thank you, this is exactly what I needed.

Akiz09:05:08

Hi, version 1 has build fn, i use it for removing keys with missing values to build a valid sql map. Is there something simiÄşlar in v2?

seancorfield16:05:02

@zikajk No, V2 does not have that function. Can you explain what you mean by “removing keys with missing values”?

Akiz16:05:15

@U04V70XH6 In HoneySQL v1 I had got a function like this: (defn build-select [table-name where limit] (hsql/format (hsql/build :select :* :from table-name :where where :limit limit))) All this was valid thanks to the build fn: (build-select :public.table) (build-select :public.table [:= :table.field "string"]) (build-select :public.table [:= :table.field "string"] 10) Now I changed the logic in HoneySQL v2 to this - so I can supply map with/out :where or :limit as before. (defn build-select [table-opts] (hsql/format (merge {:select [:*] :from [(:table-name table-opts)]} (select-keys table-opts [:where :limit])))) The input got a little different of course: (build-select {:table-name :public.table}) (I am building custom sql queries from EDN files. I used to read list of [table-name where limit] now it is (:table-name string :where vector :limit int}

seancorfield16:05:44

Well, it’s “just data” so you can use cond-> and assoc to conditionally add things, or use the helper functions to make sure you get the right structure.

đź’Ż 3
seancorfield05:05:36

@zikajk BTW, if you’re reading a vector of [table-name where limit] you can always turn that into a map programmatically with (zipmap [table-name where limit] [:table-name :where :limit]) or your build-select could take [[table-name where limit]] as before and do (hsql/format (cond-> {:select [:*] :from [table-name]} where (assoc :where where) limit (assoc :limit limit)))

🎯 3
seancorfield05:05:50

(in case you wanted to preserve the calling format)

Akiz06:05:56

@U04V70XH6 Thank you. I already changed my code to use maps, no problem here. It is just that build fn is one of the first thing mentioned in HoneySQL v1 tutorial so I have been looking for it (as I migrated to version2 because of {:inline true} which is a great option btw.)