Skip to content

Commit

Permalink
Implement a dbCreateTable method (#568)
Browse files Browse the repository at this point in the history
Fixes #483
  • Loading branch information
hadley authored Nov 7, 2023
1 parent 85eab12 commit c8cfc4e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# bigrquery (development version)

* Now supports `dbAppendTable()` (#539) and `dbCreateTable()` (#483).

* `dbWriteTable()` now correct uses the `billing` value set in the
connection (#486).

* Now supports `dbAppendTable()` (#539).

* `dbReadTable()`, `dbWriteTable()`, `dbExistsTable()`, `dbRemoveTable()`,
and `dbListFields()` now all work with `DBI::Id()` (#537).

Expand Down
24 changes: 24 additions & 0 deletions R/dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,30 @@ setMethod("dbAppendTable", c("BigQueryConnection", "character", "data.frame"), d
#' @export
setMethod("dbAppendTable", c("BigQueryConnection", "Id", "data.frame"), dbAppendTable_bq)

dbCreateTable_bq <- function(conn,
name,
fields,
...,
row.names = NULL,
temporary = FALSE) {
if (!identical(temporary, FALSE)) {
stop("Temporary tables not supported by bigrquery", call. = FALSE)
}

tb <- as_bq_table(conn, name)
bq_table_create(tb, fields)

invisible(TRUE)
}

#' @inheritParams DBI::dbCreateTable
#' @rdname DBI
#' @export
setMethod("dbCreateTable", "BigQueryConnection", dbCreateTable_bq)

#' @rdname DBI
#' @export
setMethod("dbCreateTable", "BigQueryConnection", dbCreateTable_bq)

dbReadTable_bq <- function(conn, name, ...) {
tb <- as_bq_table(conn, name)
Expand Down
23 changes: 13 additions & 10 deletions tests/testthat/test-dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,30 @@ test_that("can use DBI::Id()", {
df <- data.frame(x = 1:10)
id <- DBI::Id(table = "mytable")

expect_no_error(DBI::dbWriteTable(con, id, df))
expect_no_error(DBI::dbCreateTable(con, id, df))
expect_no_error(DBI::dbAppendTable(con, id, df))
expect_no_error(DBI::dbWriteTable(con, id, df, overwrite = TRUE))
expect_no_error(DBI::dbReadTable(con, id))
expect_true(DBI::dbExistsTable(con, id))
expect_no_error(DBI::dbListFields(con, id))
expect_no_error(DBI::dbRemoveTable(con, id))
})

test_that("can append to an existing dataset", {
ds <- bq_test_dataset()
con <- DBI::dbConnect(ds)
test_that("can create an empty dataset then append to it", {
tb <- bq_test_table()
con <- DBI::dbConnect(bq_dataset(tb$project, tb$dataset))

df <- data.frame(x = 1, y = 2)
DBI::dbWriteTable(con, "df", df)
DBI::dbWriteTable(con, "df", df, append = TRUE)
DBI::dbCreateTable(con, tb$table, df)
expect_equal(bq_table_nrow(tb), 0)

# Or with dbAppend
DBI::dbAppendTable(con, "df", df)
# With dbWriteTable
DBI::dbWriteTable(con, tb$table, df, append = TRUE)
expect_equal(bq_table_nrow(tb), 1)

df2 <- DBI::dbReadTable(con, "df")
expect(nrow(df2), 3L)
# Or with dbAppend
DBI::dbAppendTable(con, tb$table, df)
expect_equal(bq_table_nrow(tb), 2)
})

test_that("dataset is optional", {
Expand Down

0 comments on commit c8cfc4e

Please sign in to comment.