This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-12-13
Channels
- # adventofcode (84)
- # aleph (1)
- # announcements (2)
- # aws (27)
- # beginners (52)
- # braveandtrue (2)
- # calva (440)
- # cider (7)
- # clara (2)
- # cljdoc (26)
- # cljs-dev (70)
- # clojure (131)
- # clojure-berlin (4)
- # clojure-brasil (1)
- # clojure-europe (2)
- # clojure-greece (4)
- # clojure-hamburg (1)
- # clojure-italy (4)
- # clojure-losangeles (6)
- # clojure-nl (14)
- # clojure-spec (7)
- # clojure-uk (25)
- # clojurescript (26)
- # component (2)
- # cursive (13)
- # datomic (60)
- # dirac (59)
- # docker (1)
- # figwheel (1)
- # figwheel-main (2)
- # fulcro (12)
- # graphql (5)
- # juxt (33)
- # leiningen (19)
- # nrepl (1)
- # off-topic (37)
- # protorepl (2)
- # re-frame (18)
- # reagent (46)
- # remote-jobs (1)
- # ring-swagger (1)
- # shadow-cljs (88)
- # sql (10)
- # tools-deps (64)
- # vim (24)
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])
?
Is there a way other than doing (str "(" (clojure.string/join "," (repeat (count ids) "?")) ")")
to get those "?"s in there?
Unfortunately, that's what I see here 😕:
^ In the last example
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.
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?
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]})
I'll take a look at HugSQL. Not familiar with it. Thanks for the suggestion.