sql

All things SQL and JDBC...
2020-07-08T08:49:45.162200Z

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.

2020-07-09T07:51:37.192Z

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.

2020-07-09T07:51:53.192200Z

There is nothing to change, the documentation is fine.

seancorfield 2020-07-09T16:08:21.192400Z

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.

seancorfield 2020-07-09T16:10:35.192600Z

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

👍 1
2020-07-08T08:52:22.163800Z

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

viesti 2020-07-08T10:30:27.164700Z

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

2020-07-08T10:41:13.164900Z

which part of the query do you struggle with?

viesti 2020-07-08T11:06:41.165100Z

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

viesti 2020-07-08T11:08:24.165300Z

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

viesti 2020-07-08T11:08:46.165600Z

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

viesti 2020-07-08T11:09:03.165800Z

but maybe temporary table woud be more simple actually

2020-07-08T15:55:31.166Z

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

seancorfield 2020-07-08T16:01:47.166200Z

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

1
2020-07-08T16:02:54.166400Z

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

seancorfield 2020-07-08T16:43:49.166700Z

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.

2020-07-08T16:44:30.166900Z

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

seancorfield 2020-07-08T16:44:37.167100Z

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

2020-07-08T16:45:19.167300Z

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

seancorfield 2020-07-08T16:47:13.167500Z

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

seancorfield 2020-07-08T16:48:11.167900Z

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

viesti 2020-07-08T17:12:39.168100Z

thanks! I didn't notice :)