Fix logging for streaming and updateMany#2064
Conversation
|
Thanks for this improvement @jatcwang . I'm trying to convert some existing code when migrating from RC6 -> RC7 due to the deprecation and wonder if you have a minute to provide some guidance? 🙏🏻 Old code (RC6): private def exec(sql: String): ConnectionIO[RawResultSet] = {
HC.prepareStatement(sql)(HPS.executeQuery {
for {
md <- FRS.getMetaData
rs <- row(md).whileM[List](HRS.next)
} yield RawResultSet(header(md), rs)
})It's unclear to me how to convert this to the FC.prepareStatement / executeWithResultSet style, i've got something like this so far (RC7): private def exec(sql: String): ConnectionIO[RawResultSet] = {
val preparedStatement = FC.prepareStatement(sql)
doobie.hi.connection.executeWithResultSet(
create = preparedStatement,
prep = ???,
exec = ???,
process = ???,
LoggingInfo(sql, NonBatch(List.empty), "fooLabel")
)It's unclear to me how to/what to put in prep, exec and process - might you have any pointers? Or a method I'm missing which just takes care of it for me? My use case here is running (admin) user provided SQL without any bound parameters - just a straight string -> query mapping. |
|
Hi @henricook sorry the documentation for custom JDBC operations isn't really there and I haven't gotten around to #2134 yet. In your case,
Hope that works! |
|
Thanks for a super speedy response @jatcwang. I was reading the MR but Doobie novice that I am it took me a while to realise that IFPS FPS etc are all remappings of things like Here's my compiling and working final item based on your comments, currently without the 'doobie idiomatic' remapping of Notes:
private def exec(sql: String): ConnectionIO[RawResultSet] = {
val preparedStatement = FC.prepareStatement(sql)
doobie.hi.connection.executeWithResultSet(
create = preparedStatement,
prep = doobie.free.preparedstatement.unit,
exec = doobie.free.preparedstatement.executeQuery,
process = (for {
md <- FRS.getMetaData
rs <- row(md).whileM[List](HRS.next)
} yield RawResultSet(header(md), rs)),
LoggingInfo(sql, NonBatch(List.empty), mkLabel("executeQuery"))
) |
|
Thanks for posting an update @henricook glad it worked 🥳 |
|
Thanks @henricook for the shared code, it was really useful to me today |
Introduces three new functions in the high level API for Connection (
doobie.hi.HC).Fixes #533, #1888
These helpers offer a few improvements:
executeWithResultSetThis is a generalization for any query that executes a PreparedStatement and processes a ResultSet. This covers two cases:
executeQuery)RETURNING/.executeUpdate + .getGeneratedKeys)All query methods like Query#to and Update#withUniqueGeneratedKeys have switched to use this internally.
executeWithoutResultSetSimilar to
executeWithResultSetbut for queries that doesn't need to process a ResultSet (i.e. simple updates).Methods like
Update'supdateMany,updatehave switched to use this internallystreamThis will be replacing the existing
streammethod (which is currently deprecated because it doesn't support logging)Query#streamand all theUpdate#withGeneratedKeys*have switched to this internally.Further improvements can probably be made on top of these methods so users don't need to construct
LoggingInfothemselves.