-
Notifications
You must be signed in to change notification settings - Fork 77
Description
I created an issue (quite a while back!) specifically asking about this with RPostgres, where libpq has functionality to handle multi-statement SQL being sent to the DB server. The recommendation was to bubble this up to DBI, but I found a duplicate DBI issue with essentially the same use case, too: SQL scripts holding multiple statements that we'd like to relay (perhaps after string interpolation) to the DB via R/DBI. The recommendation there was to bubble this to projects like squr or dbr, but neither of those projects seem to be active (the last commits were 3 and 2 years ago, respectively, and neither are available on CRAN).
The general pattern of:
readr::read_file("some-sql-script-template.sql") %>%
glue::glue_sql(.con = my_con) %>% ## maybe we do some interpolation here
DBI::dbMultiExecute(my_con) ## imaginary DBI method
... still seems very appealing to me and I've written wrappers around this to try to robustly split the SQL script into the separate statements and loop over them, but I can't help but wonder if this should still be available within DBI, especially for the DB back-ends that support multi-statement SQL.
DB back-ends differ on what multi-statement execution returns to the client and how it should be handled (PostgreSQL, for example, simply sends back the standard return of the final statement; MySQL, on the other hand, wants to send back multiple resultsets). For this reason, I think a new optionally-supported execution method, like DBI::dbMultiExecute()
could be used, with return-value semantics that align with the traditional DBI::dbExecute()
method: simply the last status value sent back by the DB. The risk seems to be poor interpretation of that status, e.g., if there's a failure mid-way, on which statement did the full execution fail? But this also seems (to me) to be a standard 'here-be-dragons' situation with programming, and anyone using multi-statement SQL with these databases outside of R are already accepting this risk. (Experienced folks should probably be wrapping those scripts in transactions anyhow, one way or another.)
This might wind up being a "won't support" feature, but I keep finding myself wanting to adopt the example pattern shown above, and I imagine there are plenty of other folks out there with SQL scripts that want to do the same without needing to tackle some amount of script-parsing on their own, either with-or-without an existing R package to do so. (FWIW, I've been using {reticulate} with the sqlparse Python package to help with this, but this seems like a heavyweight solution to a problem that shouldn't exist -- at least for me, because I use PostgreSQL/libpq most of the time :-)
Curious about your thoughts, but regardless of them, thanks much for the continued DBI work (it's highly-valued)!