Fork me on GitHub
#sql
<
2020-07-08
>
Vincent Cantin08:07:45

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.

seancorfield16:07:47

@U8MJBRSR5 I would use select-keys if I only wanted part of it or rs/datafiable-row if I wanted all of it.

sheepy 3
Vincent Cantin16:07:54

Thank you! Maybe it can be added to the Wiki.

seancorfield16:07:49

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.

Vincent Cantin16:07:30

oh .... I was looking for it but did not see it - my bad

seancorfield16:07:37

(which is then followed by examples showing the different ways to build a hash map)

Vincent Cantin16:07:19

I looked for it around the documentation of plan .. sorry

seancorfield16:07:13

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` &amp; Reducing Result Sets

seancorfield16:07:11

Suggestions welcome on how to make it easier to find -- feel free to create issues with ideas about documentation improvements.

Vincent Cantin07:07:37

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.

Vincent Cantin07:07:53

There is nothing to change, the documentation is fine.

seancorfield16:07:21

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.

seancorfield16:07:35

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."

👍 3
Vincent Cantin08:07:22

My use case is that I am passing the hashmap to a function that may only use it later, after next.jdbc/plan finished.

viesti10:07:27

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);

Vincent Cantin10:07:13

which part of the query do you struggle with?

viesti11:07:41

the values part, thinking that this is probably uncommong thing to do

viesti11:07:24

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

viesti11:07:46

just had an idea to use values list for testing query fragments without creating an actual table

viesti11:07:03

but maybe temporary table woud be more simple actually

Vincent Cantin15:07:31

There is a #honeysql room where you might get the help you seek.

viesti17:07:39

thanks! I didn't notice :)