Skip to content

Commit

Permalink
Support I() in DBI wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Mar 14, 2024
1 parent 9caecc5 commit 7657aa6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
33 changes: 32 additions & 1 deletion R/dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ setMethod(
dbWriteTable_bq
)

#' @rdname DBI
#' @export
setMethod(
"dbWriteTable",
c("BigQueryConnection", "AsIs", "data.frame"),
dbWriteTable_bq
)


dbAppendTable_bq <- function(conn, name, value, ..., row.names = NULL) {
tb <- as_bq_table(conn, name)

Expand All @@ -274,6 +283,10 @@ setMethod("dbAppendTable", c("BigQueryConnection", "character", "data.frame"), d
#' @export
setMethod("dbAppendTable", c("BigQueryConnection", "Id", "data.frame"), dbAppendTable_bq)

#' @rdname DBI
#' @export
setMethod("dbAppendTable", c("BigQueryConnection", "AsIs", "data.frame"), dbAppendTable_bq)

dbCreateTable_bq <- function(conn,
name,
fields,
Expand Down Expand Up @@ -317,6 +330,10 @@ setMethod("dbReadTable", c("BigQueryConnection", "character"), dbReadTable_bq)
#' @export
setMethod("dbReadTable", c("BigQueryConnection", "Id"), dbReadTable_bq)

#' @rdname DBI
#' @export
setMethod("dbReadTable", c("BigQueryConnection", "AsIs"), dbReadTable_bq)

#' @rdname DBI
#' @inheritParams DBI::dbListTables
#' @export
Expand Down Expand Up @@ -345,6 +362,10 @@ setMethod("dbExistsTable", c("BigQueryConnection", "character"), dbExistsTable_b
#' @export
setMethod("dbExistsTable", c("BigQueryConnection", "Id"), dbExistsTable_bq)

#' @rdname DBI
#' @export
setMethod("dbExistsTable", c("BigQueryConnection", "AsIs"), dbExistsTable_bq)

dbListFields_bq <- function(conn, name, ...) {
tb <- as_bq_table(conn, name)
flds <- bq_table_fields(tb)
Expand All @@ -360,6 +381,10 @@ setMethod("dbListFields", c("BigQueryConnection", "character"), dbListFields_bq)
#' @export
setMethod("dbListFields", c("BigQueryConnection", "Id"), dbListFields_bq)

#' @rdname DBI
#' @export
setMethod("dbListFields", c("BigQueryConnection", "AsIs"), dbListFields_bq)

dbRemoveTable_bq <- function(conn, name, ...) {
tb <- as_bq_table(conn, name)
bq_table_delete(tb)
Expand All @@ -376,6 +401,10 @@ setMethod("dbRemoveTable", c("BigQueryConnection", "character"), dbRemoveTable_b
#' @export
setMethod("dbRemoveTable", c("BigQueryConnection", "Id"), dbRemoveTable_bq)

#' @rdname DBI
#' @export
setMethod("dbRemoveTable", c("BigQueryConnection", "AsIs"), dbRemoveTable_bq)

# nocov start
#' @rdname DBI
#' @inheritParams DBI::dbGetInfo
Expand Down Expand Up @@ -432,7 +461,7 @@ as_bq_dataset.BigQueryConnection <- function(x, ..., error_arg, error_call) {
#' @export
as_bq_table.BigQueryConnection <- function(x, name, ...) {
if (inherits(name, "dbplyr_table_path")) { # dbplyr 2.5.0
pieces <- dbplyr:::table_path_components(name, x)[[1]]
pieces <- dbplyr::table_path_components(name, x)[[1]]
} else if (inherits(name, "dbplyr_table_ident")) { # dbplyr 2.4.0
name <- unclass(name)
pieces <- c(name$catalog, name$schema, name$table)
Expand All @@ -444,6 +473,8 @@ as_bq_table.BigQueryConnection <- function(x, name, ...) {
} else if (is(name, "Id")) {
pieces <- unname(name@name)
} else if (is.character(name) && length(name) == 1) {
# Technically incorrect according to the DBI spec, because it should
# automatically quote the name; but too high risk to change now
pieces <- strsplit(name, ".", fixed = TRUE)[[1]]
} else {
cli::cli_abort("{.arg name} must be a string or a dbplyr_table_ident.")
Expand Down
2 changes: 1 addition & 1 deletion R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ src_bigquery <- function(project, dataset, billing = project, max_pages = 10) {
tbl.BigQueryConnection <- function(src, from, ...) {
src <- dbplyr::src_dbi(src, auto_disconnect = FALSE)

if (dbplyr_2.5.0) {
if (utils::packageVersion("dbplyr") >= "2.4.0.9000") {
tbl <- dplyr::tbl(src, from = from)
} else {
tbl <- dplyr::tbl(src, from = from, check_from = FALSE)
Expand Down
1 change: 0 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.onLoad <- function(libname, pkgname) {

.auth <<- gargle::init_AuthState(
package = "bigrquery",
auth_active = TRUE
Expand Down
28 changes: 28 additions & 0 deletions man/DBI.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7657aa6

Please sign in to comment.