Fork me on GitHub
#sql
<
2018-12-13
>
jaihindhreddy-duplicate09:12:19

With clojure.java.jdbc, how can do a query like (j/query db ["select a from foo where id in ?" ids]) where (def ids [1 2 3])?

jaihindhreddy-duplicate09:12:46

Is there a way other than doing (str "(" (clojure.string/join "," (repeat (count ids) "?")) ")") to get those "?"s in there?

jaihindhreddy-duplicate09:12:11

Unfortunately, that's what I see here 😕:

jaihindhreddy-duplicate09:12:28

^ In the last example

jaihindhreddy-duplicate09:12:50

This doesn't work either: (extend clojure.lang.PersistentVector j/ISQLValue {:sql-value (fn [coll] (str "(" (clojure.string/join "," (map j/sql-value coll)) ")"))}). Seems to wrap my thing in single quotes.

jaihindhreddy-duplicate10:12:05

Honey SQL seems to do what I want, (j/query db (sql/format {:select [:a] :from [:foo] :where [:in :id ids]})). But I wonder if something lighter weight that introducing a dep can be done here?

mpenet10:12:45

you are using pg?

curtis.summers12:12:50

You can use HugSQL's value list parameters (https://www.hugsql.org/#param-value-list) for this. If you prefer to have the SQL in your Clojure code instead of in a separate file, you can use the db-run function (http://layerware.github.io/hugsql/hugsql.core.html#var-db-run) like so: (hugsql/db-run db "select a from foo where id in :v*:ids" {:ids [1 2 3]})

jaihindhreddy-duplicate17:12:28

I'll take a look at HugSQL. Not familiar with it. Thanks for the suggestion.