Using next.jdbc/plan
, what is the recommended way to force a ResultSet
into a Clojure hash-map?
For now, I am using (into {} row)
but that might be overkill.
it was my mistake .. I read the whole documentation a few weeks ago but forgot about that part. Then when I searched again, I jumped to another section.
There is nothing to change, the documentation is fine.
OK, cool. I add new stuff to the docs with each release so it's often the case that someone who read them cover-to-cover a few weeks ago would not have seen something recently added.
I updated the docs around making rows in plan
. Here's the second half of the example now:
;; do not do this:
user=> (into []
(map #(into {} %))
(jdbc/plan ds ["select * from invoice where customer_id = ?" 100]))
;; do this if you just want realized rows with default qualified names:
user=> (into []
(map #(rs/datafiable-row % ds {}))
(jdbc/plan ds ["select * from invoice where customer_id = ?" 100]))
and here's the caveat added about (into {} row)
: "The third expression produces a result that looks identical but has stripped all the metadata away: it has still called rs/datafiable-row
to fully-realize a datafiable and navigable hash map but it has then "poured" that into a new, empty hash map, losing the metadata."My use case is that I am passing the hashmap to a function that may only use it later, after next.jdbc/plan
finished.
hmm, with honeysql, is it possible to create a select command from a values list? e.g.
SELECT * FROM (VALUES ('2020-01-01T08:00Z'::timestamptz, 1)) AS t (datetime, int);
which part of the query do you struggle with?
the values part, thinking that this is probably uncommong thing to do
https://github.com/seancorfield/honeysql/blob/develop/src/honeysql/format.cljc#L635-L638 seems there is a case to make a "VALUES (...)"
thing, but probably not for this purpose 🙂 This is PostgresQL specific, so honeysql-postgres would be to place to look for this, but seems that values things there are for insert only
just had an idea to use values list for testing query fragments without creating an actual table
but maybe temporary table woud be more simple actually
There is a #honeysql room where you might get the help you seek.
@vincent.cantin I would use select-keys
if I only wanted part of it or rs/datafiable-row
if I wanted all of it.
Thank you! Maybe it can be added to the Wiki.
You mean, like it says in the Getting Started documentation?
This means that select-keys can be used to create regular Clojure hash map from (a subset of) columns in the row, without realizing the row, and it will not implement Datafiable or Navigable.
If you wish to create a Clojure hash map that supports that lazy navigation, you can call next.jdbc.result-set/datafiable-row, passing in the current row, a connectable, and an options hash map, just as you passed into plan.
oh .... I was looking for it but did not see it - my bad
(which is then followed by examples showing the different ways to build a hash map)
I looked for it around the documentation of plan
.. sorry
I can't put everything in the docstrings 🙂 It's in this section of Getting Started https://cljdoc.org/d/seancorfield/next.jdbc/1.1.547/doc/getting-started#plan--reducing-result-sets -- `plan` & Reducing Result Sets
Suggestions welcome on how to make it easier to find -- feel free to create issues with ideas about documentation improvements.
thanks! I didn't notice :)