Skip to content

Commit

Permalink
Create src_ classes from DBI class extensions (#918)
Browse files Browse the repository at this point in the history
Co-authored-by: Hadley Wickham <[email protected]>
  • Loading branch information
noamross and hadley authored Dec 21, 2023
1 parent 267a12e commit 81f23d4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dbplyr (development version)

* The class of remote sources now includes all S4 class names, not just
the first (#918).

* `db_explain()` now works for Oracle (@thomashulst, #1353).

* Database errors now show the generated SQL, which hopefully will make it
Expand Down
9 changes: 6 additions & 3 deletions R/src_dbi.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,20 @@ src_dbi <- function(con, auto_disconnect = FALSE) {
disco <- db_disconnector(con, quiet = is_true(auto_disconnect)) # nocov
}

subclass <- paste0("src_", class(con)[[1]])

structure(
list(
con = con,
disco = disco
),
class = c(subclass, "src_dbi", "src_sql", "src")
class = connection_s3_class(con)
)
}

connection_s3_class <- function(con) {
subclass <- setdiff(methods::is(con), methods::extends("DBIConnection"))
c(paste0("src_", subclass), "src_dbi", "src_sql", "src")
}

methods::setOldClass(c("src_dbi", "src_sql", "src"))

# nocov start
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-src_dbi.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,21 @@ test_that("tbl and src classes include connection class", {
expect_true(inherits(mf, "tbl_SQLiteConnection"))
expect_true(inherits(mf$src, "src_SQLiteConnection"))
})

test_that("generates S3 class based on S4 class name", {
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
expect_equal(
connection_s3_class(con),
c("src_SQLiteConnection", "src_dbi", "src_sql", "src")
)

on.exit(removeClass("Foo2"))
on.exit(removeClass("Foo1"))

Foo1 <- setClass("Foo1", contains = "DBIConnection")
Foo2 <- setClass("Foo2", contains = "Foo1")
expect_equal(
connection_s3_class(Foo2()),
c("src_Foo2", "src_Foo1", "src_dbi", "src_sql", "src")
)
})

0 comments on commit 81f23d4

Please sign in to comment.