This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-10-08
Channels
- # announcements (3)
- # babashka (3)
- # beginners (25)
- # calva (12)
- # cider (58)
- # clara (11)
- # clj-kondo (19)
- # cljsrn (2)
- # clojure (84)
- # clojure-austin (1)
- # clojure-europe (5)
- # clojure-nl (4)
- # clojure-spec (23)
- # clojure-uk (53)
- # clojuredesign-podcast (5)
- # clojurescript (24)
- # core-async (57)
- # cursive (16)
- # datomic (39)
- # emacs (1)
- # fulcro (40)
- # funcool (2)
- # graphql (17)
- # jackdaw (31)
- # jobs (2)
- # joker (3)
- # malli (7)
- # off-topic (12)
- # re-frame (9)
- # reagent (2)
- # reitit (1)
- # ring (4)
- # shadow-cljs (170)
- # sql (36)
- # tools-deps (5)
- # xtdb (20)
If I want to run a sql query using next.jdbc/plan
, and just realize the full result, how do I do that ? shouldnt (into [] results)
realize it ?
@murtaza52 You need to map
something that realizes each row.
You could just use execute!
if you want the default array of realized hash map rows.
But (into [] (map rs/datafiable-row) (jdbc/plan ,,,))
is roughly the same.
I tried (into [] (map identity) results)
to realize the rows, but that doesnt work too.
No, because identity
doesn't do anything to its argument.
What error?
Wrong number of args (1) passed to:
next.jdbc.result-set/eval6983/fn--6984/G--6974--6993
Ah, yeah, my bad. I was doing that off the top of my head. datafiable-row
needs arguments.
Seriously tho', if you are just trying to get a fully realized result set, use execute!
instead of plan
.
plan
is for when you want to process a result set without realizing each row as a Clojure data structure.
execute!
is for obtaining a fully realized result set as Clojure data.
(jdbc/execute! ds sql-vec opts)
== (into [] (map #(rs/datafiable-row % ds opts)) (jdbc/plan ds sql-vec opts))
-- that's the full equivalence.
(in order to datafy
a row, datafiable-row
needs the connectable and options so that it can perform SQL queries on your behalf if you nav
igate through the result)
And when I say "full equivalence" I mean functionally the same -- under the hood they are implemented differently for performance reasons, so use the right tool (function) for the job 🙂
@murtaza52 Does that help? ^
@seancorfield thanks. I am trying to run initialization commands - CREATE DATABASE foo;
, I dont need datafy/nav functionality, and I do want something that is eager and I dont have to realize.
So I guess I will have to just run plan
and then realize the result (so that it actually gets executed)?
If you read the docs, for DDL operations, it recommends using execute-one!
Since they will only ever return one "row" of information: an update count.
The Getting Started page of the docs shows this in the example REPL session.
DDL doesn't produce a result set so there's no point in trying to use plan
with it -- you're only ever going to get back update counts.
yup true, thanks for pointing out .... in plan
does the query gets executed only when the result is reduced ?
Yes, that's why plan
has no !
-- on its own, it doesn't actually "do" anything. Only when it is reduced somehow does it actually run the SQL and process the result set.
also is there an easy way to realize the result of plan
for debugging, console printing etc ..
@murtaza52 when working with plan
, you will always have some sort of reduction process -- so that is what you would debug. plan
doesn't have a "result" until you reduce it.
In particular, if you (map str)
over the rows, you will get the string representation of each row as a (realized) hash map.
So inside your reduction process, just println
ing (str row)
should realize the row as a Clojure hash map and then turn it into a string and then print it.
That will give you a vector of strings (if that's what you want for debugging).
I was thinking more of something like this:
(into []
(map #(do (println (str %)) (:id %)))
(jdbc/plan ds ["select * from user where field = ?" some-value]))
So that prints each row but still also returns a vector of IDsThe non-debugging form would just have (map :id)
You can also just use (run! println (jdbc/plan ,,,))
-- but, generally, for debugging this stuff, I'd recommend instrumenting the reducing process itself instead.