From 906cabf0e38b24ad229793b3dff937ab59a04444 Mon Sep 17 00:00:00 2001 From: Noam Ross Date: Mon, 20 Jun 2022 11:16:36 -0400 Subject: [PATCH 1/3] Create `src_` classes from multiple DBI inheritance --- R/src_dbi.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/src_dbi.R b/R/src_dbi.R index 580da6361..08efc8527 100644 --- a/R/src_dbi.R +++ b/R/src_dbi.R @@ -123,7 +123,9 @@ src_dbi <- function(con, auto_disconnect = FALSE) { disco <- db_disconnector(con, quiet = is_true(auto_disconnect)) # nocov } - subclass <- paste0("src_", class(con)[[1]]) + con_classes <- extends(class(con))[ + grepl("(? Date: Thu, 21 Dec 2023 14:47:48 -0600 Subject: [PATCH 2/3] extract into separate function --- R/src_dbi.R | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/R/src_dbi.R b/R/src_dbi.R index 7c61bac60..3f47e2d57 100644 --- a/R/src_dbi.R +++ b/R/src_dbi.R @@ -123,19 +123,20 @@ src_dbi <- function(con, auto_disconnect = FALSE) { disco <- db_disconnector(con, quiet = is_true(auto_disconnect)) # nocov } - con_classes <- extends(class(con))[ - grepl("(? Date: Thu, 21 Dec 2023 14:56:37 -0600 Subject: [PATCH 3/3] Add test & news bullet --- NEWS.md | 3 +++ R/src_dbi.R | 2 +- tests/testthat/test-src_dbi.R | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 316b50d0d..ad78e01f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/src_dbi.R b/R/src_dbi.R index 3f47e2d57..978be8439 100644 --- a/R/src_dbi.R +++ b/R/src_dbi.R @@ -133,7 +133,7 @@ src_dbi <- function(con, auto_disconnect = FALSE) { } connection_s3_class <- function(con) { - subclass <- setdiff(is(con), extends("DBIConnection")) + subclass <- setdiff(methods::is(con), methods::extends("DBIConnection")) c(paste0("src_", subclass), "src_dbi", "src_sql", "src") } diff --git a/tests/testthat/test-src_dbi.R b/tests/testthat/test-src_dbi.R index e7b8c23a3..337808779 100644 --- a/tests/testthat/test-src_dbi.R +++ b/tests/testthat/test-src_dbi.R @@ -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") + ) +})