sql

All things SQL and JDBC...
agata_anastazja (she/her) 2020-12-29T14:08:50.030200Z

Hi, I'm using next-jdbc with hugsql . I would like to pass an argument to my query to limit batch size of how many rows I am selecting, but I also want to have an adapter that allows me not to have namespace

(hugsql/def-db-fns
  "path-to-query.sql"
  {:adapter (next-adapter/hugsql-adapter-next-jdbc {:builder-fn rs/as-unqualified-maps})})
now my results look a bit like this {events/id "1a252d29-e19f-4e84-9e96-14ce82254988", :events/sequence_number 1} but I would like them to look like this {id "1a252d29-e19f-4e84-9e96-14ce82254988", :sequence_number 1}

agata_anastazja (she/her) 2020-12-29T15:31:00.033100Z

it seems like all I needed was to add the builder-fn as a parameter to the execute function, like this!

(jdbc/execute! db [query batch-size]  {:builder-fn rs/as-unqualified-maps})

lukasz 2020-12-29T15:34:00.033900Z

I see - I assumed you get the wrong result when using hugsql functions

lukasz 2020-12-29T15:34:25.034500Z

Yes, jdbc/execute! (and related functions) bypass your hugsql adapter configuration

agata_anastazja (she/her) 2020-12-29T16:53:40.035900Z

well I tried it both ways, and it worked with execute!

agata_anastazja (she/her) 2020-12-29T16:53:58.036300Z

thanks for your advice anyway 🙂

seancorfield 2020-12-29T17:22:31.038200Z

@brzoskwinka18 :builder-fn is an option for next.jdbc which is why you need it in direct calls to that library's functions. There's an example of how to pass next.jdbc options to the HugSQL adapter in the next.jdbc docs: https://cljdoc.org/d/seancorfield/next.jdbc/1.1.613/doc/getting-started/friendly-sql-functions#hugsql-quick-start -- scroll down a bit to see:

;; add require next.jdbc.result-set :as rs to your ns

(hugsql/def-db-fns "db/example.sql"
                   {:adapter (adapter/hugsql-adapter-next-jdbc
                              {:builder-fn rs/as-maps})})

💜 1
seancorfield 2020-12-29T17:23:15.038900Z

Always happy to hear ways folks think the docs can be improved...

seancorfield 2020-12-29T17:24:45.039500Z

(I don't know what HugSQL has in terms of docs on how to use next.jdbc?)

seancorfield 2020-12-29T17:25:12.040Z

BTW, in case folks aren't aware, there's a dedicated #hugsql channel (and I lurk there as well).

lukasz 2020-12-29T17:56:33.040600Z

HugSQL docs have a dedicated "Adapters" section: https://www.hugsql.org/#adapter

lukasz 2020-12-29T17:56:55.040900Z

looks like default is to pull in clojure.java.jdbc

seancorfield 2020-12-29T18:03:23.042100Z

Yes, that's their historical default since it's been around much longer. The recommendation these days is to use next.jdbc (since clojure.java.jdbc is no longer being maintained) but I can understand why they wouldn't necessarily want to change their default.

lukasz 2020-12-29T18:17:54.043Z

Yeah, we still had to create a small wrapper to smooth the transition from c.j.jdbc to next but it wasn't as painful as it seemed initially