honeysql

Nim Sadeh 2025-04-05T16:36:25.283309Z

More of next.jdbc question - am I using reify correctly here?

(defn wrap-datasource [ds]
  (reify javax.sql.DataSource
    proto/Executable
    (-execute [_ sql opts]
      (µ/trace :database/execute [:statement sql]
               (proto/-execute ds sql opts)))
    (-execute-all [_ sql opts]
      (µ/trace :database/execute-all [:statement sql]
               (proto/-execute-all ds sql opts)))
    (-execute-one [_ sql opts]
      (µ/trace :database/execute-one [:statement sql]
               (proto/-execute-one ds sql opts)))))

(def database
  (-> env/postgres
      jdbc/get-datasource
      wrap-datasource))

(comment (jdbc/execute! database ["SELECT * FROM client_events"]))

p-himik 2025-04-05T16:40:58.857059Z

> More of next.jdbc question In that case it belongs more to #sql. > am I using reify correctly here? No because you provide it with the DataSource interface but don't implement the actual methods of that interface. But do you really need javax.sql.DataSource there?

Nim Sadeh 2025-04-05T17:27:50.960759Z

Don't I? Isn't that the class used?

p-himik 2025-04-05T17:30:26.103249Z

If something requires for some object to be an instance of DataSource, it is very likely to also require implementations of at least some of the methods on that interface. Since DataSource is not a marker interface. But next.jdbc doesn't require that interface. Implementing that proto/Executable protocol ought to be enough.

Nim Sadeh 2025-04-05T17:40:36.547079Z

So I just need a defrecord that implements the right methods?

Nim Sadeh 2025-04-05T17:50:18.701549Z

The answer to that is yes:

(defrecord LoggingDataSource [^javax.sql.DataSource ds]
  proto/Transactable
  (-transact [_ body-fn opts]
    (proto/-transact ds body-fn opts))

  proto/Connectable
  (get-connection [_ opts]
    (proto/get-connection ds opts))

  proto/Executable
  (-execute [_ sql opts]
    (µ/trace :database/execute [:statement sql]
             (proto/-execute ds sql opts)))
  (-execute-all [_ sql opts]
    (µ/trace :database/execute-all [:statement sql]
             (proto/-execute-all ds sql opts)))
  (-execute-one [_ sql opts]
    (µ/trace :database/execute-one [:statement sql]
             (proto/-execute-one ds sql opts))))

p-himik 2025-04-05T18:38:05.367129Z

You shouldn't need defrecord, that reify should be enough it you remove javax.sql.DataSource. If it's not enough, you can do the same as you did with defrecord, but with reify instead.

seancorfield 2025-04-05T19:15:54.497769Z

There's already a logging example in the docs and built into the source. You don't need to define your own.

Nim Sadeh 2025-04-05T19:17:04.862569Z

Yea I’m familiar with with-logging but I specifically wanted to use trace which works a bit differently than common log patterns and so I don’t think is compatible with the existing logging functionality

Nim Sadeh 2025-04-05T19:17:24.768679Z

This ended up working quite well tho

seancorfield 2025-04-05T19:18:20.121729Z

And in future, please use #sql for next.jdbc questions.

Nim Sadeh 2025-04-05T19:19:35.084929Z

Sorry, I searched for #next.jdbc and didn’t see anything following the usual format of channels named after libraries. I didn’t think to look for #sql but now I know