Fork me on GitHub
#sql
<
2020-12-29
>
agata_anastazja (she/her)14:12:50

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)15:12:00

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

lukasz15:12:00

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

lukasz15:12:25

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

agata_anastazja (she/her)16:12:40

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

agata_anastazja (she/her)16:12:58

thanks for your advice anyway 🙂

seancorfield17:12:31

@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})})

💜 3
seancorfield17:12:15

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

seancorfield17:12:45

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

seancorfield17:12:12

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

lukasz17:12:33

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

lukasz17:12:55

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

seancorfield18:12:23

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.

lukasz18:12:54

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