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"]))> 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?
Don't I? Isn't that the class used?
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.
So I just need a defrecord that implements the right methods?
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))))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.
There's already a logging example in the docs and built into the source. You don't need to define your own.
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
This ended up working quite well tho
And in future, please use #sql for next.jdbc questions.
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