diff --git a/NAMESPACE b/NAMESPACE index 63d11fe7..278d9c5a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -122,8 +122,10 @@ exportMethods(dbClearResult) exportMethods(dbColumnInfo) exportMethods(dbCommit) exportMethods(dbConnect) +exportMethods(dbCreateTable) exportMethods(dbDataType) exportMethods(dbDisconnect) +exportMethods(dbExecute) exportMethods(dbExistsTable) exportMethods(dbFetch) exportMethods(dbGetInfo) diff --git a/NEWS.md b/NEWS.md index 4afeafd1..ab35398c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # bigrquery (development version) -* Now supports `dbAppendTable()` (#539) and `dbCreateTable()` (#483). +* Now supports `dbAppendTable()` (#539), `dbCreateTable()` (#483), + and `dbExecute` (#502). * `dbWriteTable()` now correct uses the `billing` value set in the connection (#486). diff --git a/R/dbi-connection.R b/R/dbi-connection.R index 334983d9..1dc5dcf6 100644 --- a/R/dbi-connection.R +++ b/R/dbi-connection.R @@ -77,6 +77,26 @@ setMethod( BigQueryResult(conn, statement, ...) }) + +#' @rdname DBI +#' @inheritParams DBI::dbSendQuery +#' @export +setMethod("dbExecute", c("BigQueryConnection", "character"), function(conn, statement, ...) { + ds <- if (!is.null(conn@dataset)) as_bq_dataset(conn) + + job <- bq_perform_query(statement, + billing = conn@billing, + default_dataset = ds, + quiet = conn@quiet, + ... + ) + bq_job_wait(job, quiet = conn@quiet) + + meta <- bq_job_meta(job, "statistics(query(numDmlAffectedRows))") + as.numeric(meta$statistics$query$numDmlAffectedRows %||% 0) +}) + + #' @rdname DBI #' @inheritParams DBI::dbQuoteString #' @export diff --git a/man/DBI.Rd b/man/DBI.Rd index 74584468..dbbbe79e 100644 --- a/man/DBI.Rd +++ b/man/DBI.Rd @@ -15,6 +15,7 @@ \alias{dbIsValid,BigQueryConnection-method} \alias{dbDisconnect,BigQueryConnection-method} \alias{dbSendQuery,BigQueryConnection,character-method} +\alias{dbExecute,BigQueryConnection,character-method} \alias{dbQuoteString,BigQueryConnection,character-method} \alias{dbQuoteString,BigQueryConnection,SQL-method} \alias{dbQuoteIdentifier,BigQueryConnection,character-method} @@ -25,6 +26,7 @@ \alias{dbWriteTable,BigQueryConnection,Id,data.frame-method} \alias{dbAppendTable,BigQueryConnection,character,data.frame-method} \alias{dbAppendTable,BigQueryConnection,Id,data.frame-method} +\alias{dbCreateTable,BigQueryConnection-method} \alias{dbReadTable,BigQueryConnection,character-method} \alias{dbReadTable,BigQueryConnection,Id-method} \alias{dbListTables,BigQueryConnection-method} @@ -69,6 +71,8 @@ \S4method{dbSendQuery}{BigQueryConnection,character}(conn, statement, ...) +\S4method{dbExecute}{BigQueryConnection,character}(conn, statement, ...) + \S4method{dbQuoteString}{BigQueryConnection,character}(conn, x, ...) \S4method{dbQuoteString}{BigQueryConnection,SQL}(conn, x, ...) @@ -109,6 +113,10 @@ \S4method{dbAppendTable}{BigQueryConnection,Id,data.frame}(conn, name, value, ..., row.names = NULL) +\S4method{dbCreateTable}{BigQueryConnection}(conn, name, fields, ..., row.names = NULL, temporary = FALSE) + +\S4method{dbCreateTable}{BigQueryConnection}(conn, name, fields, ..., row.names = NULL, temporary = FALSE) + \S4method{dbReadTable}{BigQueryConnection,character}(conn, name, ...) \S4method{dbReadTable}{BigQueryConnection,Id}(conn, name, ...) @@ -200,6 +208,15 @@ will be whatever the R identifier \code{"row.names"} maps to the DBMS (see \code{\link[DBI:make.db.names]{DBI::make.db.names()}}). If \code{NA} will add rows names if they are characters, otherwise will ignore.} +\item{fields}{Either a character vector or a data frame. + +A named character vector: Names are column names, values are types. +Names are escaped with \code{\link[DBI:dbQuoteIdentifier]{dbQuoteIdentifier()}}. +Field types are unescaped. + +A data frame: field types are generated using +\code{\link[DBI:dbDataType]{dbDataType()}}.} + \item{res}{An object inheriting from \linkS4class{DBIResult}.} \item{n}{maximum number of records to retrieve per fetch. Use \code{n = -1} diff --git a/tests/testthat/test-dbi-connection.R b/tests/testthat/test-dbi-connection.R index 91adc4d9..296403aa 100644 --- a/tests/testthat/test-dbi-connection.R +++ b/tests/testthat/test-dbi-connection.R @@ -70,6 +70,18 @@ test_that("can roundtrip a data frame", { expect_equal(ncol(df), 11) }) +test_that("can execute a query", { + tb <- bq_test_table() + con <- DBI::dbConnect(bq_dataset(tb$project, tb$dataset)) + + DBI::dbWriteTable(con, tb$table, data.frame(x = 1:4)) + out <- dbExecute(con, glue("UPDATE {tb$table} SET x = x + 1 WHERE true")) + expect_equal(out, 4) + + out <- dbExecute(con, glue("DELETE {tb$table} WHERE x <= 3")) + expect_equal(out, 2) +}) + test_that("can use DBI::Id()", { ds <- bq_test_dataset() con <- DBI::dbConnect(ds)