This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-18
Channels
- # announcements (7)
- # babashka (4)
- # babashka-sci-dev (73)
- # beginners (101)
- # biff (4)
- # calva (33)
- # clerk (36)
- # clj-commons (23)
- # clj-kondo (3)
- # clojure (38)
- # clojure-europe (2)
- # clojurescript (29)
- # datalevin (15)
- # emacs (2)
- # fulcro (8)
- # gratitude (1)
- # hugsql (9)
- # hyperfiddle (43)
- # jobs-discuss (4)
- # lsp (47)
- # malli (7)
- # off-topic (14)
- # pathom (5)
- # practicalli (1)
- # releases (7)
- # shadow-cljs (4)
- # spacemacs (6)
- # sql (7)
- # tools-deps (7)
- # transit (8)
- # xtdb (6)
So our usage of next.jdbc for querying looks something like this:
(map (partial post-read some-config) (next.jdbc/execute! ds q {:builder-fn }))
Which seems kind’a not so great, since we’re now looping over stuff twice.
(next.jdbc/execute! ds q {:builder-fn (comp (partial post-read some-config) )})
Would perhaps be better, because we’d then only loop over the result once.
I’ve been skimming the docs, but didn’t quite realize that in theory, it should be as simple as the above, question is, is it, or should this be done some other way?Reduce over plan would be my approach. I use plan a lot.
But how do you do that efficiently given that I want maps with unqualified kws back?
Looking a bit into the code, I see that with execute
we can pass a builder-fn which gets access to the result set metadata which has the column names, but for plan, I don’t have access to that metadata, which means I have to do the key operating on the result map one at a time? like:
(reduce (fn [a r] (conj a (medley/map-keys (comp keyword name) r))) [] (jdbc/plan ds ["select * from foo"])))
What am I missing?plan
operates on the raw ResultSet
which uses plain String
labels for columns, and you can produce whatever data you want from the reducing function. The only "conversion" it does is from keywords to strings.
(into [] (map #(select-keys % [:timezone]))
(jdbc/plan ds ["select * from user where siteid = 45"]))
produces a vector of maps with plain :timezone
keys. You'll only get qualified keys out of plan
if you perform a (slow) operation that inflates the wrapped ResultSet
back into a full Clojure data structure -- which you're trying to avoid by using plan
... that's kind of the whole point.So it depends on exactly what post-read
does to the map-like object it is passed in...
(you could pass the as-unqualified-maps
builder to plan
but then you're just hiding any inefficiencies you create by accidentally inflating the Java object into a full Clojure data structure so I would avoid that until you get your reducing function right)