sql

All things SQL and JDBC...
camsaul 2021-06-18T00:56:41.240300Z

@ben.sless If you really need to optimize fetching stuff to the point that you can't use map->SomeRecordType, have you considered just writing your own low-level code to fetch the rows from the ResultSet in this case? I think that would be a little easier and faster than the big macro to reify RowBuilder and ResultSetBuilder. If you know what columns are coming back you can avoid the overhead of having to programatically figure that out via the ResultSetMetaData, the calls to read-column-by-index, etc. e.g.

(defrecord Person [id name])

(defn reducible-people-results [^java.sql.ResultSet rs]
  (reify
    clojure.lang.IReduceInit
    (reduce [_ rf init]
      (loop [acc init]
        (cond
          (reduced? acc) acc
          (.next rs)     (recur (rf acc (->Person
                                         (.getInt rs "id")
                                         (.getString rs "name"))))
          :else          acc)))))

(toucan2.conn/with-connection [conn :test/postgres]
  (with-open [stmt (.prepareStatement conn "SELECT id, name FROM people;")
              rset (.executeQuery stmt)]
    (reduce
     conj
     []
     (reducible-people-results rset))))
;; ->
[#Person{:id 1, :name "Cam"}
 #Person{:id 2, :name "Sam"}
 #Person{:id 3, :name "Pam"}
 #Person{:id 4, :name "Tam"}]
Normally I wouldn't advocate writing such low-level JDBC code but if you're at the point you don't want to use the map->Record fn because of overhead then it might be time to tune stuff a little further

👏 1
Ben Sless 2021-06-21T17:30:32.240900Z

Thanks Cam, I'm trying to make the least amount of mess in the code base I'm in. Adding adapters in hugsql using jdbc.next isn't a big change. Changing what actually returns from the query will give my colleagues a fit

Ben Sless 2021-06-21T17:32:17.241800Z

And this is all temporary and I hope to tear it all out in the end