Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dbExecute() method #569

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
20 changes: 20 additions & 0 deletions R/dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions man/DBI.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading